mirror of
https://github.com/TangentFoxy/lua-htmlparser.git
synced 2025-07-28 02:52:19 +00:00
added test_attr (and fixed one or two edge cases)
This commit is contained in:
61
tst/init.lua
61
tst/init.lua
@@ -6,10 +6,9 @@ local lunitx = require("lunitx")
|
||||
module("html", lunitx.testcase, package.seeall)
|
||||
|
||||
local htmlparser = require("htmlparser")
|
||||
local tree, sel
|
||||
|
||||
function test_void()
|
||||
tree = htmlparser.parse([[
|
||||
local tree = htmlparser.parse([[
|
||||
<p>
|
||||
<br>
|
||||
<br/>
|
||||
@@ -27,14 +26,62 @@ function test_void()
|
||||
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")
|
||||
for _ in pairs(n.attributes) do
|
||||
fail("should not have attributes")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function test_attr()
|
||||
local tree = htmlparser.parse([[
|
||||
<n a1 a2= a3='' a4=""
|
||||
a5='a"5"' a6="a'6'" a7='a 7' a8='a=8'
|
||||
a9='en-gb' a10='enen'
|
||||
a11='one two three'
|
||||
></n>
|
||||
<m a9="en-us" a10></m>
|
||||
<l a9="enen" a11="three four five"></l>
|
||||
]])
|
||||
assert_equal(3, #tree.nodes, "top level")
|
||||
local n
|
||||
for _,v in ipairs(tree.nodes) do
|
||||
if v.name == "n" then n = v break end
|
||||
end
|
||||
assert(tree("[a1]")[n], "a1")
|
||||
assert(tree("[a2]")[n], "a2")
|
||||
assert(tree("[a3]")[n], "a3")
|
||||
assert(tree("[a4]")[n], "a4")
|
||||
assert(tree("[a5]")[n], "a5")
|
||||
assert(tree("[a6]")[n], "a6")
|
||||
assert(tree("[a7]")[n], "a7")
|
||||
assert(tree("[a8]")[n], "a8")
|
||||
assert(tree("[a1='']")[n], "a1=''")
|
||||
assert(tree("[a2='']")[n], "a2=''")
|
||||
assert(tree("[a3='']")[n], "a3=''")
|
||||
assert(tree("[a4='']")[n], "a4=''")
|
||||
assert(tree("[a5='a\"5\"']")[n], "a5='a\"5\"'")
|
||||
assert(tree("[a6=\"a'6'\"]")[n], "a6=\"a'6'\"")
|
||||
assert(tree("[a8='a=8']")[n], "a8='a=8'")
|
||||
assert_equal(1, tree("[a10=]"):len(), "a10=")
|
||||
assert_equal(1, tree("[a10='']"):len(), "a10=''")
|
||||
assert_equal(2, tree("[a10!='enen']"):len(), "a10!='enen'")
|
||||
assert_equal(2, tree("[a10!='']"):len(), "a10!=''")
|
||||
assert_equal(3, tree("[a0!='']"):len(), "a0!=''")
|
||||
assert_equal(0, tree("[a0='']"):len(), "a0=''")
|
||||
assert_equal(2, tree("[a9|='en']"):len(), "a9|='en'")
|
||||
assert_equal(3, tree("[a9^='en']"):len(), "a9^='en'")
|
||||
assert_equal(1, tree("[a9$='en']"):len(), "a9$='en'")
|
||||
assert_equal(1, tree("[a11~='two']"):len(), "a1~='two'")
|
||||
assert_equal(2, tree("[a11~='three']"):len(), "a1~='three'")
|
||||
assert_equal(1, tree("[a11~='four']"):len(), "a1~='four'")
|
||||
assert_equal(1, tree("[a7*='7']"):len(), "a7*='7'")
|
||||
assert_equal(1, tree("[a11*='f']"):len(), "a11*='f'")
|
||||
end
|
||||
|
||||
function test_descendants()
|
||||
tree = htmlparser.parse([[
|
||||
local tree = htmlparser.parse([[
|
||||
<parent>1
|
||||
<child>1</child>
|
||||
<child>2
|
||||
@@ -57,12 +104,12 @@ function test_descendants()
|
||||
<child>not</child>
|
||||
</arbitrary>
|
||||
]])
|
||||
sel = tree("parent child")
|
||||
local sel = tree("parent child")
|
||||
assert_equal(8, sel:len(), 'parent child')
|
||||
end
|
||||
|
||||
function test_children()
|
||||
tree = htmlparser.parse([[
|
||||
local tree = htmlparser.parse([[
|
||||
<parent>1
|
||||
<child>1</child>
|
||||
<child>2
|
||||
@@ -85,6 +132,6 @@ function test_children()
|
||||
<child>not</child>
|
||||
</arbitrary>
|
||||
]])
|
||||
sel = tree("parent > child")
|
||||
local sel = tree("parent > child")
|
||||
assert_equal(4, sel:len(), 'parent > child')
|
||||
end
|
Reference in New Issue
Block a user