8000 [YARP] Restructure tests to compare YARP evaluation to CRuby evaluati… · ruby/ruby@54e676a · GitHub
[go: up one dir, main page]

Skip to content

Commit 54e676a

Browse files
authored
[YARP] Restructure tests to compare YARP evaluation to CRuby evaluation (#8467)
Previously, we were hardcoding the expected evaluation value. Now, instead, we structure it so that we compare the YARP evaluation value against CRuby's evaluation value.
1 parent ec37636 commit 54e676a

File tree

1 file changed

+80
-73
lines changed

1 file changed

+80
-73
lines changed

test/yarp/compiler_test.rb

Lines changed: 80 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,215 +3,219 @@
33
module YARP
44
class CompilerTest < Test::Unit::TestCase
55
def test_empty_program
6-
assert_nil compile("")
6+
test_yarp_eval("")
77
end
88

99
############################################################################
1010
# Literals #
1111
############################################################################
1212

1313
def test_FalseNode
14-
assert_equal false, compile("false")
14+
test_yarp_eval("false")
1515
end
1616

1717
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")
2222
end
2323

2424
def test_ImaginaryNode
25-
assert_equal 1i, compile("1i")
26-
assert_equal +1.0i, compile("+1.0i")
27-
assert_equal 1ri, compile("1ri")
25+
test_yarp_eval("1i")
26+
test_yarp_eval("+1.0i")
27+
test_yarp_eval("1ri")
2828
end
2929

3030
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")
3838
end
3939

4040
def test_NilNode
41-
assert_nil compile("nil")
41+
test_yarp_eval("nil")
4242
end
4343

4444
def test_RationalNode
45-
assert_equal 1.2r, compile("1.2r")
46-
assert_equal +1.2r, compile("+1.2r")
45+
test_yarp_eval("1.2r")
46+
test_yarp_eval("+1.2r")
4747
end
4848

4949
def test_SelfNode
50-
assert_equal TOPLEVEL_BINDING.eval("self"), compile("self")
50+
test_yarp_eval("self")
5151
end
5252

5353
def test_TrueNode
54-
assert_equal true, compile("true")
54+
test_yarp_eval("true")
5555
end
5656

5757
############################################################################
5858
# Reads #
5959
############################################################################
6060

6161
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")
6363
end
6464

6565
def test_ConstantPathNode
66-
assert_equal YARP::CompilerTest, compile("YARP::CompilerTest")
66+
test_yarp_eval("YARP::CompilerTest")
6767
end
6868

6969
def test_ConstantReadNode
70-
assert_equal YARP, compile("YARP")
70+
test_yarp_eval("YARP")
7171
end
7272

7373
def test_GlobalVariableReadNode
74-
assert_equal 1, compile("$yct = 1; $yct")
74+
test_yarp_eval("$yct = 1; $yct")
7575
end
7676

7777
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")
7979
end
8080< F438 code class="diff-text syntax-highlighted-line">

8181
def test_LocalVariableReadNode
82-
assert_equal 1, compile("yct = 1; yct")
82+
test_yarp_eval("yct = 1; yct")
8383
end
8484

8585
############################################################################
8686
# Writes #
8787
############################################################################
8888

8989
def test_ClassVariableWriteNode
90-
assert_equal 1, compile("class YARP::CompilerTest; @@yct = 1; end")
90+
test_yarp_eval("class YARP::CompilerTest; @@yct = 1; end")
9191
end
9292

9393
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")
9595
end
9696

9797
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")
100100
end
101101

102102
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")
104104
end
105105

106106
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
107111
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
111115
Object.send(:remove_const, constant_name)
112116
end
113117

114118
def test_ConstantPathWriteNode
115-
# assert_equal 1, compile("YARP::YCT = 1")
119+
# test_yarp_eval("YARP::YCT = 1")
116120
end
117121

118122
def test_GlobalVariableWriteNode
119-
assert_equal 1, compile("$yct = 1")
123+
test_yarp_eval("$yct = 1")
120124
end
121125

122126
def test_GlobalVariableAndWriteNode
123-
assert_equal 1, compile("$yct = 0; $yct &&= 1")
127+
test_yarp_eval("$yct = 0; $yct &&= 1")
124128
end
125129

126130
def test_GlobalVariableOrWriteNode
127-
assert_equal 1, compile("$yct ||= 1")
131+
test_yarp_eval("$yct ||= 1")
128132
end
129133

130134
def test_GlobalVariableOperatorWriteNode
131-
assert_equal 1, compile("$yct = 0; $yct += 1")
135+
test_yarp_eval("$yct = 0; $yct += 1")
132136
end
133137

134138
def test_InstanceVariableWriteNode
135-
assert_equal 1, compile("class YARP::CompilerTest; @yct = 1; end")
139+
test_yarp_eval("class YARP::CompilerTest; @yct = 1; end")
136140
end
137141

138142
def test_InstanceVariableAndWriteNode
139-
assert_equal 1, compile("@yct = 0; @yct &&= 1")
143+
test_yarp_eval("@yct = 0; @yct &&= 1")
140144
end
141145

142146
def test_InstanceVariableOrWriteNode
143-
assert_equal 1, compile("@yct ||= 1")
147+
test_yarp_eval("@yct ||= 1")
144148
end
145149

146150
def test_InstanceVariableOperatorWriteNode
147-
assert_equal 1, compile("@yct = 0; @yct += 1")
151+
test_yarp_eval("@yct = 0; @yct += 1")
148152
end
149153

150154
def test_LocalVariableWriteNode
151-
assert_equal 1, compile("yct = 1")
155+
test_yarp_eval("yct = 1")
152156
end
153157

154158
def test_LocalVariableAndWriteNode
155-
assert_equal 1, compile("yct = 0; yct &&= 1")
159+
test_yarp_eval("yct = 0; yct &&= 1")
156160
end
157161

158162
def test_LocalVariableOrWriteNode
159-
assert_equal 1, compile("yct ||= 1")
163+
test_yarp_eval("yct ||= 1")
160164
end
161165

162166
def test_LocalVariableOperatorWriteNode
163-
assert_equal 1, compile("yct = 0; yct += 1")
167+
test_yarp_eval("yct = 0; yct += 1")
164168
end
165169

166170
############################################################################
167171
# String-likes #
168172
############################################################################
169173

170174
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"')
174178
end
175179

176180
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/')
180184
end
181185

182186
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"')
185189
end
186190

187191
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"')
190194
end
191195

192196
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"`')
195199
end
196200

197201
def test_RegularExpressionNode
198-
assert_equal /yct/, compile('/yct/')
202+
test_yarp_eval('/yct/')
199203
end
200204

201205
def test_StringConcatNode
202-
# assert_equal "YARP::CompilerTest", compile('"YARP" "::" "CompilerTest"')
206+
# test_yarp_eval('"YARP" "::" "CompilerTest"')
203207
end
204208

205209
def test_StringNode
206-
assert_equal "yct", compile('"yct"')
210+
test_yarp_eval('"yct"')
207211
end
208212

209213
def test_SymbolNode
210-
assert_equal :yct, compile(":yct")
214+
test_yarp_eval(":yct")
211215
end
212216

213217
def test_XStringNode
214-
# assert_equal "yctyct", compile(<<~RUBY)
218+
# test_yarp_eval(<<~RUBY)
215219
# class YARP::CompilerTest
216220
# def self.`(command) = command * 2
217221
# `yct`
@@ -224,28 +228,31 @@ def test_XStringNode
224228
############################################################################
225229

226230
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")
229233
end
230234

231235
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")
234238
end
235239

236240
############################################################################
237241
# Scopes/statements #
238242
############################################################################
239243

240244
def test_ParenthesesNode
241-
assert_equal (), compile("()")
242-
assert_equal (1), compile("(1)")
245+
test_yarp_eval("()")
246+
test_yarp_eval("(1)")
243247
end
244248

245249
private
246250

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
249256
end
250257
end
251258
end

0 commit comments

Comments
 (0)
0