2013-04-02 10:07:49 +00:00
|
|
|
require("luarocks.loader")
|
|
|
|
-- Omit next line in actual module clients; it's only to support development of the module itself
|
|
|
|
package.path = "../src/?.lua;" .. package.path
|
|
|
|
|
|
|
|
local lunitx = require("lunitx")
|
|
|
|
module("html", lunitx.testcase, package.seeall)
|
|
|
|
|
|
|
|
local htmlparser = require("htmlparser")
|
2013-04-02 10:13:23 +00:00
|
|
|
local tree, sel
|
2013-04-02 10:07:49 +00:00
|
|
|
|
2013-04-02 10:59:44 +00:00
|
|
|
function test_void()
|
|
|
|
tree = htmlparser.parse([[
|
|
|
|
<p>
|
|
|
|
<br>
|
|
|
|
<br/>
|
|
|
|
<br >
|
|
|
|
<br />
|
|
|
|
</p>
|
|
|
|
<br>
|
|
|
|
<br/>
|
|
|
|
<br >
|
|
|
|
<br />
|
|
|
|
]])
|
|
|
|
assert_equal(5, #tree.nodes, "top level")
|
|
|
|
for _,n in ipairs(tree.nodes) do
|
|
|
|
if n.name == "p" then
|
|
|
|
assert_equal(4, #n.nodes, "deeper level")
|
|
|
|
else
|
|
|
|
assert_equal("br", n.name, "name")
|
|
|
|
assert_equal(0, #n.attributes, "attributes")
|
|
|
|
assert_equal("", n:getcontent(), "content")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-02 10:13:23 +00:00
|
|
|
function test_descendants()
|
2013-04-02 10:07:49 +00:00
|
|
|
tree = htmlparser.parse([[
|
|
|
|
<parent>1
|
2013-04-02 10:59:44 +00:00
|
|
|
<child>1</child>
|
|
|
|
<child>2
|
|
|
|
<child>3</child>
|
2013-04-02 10:07:49 +00:00
|
|
|
</child>
|
2013-04-02 10:59:44 +00:00
|
|
|
<arbitrary>
|
|
|
|
<child>4</child>
|
|
|
|
</arbitrary>
|
2013-04-02 10:07:49 +00:00
|
|
|
</parent>
|
|
|
|
<parent>2
|
2013-04-02 10:59:44 +00:00
|
|
|
<child>5</child>
|
|
|
|
<child>6
|
|
|
|
<child>7</child>
|
2013-04-02 10:07:49 +00:00
|
|
|
</child>
|
2013-04-02 10:59:44 +00:00
|
|
|
<arbitrary>
|
|
|
|
<child>8</child>
|
|
|
|
</arbitrary>
|
2013-04-02 10:07:49 +00:00
|
|
|
</parent>
|
|
|
|
]])
|
|
|
|
sel = tree("parent child")
|
2013-04-02 10:59:44 +00:00
|
|
|
assert_equal(8, sel:len(), 'parent child')
|
2013-04-02 10:13:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function test_children()
|
|
|
|
tree = htmlparser.parse([[
|
|
|
|
<parent>1
|
2013-04-02 10:59:44 +00:00
|
|
|
<child>1</child>
|
|
|
|
<child>2
|
|
|
|
<child>not</child>
|
2013-04-02 10:13:23 +00:00
|
|
|
</child>
|
2013-04-02 10:59:44 +00:00
|
|
|
<arbitrary>
|
|
|
|
<child>not</child>
|
|
|
|
</arbitrary>
|
2013-04-02 10:13:23 +00:00
|
|
|
</parent>
|
|
|
|
<parent>2
|
2013-04-02 10:59:44 +00:00
|
|
|
<child>3</child>
|
|
|
|
<child>4
|
|
|
|
<child>not</child>
|
2013-04-02 10:13:23 +00:00
|
|
|
</child>
|
2013-04-02 10:59:44 +00:00
|
|
|
<arbitrary>
|
|
|
|
<child>not</child>
|
|
|
|
</arbitrary>
|
2013-04-02 10:13:23 +00:00
|
|
|
</parent>
|
|
|
|
]])
|
2013-04-02 10:07:49 +00:00
|
|
|
sel = tree("parent > child")
|
|
|
|
assert_equal(4, sel:len(), 'parent > child')
|
|
|
|
end
|