1
require 'abstract_unit'
3
class TagNodeTest < Test::Unit::TestCase
4
def test_open_without_attributes
6
assert_equal "tag", node.name
7
assert_equal Hash.new, node.attributes
8
assert_nil node.closing
11
def test_open_with_attributes
12
node = tag("<TAG1 foo=hey_ho x:bar=\"blah blah\" BAZ='blah blah blah' >")
13
assert_equal "tag1", node.name
14
assert_equal "hey_ho", node["foo"]
15
assert_equal "blah blah", node["x:bar"]
16
assert_equal "blah blah blah", node["baz"]
19
def test_self_closing_without_attributes
21
assert_equal "tag", node.name
22
assert_equal Hash.new, node.attributes
23
assert_equal :self, node.closing
26
def test_self_closing_with_attributes
27
node = tag("<tag a=b/>")
28
assert_equal "tag", node.name
29
assert_equal( { "a" => "b" }, node.attributes )
30
assert_equal :self, node.closing
33
def test_closing_without_attributes
35
assert_equal "tag", node.name
36
assert_nil node.attributes
37
assert_equal :close, node.closing
40
def test_bracket_op_when_no_attributes
42
assert_nil node["foo"]
45
def test_bracket_op_when_attributes
46
node = tag("<tag a=b/>")
47
assert_equal "b", node["a"]
50
def test_attributes_with_escaped_quotes
51
node = tag("<tag a='b\\'c' b=\"bob \\\"float\\\"\">")
52
assert_equal "b\\'c", node["a"]
53
assert_equal "bob \\\"float\\\"", node["b"]
57
node = tag("<a b=c d='f' g=\"h 'i'\" />")
58
assert_equal %(<a b='c' d='f' g='h \\'i\\'' />), node.to_s
62
assert tag("<tag>").tag?
65
def test_match_tag_as_string
66
assert tag("<tag>").match(:tag => "tag")
67
assert !tag("<tag>").match(:tag => "b")
70
def test_match_tag_as_regexp
71
assert tag("<tag>").match(:tag => /t.g/)
72
assert !tag("<tag>").match(:tag => /t[bqs]g/)
75
def test_match_attributes_as_string
76
t = tag("<tag a=something b=else />")
77
assert t.match(:attributes => {"a" => "something"})
78
assert t.match(:attributes => {"b" => "else"})
81
def test_match_attributes_as_regexp
82
t = tag("<tag a=something b=else />")
83
assert t.match(:attributes => {"a" => /^something$/})
84
assert t.match(:attributes => {"b" => /e.*e/})
85
assert t.match(:attributes => {"a" => /me..i/, "b" => /.ls.$/})
88
def test_match_attributes_as_number
89
t = tag("<tag a=15 b=3.1415 />")
90
assert t.match(:attributes => {"a" => 15})
91
assert t.match(:attributes => {"b" => 3.1415})
92
assert t.match(:attributes => {"a" => 15, "b" => 3.1415})
95
def test_match_attributes_exist
96
t = tag("<tag a=15 b=3.1415 />")
97
assert t.match(:attributes => {"a" => true})
98
assert t.match(:attributes => {"b" => true})
99
assert t.match(:attributes => {"a" => true, "b" => true})
102
def test_match_attributes_not_exist
103
t = tag("<tag a=15 b=3.1415 />")
104
assert t.match(:attributes => {"c" => false})
105
assert t.match(:attributes => {"c" => nil})
106
assert t.match(:attributes => {"a" => true, "c" => false})
109
def test_match_parent_success
110
t = tag("<tag a=15 b='hello'>", tag("<foo k='value'>"))
111
assert t.match(:parent => {:tag => "foo", :attributes => {"k" => /v.l/, "j" => false}})
114
def test_match_parent_fail
115
t = tag("<tag a=15 b='hello'>", tag("<foo k='value'>"))
116
assert !t.match(:parent => {:tag => /kafka/})
119
def test_match_child_success
120
t = tag("<tag x:k='something'>")
121
tag("<child v=john a=kelly>", t)
122
tag("<sib m=vaughn v=james>", t)
123
assert t.match(:child => { :tag => "sib", :attributes => {"v" => /j/}})
124
assert t.match(:child => { :attributes => {"a" => "kelly"}})
127
def test_match_child_fail
128
t = tag("<tag x:k='something'>")
129
tag("<child v=john a=kelly>", t)
130
tag("<sib m=vaughn v=james>", t)
131
assert !t.match(:child => { :tag => "sib", :attributes => {"v" => /r/}})
132
assert !t.match(:child => { :attributes => {"v" => false}})
135
def test_match_ancestor_success
136
t = tag("<tag x:k='something'>", tag("<parent v=john a=kelly>", tag("<grandparent m=vaughn v=james>")))
137
assert t.match(:ancestor => {:tag => "parent", :attributes => {"a" => /ll/}})
138
assert t.match(:ancestor => {:attributes => {"m" => "vaughn"}})
141
def test_match_ancestor_fail
142
t = tag("<tag x:k='something'>", tag("<parent v=john a=kelly>", tag("<grandparent m=vaughn v=james>")))
143
assert !t.match(:ancestor => {:tag => /^parent/, :attributes => {"v" => /m/}})
144
assert !t.match(:ancestor => {:attributes => {"v" => false}})
147
def test_match_descendant_success
148
tag("<grandchild m=vaughn v=james>", tag("<child v=john a=kelly>", t = tag("<tag x:k='something'>")))
149
assert t.match(:descendant => {:tag => "child", :attributes => {"a" => /ll/}})
150
assert t.match(:descendant => {:attributes => {"m" => "vaughn"}})
153
def test_match_descendant_fail
154
tag("<grandchild m=vaughn v=james>", tag("<child v=john a=kelly>", t = tag("<tag x:k='something'>")))
155
assert !t.match(:descendant => {:tag => /^child/, :attributes => {"v" => /m/}})
156
assert !t.match(:descendant => {:attributes => {"v" => false}})
159
def test_match_child_count
160
t = tag("<tag x:k='something'>")
162
tag("<child v=john a=kelly>", t)
163
tag("<sib m=vaughn v=james>", t)
164
assert t.match(:children => { :count => 2 })
165
assert t.match(:children => { :count => 2..4 })
166
assert t.match(:children => { :less_than => 4 })
167
assert t.match(:children => { :greater_than => 1 })
168
assert !t.match(:children => { :count => 3 })
171
def test_conditions_as_strings
172
t = tag("<tag x:k='something'>")
173
assert t.match("tag" => "tag")
174
assert t.match("attributes" => { "x:k" => "something" })
175
assert !t.match("tag" => "gat")
176
assert !t.match("attributes" => { "x:j" => "something" })
179
def test_attributes_as_symbols
180
t = tag("<child v=john a=kelly>")
181
assert t.match(:attributes => { :v => /oh/ })
182
assert t.match(:attributes => { :a => /ll/ })
185
def test_match_sibling
186
t = tag("<tag x:k='something'>")
190
m = tag("<span k=r>", t)
193
assert m.match(:sibling => {:tag => "span", :attributes => {:a => true}})
194
assert m.match(:sibling => {:tag => "span", :attributes => {:m => true}})
195
assert !m.match(:sibling => {:tag => "span", :attributes => {:k => true}})
198
def test_match_sibling_before
199
t = tag("<tag x:k='something'>")
203
m = tag("<span k=r>", t)
206
assert m.match(:before => {:tag => "span", :attributes => {:m => true}})
207
assert !m.match(:before => {:tag => "span", :attributes => {:a => true}})
208
assert !m.match(:before => {:tag => "span", :attributes => {:k => true}})
211
def test_match_sibling_after
212
t = tag("<tag x:k='something'>")
216
m = tag("<span k=r>", t)
219
assert m.match(:after => {:tag => "span", :attributes => {:a => true}})
220
assert !m.match(:after => {:tag => "span", :attributes => {:m => true}})
221
assert !m.match(:after => {:tag => "span", :attributes => {:k => true}})
225
t = tag("<b x='foo'>")
228
assert_equal %(<b x="foo">hello<hr /></b>), t.to_s
233
def tag(content, parent=nil)
234
node = HTML::Node.parse(parent,0,0,content)
235
parent.children << node if parent