-
Notifications
You must be signed in to change notification settings - Fork 580
/
HTMLHintBearTest.py
209 lines (191 loc) · 8.89 KB
/
HTMLHintBearTest.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
import os
from queue import Queue
from bears.hypertext.HTMLHintBear import HTMLHintBear
from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.settings.Section import Section
from coalib.testing.BearTestHelper import generate_skip_decorator
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper
def get_testfile_path(name):
return os.path.join(os.path.dirname(__file__),
'htmlhint_test_files',
name)
def load_testfile(name):
with open(get_testfile_path(name)) as f:
return f.readlines()
@generate_skip_decorator(HTMLHintBear)
class HTMLHintBearTest(LocalBearTestHelper):
def setUp(self):
self.uut = HTMLHintBear(Section('name'), Queue())
def test_bad_lowercase_tagname(self):
filename = 'test_bad_lowercase_tagname.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='The html element name of [ SPAN ] '
'must be in lowercase.',
file=get_testfile_path(filename),
line=2,
column=1,
end_line=2,
end_column=1,
severity=RESULT_SEVERITY.MAJOR),
Result.from_values('HTMLHintBear',
message='The html element name of [ SPAN ] '
'must be in lowercase.',
file=get_testfile_path(filename),
line=3,
column=1,
end_line=3,
end_column=1,
severity=RESULT_SEVERITY.MAJOR)],
filename=get_testfile_path(filename))
def test_bad_lowercase_attribute(self):
filename = 'test_bad_lowercase_attribute.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='The attribute name of [ TYPE ] must '
'be in lowercase.',
file=get_testfile_path(filename),
line=2,
column=7,
end_line=2,
end_column=7,
severity=RESULT_SEVERITY.MAJOR)],
filename=get_testfile_path(filename))
def test_attribute_duplication(self):
filename = 'test_attribute_duplication.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='Duplicate of attribute name [ type ] '
'was found.',
file=get_testfile_path(filename),
line=2,
column=36,
end_line=2,
end_column=36,
severity=RESULT_SEVERITY.MAJOR)],
filename=get_testfile_path(filename))
def test_bad_tag_pair(self):
filename = 'test_bad_tag_pair.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='Tag must be paired, missing: '
'[ </li> ], start tag match failed '
'[ <li> ] on line 2.',
file=get_testfile_path(filename),
line=2,
column=9,
end_line=2,
end_column=9,
severity=RESULT_SEVERITY.MAJOR)],
filename=get_testfile_path(filename))
def test_doctype_at_beginning(self):
filename = 'test_doctype_at_beginning.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='Doctype must be declared first.',
file=get_testfile_path(filename),
line=1,
column=1,
end_line=1,
end_column=1,
severity=RESULT_SEVERITY.MAJOR)],
filename=get_testfile_path(filename))
def test_bad_unique_attribute_id(self):
filename = 'test_bad_unique_attribute_id.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='The id value [ id1 ] must be unique.',
file=get_testfile_path(filename),
line=2,
column=25,
end_line=2,
end_column=25,
severity=RESULT_SEVERITY.MAJOR)],
filename=get_testfile_path(filename))
def test_require_alt_attribute(self):
filename = 'test_require_alt_attribute.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='An alt attribute must be present on '
'<img> elements.',
file=get_testfile_path(filename),
line=2,
column=5,
end_line=2,
end_column=5,
severity=RESULT_SEVERITY.NORMAL)],
filename=get_testfile_path(filename))
def test_config_file(self):
filename = 'test_bad_inline_style.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='Inline style [ style="color:red" ] '
'cannot be use.',
file=get_testfile_path(filename),
line=2,
column=5,
end_line=2,
end_column=5,
severity=RESULT_SEVERITY.NORMAL)],
filename=get_testfile_path(filename),
settings={'htmlhint_config': get_testfile_path('.htmlhintrc')})
def test_absolute_links_in_href(self):
filename = 'test_links_in_href.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='The value of the href attribute '
'[ test.html ] must be absolute.',
file=get_testfile_path(filename),
line=2,
column=3,
end_line=2,
end_column=3,
severity=RESULT_SEVERITY.NORMAL)],
filename=get_testfile_path(filename),
settings={'require_relative_links_in_href': False})
def test_relative_links_in_href(self):
filename = 'test_links_in_href.html'
file_contents = load_testfile(filename)
self.check_results(
self.uut,
file_contents,
[Result.from_values('HTMLHintBear',
message='The value of the href attribute '
'[ http://www.alibaba.com/ ] must be '
'relative.',
file=get_testfile_path(filename),
line=3,
column=3,
end_line=3,
end_column=3,
severity=RESULT_SEVERITY.NORMAL)],
filename=get_testfile_path(filename),
settings={'require_relative_links_in_href': True})