unify grammar file
This commit is contained in:
@ -4,46 +4,16 @@ require_relative "setup"
|
||||
module ParserHelper
|
||||
|
||||
def self.included(base)
|
||||
base.send :include, InstanceMethods #provides helpers and setup
|
||||
base.send :extend, ClassMethods #gets the method creation going
|
||||
# base.send :include, InstanceMethods #provides helpers and setup
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def setup
|
||||
@parser = Keywords
|
||||
end
|
||||
|
||||
# check that @input parses correctly to @input
|
||||
def check_parse
|
||||
is = @parser.parse(@input , :root => @root)
|
||||
assert_equal @input , is
|
||||
end
|
||||
|
||||
# check that @input parses and transforms to @output
|
||||
def check_ast
|
||||
syntax = @parser.parse(@input , :root => @root)
|
||||
#puts is.inspect
|
||||
assert_equal @output , syntax.value
|
||||
end
|
||||
def setup
|
||||
@parser = Bosl
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# this creates test methods dynamically. For each test_* method we create
|
||||
# three test_*[ast/parse/transf] methods that in turn check the three phases.
|
||||
# runnable_methods is called by minitest to determine which tests to run
|
||||
def runnable_methods
|
||||
tests = []
|
||||
public_instance_methods(true).grep(/^test_/).map(&:to_s).each do |test|
|
||||
["ast" , "parse"].each do |what|
|
||||
name = "#{test}_#{what}"
|
||||
tests << name
|
||||
self.send(:define_method, name ) do
|
||||
send(test)
|
||||
send("check_#{what}")
|
||||
end
|
||||
end
|
||||
end
|
||||
tests
|
||||
end
|
||||
end
|
||||
def check rule = :root
|
||||
parse = @parser.parse(@input , :root => rule)
|
||||
assert_equal @input , parse
|
||||
assert_equal @output , parse.value
|
||||
end
|
||||
end
|
||||
|
@ -1,16 +1,8 @@
|
||||
require_relative "../setup"
|
||||
require_relative "../parser_helper"
|
||||
|
||||
class TestBasic < MiniTest::Test
|
||||
#test basics and keyword expressions. Keywords includes BasicTypes
|
||||
def setup
|
||||
@parser = BasicTypes
|
||||
end
|
||||
|
||||
def check rule = :root
|
||||
parse = @parser.parse(@input , :root => rule)
|
||||
assert_equal @input , parse
|
||||
assert_equal @output , parse.value
|
||||
end
|
||||
include ParserHelper
|
||||
|
||||
def test_true
|
||||
@input = 'true '
|
||||
|
@ -1,39 +1,31 @@
|
||||
require_relative "../parser_helper"
|
||||
|
||||
class TestCallSite < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@parser = Expression
|
||||
end
|
||||
def check
|
||||
parse = @parser.parse(@input)
|
||||
assert_equal @input , parse
|
||||
assert_equal @output , parse.value
|
||||
end
|
||||
include ParserHelper
|
||||
|
||||
def test_single_self
|
||||
@input = 'self.foo(42)'
|
||||
@output = Ast::CallSiteExpression.new :foo, [Ast::IntegerExpression.new(42)]
|
||||
check
|
||||
check :call_site
|
||||
end
|
||||
|
||||
def test_single_name
|
||||
@input = 'my_my.foo(42)'
|
||||
@parse_output = {:receiver=>{:name=>"my_my"}, :call_site=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"42"}}]}
|
||||
@output = Ast::CallSiteExpression.new(:foo, [Ast::IntegerExpression.new(42)] ,Ast::NameExpression.new("my_my"))
|
||||
check
|
||||
check :call_site
|
||||
end
|
||||
|
||||
def test_int_receiver
|
||||
@input = '42.put()'
|
||||
@output = Ast::CallSiteExpression.new(:put, [] ,Ast::IntegerExpression.new(42))
|
||||
check
|
||||
check :call_site
|
||||
end
|
||||
|
||||
def test_string_receiver
|
||||
@input = '"hello".puts()'
|
||||
@output = Ast::CallSiteExpression.new(:puts, [] ,Ast::StringExpression.new("hello"))
|
||||
check
|
||||
check :call_site
|
||||
end
|
||||
|
||||
def test_call_site_2
|
||||
@ -41,7 +33,7 @@ class TestCallSite < MiniTest::Test
|
||||
@output = Ast::CallSiteExpression.new :baz,
|
||||
[Ast::IntegerExpression.new(42), Ast::NameExpression.new("foo") ] ,
|
||||
Ast::NameExpression.new(:self)
|
||||
check
|
||||
check :call_site
|
||||
end
|
||||
|
||||
def test_call_site_3
|
||||
@ -51,7 +43,7 @@ class TestCallSite < MiniTest::Test
|
||||
Ast::NameExpression.new("foo") ,
|
||||
Ast::NameExpression.new("bar")] ,
|
||||
Ast::NameExpression.new(:self)
|
||||
check
|
||||
check :call_site
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -1,33 +1,24 @@
|
||||
require_relative "../parser_helper"
|
||||
|
||||
class TestConditional < MiniTest::Test
|
||||
|
||||
def setup
|
||||
@parser = Statement
|
||||
end
|
||||
|
||||
def check
|
||||
parse = @parser.parse(@input)
|
||||
assert_equal @input , parse
|
||||
assert_equal @output , parse.value
|
||||
end
|
||||
include ParserHelper
|
||||
|
||||
def test_assignment
|
||||
@input = "myvar = 42"
|
||||
@output = Ast::AssignmentExpression.new(:myvar,Ast::IntegerExpression.new(42))
|
||||
check
|
||||
check :assignment
|
||||
end
|
||||
|
||||
def test_variable_declaration
|
||||
@input = "int myvar"
|
||||
@output = Ast::VariableDefinition.new(:int,:myvar,nil)
|
||||
check
|
||||
check :variable_definition
|
||||
end
|
||||
|
||||
def test_variable_declaration_value
|
||||
@input = "int myvar = 42"
|
||||
@output = Ast::VariableDefinition.new(:int,:myvar,Ast::IntegerExpression.new(42))
|
||||
check
|
||||
check :variable_definition
|
||||
end
|
||||
|
||||
def test_if
|
||||
@ -37,7 +28,7 @@ if( 1 )
|
||||
end
|
||||
HERE
|
||||
@output = Ast::IfExpression.new(Ast::IntegerExpression.new(1), [Ast::VariableDefinition.new(:int,:num,Ast::IntegerExpression.new(42))],nil )
|
||||
check
|
||||
check :conditional
|
||||
end
|
||||
|
||||
def ttest_conditional_with_calls
|
||||
|
Reference in New Issue
Block a user