2014-06-26 10:55:49 +02:00
|
|
|
require_relative "../parser_helper"
|
2014-06-04 18:56:50 +02:00
|
|
|
|
|
|
|
class TestExpressions < MiniTest::Test
|
|
|
|
# include the magic (setup and parse -> test method translation), see there
|
|
|
|
include ParserHelper
|
|
|
|
|
|
|
|
def simple_op op
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = "5 #{op} 3"
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:integer=>"5"}, :o=>"#{op} ", :r=>{:integer=>"3"}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::OperatorExpression.new(op, Ast::IntegerExpression.new(5),Ast::IntegerExpression.new(3))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
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_simple_greater
|
|
|
|
simple_op ">"
|
|
|
|
end
|
|
|
|
def test_simple_smaller
|
|
|
|
simple_op "<"
|
|
|
|
end
|
|
|
|
def test_op_variable
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = "a + 35"
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:name=>"a"}, :o=>"+ ", :r=>{:integer=>"35"}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::OperatorExpression.new("+", Ast::NameExpression.new(:a),Ast::IntegerExpression.new(35))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
def test_op_two_variable
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = "a - b"
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:name=>"b"}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::NameExpression.new("b"))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
def test_op_instance_variable
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = "@a - 5"
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"- ", :r=>{:integer=>"5"}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::OperatorExpression.new("-", Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
def test_op_variable_string
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = 'a - "st"'
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:name=>"a"}, :o=>"- ", :r=>{:string=>[{:char=>"s"}, {:char=>"t"}]}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::OperatorExpression.new("-", Ast::NameExpression.new(:a),Ast::StringExpression.new("st"))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
def test_two_same_ops
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = '2 + 3 + 4'
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
def test_two_different_ops
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = '2 + 3 * 4'
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:integer=>"2"}, :o=>"+ ", :r=>{:l=>{:integer=>"3"}, :o=>"* ", :r=>{:integer=>"4"}}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(2),Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
def test_two_different_ops_order
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = '2 * 3 + 4'
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:l=>{:integer=>"2"}, :o=>"* ", :r=>{:integer=>"3"}}, :o=>"+ ", :r=>{:integer=>"4"}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::OperatorExpression.new("+", Ast::OperatorExpression.new("*", Ast::IntegerExpression.new(2),Ast::IntegerExpression.new(3)),Ast::IntegerExpression.new(4))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
def test_assignment
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = "a = 5"
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:name=>"a"}, :o=>"= ", :r=>{:integer=>"5"}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::AssignmentExpression.new(Ast::NameExpression.new(:a),Ast::IntegerExpression.new(5))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
def test_assignment_instance
|
2015-08-28 22:37:25 +02:00
|
|
|
@input = "@a = 5"
|
2014-06-04 18:56:50 +02:00
|
|
|
@parse_output = {:l=>{:instance_variable=>{:name=>"a"}}, :o=>"= ", :r=>{:integer=>"5"}}
|
2015-08-28 22:37:25 +02:00
|
|
|
@output = Ast::AssignmentExpression.new(Ast::VariableExpression.new(:a),Ast::IntegerExpression.new(5))
|
|
|
|
@root = :operator_expression
|
2014-06-04 18:56:50 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|