2014-05-12 11:20:31 +02:00
|
|
|
require_relative "helper"
|
|
|
|
|
|
|
|
class TestWhile < MiniTest::Test
|
|
|
|
# include the magic (setup and parse -> test method translation), see there
|
|
|
|
include ParserHelper
|
|
|
|
|
|
|
|
def test_while
|
|
|
|
@string_input = <<HERE
|
2014-05-12 11:26:38 +02:00
|
|
|
while(1) do
|
2014-05-12 11:20:31 +02:00
|
|
|
tmp = a
|
|
|
|
puts(b)
|
|
|
|
end
|
|
|
|
HERE
|
2014-05-12 12:47:47 +02:00
|
|
|
@string_input.chop!
|
2014-05-12 11:20:31 +02:00
|
|
|
@parse_output = {:while=>"while",
|
2014-05-12 11:26:38 +02:00
|
|
|
:while_cond=>{:integer=>"1"},
|
|
|
|
:do=>"do",
|
|
|
|
:body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}},
|
2014-05-13 20:15:02 +02:00
|
|
|
{:call_site=>{:name=>"puts"}, :argument_list=>[{:argument=>{:name=>"b"}}]}], :end=>"end"}}
|
2014-05-12 11:20:31 +02:00
|
|
|
@transform_output = Ast::WhileExpression.new(
|
2014-05-12 11:26:38 +02:00
|
|
|
Ast::IntegerExpression.new(1),
|
2014-05-12 11:20:31 +02:00
|
|
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("a")),
|
2014-05-13 20:15:02 +02:00
|
|
|
Ast::CallSiteExpression.new("puts", [Ast::NameExpression.new("b")] )] )
|
2014-05-12 11:20:31 +02:00
|
|
|
@parser = @parser.while_do
|
|
|
|
end
|
2014-05-12 13:24:36 +02:00
|
|
|
|
|
|
|
def test_big_while
|
|
|
|
@string_input = <<HERE
|
|
|
|
while( n > 1) do
|
|
|
|
tmp = a
|
|
|
|
a = b
|
|
|
|
b = tmp + b
|
|
|
|
puts(b)
|
|
|
|
n = n - 1
|
|
|
|
end
|
|
|
|
HERE
|
|
|
|
@string_input.chop!
|
|
|
|
@parse_output = {:while=>"while",
|
|
|
|
:while_cond=>{:l=>{:name=>"n"}, :o=>"> ", :r=>{:integer=>"1"}},
|
|
|
|
:do=>"do",
|
|
|
|
:body=>{:expressions=>[{:l=>{:name=>"tmp"}, :o=>"= ", :r=>{:name=>"a"}},
|
|
|
|
{:l=>{:name=>"a"}, :o=>"= ", :r=>{:name=>"b"}},
|
|
|
|
{:l=>{:name=>"b"}, :o=>"= ", :r=>{:l=>{:name=>"tmp"}, :o=>"+ ", :r=>{:name=>"b"}}},
|
2014-05-13 20:15:02 +02:00
|
|
|
{:call_site=>{:name=>"puts"},
|
2014-05-12 13:24:36 +02:00
|
|
|
:argument_list=>[{:argument=>{:name=>"b"}}]},
|
|
|
|
{:l=>{:name=>"n"}, :o=>"= ", :r=>{:l=>{:name=>"n"}, :o=>"- ", :r=>{:integer=>"1"}}}],
|
|
|
|
:end=>"end"}}
|
|
|
|
@transform_output = Ast::WhileExpression.new(
|
|
|
|
Ast::OperatorExpression.new(">", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)),
|
|
|
|
[Ast::OperatorExpression.new("=", Ast::NameExpression.new("tmp"),Ast::NameExpression.new("a")),
|
|
|
|
Ast::OperatorExpression.new("=", Ast::NameExpression.new("a"),Ast::NameExpression.new("b")),
|
|
|
|
Ast::OperatorExpression.new("=", Ast::NameExpression.new("b"),Ast::OperatorExpression.new("+", Ast::NameExpression.new("tmp"),
|
2014-05-13 20:15:02 +02:00
|
|
|
Ast::NameExpression.new("b"))), Ast::CallSiteExpression.new("puts", [Ast::NameExpression.new("b")] ),
|
2014-05-12 13:24:36 +02:00
|
|
|
Ast::OperatorExpression.new("=", Ast::NameExpression.new("n"),Ast::OperatorExpression.new("-", Ast::NameExpression.new("n"),Ast::IntegerExpression.new(1)))] )
|
|
|
|
@parser = @parser.while_do
|
|
|
|
end
|
2014-05-12 11:20:31 +02:00
|
|
|
end
|