pseudohikiparser 0.0.0.4.develop

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,190 @@
1
+ #/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'htmlelement/htmltemplate'
5
+
6
+ class TC_HtmlTemplate < Test::Unit::TestCase
7
+
8
+ def test_new
9
+ html_result = <<HTML
10
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
11
+ "http://www.w3.org/TR/html4/loose.dtd">
12
+ <html lang="en">
13
+ <head>
14
+ <meta content="en" http-equiv="Content-Language">
15
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
16
+ <meta content="text/javascript" http-equiv="Content-Script-Type">
17
+ <title></title>
18
+ <link href="default.css" rel="stylesheet" type="text/css">
19
+ </head>
20
+ <body>
21
+ </body>
22
+ </html>
23
+ HTML
24
+
25
+ assert_equal(html_result, HtmlTemplate.new.to_s)
26
+ end
27
+
28
+ def test_charset
29
+ sjis_result =<<HTML
30
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
31
+ "http://www.w3.org/TR/html4/loose.dtd">
32
+ <html lang="ja">
33
+ <head>
34
+ <meta content="ja" http-equiv="Content-Language">
35
+ <meta content="text/html; charset=Shift_JIS" http-equiv="Content-Type">
36
+ <meta content="text/javascript" http-equiv="Content-Script-Type">
37
+ <title></title>
38
+ <link href="default.css" rel="stylesheet" type="text/css">
39
+ </head>
40
+ <body>
41
+ </body>
42
+ </html>
43
+ HTML
44
+
45
+ euc_jp_result = <<HTML
46
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
47
+ "http://www.w3.org/TR/html4/loose.dtd">
48
+ <html lang="ja">
49
+ <head>
50
+ <meta content="ja" http-equiv="Content-Language">
51
+ <meta content="text/html; charset=EUC-JP" http-equiv="Content-Type">
52
+ <meta content="text/javascript" http-equiv="Content-Script-Type">
53
+ <title></title>
54
+ <link href="default.css" rel="stylesheet" type="text/css">
55
+ </head>
56
+ <body>
57
+ </body>
58
+ </html>
59
+ HTML
60
+
61
+ html = HtmlTemplate.new
62
+
63
+ html.sjis!
64
+ assert_equal(sjis_result, html.to_s)
65
+
66
+ html.euc_jp!
67
+ assert_equal(euc_jp_result, html.to_s)
68
+
69
+ html.charset = 'Shift_JIS'
70
+ assert_equal(sjis_result, html.to_s)
71
+ end
72
+
73
+ def test_base
74
+ html_result = <<HTML
75
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
76
+ "http://www.w3.org/TR/html4/loose.dtd">
77
+ <html lang="en">
78
+ <head>
79
+ <meta content="en" http-equiv="Content-Language">
80
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
81
+ <meta content="text/javascript" http-equiv="Content-Script-Type">
82
+ <title></title>
83
+ <link href="default.css" rel="stylesheet" type="text/css">
84
+ <base href="/base/path">
85
+ </head>
86
+ <body>
87
+ </body>
88
+ </html>
89
+ HTML
90
+
91
+ html = HtmlTemplate.new
92
+ html.base = '/base/path'
93
+ assert_equal(html_result, html.to_s)
94
+ end
95
+ end
96
+
97
+ class TC_XhtmlTemplate < Test::Unit::TestCase
98
+
99
+ def test_new
100
+ html_result = <<HTML
101
+ <?xml version="1.0" encoding="UTF-8"?>
102
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
103
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
104
+ <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
105
+ <head>
106
+ <meta content="en" http-equiv="Content-Language" />
107
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
108
+ <meta content="text/javascript" http-equiv="Content-Script-Type" />
109
+ <title></title>
110
+ <link href="default.css" rel="stylesheet" type="text/css" />
111
+ </head>
112
+ <body>
113
+ </body>
114
+ </html>
115
+ HTML
116
+
117
+ assert_equal(html_result, XhtmlTemplate.new.to_s)
118
+ end
119
+
120
+ def test_charset
121
+ sjis_result =<<HTML
122
+ <?xml version="1.0" encoding="Shift_JIS"?>
123
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
124
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
125
+ <html lang="ja" xml:lang="ja" xmlns="http://www.w3.org/1999/xhtml">
126
+ <head>
127
+ <meta content="ja" http-equiv="Content-Language" />
128
+ <meta content="text/html; charset=Shift_JIS" http-equiv="Content-Type" />
129
+ <meta content="text/javascript" http-equiv="Content-Script-Type" />
130
+ <title></title>
131
+ <link href="default.css" rel="stylesheet" type="text/css" />
132
+ </head>
133
+ <body>
134
+ </body>
135
+ </html>
136
+ HTML
137
+
138
+ euc_jp_result = <<HTML
139
+ <?xml version="1.0" encoding="EUC-JP"?>
140
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
141
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
142
+ <html lang="ja" xml:lang="ja" xmlns="http://www.w3.org/1999/xhtml">
143
+ <head>
144
+ <meta content="ja" http-equiv="Content-Language" />
145
+ <meta content="text/html; charset=EUC-JP" http-equiv="Content-Type" />
146
+ <meta content="text/javascript" http-equiv="Content-Script-Type" />
147
+ <title></title>
148
+ <link href="default.css" rel="stylesheet" type="text/css" />
149
+ </head>
150
+ <body>
151
+ </body>
152
+ </html>
153
+ HTML
154
+
155
+ html = XhtmlTemplate.new
156
+
157
+ html.sjis!
158
+ assert_equal(sjis_result, html.to_s)
159
+
160
+ html.euc_jp!
161
+ assert_equal(euc_jp_result, html.to_s)
162
+
163
+ html.charset = 'Shift_JIS'
164
+ assert_equal(sjis_result, html.to_s)
165
+ end
166
+
167
+ def test_base
168
+ html_result = <<HTML
169
+ <?xml version="1.0" encoding="UTF-8"?>
170
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
171
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
172
+ <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
173
+ <head>
174
+ <meta content="en" http-equiv="Content-Language" />
175
+ <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
176
+ <meta content="text/javascript" http-equiv="Content-Script-Type" />
177
+ <title></title>
178
+ <link href="default.css" rel="stylesheet" type="text/css" />
179
+ <base href="/base/path" />
180
+ </head>
181
+ <body>
182
+ </body>
183
+ </html>
184
+ HTML
185
+
186
+ html = XhtmlTemplate.new
187
+ html.base = '/base/path'
188
+ assert_equal(html_result, html.to_s)
189
+ end
190
+ end
@@ -0,0 +1,94 @@
1
+ #/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'lib/pseudohiki/inlineparser'
5
+ require 'lib/pseudohiki/htmlformat'
6
+
7
+
8
+ class TC_InlineParser < Test::Unit::TestCase
9
+ include PseudoHiki
10
+
11
+ def test_inlineparser_compile_token_pat
12
+ parser = InlineParser.new("")
13
+ assert_equal(/'''|\}\}|\|\||\{\{|\]\]|\[\[|==|''|\||:/,parser.token_pat)
14
+ end
15
+
16
+ def test_inlineparser_split_into_tokens
17
+ parser = InlineParser.new("")
18
+ tokens = parser.split_into_tokens("As a test case, '''this part''' must be in <strong>.")
19
+ assert_equal(["As a test case, ","'''","this part","'''"," must be in <strong>."],tokens)
20
+ tokens = parser.split_into_tokens("As another {{test case}}, '''this part''' must be in <strong>.")
21
+ assert_equal(["As another ","{{","test case","}}", ", ","'''","this part","'''"," must be in <strong>."],tokens)
22
+ tokens = parser.split_into_tokens("As another ''test case'', '''this part''' must be in <strong>.")
23
+ assert_equal(["As another ","''","test case","''", ", ","'''","this part","'''"," must be in <strong>."],tokens)
24
+ end
25
+
26
+ def test_inlineparser_parse
27
+ tree = InlineParser.parse("As {{''another test'' case}}, '''this part''' must be in <strong>.")
28
+ assert_equal([["As "], [[["another test"]], [" case"]], [", "], [["this part"]], [" must be in <strong>."]],tree)
29
+ tree = InlineParser.parse("this is a line that ends with a {{node}}")
30
+ assert_equal([["this is a line that ends with a "],[["node"]]],tree)
31
+ tree = InlineParser.parse("this is another line that ends with a {{node")
32
+ assert_equal([["this is another line that ends with a "],[["node"]]],tree)
33
+ end
34
+
35
+ def test_inlineparser_convert_last_node_into_leaf
36
+ parser = InlineParser.new("this is another line that ends with a {{node")
37
+ stack = parser.parse
38
+ assert_equal([["this is another line that ends with a "],[["node"]]],stack.tree)
39
+ stack.convert_last_node_into_leaf
40
+ assert_equal([["this is another line that ends with a "],["{{"], ["node"]],stack.tree)
41
+ tree = InlineParser.parse("As {{''another '''test'' case}}, '''this part''' must be in <strong>.")
42
+ assert_equal([["As "], [[["another "], ["'''"], ["test"]], [" case"]], [", "], [["this part"]], [" must be in <strong>."]],tree)
43
+ tree = InlineParser.parse("As {{''another ]]test case with a [[node]]''.}}, '''this part''' must be in <strong>.")
44
+ assert_equal([["As "], [[["another "], ["]]"], ["test case with a "], [["node"]]], ["."]], [", "], [["this part"]], [" must be in <strong>."]],tree)
45
+ tree = InlineParser.parse("As {{''another {{test'' case}}, '''this part''' must be in <strong>.")
46
+ assert_equal([["As "], [[["another "], ["{{"], ["test"]], [" case"]], [", "], [["this part"]], [" must be in <strong>."]],tree)
47
+ end
48
+ end
49
+
50
+ class TC_HtmlFormat < Test::Unit::TestCase
51
+ include PseudoHiki
52
+
53
+ def test_visit_linknode
54
+ formatter = HtmlFormat.get_plain
55
+
56
+ tree = InlineParser.parse("[[image.html]] is a link to a html file.")
57
+ assert_equal('<a href="image.html">image.html</a> is a link to a html file.', tree.accept(formatter).to_s)
58
+
59
+ tree = InlineParser.parse("[[LINK|image.html]] is a link to a html file.")
60
+ assert_equal('<a href="image.html">LINK</a> is a link to a html file.', tree.accept(formatter).to_s)
61
+ assert_equal('<a href="image.html">LINK</a> is a link to a html file.', tree.accept(formatter).to_s)
62
+
63
+ tree = InlineParser.parse("[[LINK|http://www.example.org/]] is a link to an url.")
64
+ assert_equal('<a href="http://www.example.org/">LINK</a> is a link to an url.', tree.accept(formatter).to_s)
65
+ assert_equal('<a href="http://www.example.org/">LINK</a> is a link to an url.', tree.accept(formatter).to_s)
66
+
67
+ tree = InlineParser.parse("[[an explanation about {{co2}}|co2.html]] is a link to a html file.")
68
+ assert_equal('<a href="co2.html">an explanation about <span>co2</span></a> is a link to a html file.', tree.accept(formatter).to_s)
69
+ assert_equal('<a href="co2.html">an explanation about <span>co2</span></a> is a link to a html file.', tree.accept(formatter).to_s)
70
+
71
+ tree = InlineParser.parse("[[image name|image.png]] is a link to a image file.")
72
+ assert_equal("<img alt=\"image name\" src=\"image.png\">\n is a link to a image file.", tree.accept(formatter).to_s)
73
+
74
+ tree = InlineParser.parse("[[image.png]] is a link to a image file.")
75
+ assert_equal("<img src=\"image.png\">\n is a link to a image file.", tree.accept(formatter).to_s)
76
+
77
+ tree = InlineParser.parse("[[link with an empty uri|]]")
78
+ assert_equal("<a href=\"\">link with an empty uri</a>", tree.accept(formatter).to_s)
79
+ end
80
+
81
+ def test_visit_leafnode
82
+ formatter = HtmlFormat.get_plain
83
+ tree = InlineParser.parse("a string with <charactors> that are replaced by &entity references.")
84
+ assert_equal("a string with &lt;charactors&gt; that are replaced by &amp;entity references.", tree.accept(formatter).to_s)
85
+ tree = InlineParser.parse("a string with a token |.")
86
+ assert_equal("a string with a token |.", tree.accept(formatter).to_s)
87
+ end
88
+
89
+ def test_visit_pluginnode
90
+ formatter = HtmlFormat.get_plain
91
+ tree = InlineParser.new("{{co2}} represents the carbon dioxide.").parse.tree
92
+ assert_equal("<span>co2</span> represents the carbon dioxide.",tree.accept(formatter).to_s)
93
+ end
94
+ end
@@ -0,0 +1,205 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'pseudohiki/plaintextformat'
5
+
6
+ class TC_PlainTextFormat < Test::Unit::TestCase
7
+ include PseudoHiki
8
+
9
+ def setup
10
+ @formatter = PlainTextFormat.create
11
+ @verbose_formatter = PlainTextFormat.create(:verbose_mode => true)
12
+ @strict_formatter = PlainTextFormat.create(:strict_mode => true)
13
+ end
14
+
15
+ def test_plain
16
+ text = <<TEXT
17
+ test string
18
+ TEXT
19
+ tree = BlockParser.parse(text.lines.to_a)
20
+ assert_equal("test string\n\n", @formatter.format(tree).to_s)
21
+ end
22
+
23
+ def test_em
24
+ text = <<TEXT
25
+ A test string with ''emphasis'' is here.
26
+ TEXT
27
+ tree = BlockParser.parse(text.lines.to_a)
28
+ assert_equal("A test string with emphasis is here.\n\n", @formatter.format(tree).to_s)
29
+ end
30
+
31
+ def test_strong
32
+ text = <<TEXT
33
+ A test string with '''strong''' is here.
34
+ TEXT
35
+ tree = BlockParser.parse(text.lines.to_a)
36
+ assert_equal("A test string with strong is here.\n\n", @formatter.format(tree).to_s)
37
+ end
38
+
39
+ def test_del
40
+ text = <<TEXT
41
+ A test string ==with deleted words ==is here.
42
+ TEXT
43
+ expected_text = <<TEXT
44
+ A test string is here.
45
+
46
+ TEXT
47
+
48
+ expected_text_in_verbose_mode = <<TEXT
49
+ A test string [deleted:with deleted words ]is here.
50
+
51
+ TEXT
52
+ tree = BlockParser.parse(text.lines.to_a)
53
+ assert_equal(expected_text, @formatter.format(tree).to_s)
54
+ assert_equal(expected_text_in_verbose_mode, @verbose_formatter.format(tree).to_s)
55
+ end
56
+
57
+ def test_link_url
58
+ text = <<TEXT
59
+ A test string with a [[link|http://www.example.org/]] is here.
60
+ TEXT
61
+ tree = BlockParser.parse(text.lines.to_a)
62
+ assert_equal("A test string with a link is here.\n\n", @formatter.format(tree).to_s)
63
+ assert_equal("A test string with a link (http://www.example.org/) is here.\n\n",
64
+ @verbose_formatter.format(tree).to_s)
65
+ end
66
+
67
+ def test_link_image
68
+ text = <<TEXT
69
+ A test string with an [[image|image.jpg]] is here.
70
+ TEXT
71
+ tree = BlockParser.parse(text.lines.to_a)
72
+ assert_equal("A test string with an image is here.\n\n", @formatter.format(tree).to_s)
73
+ end
74
+
75
+ def test_commentout
76
+ text = <<TEXT
77
+ lines including a comment out in these
78
+ //Comment out
79
+ another line
80
+ TEXT
81
+ expected_text = <<TEXT
82
+ lines including a comment out in these
83
+
84
+ another line
85
+
86
+ TEXT
87
+
88
+ tree = BlockParser.parse(text.lines.to_a)
89
+ assert_equal(expected_text, @formatter.format(tree).to_s)
90
+ end
91
+
92
+ def test_heading
93
+ text = <<TEXT
94
+ !!heading
95
+ a normal line
96
+ TEXT
97
+
98
+ expected_text = <<TEXT
99
+ heading
100
+ a normal line
101
+
102
+ TEXT
103
+ tree = BlockParser.parse(text.lines.to_a)
104
+ assert_equal(expected_text, @formatter.format(tree).to_s)
105
+ end
106
+
107
+ def test_desc
108
+ text = <<TEXT
109
+ :tel: 03-xxxx-xxxx
110
+ :: 03-yyyy-yyyy
111
+ :fax: 03-xxxx-xxxx
112
+ TEXT
113
+
114
+ expected_text = <<TEXT
115
+ tel: 03-xxxx-xxxx
116
+ 03-yyyy-yyyy
117
+ fax: 03-xxxx-xxxx
118
+ TEXT
119
+ tree = BlockParser.parse(text.lines.to_a)
120
+ assert_equal(expected_text, @formatter.format(tree).to_s)
121
+ end
122
+
123
+ def test_table
124
+ text = <<TEXT
125
+ ||cell 1-1||^>> cell 1-2||cell 1-5
126
+ ||cell 2-1||cell 2-5
127
+ TEXT
128
+
129
+ expected_text = <<TEXT
130
+ cell 1-1 cell 1-2 cell 1-5
131
+ cell 2-1 cell 2-5
132
+ TEXT
133
+ tree = BlockParser.parse(text.lines.to_a)
134
+ assert_equal(expected_text, @formatter.format(tree).to_s)
135
+ end
136
+
137
+ def test_verbose_mode_table
138
+ text = <<TEXT
139
+ ||cell 1-1||^>> cell 1-2||cell 1-5
140
+ ||cell 2-1||cell 2-5
141
+ TEXT
142
+
143
+ expected_verbose_text = <<TEXT
144
+ cell 1-1 cell 1-2 == == cell 1-5
145
+ cell 2-1 || || || cell 2-5
146
+ TEXT
147
+ tree = BlockParser.parse(text.lines.to_a)
148
+ assert_equal(expected_verbose_text, @verbose_formatter.format(tree).to_s)
149
+ end
150
+
151
+ def test_verbose_mode_table_with_expansion_in_the_last_column
152
+ text = <<TEXT
153
+ ||cell 1-1||^> cell 1-2||>cell 1-4
154
+ ||cell 2-1||cell 2-4||cell 2-5
155
+ TEXT
156
+
157
+ expected_verbose_text = <<TEXT
158
+ cell 1-1 cell 1-2 == cell 1-4 ==
159
+ cell 2-1 || || cell 2-4 cell 2-5
160
+ TEXT
161
+ tree = BlockParser.parse(text.lines.to_a)
162
+ assert_equal(expected_verbose_text, @verbose_formatter.format(tree).to_s)
163
+ end
164
+
165
+ def test_malformed_table
166
+ well_formed_text = <<TEXT
167
+ ||cell 1-1||>>cell 1-2,3,4||cell 1-5
168
+ ||cell 2-1||^>cell 2-2,3 3-2,3||cell 2-4||cell 2-5
169
+ ||cell 3-1||cell 3-4||cell 3-5
170
+ ||cell 4-1||cell 4-2||cell 4-3||cell 4-4||cell 4-5
171
+ TEXT
172
+
173
+ expected_well_formed_text = <<TEXT
174
+ cell 1-1 cell 1-2,3,4 == == cell 1-5
175
+ cell 2-1 cell 2-2,3 3-2,3 == cell 2-4 cell 2-5
176
+ cell 3-1 || || cell 3-4 cell 3-5
177
+ cell 4-1 cell 4-2 cell 4-3 cell 4-4 cell 4-5
178
+ TEXT
179
+
180
+ tree = BlockParser.parse(well_formed_text.lines.to_a)
181
+ assert_equal(expected_well_formed_text, @verbose_formatter.format(tree).to_s)
182
+
183
+ mal_formed_text = <<TEXT
184
+ ||cell 1-1||>>cell 1-2,3,4||cell 1-5
185
+ ||cell 2-1||^>cell 2-2,3 3-2,3||cell 2-5
186
+ ||cell 3-1||cell 3-4||cell 3-5
187
+ ||cell 4-1||cell 4-2||cell 4-3||cell 4-4||cell 4-5
188
+ TEXT
189
+
190
+ expected_mal_formed_text = <<TEXT
191
+ cell 1-1 cell 1-2,3,4 == == cell 1-5
192
+ cell 2-1 cell 2-2,3 3-2,3 == cell 2-5
193
+ cell 3-1 || || cell 3-4 cell 3-5
194
+ cell 4-1 cell 4-2 cell 4-3 cell 4-4 cell 4-5
195
+ TEXT
196
+
197
+ assert_raise(PlainTextFormat::TableNodeFormatter::MalFormedTableError) do
198
+ tree = BlockParser.parse(mal_formed_text.lines.to_a)
199
+ @strict_formatter.format(tree).to_s
200
+ end
201
+
202
+ tree = BlockParser.parse(mal_formed_text.lines.to_a)
203
+ assert_equal(expected_mal_formed_text, @verbose_formatter.format(tree).to_s)
204
+ end
205
+ end
@@ -0,0 +1,133 @@
1
+ #/usr/bin/env ruby
2
+
3
+ require 'test/unit'
4
+ require 'pseudohiki/treestack'
5
+
6
+ class TC_TreeStack < Test::Unit::TestCase
7
+
8
+ def test_push_node
9
+ stack = TreeStack.new
10
+ node = TreeStack::Node.new
11
+ stack.push(node)
12
+ assert_equal([[]],stack.tree)
13
+ second_node = TreeStack::Node.new
14
+ stack.push(second_node)
15
+ assert_equal([[[]]],stack.tree)
16
+ end
17
+
18
+ def test_push_leaf
19
+ stack = TreeStack.new
20
+ leaf = TreeStack::Leaf.new
21
+ leaf.push "leaf"
22
+ stack.push(leaf)
23
+ assert_equal([["leaf"]],stack.tree)
24
+ second_leaf = TreeStack::Leaf.new
25
+ second_leaf.push "second_leaf"
26
+ stack.push(second_leaf)
27
+ assert_equal([["leaf"],["second_leaf"]],stack.tree)
28
+ end
29
+
30
+ def test_push_leaf_then_node
31
+ end
32
+
33
+ def test_without_mergeable
34
+ node = TreeStack::Node.new
35
+ assert_same node.class, TreeStack::Node
36
+ assert_equal true, node.kind_of?(TreeStack::Node)
37
+ assert_not_equal true, node.kind_of?(TreeStack::Mergeable)
38
+ end
39
+
40
+ def test_with_mergeable
41
+ node = TreeStack::Node.new
42
+ class <<node #
43
+ include TreeStack::Mergeable
44
+ end
45
+ assert node.kind_of?(TreeStack::Mergeable)
46
+ end
47
+
48
+ def test_node_end
49
+ tree = TreeStack.new
50
+ assert tree.node_end.kind_of?(TreeStack::NodeEnd)
51
+ end
52
+
53
+ def test_leaf_create
54
+ leaf = TreeStack::Leaf.create("leaf")
55
+ assert_equal(["leaf"],leaf)
56
+ assert_kind_of(TreeStack::Leaf,leaf)
57
+ empty_leaf = TreeStack::Leaf.create
58
+ assert_equal([],empty_leaf)
59
+ assert_kind_of(TreeStack::Leaf,leaf)
60
+ end
61
+
62
+ def test_push_as_sibling
63
+ stack = TreeStack.new
64
+ leaf = TreeStack::Leaf.create("leaf")
65
+ # leaf.push "leaf"
66
+ stack.push(leaf)
67
+ assert_equal([["leaf"]],stack.tree)
68
+ node = TreeStack::Node.new
69
+ stack.push(node)
70
+ stack.push(leaf)
71
+ assert_equal([["leaf"],[["leaf"]]],stack.tree)
72
+ subnode = TreeStack::Node.new
73
+ stack.push(subnode)
74
+ stack.push(leaf)
75
+ assert_equal([["leaf"],[["leaf"],[["leaf"]]]],stack.tree)
76
+ sibling_node = TreeStack::Node.new
77
+ stack.push_as_sibling(sibling_node)
78
+ stack.push(leaf)
79
+ assert_equal([["leaf"],[["leaf"],[["leaf"]],[["leaf"]]]],stack.tree)
80
+ end
81
+
82
+ def test_depth
83
+ stack = TreeStack.new
84
+ leaf = TreeStack::Leaf.create("leaf")
85
+ leaf2 = TreeStack::Leaf.create("leaf2")
86
+ leaf3 = TreeStack::Leaf.create("leaf2")
87
+ leaf4 = TreeStack::Leaf.create("leaf2")
88
+ node = TreeStack::Node.new
89
+ node2 = TreeStack::Node.new
90
+ assert_nil(leaf.depth)
91
+ stack.push(leaf)
92
+ assert_equal(1,leaf.depth)
93
+ stack.push(leaf2)
94
+ assert_equal(1,leaf2.depth)
95
+ stack.push(node)
96
+ assert_equal(1,node.depth)
97
+ stack.push(leaf3)
98
+ assert_equal(2,leaf3.depth)
99
+ stack.push(node2)
100
+ assert_equal(2,node2.depth)
101
+ stack.push(leaf4)
102
+ assert_equal(3,leaf4.depth)
103
+ end
104
+
105
+ def test_last_leaf
106
+ stack = TreeStack.new
107
+ leaf = TreeStack::Leaf.create("leaf")
108
+ node = TreeStack::Node.new
109
+ stack.push(leaf)
110
+ assert_equal(leaf,stack.last_leaf)
111
+ stack.push(node)
112
+ assert_nil(stack.last_leaf)
113
+ end
114
+
115
+ def test_node_end
116
+ stack = TreeStack.new
117
+ leaf1 = TreeStack::Leaf.create("leaf1")
118
+ leaf2 = TreeStack::Leaf.create("leaf2")
119
+ leaf3 = TreeStack::Leaf.create("leaf3")
120
+ leaf4 = TreeStack::Leaf.create("leaf4")
121
+ node = TreeStack::Node.new
122
+ node_end = stack.node_end
123
+ stack.push(leaf1)
124
+ stack.push(node)
125
+ stack.push(leaf2)
126
+ stack.push(leaf3)
127
+ assert_equal(["leaf3"],stack.last_leaf)
128
+ stack.push(node_end)
129
+ assert_nil(stack.last_leaf)
130
+ stack.push(leaf4)
131
+ assert_equal([["leaf1"],[["leaf2"],["leaf3"]],["leaf4"]],stack.tree)
132
+ end
133
+ end