Skip to content

Commit 422aa68

Browse files
author
Arzaroth Lekva
committed
adding some tests
1 parent 8f69da3 commit 422aa68

File tree

5 files changed

+61
-7
lines changed

5 files changed

+61
-7
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ A library providing python bindings for rapidxml
66

77
import rapidxml
88

9-
r = rapidxml.RapidXml("<test/><test2>foo</test2><test></test>") # parsing from string
9+
r = rapidxml.RapidXml(b"<test/><test2>foo</test2><test></test>") # parsing from bytes
1010
test = r.first_node("test") # get first node named test
1111
test.name = "foo" # changing node's name to foo
1212
r.first_node("test2").value = "bar" # changing node's value to bar
1313

14-
print(str(r)) # will output prettified string of the xml document
14+
print(str(r)) # will output a prettified string of the xml document
15+
print(r.unparse(pretty=False, raw=True)) # will output the xml document as flat bytes
1516
print(test) # also works for nodes
1617

1718
with open('dump.xml', 'w') as f:
1819
f.write(str(r))
1920
r = rapidxml.RapidXml("dump.xml", True) # loading from file
2021

21-
assert(str(r) == r.unparse(True)) # is always True
22-
assert(repr(r) == r.unparse()) # also always True, returns flat version
22+
assert(str(r) == r.unparse(True, False)) # is always True
23+
assert(repr(r) == r.unparse(pretty=False, raw=False)) # also always True
2324

2425

2526
### Install

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
@pytest.fixture(scope="function")
1313
def init_rapidxml():
14-
r = rapidxml.RapidXml(bytearray(b"""
14+
r = rapidxml.RapidXml(b"""
1515
<root>
1616
<test attr1="one" attr2="two" attr3="three" />
1717
<test2>
@@ -20,5 +20,5 @@ def init_rapidxml():
2020
<node id="3"/>
2121
</test2>
2222
<test>some text</test>
23-
</root>"""))
23+
</root>""")
2424
return r

tests/test_attributes.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,35 @@ def test_last_attribute(init_rapidxml):
2121
attr2 = test.last_attribute("attr2")
2222
assert attr2.name == "attr2"
2323
assert attr2.value == "two"
24+
25+
def test_append_attribute(init_rapidxml):
26+
root = init_rapidxml.first_node()
27+
root.append_attribute(init_rapidxml.allocate_attribute())
28+
assert root.last_attribute().name == ""
29+
assert root.last_attribute().value == ""
30+
root.append_attribute(init_rapidxml.allocate_attribute("test"))
31+
assert root.last_attribute().name == "test"
32+
assert root.last_attribute().value == ""
33+
root.append_attribute(init_rapidxml.allocate_attribute(value="test"))
34+
assert root.last_attribute().name == ""
35+
assert root.last_attribute().value == "test"
36+
root.append_attribute(init_rapidxml.allocate_attribute("test", "test"))
37+
assert root.last_attribute().name == "test"
38+
assert root.last_attribute().value == "test"
39+
40+
def test_prepend_attribute(init_rapidxml):
41+
root = init_rapidxml.first_node()
42+
root.prepend_attribute(init_rapidxml.allocate_attribute("test", "test"))
43+
assert root.first_attribute().name == "test"
44+
assert root.first_attribute().value == "test"
45+
root.prepend_attribute(init_rapidxml.allocate_attribute("test2", "test2"))
46+
assert root.first_attribute().name == "test2"
47+
assert root.first_attribute().value == "test2"
48+
49+
def test_insert_attribute(init_rapidxml):
50+
test = init_rapidxml.first_node().first_node()
51+
attr2 = test.first_attribute("attr2")
52+
test.insert_attribute(attr2, init_rapidxml.allocate_attribute("test", "test"))
53+
test = attr2.previous_attribute()
54+
assert test.name == "test"
55+
assert test.value == "test"

tests/test_basic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_parse(init_rapidxml):
2222
data = init_rapidxml.unparse().encode('utf-8')
2323
except UnicodeDecodeError:
2424
data = init_rapidxml.unparse()
25-
r.parse(bytearray(data))
25+
r.parse(data)
2626
assert str(r) == str(init_rapidxml)
2727

2828
def test_parse_from_file(init_rapidxml, tmpdir):

tests/test_utf8.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# File: test_utf8.py
5+
# by Arzaroth Lekva
6+
7+
#
8+
9+
import sys
10+
import rapidxml
11+
12+
def test_utf8():
13+
data = "<root>éè</root>"
14+
if sys.version_info.major >= 3:
15+
data = data.encode('utf-8')
16+
r = rapidxml.RapidXml(data)
17+
assert str(r) == r.unparse(pretty=True)
18+
assert repr(r) == r.unparse()
19+
assert data == r.unparse(raw=True)
20+
r.first_node().value = b'\x85'
21+
assert r.unparse(raw=True).decode('cp1252').encode('utf-8') == b"<root>\xe2\x80\xa6</root>"

0 commit comments

Comments
 (0)