mirror of
https://github.com/ruby/ruby.git
synced 2025-08-28 07:26:00 +02:00
parent
3cec94624b
commit
b18e05b18f
10 changed files with 154 additions and 36 deletions
30
test/yarp/compiler_test.rb
Normal file
30
test/yarp/compiler_test.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require_relative "test_helper"
|
||||
|
||||
module YARP
|
||||
class CompilerTest < TestCase
|
||||
class SExpressions < YARP::Compiler
|
||||
def visit_arguments_node(node)
|
||||
[:arguments, super]
|
||||
end
|
||||
|
||||
def visit_call_node(node)
|
||||
[:call, super]
|
||||
end
|
||||
|
||||
def visit_integer_node(node)
|
||||
[:integer]
|
||||
end
|
||||
|
||||
def visit_program_node(node)
|
||||
[:program, super]
|
||||
end
|
||||
end
|
||||
|
||||
def test_compiler
|
||||
expected = [:program, [[[:call, [[:integer], [:arguments, [[:integer]]]]]]]]
|
||||
assert_equal expected, YARP.parse("1 + 2").value.accept(SExpressions.new)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -3,7 +3,7 @@
|
|||
require_relative "test_helper"
|
||||
|
||||
module YARP
|
||||
class DesugarVisitorTest < TestCase
|
||||
class DesugarCompilerTest < TestCase
|
||||
def test_and_write
|
||||
assert_desugars("(AndNode (ClassVariableReadNode) (ClassVariableWriteNode (CallNode)))", "@@foo &&= bar")
|
||||
assert_not_desugared("Foo::Bar &&= baz", "Desugaring would execute Foo twice or need temporary variables")
|
||||
|
@ -72,7 +72,7 @@ module YARP
|
|||
end
|
||||
|
||||
def assert_desugars(expected, source)
|
||||
ast = YARP.parse(source).value.accept(DesugarVisitor.new)
|
||||
ast = YARP.parse(source).value.accept(DesugarCompiler.new)
|
||||
assert_equal expected, ast_inspect(ast.statements.body.last)
|
||||
|
||||
ast.accept(EnsureEveryNodeOnceInAST.new)
|
||||
|
@ -80,7 +80,7 @@ module YARP
|
|||
|
||||
def assert_not_desugared(source, reason)
|
||||
ast = YARP.parse(source).value
|
||||
assert_equal_nodes(ast, ast.accept(DesugarVisitor.new))
|
||||
assert_equal_nodes(ast, ast.accept(DesugarCompiler.new))
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue