convert to test case

This commit is contained in:
Torsten Ruger 2014-04-27 15:56:22 +03:00
parent d72c9a3bb0
commit 5423bc8f7e
3 changed files with 32 additions and 40 deletions

View File

@ -1,7 +0,0 @@
(puts("Usage: #{} SOURCE"); exit) if ARGV.empty?
require_relative 'helper'
require 'vm/compiler'
Thnad::Compiler.new(ARGV.first).compile

View File

@ -4,7 +4,7 @@ require_relative "helper"
#testing that parsing strings that we know to be correct returns the nodes we expect #testing that parsing strings that we know to be correct returns the nodes we expect
# in a way the combination of test_parser and test_transform # in a way the combination of test_parser and test_transform
class NodesCase < MiniTest::Test class TestNodes < MiniTest::Test
def setup def setup
@parser = Parser::Parser.new @parser = Parser::Parser.new

View File

@ -1,69 +1,68 @@
require_relative 'helper' require_relative 'helper'
require 'minitest/spec' require 'minitest/spec'
include Vm class ParserTest < MiniTest::Test
describe Parser do def setup
before do
@parser = Parser::Parser.new @parser = Parser::Parser.new
end end
it 'reads a number' do def check
input = '42 ' is = @parser.parse(@@input)
expected = {:number => '42'} assert is
assert_equal @expected , is
@parser.number.parse(input).must_equal expected end
def test_number
@input = '42 '
@expected = {:number => '42'}
@parser = @parser.number
end end
it 'reads a name' do def test_name
input = 'foo ' @input = 'foo '
expected = {:name => 'foo'} @expected = {:name => 'foo'}
@parser = @parser.name
@parser.name.parse(input).must_equal expected
end end
it 'reads an argument list' do def test_argument_list
input = '(42, foo)' @input = '(42, foo)'
expected = {:args => [{:arg => {:number => '42'}}, @expected = {:args => [{:arg => {:number => '42'}},
{:arg => {:name => 'foo'}}]} {:arg => {:name => 'foo'}}]}
@parser = @parser.args
@parser.args.parse(input).must_equal expected
end end
it 'reads a function call' do def test_function_call
input = 'baz(42, foo)' @input = 'baz(42, foo)'
expected = {:funcall => {:name => 'baz' }, @expected = {:funcall => {:name => 'baz' },
:args => [{:arg => {:number => '42'}}, :args => [{:arg => {:number => '42'}},
{:arg => {:name => 'foo'}}]} {:arg => {:name => 'foo'}}]}
@parser.funcall.parse(input).must_equal expected @parser = @parser.funcall
end end
it 'reads a conditional' do def test_conditional
input = <<HERE @input = <<HERE
if (0) { if (0) {
42 42
} else { } else {
667 667
} }
HERE HERE
@expected = {:cond => {:number => '0'},
expected = {:cond => {:number => '0'},
:if_true => {:body => {:number => '42'}}, :if_true => {:body => {:number => '42'}},
:if_false => {:body => {:number => '667'}}} :if_false => {:body => {:number => '667'}}}
@parser = @parser.cond
@parser.cond.parse(input).must_equal expected
end end
it 'reads a function definition' do def test_function_definition
input = <<HERE @input = <<HERE
function foo(x) { function foo(x) {
5 5
} }
HERE HERE
expected = {:func => {:name => 'foo'}, @expected = {:func => {:name => 'foo'},
:params => {:param => {:name => 'x'}}, :params => {:param => {:name => 'x'}},
:body => {:number => '5'}} :body => {:number => '5'}}
@parser.func.parse(input).must_equal expected @parser = @parser.func
end end
end end