-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_text_nodes.py
361 lines (254 loc) · 9.7 KB
/
test_text_nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import pytest
from delb import Document, ParserOptions, TagNode, TextNode, is_text_node, tag
from _delb.nodes import TAIL, APPENDED
def test_add_tag_before_tail():
root = Document("<root><a/>b</root>").root
root[1].add_preceding_siblings(tag("c"))
assert str(root) == "<root><a/><c/>b</root>"
def test_add_tag_after_tail_appended_text():
root = Document("<root><a/>b</root>").root
root.append_children("c", tag("d"))
assert str(root) == "<root><a/>bc<d/></root>"
def test_add_tag_after_tail():
root = Document("<root><node/>tail</root>").root
tail = root[1]
tail.add_following_siblings(tag("end"))
assert str(root) == "<root><node/>tail<end/></root>"
def test_add_text_after_tag():
document = Document("<root><tag/></root>")
tag = document.root[0]
tag.add_following_siblings(TextNode("foo"))
assert tag._etree_obj.text is None
assert tag._etree_obj.tail == "foo"
foo = tag.fetch_following_sibling()
assert isinstance(foo, TextNode)
assert foo._position == TAIL
assert foo.content == "foo"
assert foo._appended_text_node is None
def test_add_text_after_tail():
document = Document("<root><tag/>foo</root>")
root = document.root
foo = root[1]
bar = TextNode("bar")
foo.add_following_siblings(bar)
assert foo.fetch_following_sibling() is bar
assert len(root) == 3
assert foo._appended_text_node is bar
assert bar._bound_to is foo
assert isinstance(bar, TextNode)
assert bar._position == APPENDED
assert bar.content == "bar"
assert bar._appended_text_node is None
document.merge_text_nodes()
assert len(root) == 2
assert root[0]._etree_obj.tail == "foobar"
def test_add_text_after_appended():
document = Document("<root><tag/>foo</root>")
root = document.root
foo = root[1]
bar = TextNode("bar")
foo.add_following_siblings(bar)
peng = TextNode("peng")
bar.add_following_siblings(peng)
assert len(root) == 4
assert foo._appended_text_node is bar
assert foo._position == TAIL
assert bar._bound_to is foo
assert bar._appended_text_node is peng
assert bar._position == APPENDED
assert peng._bound_to is bar
assert peng._appended_text_node is None
assert peng._position == APPENDED
assert peng._bound_to is bar
document.merge_text_nodes()
assert len(root) == 2
assert root[0]._etree_obj.tail == "foobarpeng"
def test_add_tag_between_text_nodes_at_tail_position():
root = Document("<root><a/>tail</root>").root
root[1].add_following_siblings("the")
root[2].add_following_siblings(" end")
root[1].add_following_siblings(tag("node"))
assert len(root) == 5
assert str(root) == "<root><a/>tail<node/>the end</root>"
def test_add_text_before_data():
root = Document("<root>data</root>").root
root[0].add_preceding_siblings("head ")
assert len(root) == 2
assert str(root) == "<root>head data</root>"
def test_add_text_between_text_at_data_position():
root = Document("<root>data</root>").root
node = root[0]
node.add_following_siblings(" tailing")
node.add_following_siblings(" more more more")
assert len(root) == 3
assert str(root) == "<root>data more more more tailing</root>"
def test_add_tag_between_two_appended():
root = Document("<root>data</root>").root
root.append_children("appended")
root[0].add_following_siblings(tag("node"))
assert str(root) == "<root>data<node/>appended</root>"
root = Document("<root>data</root>").root
root.append_children("appended")
root[1].add_preceding_siblings(tag("node"))
assert str(root) == "<root>data<node/>appended</root>"
def test_add_text_between_two_appended():
root = Document("<root>data</root>").root
root[0].add_following_siblings(" appended_1")
root[1].add_following_siblings(" appended_2")
root[2].add_following_siblings(" appended_3")
root[1].add_following_siblings(tag("tag"))
assert len(root) == 5
assert str(root) == "<root>data appended_1<tag/> appended_2 appended_3</root>"
def test_appended_text_nodes():
document = Document("<root/>")
tokens = ("How ", "much ", "is ", "the ", "fish", "?")
root = document.root
root.append_children(*tokens)
assert len(root) == 6, len(root)
for i, child in enumerate(root.iterate_children()):
assert isinstance(child, TextNode)
assert child.content == str(child) == tokens[i]
document.merge_text_nodes()
assert len(root) == 1
assert root[0].content == "How much is the fish?"
def test_bindings(sample_document):
p = sample_document.root[1][1]
text_nodes = tuple(x for x in p.iterate_children(is_text_node))
x, y = text_nodes[1], p[2]
assert x is y
def test_construction():
root = Document(
"<root><node>one</node> two </root>",
parser_options=ParserOptions(reduce_whitespace=True),
).root
node, two = tuple(x for x in root.iterate_children())
one = node[0]
assert one.content == "one"
assert two.content == " two"
one.add_following_siblings(TextNode(" threehalfs"))
assert str(root) == "<root><node>one threehalfs</node> two</root>"
three = TextNode(" three")
two.add_following_siblings(three)
assert str(root) == "<root><node>one threehalfs</node> two three</root>"
three.add_following_siblings(" four")
assert str(root) == "<root><node>one threehalfs</node> two three four</root>"
node.append_children(" sevenquarters")
assert (
str(root)
== "<root><node>one threehalfs sevenquarters</node> two three four</root>"
)
twoandahalf = TextNode(" twoandahalf")
three.add_preceding_siblings(twoandahalf)
assert (
str(root)
== "<root><node>one threehalfs sevenquarters</node> two twoandahalf three "
"four</root>"
)
twoandahalf.add_preceding_siblings(" 2/3π")
assert (
str(root)
== "<root><node>one threehalfs sevenquarters</node> two 2/3π twoandahalf three "
"four</root>"
)
two.add_preceding_siblings(" almosttwo")
assert (
str(root)
== "<root><node>one threehalfs sevenquarters</node> almosttwo two 2/3π "
"twoandahalf three four</root>"
)
def test_content_is_coerced():
node = TextNode("")
with pytest.raises(TypeError):
node.content = 0
def test_depth():
document = Document("<root>1<a><b/>2</a></root>")
root = document.root
one = root[0]
assert one.depth == 1
two = root[1][1]
assert two.depth == 2
def test_detach_data_node():
root = Document("<root><a>b</a></root>").root
b = root[0][0].detach()
assert b.parent is None
assert b.depth == 0
assert b.content == "b"
assert str(root) == "<root><a/></root>"
root = Document("<root><a>b<c/></a></root>").root
b = root[0][0].detach()
assert b.parent is None
assert b.depth == 0
assert b.content == "b"
assert str(root) == "<root><a><c/></a></root>"
def test_detach_tag_sandwiched_node():
root = Document("<root><a/>tail<b/></root>").root
tail = root[1].detach()
assert tail.parent is None
assert tail.depth == 0
assert isinstance(tail, TextNode)
assert tail.content == "tail"
assert str(root) == "<root><a/><b/></root>"
def test_detach_text_sandwiched_node():
root = Document("<root>data</root>").root
data = root[0]
data.add_following_siblings(" tailing")
data.add_following_siblings(" more more more")
more = root[1].detach()
assert str(more) == " more more more"
assert str(root) == "<root>data tailing</root>"
data.add_following_siblings(more)
data.detach()
assert str(root) == "<root> more more more tailing</root>"
root.insert_children(0, tag("tag"))
more.detach()
assert str(root) == "<root><tag/> tailing</root>"
def test_document():
document = Document("<root>data<node/>tail</root>")
root = document.root
root.append_children("more")
assert root[0].document is document
assert root[2].document is document
assert root[3].document is document
detached = TextNode("detached")
assert detached.document is None
def test_entities():
document = Document("<root>< spock ></root>")
assert document.root[0].content == "< spock >"
def test_equality():
assert TextNode("1") != 1
def test_index():
assert Document("<root>test</root>").root.first_child.index == 0
def test_none_content_wrapping():
document = Document("<root><e1/></root>")
with pytest.raises(IndexError):
document.root[1]
e1 = document.root[0]
assert e1.fetch_following_sibling() is None
e1.add_following_siblings(tag("e2"))
assert isinstance(e1.fetch_following_sibling(), TagNode)
def test_fetch_preceding_sibling():
document = Document("<root><a/>b</root>")
root = document.root
root.append_children("c")
b = root[2].fetch_preceding_sibling()
assert b == "b"
c = TextNode("c")
root[0].append_children(c)
assert c.fetch_preceding_sibling() is None
def test_sample_document_structure_and_content(sample_document):
p = sample_document.root[1][1]
text_nodes = tuple(x for x in p.iterate_children(is_text_node))
assert all(isinstance(x, TextNode) for x in text_nodes)
assert len(text_nodes) == 2
assert text_nodes[0] is p[0]
assert text_nodes[1] is p[2]
assert text_nodes[0].content.strip() == "Lorem ipsum"
assert text_nodes[1].content.strip() == "dolor sit amet"
def test_string_methods():
node = TextNode("Herrasmiehet Pitävät Laserista")
assert node.count("ä") == 2
assert node.split() == ["Herrasmiehet", "Pitävät", "Laserista"]
assert "Laser" in node
assert node[13:20] == "Pitävät"
with pytest.raises(AttributeError):
node.this_is_not_a_method()