Feature/xmltree additions (#20988)
* [change] add/insert/delete family of xmltree expanded with several variations. Added replace methods family * [change] Lifted child limitations on insert methods (consulted with @araq) * [tests] add/insert/replace/delete of xmltree XmlNodes tests added
This commit is contained in:
parent
93b59da490
commit
7931bdac95
9 changed files with 552 additions and 3 deletions
51
tests/xml/ttree_add.nim
Normal file
51
tests/xml/ttree_add.nim
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
discard """
|
||||
output: '''
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
'''
|
||||
"""
|
||||
|
||||
# Test xmltree add/insert/delete/replace operations
|
||||
import xmlparser
|
||||
import xmltree
|
||||
var baseDocHead = """
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
</xml>
|
||||
"""
|
||||
var baseDocHeadTree = parseXml(baseDocHead)
|
||||
var baseDocBody = """
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
"""
|
||||
var baseDocBodyTree = parseXml(baseDocBody)
|
||||
|
||||
proc test_add() =
|
||||
var testDoc = baseDocHeadTree
|
||||
var newBody = newElement("body")
|
||||
for item in baseDocBodyTree.items():
|
||||
newBody.add(item)
|
||||
|
||||
echo $newBody
|
||||
|
||||
testDoc.add(newBody)
|
||||
echo $testDoc
|
||||
|
||||
test_add()
|
||||
53
tests/xml/ttree_add1.nim
Normal file
53
tests/xml/ttree_add1.nim
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
discard """
|
||||
output: '''
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
'''
|
||||
"""
|
||||
|
||||
# Test xmltree add/insert/delete/replace operations
|
||||
import xmlparser
|
||||
import xmltree
|
||||
var baseDocHead = """
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
</xml>
|
||||
"""
|
||||
var baseDocHeadTree = parseXml(baseDocHead)
|
||||
var baseDocBody = """
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
"""
|
||||
var baseDocBodyTree = parseXml(baseDocBody)
|
||||
|
||||
proc test_add() =
|
||||
var testDoc = baseDocHeadTree
|
||||
var newBody = newElement("body")
|
||||
var bodyItems: seq[XmlNode] = @[]
|
||||
for item in baseDocBodyTree.items():
|
||||
bodyItems.add(item)
|
||||
newBody.add(bodyItems)
|
||||
|
||||
echo $newBody
|
||||
|
||||
testDoc.add(newBody)
|
||||
echo $testDoc
|
||||
|
||||
test_add()
|
||||
47
tests/xml/ttree_delete.nim
Normal file
47
tests/xml/ttree_delete.nim
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
discard """
|
||||
output: '''
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
'''
|
||||
"""
|
||||
|
||||
# Test xmltree add/insert/delete/replace operations
|
||||
import xmlparser
|
||||
import xmltree
|
||||
let initialDocBase = """
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<tag>
|
||||
<div>MORE TEXT </div>
|
||||
<div>MORE TEXT Some more text</div>
|
||||
</tag>
|
||||
<tag>
|
||||
<div>MORE TEXT </div>
|
||||
<div>MORE TEXT Some more text</div>
|
||||
</tag>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
"""
|
||||
var initialDocBaseTree = parseXml(initialDocBase)
|
||||
|
||||
proc test_delete() =
|
||||
var testDoc = initialDocBaseTree
|
||||
|
||||
testDoc.delete(1..2)
|
||||
echo $testDoc
|
||||
|
||||
test_delete()
|
||||
48
tests/xml/ttree_delete1.nim
Normal file
48
tests/xml/ttree_delete1.nim
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
discard """
|
||||
output: '''
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
'''
|
||||
"""
|
||||
|
||||
# Test xmltree add/insert/delete/replace operations
|
||||
import xmlparser
|
||||
import xmltree
|
||||
let initialDocBase = """
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<tag>
|
||||
<div>MORE TEXT </div>
|
||||
<div>MORE TEXT Some more text</div>
|
||||
</tag>
|
||||
<tag>
|
||||
<div>MORE TEXT </div>
|
||||
<div>MORE TEXT Some more text</div>
|
||||
</tag>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
"""
|
||||
var initialDocBaseTree = parseXml(initialDocBase)
|
||||
|
||||
proc test_delete() =
|
||||
var testDoc = initialDocBaseTree
|
||||
|
||||
testDoc.delete(1)
|
||||
testDoc.delete(1)
|
||||
echo $testDoc
|
||||
|
||||
test_delete()
|
||||
53
tests/xml/ttree_insert.nim
Normal file
53
tests/xml/ttree_insert.nim
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
discard """
|
||||
output: '''
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
'''
|
||||
"""
|
||||
|
||||
# Test xmltree add/insert/delete/replace operations
|
||||
import xmlparser
|
||||
import xmltree
|
||||
var baseDocHead = """
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
</xml>
|
||||
"""
|
||||
var baseDocHeadTree = parseXml(baseDocHead)
|
||||
var baseDocBody = """
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
"""
|
||||
var baseDocBodyTree = parseXml(baseDocBody)
|
||||
|
||||
proc test_insert() =
|
||||
var testDoc = baseDocHeadTree
|
||||
var newBody = newElement("body")
|
||||
var bodyItems: seq[XmlNode] = @[]
|
||||
for item in baseDocBodyTree.items():
|
||||
bodyItems.insert(item, len(bodyItems))
|
||||
newBody.insert(bodyItems, 1)
|
||||
|
||||
echo $newBody
|
||||
|
||||
testDoc.insert(newBody, 1)
|
||||
echo $testDoc
|
||||
|
||||
test_insert()
|
||||
51
tests/xml/ttree_insert1.nim
Normal file
51
tests/xml/ttree_insert1.nim
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
discard """
|
||||
output: '''
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
'''
|
||||
"""
|
||||
|
||||
# Test xmltree add/insert/delete/replace operations
|
||||
import xmlparser
|
||||
import xmltree
|
||||
var baseDocHead = """
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
</xml>
|
||||
"""
|
||||
var baseDocHeadTree = parseXml(baseDocHead)
|
||||
var baseDocBody = """
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
"""
|
||||
var baseDocBodyTree = parseXml(baseDocBody)
|
||||
|
||||
proc test_insert() =
|
||||
var testDoc = baseDocHeadTree
|
||||
var newBody = newElement("body")
|
||||
for item in baseDocBodyTree.items():
|
||||
newBody.insert(item, len(newBody))
|
||||
|
||||
echo $newBody
|
||||
|
||||
testDoc.insert(newBody, 1)
|
||||
echo $testDoc
|
||||
|
||||
test_insert()
|
||||
46
tests/xml/ttree_replace.nim
Normal file
46
tests/xml/ttree_replace.nim
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
discard """
|
||||
output: '''
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
'''
|
||||
"""
|
||||
|
||||
# Test xmltree add/insert/delete/replace operations
|
||||
import xmlparser
|
||||
import xmltree
|
||||
var baseDocBody = """
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
"""
|
||||
var baseDocBodyTree = parseXml(baseDocBody)
|
||||
let initialDocBase = """
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body before replace </div>
|
||||
<div>Some more text in body before replace </div>
|
||||
</body>
|
||||
</xml>
|
||||
"""
|
||||
var initialDocBaseTree = parseXml(initialDocBase)
|
||||
|
||||
proc test_replace() =
|
||||
var testDoc = initialDocBaseTree
|
||||
|
||||
testDoc.replace(1, @[baseDocBodyTree])
|
||||
echo $testDoc
|
||||
|
||||
test_replace()
|
||||
53
tests/xml/ttree_replace1.nim
Normal file
53
tests/xml/ttree_replace1.nim
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
discard """
|
||||
output: '''
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
</xml>
|
||||
'''
|
||||
"""
|
||||
|
||||
# Test xmltree add/insert/delete/replace operations
|
||||
import xmlparser
|
||||
import xmltree
|
||||
var baseDocHead = """
|
||||
<head>
|
||||
<div>Some text</div>
|
||||
<div>Some more text </div>
|
||||
</head>
|
||||
"""
|
||||
var baseDocHeadTree = parseXml(baseDocHead)
|
||||
var baseDocBody = """
|
||||
<body>
|
||||
<div>Some text in body</div>
|
||||
<div>Some more text in body </div>
|
||||
</body>
|
||||
"""
|
||||
var baseDocBodyTree = parseXml(baseDocBody)
|
||||
let initialDocBase = """
|
||||
<xml>
|
||||
<head>
|
||||
<div>Some text before replace </div>
|
||||
<div>Some more text before replace </div>
|
||||
</head>
|
||||
<body>
|
||||
<div>Some text in body before replace </div>
|
||||
<div>Some more text in body before replace </div>
|
||||
</body>
|
||||
</xml>
|
||||
"""
|
||||
var initialDocBaseTree = parseXml(initialDocBase)
|
||||
|
||||
proc test_replace() =
|
||||
var testDoc = initialDocBaseTree
|
||||
|
||||
testDoc.replace(0..1, @[baseDocHeadTree, baseDocBodyTree])
|
||||
echo $testDoc
|
||||
|
||||
test_replace()
|
||||
Loading…
Add table
Add a link
Reference in a new issue