forked from p12tic/cppreference-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_preprocess.py
More file actions
231 lines (193 loc) · 10.4 KB
/
test_preprocess.py
File metadata and controls
231 lines (193 loc) · 10.4 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2018 Monika Kairaityte <monika@kibit.lt>
#
# This file is part of cppreference-doc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
from commands.preprocess import * #pylint: disable=unused-wildcard-import
import io
import os
import unittest
from lxml import etree
class TestConvertLoaderName(unittest.TestCase):
def test_convert_loader_name(self):
url = 'http://en.cppreference.com/mwiki/load.php?debug=false&lang=en&modules=site&only=scripts&skin=cppreference2&*'
self.assertEqual('site_scripts.js', convert_loader_name(url))
url = 'http://en.cppreference.com/mwiki/load.php?debug=false&lang=en&modules=site&only=styles&skin=cppreference2&*'
self.assertEqual('site_modules.css', convert_loader_name(url))
url = 'http://en.cppreference.com/mwiki/load.php?debug=false&lang=en&modules=skins.cppreference2&only=scripts&skin=cppreference2&*'
self.assertEqual('skin_scripts.js', convert_loader_name(url))
url = 'http://en.cppreference.com/mwiki/load.php?debug=false&lang=en&modules=startup&only=scripts&skin=cppreference2&*'
self.assertEqual('startup_scripts.js', convert_loader_name(url))
url = 'http://en.cppreference.com/mwiki/load.php?debug=false&lang=en&modules=ext.gadget.ColiruCompiler%2CMathJax%7Cext.rtlcite%7Cmediawiki.legacy.commonPrint%2Cshared%7Cskins.cppreference2&only=styles&skin=cppreference2&*'
self.assertEqual('ext.css', convert_loader_name(url))
with self.assertRaises(Exception):
convert_loader_name('')
class TestHasClass(unittest.TestCase):
def test_has_class(self):
el = etree.Element('tag')
self.assertEqual(False, has_class(el))
self.assertEqual(False, has_class(el, ''))
self.assertEqual(False, has_class(el, 'a'))
self.assertEqual(False, has_class(el, 'tag'))
el.set('class', '')
self.assertEqual(False, has_class(el, ))
self.assertEqual(False, has_class(el, ''))
self.assertEqual(False, has_class(el, 'a'))
self.assertEqual(False, has_class(el, 'tag'))
el.set('class', 'a')
self.assertEqual(False, has_class(el, ))
self.assertEqual(False, has_class(el, ''))
self.assertEqual(True, has_class(el, 'a'))
self.assertEqual(False, has_class(el, 'b'))
self.assertEqual(False, has_class(el, 'tag'))
self.assertEqual(True, has_class(el, 'a', 'b'))
self.assertEqual(True, has_class(el, 'b', 'a'))
self.assertEqual(False, has_class(el, 'b', 'c'))
el.set('class', 'a b')
self.assertEqual(False, has_class(el, ))
self.assertEqual(False, has_class(el, ''))
self.assertEqual(True, has_class(el, 'a'))
self.assertEqual(True, has_class(el, 'b'))
self.assertEqual(False, has_class(el, 'tag'))
self.assertEqual(True, has_class(el, 'a', 'b'))
self.assertEqual(True, has_class(el, 'b', 'a'))
self.assertEqual(True, has_class(el, 'b', 'c'))
el.set('class', 'a b')
self.assertEqual(False, has_class(el, ))
self.assertEqual(False, has_class(el, ''))
self.assertEqual(True, has_class(el, 'a'))
self.assertEqual(True, has_class(el, 'b'))
self.assertEqual(False, has_class(el, 'tag'))
self.assertEqual(True, has_class(el, 'a', 'b'))
self.assertEqual(True, has_class(el, 'b', 'a'))
self.assertEqual(True, has_class(el, 'b', 'c'))
class TestIsExternalLink(unittest.TestCase):
def test_is_external_link(self):
self.assertEqual(True, is_external_link('http://a'))
self.assertEqual(True, is_external_link('https://a'))
self.assertEqual(True, is_external_link('ftp://a'))
self.assertEqual(False, is_external_link('ahttp://a'))
self.assertEqual(False, is_external_link(' http://a'))
class TestPlaceholderLinks(unittest.TestCase):
# Placeholder link replacement is implemented in the MediaWiki site JS at
# https://en.cppreference.com/w/MediaWiki:Common.js
def test_is_ranges_placeholder(self):
match = [
'http://en.cppreference.com/w/cpp/ranges-placeholder/concepts/Assignable',
'http://en.cppreference.com/w/cpp/ranges-placeholder/iterator/Incrementable',
'http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/all_any_none_of',
'http://en.cppreference.com/w/cpp/ranges-iterator-placeholder/dangling',
'http://en.cppreference.com/w/cpp/ranges-utility-placeholder/swap',
]
for target in match:
self.assertTrue(is_ranges_placeholder(target),
msg="Should match '{}'".format(target))
target = target.replace("http://", "https://")
self.assertTrue(is_ranges_placeholder(target),
msg="Should match '{}'".format(target))
nomatch = [
'http://en.cppreference.com/w/cpp/ranges--placeholder/swap',
'https://en.cppreference.com/w/cpp/ranges--placeholder/swap',
'http://www.mediawiki.org/',
'http://en.cppreference.com/w/Cppreference:About',
'http://en.wikipedia.org/wiki/Normal_distribution',
'https://www.mediawiki.org/',
'https://en.cppreference.com/w/Cppreference:About',
'https://en.wikipedia.org/wiki/Normal_distribution',
'w/cpp.html',
'w/cpp/language/expressions.html',
'../utility/functional/is_placeholder.html',
'utility/functional/placeholders.html',
'w/cpp/experimental/ranges.html',
]
for target in nomatch:
self.assertFalse(is_ranges_placeholder(target),
msg="Should not match '{}'".format(target))
def test_transform_ranges_placeholder(self):
entries = [
# (target, file, expected)
('http://en.cppreference.com/w/cpp/ranges-placeholder/concepts/Assignable',
'output/reference/en/cpp/concepts/ConvertibleTo.html',
'Assignable.html'),
('http://en.cppreference.com/w/cpp/ranges-placeholder/concepts/Assignable',
'output/reference/en/cpp/concepts/long/path/ConvertibleTo.html',
'../../Assignable.html'),
('http://en.cppreference.com/w/cpp/ranges-placeholder/concepts/Assignable',
'output/reference/en/cpp/other/path/ConvertibleTo.html',
'../../concepts/Assignable.html'),
('http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/all_any_none_of',
'output/reference/en/cpp/concepts/ConvertibleTo.html',
'../algorithm/ranges/all_any_none_of.html'),
('http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/all_any_none_of',
'output/reference/en/cpp/algorithm/ConvertibleTo.html',
'ranges/all_any_none_of.html'),
('http://en.cppreference.com/w/cpp/ranges-placeholder/concepts/Assignable',
'output/reference/en/cpp/experimental/ranges/concepts/View.html',
'Assignable.html'),
('http://en.cppreference.com/w/cpp/ranges-placeholder/concepts/Assignable',
'output/reference/en/cpp/experimental/ranges/View.html',
'concepts/Assignable.html'),
('http://en.cppreference.com/w/cpp/ranges-placeholder/concepts/Assignable',
'output/reference/en/cpp/experimental/ranges/range/View.html',
'../concepts/Assignable.html'),
('http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/all_any_none_of',
'output/reference/en/cpp/experimental/ranges/View.html',
'algorithm/all_any_none_of.html'),
('http://en.cppreference.com/w/cpp/ranges-algorithm-placeholder/all_any_none_of',
'output/reference/en/cpp/experimental/ranges/range/View.html',
'../algorithm/all_any_none_of.html'),
]
# transform_ranges_placeholder(target, file, root)
# target: the placeholder link
# file: path of the file that contains the link
# root: path to the site root (where '/' should link to)
root = "output/reference"
for target, file, expected in entries:
self.assertEqual(expected, transform_ranges_placeholder(target, file, root),
msg="target='{}', file='{}', root='{}'".format(target, file, root))
target = target.replace('http://', 'https://')
self.assertEqual(expected, transform_ranges_placeholder(target, file, root),
msg="target='{}', file='{}', root='{}'".format(target, file, root))
class TestPreprocessHtml(unittest.TestCase):
def setUp(self):
self.testdata = os.path.join(os.path.dirname(__file__), 'preprocess_data')
infile = os.path.join(self.testdata, "fabs.html")
self.parser = etree.HTMLParser()
self.html = etree.parse(infile, self.parser)
# Check whether the HTML matches the contents of the specified test data file
def check_output(self, expected_file):
with open(os.path.join(self.testdata, expected_file), 'rb') as f:
expected = f.read()
with io.BytesIO() as buf:
self.html.write(buf, encoding='utf-8', method='html')
actual = buf.getvalue()
self.assertEqual(expected, actual)
pass
def test_remove_noprint(self):
remove_noprint(self.html)
self.check_output("fabs_noprint.html")
def test_remove_see_also(self):
remove_see_also(self.html)
self.check_output("fabs_seealso.html")
def test_remove_google_analytics(self):
remove_google_analytics(self.html)
self.check_output("fabs_ga.html")
def test_remove_ads(self):
remove_ads(self.html)
self.check_output("fabs_ads.html")
def remove_unused_external(self):
# TODO split out and test
pass