-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
241 lines (221 loc) · 7.9 KB
/
tests.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
import ankle
import unittest
class SimpleTestCase(unittest.TestCase):
def test_works(self):
document = '''
<form id="test1" class="form"></form>
<form id="test2" class="form"></form>
'''
skeleton = '<form class="form"></form>'
ankle.find_all(skeleton, document)
def test_disallows_skeleton_with_multiple_elements(self):
document = '<html></html>'
skeleton = '<p></p><p></p>'
with self.assertRaises(ValueError):
ankle.find_all(skeleton, document)
class FindTestCase(unittest.TestCase):
def test_returns_first_found_element_when_found(self):
document = '''
<form id="test1" class="form"></form>
<form id="test2" class="form"></form>
'''
skeleton = '<form class="form"></form>'
element = ankle.find(skeleton, document)
self.assertEqual(element.attrib['id'], 'test1')
def test_returns_none_when_nothing_found(self):
document = '<form id="test"></form>'
skeleton = '<div class="other"></div>'
self.assertIsNone(ankle.find(skeleton, document))
class MatchingTestCase(unittest.TestCase):
def test_match_by_tag_name(self):
document = '''
<form id="test"></form>
'''
skeleton = '<form></form>'
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test')
def test_match_by_attribute(self):
document = '''
<form id="test1"></form>
<form id="test2"></form>
'''
skeleton = '<form id="test1"></form>'
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_match_by_child(self):
document = '''
<form id="test1"><input name="match"></form>
<form id="test2"><input name="no-match"></form>
'''
skeleton = '<form><input name="match"></form>'
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_match_by_descendant(self):
document = '''
<form id="test1">
<div><span><input name="match"></span></div>
<button>Submit</button>
</form>
<form id="test2">
<input name="whatever">
<div><button>Go</button></div>
</form>
'''
skeleton = '<form><input name="match"></form>'
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_match_by_multiple_children(self):
document = '''
<form id="login">
<input name="name">
<input name="password">
</form>
<form id="some-other-form">
<input name="no-match">
<input name="different-input">
</form>
'''
skeleton = '<form><input name="name"><input name="password"></form>'
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'login')
def test_multiple_matches(self):
document = '''
<form id="test1"></form>
<form id="test2"></form>
'''
skeleton = '<form></form>'
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 2)
self.assertEqual(matches[0].attrib['id'], 'test1')
self.assertEqual(matches[1].attrib['id'], 'test2')
def test_attribute_order_doesnt_matter(self):
document = '<form method="POST" action="." id="test1"></form>'
skeleton = '<form id="test1" action="." method="POST"></form>'
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_match_deep_descendants(self):
document = '''
<form id="test1">
<div class="red">
<input name="wonderful">
</div>
</form>
<form id="test2">
<div class="red">
<input name="different">
</div>
</form>
'''
skeleton = (
'<form><div class="red"><input name="wonderful"></form></div>'
)
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_match_text(self):
document = '''
<form id="test1">
<label for="name">
Correct label
</label>
<input name="name">
</form>
<form id="test2">
<label for="name">
Wrong label
</label>
<input name="name">
</form>
'''
skeleton = '''
<form>
<label for="name">Correct label</label>
<input name="name">
</form>
'''
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_match_by_order(self):
document = '''
<form id="test1">
<label for="name">Label</label>
<input name="name">
</form>
<form id="test2">
<input name="name">
<label for="name">Label</label>
</form>
'''
skeleton = '''
<form>
<label for="name">Label</label>
<input name="name">
</form>
'''
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_matches_skeleton_with_just_text(self):
document = '''
<h1 id="test1">Correct title</h1>
<h2 id="test2">Different title</h1>
'''
skeleton = '''
<h1>Correct title</h1>
'''
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_match_text_between_elements(self):
document = '''
<form id="test1">
<label for="name">Label</label>
Correct text
<input name="name">
</form>
<form id="test2">
<label for="name">Label</label>
Incorrect text
<input name="name">
</form>
'''
skeleton = '''
<form>
<label for="name">Label</label>
Correct text
<input name="name">
</form>
'''
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')
def test_match_text_in_the_beginning_of_element(self):
document = '''
<form id="test1">
Correct text
<label for="name">Label</label>
<input name="name">
</form>
<form id="test2">
Incorrect text
<label for="name">Label</label>
<input name="name">
</form>
'''
skeleton = '''
<form>
Correct text
<label for="name">Label</label>
<input name="name">
</form>
'''
matches = ankle.find_all(skeleton, document)
self.assertEqual(len(matches), 1)
self.assertEqual(matches[0].attrib['id'], 'test1')