This repository has been archived by the owner on Oct 23, 2019. It is now read-only.
/
test_parser.rb
72 lines (57 loc) · 1.57 KB
/
test_parser.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
$: << File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'minitest/autorun'
require 'minitest/spec'
require 'thnad/parser'
include Thnad
describe Parser do
before do
@parser = Thnad::Parser.new
end
it 'reads a number' do
input = '42 '
expected = {:number => '42'}
@parser.number.parse(input).must_equal expected
end
it 'reads a name' do
input = 'foo '
expected = {:name => 'foo'}
@parser.name.parse(input).must_equal expected
end
it 'reads an argument list' do
input = '(42, foo)'
expected = {:args => [{:arg => {:number => '42'}},
{:arg => {:name => 'foo'}}]}
@parser.args.parse(input).must_equal expected
end
it 'reads a function call' do
input = 'baz(42, foo)'
expected = {:funcall => {:name => 'baz' },
:args => [{:arg => {:number => '42'}},
{:arg => {:name => 'foo'}}]}
@parser.funcall.parse(input).must_equal expected
end
it 'reads a conditional' do
input = <<HERE
if (0) {
42
} else {
667
}
HERE
expected = {:cond => {:number => '0'},
:if_true => {:body => {:number => '42'}},
:if_false => {:body => {:number => '667'}}}
@parser.cond.parse(input).must_equal expected
end
it 'reads a function definition' do
input = <<HERE
function foo(x) {
5
}
HERE
expected = {:func => {:name => 'foo'},
:params => {:param => {:name => 'x'}},
:body => {:number => '5'}}
@parser.func.parse(input).must_equal expected
end
end