3
3
module YARP
4
4
class CompilerTest < Test ::Unit ::TestCase
5
5
def test_empty_program
6
- assert_nil compile ( "" )
6
+ test_yarp_eval ( "" )
7
7
end
8
8
9
9
############################################################################
10
10
# Literals #
11
11
############################################################################
12
12
13
13
def test_FalseNode
14
- assert_equal false , compile ( "false" )
14
+ test_yarp_eval ( "false" )
15
15
end
16
16
17
17
def test_FloatNode
18
- assert_equal 1.2 , compile ( "1.2" )
19
- assert_equal 1.2e3 , compile ( "1.2e3" )
20
- assert_equal ( + 1.2e+3 , compile ( "+1.2e+3" ) )
21
- assert_equal ( - 1.2e-3 , compile ( "-1.2e-3" ) )
18
+ test_yarp_eval ( "1.2" )
19
+ test_yarp_eval ( "1.2e3" )
20
+ test_yarp_eval ( "+1.2e+3" )
21
+ test_yarp_eval ( "-1.2e-3" )
22
22
end
23
23
24
24
def test_ImaginaryNode
25
- assert_equal 1 i , compile ( "1i" )
26
- assert_equal + 1.0 i , compile ( "+1.0i" )
27
- assert_equal 1 ri , compile ( "1ri" )
25
+ test_yarp_eval ( "1i" )
26
+ test_yarp_eval ( "+1.0i" )
27
+ test_yarp_eval ( "1ri" )
28
28
end
29
29
30
30
def test_IntegerNode
31
- assert_equal 1 , compile ( "1" )
32
- assert_equal ( + 1 , compile ( "+1" ) )
33
- assert_equal ( - 1 , compile ( "-1" ) )
34
- assert_equal 0x10 , compile ( "0x10" )
35
- assert_equal 0b10 , compile ( "0b10" )
36
- assert_equal 0o10 , compile ( "0o10" )
37
- assert_equal 010 , compile ( "010" )
31
+ test_yarp_eval ( "1" )
32
+ test_yarp_eval ( "+1" )
33
+ test_yarp_eval ( "-1" )
34
+ test_yarp_eval ( "0x10" )
35
+ test_yarp_eval ( "0b10" )
36
+ test_yarp_eval ( "0o10" )
37
+ test_yarp_eval ( "010" )
38
38
end
39
39
40
40
def test_NilNode
41
- assert_nil compile ( "nil" )
41
+ test_yarp_eval ( "nil" )
42
42
end
43
43
44
44
def test_RationalNode
45
- assert_equal 1.2 r , compile ( "1.2r" )
46
- assert_equal + 1.2 r , compile ( "+1.2r" )
45
+ test_yarp_eval ( "1.2r" )
46
+ test_yarp_eval ( "+1.2r" )
47
47
end
48
48
49
49
def test_SelfNode
50
- assert_equal TOPLEVEL_BINDING . eval ( "self" ) , compile ( "self" )
50
+ test_yarp_eval ( "self" )
51
51
end
52
52
53
53
def test_TrueNode
54
- assert_equal true , compile ( "true" )
54
+ test_yarp_eval ( "true" )
55
55
end
56
56
57
57
############################################################################
58
58
# Reads #
59
59
############################################################################
60
60
61
61
def test_ClassVariableReadNode
62
- assert_equal 1 , compile ( "class YARP::CompilerTest; @@yct = 1; @@yct; end" )
62
+ test_yarp_eval ( "class YARP::CompilerTest; @@yct = 1; @@yct; end" )
63
63
end
64
64
65
65
def test_ConstantPathNode
66
- assert_equal YARP :: CompilerTest , compile ( "YARP::CompilerTest" )
66
+ test_yarp_eval ( "YARP::CompilerTest" )
67
67
end
68
68
69
69
def test_ConstantReadNode
70
- assert_equal YARP , compile ( "YARP" )
70
+ test_yarp_eval ( "YARP" )
71
71
end
72
72
73
73
def test_GlobalVariableReadNode
74
- assert_equal 1 , compile ( "$yct = 1; $yct" )
74
+ test_yarp_eval ( "$yct = 1; $yct" )
75
75
end
76
76
77
77
def test_InstanceVariableReadNode
78
- assert_equal 1 , compile ( "class YARP::CompilerTest; @yct = 1; @yct; end" )
78
+ test_yarp_eval ( "class YARP::CompilerTest; @yct = 1; @yct; end" )
79
79
end
80
80
<
F438
code class="diff-text syntax-highlighted-line">
81
81
def test_LocalVariableReadNode
82
- assert_equal 1 , compile ( "yct = 1; yct" )
82
+ test_yarp_eval ( "yct = 1; yct" )
83
83
end
84
84
85
85
############################################################################
86
86
# Writes #
87
87
############################################################################
88
88
89
89
def test_ClassVariableWriteNode
90
- assert_equal 1 , compile ( "class YARP::CompilerTest; @@yct = 1; end" )
90
+ test_yarp_eval ( "class YARP::CompilerTest; @@yct = 1; end" )
91
91
end
92
92
93
93
def test_ClassVariableAndWriteNode
94
- assert_equal 1 , compile ( "class YARP::CompilerTest; @@yct = 0; @@yct &&= 1; end" )
94
+ test_yarp_eval ( "class YARP::CompilerTest; @@yct = 0; @@yct &&= 1; end" )
95
95
end
96
96
97
97
def test_ClassVariableOrWriteNode
98
- assert_equal 1 , compile ( "class YARP::CompilerTest; @@yct = 1; @@yct ||= 0; end" )
99
- assert_equal 1 , compile ( "class YARP::CompilerTest; @@yct = nil; @@yct ||= 1; end" )
98
+ test_yarp_eval ( "class YARP::CompilerTest; @@yct = 1; @@yct ||= 0; end" )
99
+ test_yarp_eval ( "class YARP::CompilerTest; @@yct = nil; @@yct ||= 1; end" )
100
100
end
101
101
102
102
def test_ClassVariableOperatorWriteNode
103
- assert_equal 1 , compile ( "class YARP::CompilerTest; @@yct = 0; @@yct += 1; end" )
103
+ test_yarp_eval ( "class YARP::CompilerTest; @@yct = 0; @@yct += 1; end" )
104
104
end
105
105
106
106
def test_ConstantWriteNode
107
+ # We don't call test_yarp_eval directly in this case becuase we
108
+ # don't want to assign the constant mutliple times if we run
109
+ # with `--repeat-count`
110
+ # Instead, we eval manually here, and remove the constant to
107
111
constant_name = "YCT"
108
- assert_equal 1 , compile ( "#{ constant_name } = 1" )
109
- # We remove the constant to avoid assigning it mutliple
110
- # times if we run with `--repeat_count`
112
+ source = "#{ constant_name } = 1"
113
+ yarp_eval = RubyVM :: InstructionSequence . compile_yarp ( source ) . eval
114
+ assert_equal yarp_eval , 1
111
115
Object . send ( :remove_const , constant_name )
112
116
end
113
117
114
118
def test_ConstantPathWriteNode
115
- # assert_equal 1, compile ("YARP::YCT = 1")
119
+ # test_yarp_eval ("YARP::YCT = 1")
116
120
end
117
121
118
122
def test_GlobalVariableWriteNode
119
- assert_equal 1 , compile ( "$yct = 1" )
123
+ test_yarp_eval ( "$yct = 1" )
120
124
end
121
125
122
126
def test_GlobalVariableAndWriteNode
123
- assert_equal 1 , compile ( "$yct = 0; $yct &&= 1" )
127
+ test_yarp_eval ( "$yct = 0; $yct &&= 1" )
124
128
end
125
129
126
130
def test_GlobalVariableOrWriteNode
127
- assert_equal 1 , compile ( "$yct ||= 1" )
131
+ test_yarp_eval ( "$yct ||= 1" )
128
132
end
129
133
130
134
def test_GlobalVariableOperatorWriteNode
131
- assert_equal 1 , compile ( "$yct = 0; $yct += 1" )
135
+ test_yarp_eval ( "$yct = 0; $yct += 1" )
132
136
end
133
137
134
138
def test_InstanceVariableWriteNode
135
- assert_equal 1 , compile ( "class YARP::CompilerTest; @yct = 1; end" )
139
+ test_yarp_eval ( "class YARP::CompilerTest; @yct = 1; end" )
136
140
end
137
141
138
142
def test_InstanceVariableAndWriteNode
139
- assert_equal 1 , compile ( "@yct = 0; @yct &&= 1" )
143
+ test_yarp_eval ( "@yct = 0; @yct &&= 1" )
140
144
end
141
145
142
146
def test_InstanceVariableOrWriteNode
143
- assert_equal 1 , compile ( "@yct ||= 1" )
147
+ test_yarp_eval ( "@yct ||= 1" )
144
148
end
145
149
146
150
def test_InstanceVariableOperatorWriteNode
147
- assert_equal 1 , compile ( "@yct = 0; @yct += 1" )
151
+ test_yarp_eval ( "@yct = 0; @yct += 1" )
148
152
end
149
153
150
154
def test_LocalVariableWriteNode
151
- assert_equal 1 , compile ( "yct = 1" )
155
+ test_yarp_eval ( "yct = 1" )
152
156
end
153
157
154
158
def test_LocalVariableAndWriteNode
155
- assert_equal 1 , compile ( "yct = 0; yct &&= 1" )
159
+ test_yarp_eval ( "yct = 0; yct &&= 1" )
156
160
end
157
161
158
162
def test_LocalVariableOrWriteNode
159
- assert_equal 1 , compile ( "yct ||= 1" )
163
+ test_yarp_eval ( "yct ||= 1" )
160
164
end
161
165
162
166
def test_LocalVariableOperatorWriteNode
163
- assert_equal 1 , compile ( "yct = 0; yct += 1" )
167
+ test_yarp_eval ( "yct = 0; yct += 1" )
164
168
end
165
169
166
170
############################################################################
167
171
# String-likes #
168
172
############################################################################
169
173
170
174
def test_EmbeddedVariableNode
171
- # assert_equal "1", compile ('class YARP::CompilerTest; @yct = 1; "#@yct"; end')
172
- # assert_equal "1", compile ('class YARP::CompilerTest; @@yct = 1; "#@@yct"; end')
173
- assert_equal "1" , compile ( '$yct = 1; "#$yct"' )
175
+ # test_yarp_eval ('class YARP::CompilerTest; @yct = 1; "#@yct"; end')
176
+ # test_yarp_eval ('class YARP::CompilerTest; @@yct = 1; "#@@yct"; end')
177
+ test_yarp_eval ( '$yct = 1; "#$yct"' )
174
178
end
175
179
176
180
def test_InterpolatedRegularExpressionNode
177
- assert_equal /1 1 1/ , compile ( '$yct = 1; /1 #$yct 1/' )
178
- assert_equal /1 3 1/ , compile ( '/1 #{1 + 2} 1/' )
179
- assert_equal /1 2 3 1/ , compile ( '/1 #{"2"} #{1 + 2} 1/' )
181
+ test_yarp_eval ( '$yct = 1; /1 #$yct 1/' )
182
+ test_yarp_eval ( '/1 #{1 + 2} 1/' )
183
+ test_yarp_eval ( '/1 #{"2"} #{1 + 2} 1/' )
180
184
end
181
185
182
186
def test_InterpolatedStringNode
183
- assert_equal "1 1 1" , compile ( '$yct = 1; "1 #$yct 1"' )
184
- assert_equal "1 3 1" , compile ( '"1 #{1 + 2} 1"' )
187
+ test_yarp_eval ( '$yct = 1; "1 #$yct 1"' )
188
+ test_yarp_eval ( '"1 #{1 + 2} 1"' )
185
189
end
186
190
187
191
def test_InterpolatedSymbolNode
188
- assert_equal :"1 1 1" , compile ( '$yct = 1; :"1 #$yct 1"' )
189
- assert_equal :"1 3 1" , compile ( ':"1 #{1 + 2} 1"' )
192
+ test_yarp_eval ( '$yct = 1; :"1 #$yct 1"' )
193
+ test_yarp_eval ( ':"1 #{1 + 2} 1"' )
190
194
end
191
195
192
196
def test_InterpolatedXStringNode
193
- assert_equal "1 \n " , compile ( '`echo #{1}`' )
194
- assert_equal "100" , compile ( '`printf "100"`' )
197
+ test_yarp_eval ( '`echo #{1}`' )
198
+ test_yarp_eval ( '`printf "100"`' )
195
199
end
196
200
197
201
def test_RegularExpressionNode
198
- assert_equal /yct/ , compile ( '/yct/' )
202
+ test_yarp_eval ( '/yct/' )
199
203
end
200
204
201
205
def test_StringConcatNode
202
- # assert_equal "YARP::CompilerTest", compile ('"YARP" "::" "CompilerTest"')
206
+ # test_yarp_eval ('"YARP" "::" "CompilerTest"')
203
207
end
204
208
205
209
def test_StringNode
206
- assert_equal "yct" , compile ( '"yct"' )
210
+ test_yarp_eval ( '"yct"' )
207
211
end
208
212
209
213
def test_SymbolNode
210
- assert_equal :yct , compile ( ":yct" )
214
+ test_yarp_eval ( ":yct" )
211
215
end
212
216
213
217
def test_XStringNode
214
- # assert_equal "yctyct", compile (<<~RUBY)
218
+ # test_yarp_eval (<<~RUBY)
215
219
# class YARP::CompilerTest
216
220
# def self.`(command) = command * 2
217
221
# `yct`
@@ -224,28 +228,31 @@ def test_XStringNode
224
228
############################################################################
225
229
226
230
def test_AndNode
227
- assert_equal 1 , compile ( "true && 1" )
228
- assert_equal false , compile ( "false && 1" )
231
+ test_yarp_eval ( "true && 1" )
232
+ test_yarp_eval ( "false && 1" )
229
233
end
230
234
231
235
def test_OrNode
232
- assert_equal true , compile ( "true || 1" )
233
- assert_equal 1 , compile ( "false || 1" )
236
+ test_yarp_eval ( "true || 1" )
237
+ test_yarp_eval ( "false || 1" )
234
238
end
235
239
236
240
############################################################################
237
241
# Scopes/statements #
238
242
############################################################################
239
243
240
244
def test_ParenthesesNode
241
- assert_equal ( ) , compile ( "()" )
242
- assert_equal ( 1 ) , compile ( "(1)" )
245
+ test_yarp_eval ( "()" )
246
+ test_yarp_eval ( "(1)" )
243
247
end
244
248
245
249
private
246
250
247
- def compile ( source )
248
- RubyVM ::InstructionSequence . compile_yarp ( source ) . eval
251
+ def test_yarp_eval ( source )
252
+ ruby_eval = RubyVM ::InstructionSequence . compile ( source ) . eval
253
+ yarp_eval = RubyVM ::InstructionSequence . compile_yarp ( source ) . eval
254
+
255
+ assert_equal ruby_eval , yarp_eval
249
256
end
250
257
end
251
258
end
0 commit comments