a good stab at array anf hash constants

This commit is contained in:
Torsten Ruger
2014-05-12 21:36:38 +03:00
parent 25a7c3ea68
commit fdb5dd4f67
14 changed files with 126 additions and 24 deletions

View File

@ -1,5 +1,6 @@
require_relative "test_basic"
require_relative "test_compund"
require_relative "test_arguments"
require_relative "test_expressions"
require_relative "test_function_call"

View File

@ -0,0 +1,44 @@
require_relative "helper"
class TestCompound < MiniTest::Test
# include the magic (setup and parse -> test method translation), see there
include ParserHelper
def test_one_array
@string_input = '[42]'
@parse_output = {:array=>[{:element=>{:integer=>"42"}}]}
@transform_output = Ast::ArrayExpression.new([Ast::IntegerExpression.new(42)])
@parser = @parser.array
end
def test_array_list
@string_input = '[42, foo]'
@parse_output = {:array=>[{:element=>{:integer=>"42"}}, {:element=>{:name=>"foo"}}]}
@transform_output = Ast::ArrayExpression.new([Ast::IntegerExpression.new(42), Ast::NameExpression.new("foo")])
@parser = @parser.array
end
def test_array_ops
@string_input = '[ 3 + 4 , foo(22) ]'
@parse_output = {:array=>[{:element=>{:l=>{:integer=>"3"}, :o=>"+ ", :r=>{:integer=>"4"}}}, {:element=>{:function_call=>{:name=>"foo"}, :argument_list=>[{:argument=>{:integer=>"22"}}]}}]}
@transform_output = Ast::ArrayExpression.new(
[Ast::OperatorExpression.new("+", Ast::IntegerExpression.new(3),Ast::IntegerExpression.new(4)),
Ast::FuncallExpression.new("foo", [Ast::IntegerExpression.new(22)] )])
@parser = @parser.array
end
def test_hash
@string_input = '{ foo => 33 }'
@parse_output = {:hash=>[{:hash_pair=>{:argument=>{:name=>"foo"}, :element=>{:integer=>"33"}}}]}
@transform_output = Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new("foo") , Ast::IntegerExpression.new(33))])
@parser = @parser.hash
end
def test_hash_list
@string_input = "{foo => 33 , bar => 42}"
@parse_output = {:hash=>[{:hash_pair=>{:argument=>{:name=>"foo"}, :element=>{:integer=>"33"}}}, {:hash_pair=>{:argument=>{:name=>"bar"}, :element=>{:integer=>"42"}}}]}
@transform_output = Ast::HashExpression.new([Ast::AssociationExpression.new(Ast::NameExpression.new("foo") , Ast::IntegerExpression.new(33)),Ast::AssociationExpression.new(Ast::NameExpression.new("bar") , Ast::IntegerExpression.new(42))])
@parser = @parser.hash
end
end

View File

@ -1,11 +1,11 @@
def fibonaccit(n)
a = 0
b = 1
while n > 1 do
while( n > 1 ) do
tmp = a
a = b
b = tmp + b
puts b
puts(b)
n = n - 1
end
end