starting with own node tests

This commit is contained in:
Torsten Ruger 2014-04-24 19:45:22 +03:00
parent c411ac5df8
commit 2b1a56b4fe
2 changed files with 24 additions and 89 deletions

View File

@ -45,7 +45,7 @@ module Vm
((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >> ((name.as(:param) >> (comma >> name.as(:param)).repeat(0)).maybe).as(:params) >>
rparen rparen
} }
root :args
rule(:root) { func.repeat(0) >> expression } # rule(:root) { func.repeat(0) >> expression }
end end
end end

View File

@ -1,98 +1,33 @@
require_relative "helper" require_relative "helper"
require 'minitest/spec'
include Vm
class FakeBuilder #testing that parsing strings that we know to be correct returns the nodes we expect
attr_reader :result # in a way the combination of test_parser and test_transform
Asm::InstructionTools::REGISTERS.each do |reg , number| class NodesCase < MiniTest::Test
define_method(reg) { Asm::Register.new(reg , number) }
def setup
@parser = Vm::Parser.new
@transform = Vm::Transform.new
end end
def initialize def parse string
@result = '' syntax = @parser.parse(string)
tree = @transform.apply(syntax)
tree
end end
def class_builder def test_number
'example' tree = parse "42"
assert tree.is_a? Vm::NumberExpression
assert_equal 42 , tree.value
end end
def test_args
def int tree = parse "( 42 )"
'int' assert tree.is_a? Hash
end assert tree[:args].is_a? Vm::NumberExpression
assert_equal 42 , tree[:args].value
def method_missing(name, *args, &block)
@result += ([name] + args.flatten).join(', ').sub(',', '')
@result += "\n"
block.call(self) if name.to_s == 'public_static_method'
end end
end end
describe 'Nodes' do
before do
@context = Hash.new
@builder = FakeBuilder.new
end
it 'emits a number' do
input = Vm::NumberExpression.new 42
expected = <<HERE
mov r0, 42
HERE
input.eval @context, @builder
@builder.result.must_equal expected
end
it 'emits a function call' do
@context[:params] = ['foo']
input = Vm::FuncallExpression.new 'baz', [Vm::NumberExpression.new(42),
Vm::NameExpression.new('foo')]
expected = <<HERE
mov r0, 42
iload 0
invokestatic example, baz, int, int, int
HERE
input.eval @context, @builder
@builder.result.must_equal expected
end
it 'emits a conditional' do
input = Vm::ConditionalExpression.new \
Vm::NumberExpression.new(0),
Vm::NumberExpression.new(42),
Vm::NumberExpression.new(667)
expected = <<HERE
mov r0, 0
ifeq else
mov r0, 42
goto endif
label else
mov r0, 667
label endif
HERE
input.eval @context, @builder
@builder.result.must_equal expected
end
it 'emits a function definition' do
input = Vm::FunctionExpression.new \
'foo',
Vm::NameExpression.new('x'),
Vm::NumberExpression.new(5)
expected = <<HERE
public_static_method foo, int, int
mov r0, 5
ireturn
HERE
input.eval @context, @builder
@builder.result.must_equal expected
end
end