-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreetests.rb
56 lines (47 loc) · 1.54 KB
/
treetests.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
require 'test/unit'
load 'transformer.rb'
load 'parser.rb'
class TreeTests < Test::Unit::TestCase
def print (ast, indent = 1)
puts '-' * indent << '|' << ast.class.name
return if !(ast.respond_to? :real_elements) || ast.real_elements.nil?
ast.real_elements.each { |element| print(element, indent + 1) }
end
def process(text)
ast = Parser.parse text
print ast
transformer = Transformer.new ast
transformer.transform
return transformer
end
def test_valid_tree_for_single_production_with_branching
text = "a : 'b' | 'c';"
transformer = process text
node = transformer.get_expansion 'a'
assert_equal 'a', node.name
assert_equal 2, node.children.length
assert_equal "'b'", node.children[0].name
assert_equal "'c'", node.children[1].name
end
def test_valid_tree_for_three_productions
text = "a : b c; b : 'b'; c : 'c'; "
transformer = process text
node = transformer.get_expansion 'a'
assert_equal 'a',node.name
assert_equal 1,node.children.length
assert_equal "b",node.children[0].name
assert_equal "c", (node.children[0]).children[0].name
node = transformer.get_expansion 'b'
assert_equal 'b', node.name
assert_equal "'b'", node.children[0].name
end
def test_valid_tree_for_three_productions_with_branching
text = "a : b c | b; b : 'b'; c : 'c'; "
transformer = process text
node = transformer.get_expansion 'a'
assert_equal 2, node.children.length
assert_equal "b", node.children[0].name
assert_equal "c", node.children[0].children[0].name
assert_equal "b", node.children[1].name
end
end