Commit ed61d6a7 authored by Vincent Ahlström's avatar Vincent Ahlström
Browse files

added tests

parent 7476c5f0
......@@ -338,6 +338,18 @@ class WriteTest < Test::Unit::TestCase
assert_equal(nil , gp.parse_string("write(c)"))
end
end
class IfTest < Test::Unit::TestCase
def test1()
gp = GameLanguage.new
code = "x = 1
if x <10 {'low'} else {'high'}"
assert_equal("low", gp.parse_string(code))
code = "x = 10
if x <10 {'low'} else {'high'}"
assert_equal("high", gp.parse_string(code))
end
end
# class ReadTest < Test::Unit::TestCase
# def test1()
# gp = GameLanguage.new
......
......@@ -444,8 +444,9 @@ class Case
end
def evaluate()
end
end
class While
def initialize(exp, block)
......
......@@ -5,7 +5,7 @@ require 'test/unit'
class FunctionTest < Test::Unit::TestCase
def test1()
gp = GameLanguage.new
code = "def test(i)
{
k = i + p
......@@ -15,10 +15,10 @@ class FunctionTest < Test::Unit::TestCase
assert_equal(nil, gp.parse_string(code))
end
def test2()
gp = GameLanguage.new
code = "def test(i)
{
k = i + 12
......@@ -31,10 +31,10 @@ class FunctionTest < Test::Unit::TestCase
assert_equal(32, gp.parse_string("test(20)"))
assert_equal(13, gp.parse_string("test(2 / 2)"))
end
def test3()
gp = GameLanguage.new
code = "def test1(i)
{
k = i + 12
......@@ -78,7 +78,68 @@ class FunctionTest < Test::Unit::TestCase
assert_equal(20, gp.parse_string("test1(8)"))
assert_equal(25, gp.parse_string("test2(8)"))
assert_equal(17, gp.parse_string("test2(0)"))
end
def test5()
gp = GameLanguage.new
code = "def test1(i)
{
k = i + 12
k
}
def test2(j)
{
t = 6 * test1(j)
t
}
"
assert_equal(nil, gp.parse_string(code))
assert_equal(72, gp.parse_string("test2(0)"))
assert_equal(60, gp.parse_string("test2(-2)"))
assert_equal(0, gp.parse_string("test2(-12)"))
end
def test_recursion1()
gp = GameLanguage.new
code = "def rec(x)
{
if x > 10
{
x
}
else
{
rec (x+3)
}
}
rec(0)"
assert_equal(12, gp.parse_string(code))
end
def test_recursion2()
gp = GameLanguage.new
code = "def rec(x)
{
counter = 0
if x == 5
{
counter
}
else
{
counter = counter + 1
counter + rec (x+1)
}
}
rec(0)"
assert_equal(5, gp.parse_string(code))
assert_equal(0, gp.parse_string("rec(5)"))
end
end
......@@ -150,7 +150,6 @@ class GameLanguage
rule :value do
match(LiteralString)
match(:array)
match(:function_call)
match(:exp)
end
......@@ -227,6 +226,7 @@ class GameLanguage
match("+", "(", :math_exp , ")"){|_, _, m, _| m }
match("-", "(", :math_exp , ")"){|_, _, m, _| Multiplication.new(m, -1) }
match("(", :log_exp , ")"){|_, m, _| m }
match(:function_call)
match(Identifier) {|m| IdentifierNode.new(m)}
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment