fix the tests for oprerators. Also naming keywords demands change
This commit is contained in:
parent
cc5915135a
commit
4a27314533
@ -4,5 +4,6 @@ require_relative "test_arguments"
|
|||||||
require_relative "test_expressions"
|
require_relative "test_expressions"
|
||||||
require_relative "test_function_call"
|
require_relative "test_function_call"
|
||||||
require_relative "test_conditional"
|
require_relative "test_conditional"
|
||||||
|
require_relative "test_operators"
|
||||||
require_relative "test_function_definition"
|
require_relative "test_function_definition"
|
||||||
require_relative "test_root"
|
require_relative "test_root"
|
@ -43,11 +43,4 @@ class TestBasic < MiniTest::Test
|
|||||||
@parser = @parser.string
|
@parser = @parser.string
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_assignment
|
|
||||||
@string_input = "a = 5"
|
|
||||||
@parse_output = { :asignee => { :name=>"a" } , :asigned => { :integer => "5" } }
|
|
||||||
@transform_output = Ast::AssignmentExpression.new(Ast::NameExpression.new("a"), Ast::IntegerExpression.new(5))
|
|
||||||
@parser = @parser.assignment
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
@ -12,9 +12,9 @@ else
|
|||||||
667
|
667
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = { :conditional => { :integer => "0"},
|
@parse_output = {:if=>"if", :conditional=>{:integer=>"0"},
|
||||||
:if_true => { :expressions => [ { :integer => "42" } ] } ,
|
:if_true=>{:expressions=>[{:integer=>"42"}], :else=>"else"},
|
||||||
:if_false => { :expressions => [ { :integer => "667" } ] } }
|
:if_false=>{:expressions=>[{:integer=>"667"}], :end=>"end"}}
|
||||||
@transform_output = Ast::ConditionalExpression.new( Ast::IntegerExpression.new(0),
|
@transform_output = Ast::ConditionalExpression.new( Ast::IntegerExpression.new(0),
|
||||||
[Ast::IntegerExpression.new(42)], [Ast::IntegerExpression.new(667)])
|
[Ast::IntegerExpression.new(42)], [Ast::IntegerExpression.new(667)])
|
||||||
|
|
||||||
@ -33,12 +33,15 @@ HERE
|
|||||||
# puts(b)
|
# puts(b)
|
||||||
# n = n - 1
|
# n = n - 1
|
||||||
|
|
||||||
@parse_output = {:while=>"while", :while_cond=>{:integer=>"1"}, :do=>"do", :body=>{:expressions=>[{:asignee=>{:name=>"tmp"}, :asigned=>{:name=>"a"}}, {:asignee=>{:name=>"a"}, :asigned=>{:name=>"b"}}]}}
|
@parse_output = {:while=>"while",
|
||||||
|
:while_cond=>{:integer=>"1"},
|
||||||
|
:do=>"do",
|
||||||
|
:body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}}, {:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}}],
|
||||||
|
:end=>"end"}}
|
||||||
@transform_output = Ast::WhileExpression.new(
|
@transform_output = Ast::WhileExpression.new(
|
||||||
Ast::IntegerExpression.new(1),
|
Ast::IntegerExpression.new(1),
|
||||||
[Ast::AssignmentExpression.new(Ast::NameExpression.new("tmp"),
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("a")),
|
||||||
Ast::NameExpression.new("a")),
|
Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::NameExpression.new("b"))] )
|
||||||
Ast::AssignmentExpression.new(Ast::NameExpression.new("a"), Ast::NameExpression.new("b"))] )
|
|
||||||
@parser = @parser.while
|
@parser = @parser.while
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -10,8 +10,8 @@ class TestExpressions < MiniTest::Test
|
|||||||
5
|
5
|
||||||
else
|
else
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}]}
|
@parse_output = {:expressions=>[{:integer=>"4"}, {:integer=>"5"}], :else=>"else"}
|
||||||
@transform_output = {:expressions=>[ Ast::IntegerExpression.new(4), Ast::IntegerExpression.new(5)]}
|
@transform_output = {:expressions=>[ Ast::IntegerExpression.new(4), Ast::IntegerExpression.new(5)], :else=>"else"}
|
||||||
@parser = @parser.expressions_else
|
@parser = @parser.expressions_else
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -22,72 +22,16 @@ name
|
|||||||
call(4,6)
|
call(4,6)
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:expressions => [ { :integer => "5" },
|
@parse_output = {:expressions=>[{:integer=>"5"}, {:name=>"name"},
|
||||||
{ :name => "name" },
|
{:function_call=>{:name=>"call"}, :argument_list=>[{:argument=>{:integer=>"4"}},
|
||||||
{ :function_call => { :name => "call" } ,
|
{:argument=>{:integer=>"6"}}]}], :end=>"end"}
|
||||||
:argument_list => [ {:argument => { :integer => "4" } } ,
|
|
||||||
{:argument => { :integer => "6" } } ] } ]}
|
|
||||||
args = [ Ast::IntegerExpression.new(4) , Ast::IntegerExpression.new(6) ]
|
args = [ Ast::IntegerExpression.new(4) , Ast::IntegerExpression.new(6) ]
|
||||||
@transform_output = {:expressions=>[ Ast::IntegerExpression.new(5),
|
@transform_output = {:expressions=>[ Ast::IntegerExpression.new(5),
|
||||||
Ast::NameExpression.new("name") ,
|
Ast::NameExpression.new("name") ,
|
||||||
Ast::FuncallExpression.new("call", args ) ] }
|
Ast::FuncallExpression.new("call", args ) ] , :end=>"end"}
|
||||||
|
|
||||||
@parser = @parser.expressions_end
|
@parser = @parser.expressions_end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def simple_op op
|
|
||||||
@string_input = "5 #{op} 3"
|
|
||||||
@parse_output = {:l=>{:integer=>"5"}, :o=>"#{op} ", :r=>{:integer=>"3"}}
|
|
||||||
@transform_output = Ast::OperatorExpression.new(op, Ast::IntegerExpression.new(5),Ast::IntegerExpression.new(3))
|
|
||||||
@parser = @parser.operator_expression
|
|
||||||
end
|
|
||||||
def test_simple_multiply
|
|
||||||
simple_op "*"
|
|
||||||
end
|
|
||||||
def test_simple_devide
|
|
||||||
simple_op "/"
|
|
||||||
end
|
|
||||||
def test_simple_plus
|
|
||||||
simple_op "+"
|
|
||||||
end
|
|
||||||
def test_simple_minus
|
|
||||||
simple_op "-"
|
|
||||||
end
|
|
||||||
def test_op_variable
|
|
||||||
@string_input = "a + 35"
|
|
||||||
@parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}}
|
|
||||||
@transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(35))
|
|
||||||
@parser = @parser.operator_expression
|
|
||||||
end
|
|
||||||
def test_op_two_variable
|
|
||||||
@string_input = "a - b"
|
|
||||||
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}}
|
|
||||||
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::NameExpression.new("b"))
|
|
||||||
@parser = @parser.operator_expression
|
|
||||||
end
|
|
||||||
def test_op_variable_string
|
|
||||||
@string_input = 'a - "st"'
|
|
||||||
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}
|
|
||||||
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::StringExpression.new("st"))
|
|
||||||
@parser = @parser.operator_expression
|
|
||||||
end
|
|
||||||
def test_two_same_ops
|
|
||||||
@string_input = '2 + 3 + 4'
|
|
||||||
@parse_output = {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"4"}}}
|
|
||||||
@transform_output = Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)))
|
|
||||||
@parser = @parser.operator_expression
|
|
||||||
end
|
|
||||||
def test_two_different_ops
|
|
||||||
@string_input = '2 + 3 * 4'
|
|
||||||
@parse_output = {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"* ", :r=>{:integer=>"4"}}}
|
|
||||||
@transform_output = Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)))
|
|
||||||
@parser = @parser.operator_expression
|
|
||||||
end
|
|
||||||
def test_two_different_ops_order
|
|
||||||
@string_input = '2 * 3 + 4'
|
|
||||||
@parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"* ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}
|
|
||||||
@transform_output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))
|
|
||||||
@parser = @parser.operator_expression
|
|
||||||
end
|
|
||||||
end
|
end
|
@ -10,9 +10,8 @@ def foo(x)
|
|||||||
5
|
5
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = {:function_definition => {:name => 'foo'},
|
@parse_output = {:function_definition=>{:name=>"foo"},
|
||||||
:parmeter_list => [{:parmeter => {:name => 'x'}}],
|
:parmeter_list=>[{:parmeter=>{:name=>"x"}}], :expressions=>[{:integer=>"5"}], :end=>"end"}
|
||||||
:expressions => [{:integer => '5'}]}
|
|
||||||
@transform_output = Ast::FunctionExpression.new('foo',
|
@transform_output = Ast::FunctionExpression.new('foo',
|
||||||
[Ast::NameExpression.new('x')],
|
[Ast::NameExpression.new('x')],
|
||||||
[Ast::IntegerExpression.new(5)])
|
[Ast::IntegerExpression.new(5)])
|
||||||
@ -25,12 +24,10 @@ def foo(x)
|
|||||||
abba = 5
|
abba = 5
|
||||||
end
|
end
|
||||||
HERE
|
HERE
|
||||||
@parse_output = { :function_definition => { :name => "foo" } ,
|
@parse_output = {:function_definition=>{:name=>"foo"},
|
||||||
:parmeter_list => [{ :parmeter => { :name => "x" } }],
|
:parmeter_list=>[{:parmeter=>{:name=>"x"}}],
|
||||||
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
|
:expressions=>[{:l=>{:name=>"abba"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"}
|
||||||
}
|
@transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] , [Ast::OperatorExpression.new("=", Ast::NameExpression.new("abba"),Ast::IntegerExpression.new(5))] )
|
||||||
@transform_output = Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] ,
|
|
||||||
[Ast::AssignmentExpression.new(Ast::NameExpression.new("abba"), Ast::IntegerExpression.new(5))] )
|
|
||||||
@parser = @parser.function_definition
|
@parser = @parser.function_definition
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -52,8 +49,7 @@ HERE
|
|||||||
:parmeter_list => [{ :parmeter => { :name => "x" } }],
|
:parmeter_list => [{ :parmeter => { :name => "x" } }],
|
||||||
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
|
:expressions => [ { :asignee => { :name => "abba" }, :asigned => { :integer => "5" } } ]
|
||||||
}
|
}
|
||||||
@transform_output = Ast::FunctionExpression.new( "foo", [Ast::NameExpression.new("x")],
|
@transform_output = nil
|
||||||
[Ast::AssignmentExpression.new( "abba", Ast::IntegerExpression.new(5) ) ])
|
|
||||||
@parser = @parser.function_definition
|
@parser = @parser.function_definition
|
||||||
end
|
end
|
||||||
end
|
end
|
68
test/parser/test_operators.rb
Normal file
68
test/parser/test_operators.rb
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
class TestExpressions < MiniTest::Test
|
||||||
|
# include the magic (setup and parse -> test method translation), see there
|
||||||
|
include ParserHelper
|
||||||
|
|
||||||
|
def simple_op op
|
||||||
|
@string_input = "5 #{op} 3"
|
||||||
|
@parse_output = {:l=>{:integer=>"5"}, :o=>"#{op} ", :r=>{:integer=>"3"}}
|
||||||
|
@transform_output = Ast::OperatorExpression.new(op, Ast::IntegerExpression.new(5),Ast::IntegerExpression.new(3))
|
||||||
|
@parser = @parser.operator_expression
|
||||||
|
end
|
||||||
|
def test_simple_multiply
|
||||||
|
simple_op "*"
|
||||||
|
end
|
||||||
|
def test_simple_devide
|
||||||
|
simple_op "/"
|
||||||
|
end
|
||||||
|
def test_simple_plus
|
||||||
|
simple_op "+"
|
||||||
|
end
|
||||||
|
def test_simple_minus
|
||||||
|
simple_op "-"
|
||||||
|
end
|
||||||
|
def test_op_variable
|
||||||
|
@string_input = "a + 35"
|
||||||
|
@parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}}
|
||||||
|
@transform_output = Ast::OperatorExpression.new("+", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(35))
|
||||||
|
@parser = @parser.operator_expression
|
||||||
|
end
|
||||||
|
def test_op_two_variable
|
||||||
|
@string_input = "a - b"
|
||||||
|
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}}
|
||||||
|
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::NameExpression.new("b"))
|
||||||
|
@parser = @parser.operator_expression
|
||||||
|
end
|
||||||
|
def test_op_variable_string
|
||||||
|
@string_input = 'a - "st"'
|
||||||
|
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}
|
||||||
|
@transform_output = Ast::OperatorExpression.new("-", Ast::NameExpression.new("a"),Ast::StringExpression.new("st"))
|
||||||
|
@parser = @parser.operator_expression
|
||||||
|
end
|
||||||
|
def test_two_same_ops
|
||||||
|
@string_input = '2 + 3 + 4'
|
||||||
|
@parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}
|
||||||
|
@transform_output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))
|
||||||
|
@parser = @parser.operator_expression
|
||||||
|
end
|
||||||
|
def test_two_different_ops
|
||||||
|
@string_input = '2 + 3 * 4'
|
||||||
|
@parse_output = {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"* ", :r=>{:integer=>"4"}}}
|
||||||
|
@transform_output = Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)))
|
||||||
|
@parser = @parser.operator_expression
|
||||||
|
end
|
||||||
|
def test_two_different_ops_order
|
||||||
|
@string_input = '2 * 3 + 4'
|
||||||
|
@parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"* ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}
|
||||||
|
@transform_output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))
|
||||||
|
@parser = @parser.operator_expression
|
||||||
|
end
|
||||||
|
def test_assignment
|
||||||
|
@string_input = "a = 5"
|
||||||
|
@parse_output = {:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}
|
||||||
|
@transform_output = Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(5))
|
||||||
|
@parser = @parser.operator_expression
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -12,13 +12,14 @@ end
|
|||||||
|
|
||||||
foo( 3 )
|
foo( 3 )
|
||||||
HERE
|
HERE
|
||||||
@parse_output = [
|
@parse_output = [{:function_definition=>{:name=>"foo"},
|
||||||
{:function_definition=> { :name=>"foo"}, :parmeter_list => [ {:parmeter=>{:name=>"x"}} ] ,
|
:parmeter_list=>[{:parmeter=>{:name=>"x"}}],
|
||||||
:expressions=>[{:asignee=>{:name=>"a"}, :asigned=>{:integer=>"5"} } ] } ,
|
:expressions=>[{:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}], :end=>"end"},
|
||||||
{:function_call => { :name => "foo" }, :argument_list => [ { :argument => { :integer => "3" } } ] }]
|
{:function_call=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"3"}}]}]
|
||||||
@transform_output = [Ast::FunctionExpression.new(:foo, [Ast::NameExpression.new("x")] ,
|
@transform_output = [Ast::FunctionExpression.new(:foo,
|
||||||
[Ast::AssignmentExpression.new(Ast::NameExpression.new("a"), Ast::IntegerExpression.new(5))] ),
|
[Ast::NameExpression.new("x")] ,
|
||||||
Ast::FuncallExpression.new("foo", [Ast::IntegerExpression.new(3)] )]
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::IntegerExpression.new(5))] ),
|
||||||
|
Ast::FuncallExpression.new("foo", [Ast::IntegerExpression.new(3)] )]
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user