OLD | NEW |
1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
3 # | 3 # |
4 # Adblock Plus is free software: you can redistribute it and/or modify | 4 # Adblock Plus is free software: you can redistribute it and/or modify |
5 # it under the terms of the GNU General Public License version 3 as | 5 # it under the terms of the GNU General Public License version 3 as |
6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
7 # | 7 # |
8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 line = parse_line('%include foo:bar/baz.txt%') | 151 line = parse_line('%include foo:bar/baz.txt%') |
152 assert line.type == 'include' | 152 assert line.type == 'include' |
153 assert line.target == 'foo:bar/baz.txt' | 153 assert line.target == 'foo:bar/baz.txt' |
154 | 154 |
155 | 155 |
156 def test_parse_bad_instruction(): | 156 def test_parse_bad_instruction(): |
157 with pytest.raises(ParseError): | 157 with pytest.raises(ParseError): |
158 parse_line('%foo bar%') | 158 parse_line('%foo bar%') |
159 | 159 |
160 | 160 |
161 def test_parse_bad_header(): | 161 def test_parse_start(): |
| 162 # Header-line lines are headers. |
| 163 assert parse_line('[Adblock Plus 1.1]', mode='start').type == 'header' |
| 164 # But space is not allowed in headers. |
| 165 assert parse_line(' [Adblock Plus 1.1]', mode='start').type == 'filter' |
| 166 assert parse_line('[Adblock Plus 1.1] ', mode='start').type == 'filter' |
| 167 |
162 with pytest.raises(ParseError): | 168 with pytest.raises(ParseError): |
163 parse_line('[Adblock 1.1]') | 169 # Invalid headers cause exceptions. |
| 170 parse_line('[Adblock 1.1]', mode='start') |
| 171 |
| 172 # Metadata-like lines are metadata. |
| 173 assert parse_line('! Foo: bar', mode='metadata').type == 'metadata' |
| 174 |
| 175 |
| 176 def test_parse_metadata(): |
| 177 # Header-like lines are just filters. |
| 178 assert parse_line('[Adblock 1.1]', mode='metadata').type == 'filter' |
| 179 # Metadata-like lines are metadata. |
| 180 assert parse_line('! Foo: bar', mode='metadata').type == 'metadata' |
| 181 |
| 182 |
| 183 def test_parse_body(): |
| 184 # Header-like lines are just filters. |
| 185 assert parse_line('[Adblock 1.1]', mode='body').type == 'filter' |
| 186 # Metadata-like lines are comments. |
| 187 assert parse_line('! Foo: bar', mode='body').type == 'comment' |
| 188 # But there's an exception for the checksum. |
| 189 assert parse_line('! Checksum: 42', mode='body').type == 'metadata' |
| 190 |
| 191 |
| 192 def test_parse_invalid_mode(): |
| 193 with pytest.raises(ValueError): |
| 194 parse_line('', mode='nonsense') |
164 | 195 |
165 | 196 |
166 def test_parse_filterlist(): | 197 def test_parse_filterlist(): |
167 result = parse_filterlist(['[Adblock Plus 1.1]', | 198 result = parse_filterlist(['[Adblock Plus 1.1]', |
168 '! Last modified: 26 Jul 2018 02:10 UTC', | 199 ' ! Last modified: 26 Jul 2018 02:10 UTC ', |
169 '! Homepage : http://aaa.com/b', | 200 '! Homepage : http://aaa.com/b', |
170 '||example.com^', | 201 '||example.com^', |
171 '! Checksum: OaopkIiiAl77sSHk/VAWDA', | 202 '! Checksum: OaopkIiiAl77sSHk/VAWDA', |
172 '! Note: bla bla']) | 203 '! Note: bla bla']) |
173 | 204 |
174 assert next(result) == Header('Adblock Plus 1.1') | 205 assert next(result) == Header('Adblock Plus 1.1') |
175 assert next(result) == Metadata('Last modified', '26 Jul 2018 02:10 UTC') | 206 # Check that trailing space is not stripped (like in ABP). |
| 207 assert next(result) == Metadata('Last modified', '26 Jul 2018 02:10 UTC ') |
176 assert next(result) == Metadata('Homepage', 'http://aaa.com/b') | 208 assert next(result) == Metadata('Homepage', 'http://aaa.com/b') |
177 assert next(result).type == 'filter' | 209 assert next(result).type == 'filter' |
178 assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA') | 210 assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA') |
179 assert next(result).type == 'comment' | 211 assert next(result).type == 'comment' |
180 | 212 |
181 with pytest.raises(StopIteration): | 213 with pytest.raises(StopIteration): |
182 next(result) | 214 next(result) |
183 | 215 |
184 | 216 |
185 def test_exception_timing(): | 217 def test_exception_timing(): |
186 result = parse_filterlist(['! good line', '%bad line%']) | 218 result = parse_filterlist(['! good line', '%bad line%']) |
187 assert next(result) == Comment('good line') | 219 assert next(result) == Comment('good line') |
188 with pytest.raises(ParseError): | 220 with pytest.raises(ParseError): |
189 next(result) | 221 next(result) |
190 | 222 |
191 | 223 |
192 def test_parse_line_bytes(): | 224 def test_parse_line_bytes(): |
193 line = parse_line(b'! \xc3\xbc') | 225 line = parse_line(b'! \xc3\xbc') |
194 assert line.text == '\xfc' | 226 assert line.text == '\xfc' |
OLD | NEW |