Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/test_parser.py

Issue 29880577: Issue 6877 - Only parse headers in the first line of the filter list (Closed)
Patch Set: Fix header parsing, improve argument naming and documentation Created Sept. 18, 2018, 6:06 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « abp/filters/rpy.py ('k') | tests/test_rpy.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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]', 'start').type == 'header'
164 # Even if they have extra characters around.
165 assert parse_line('foo[Adblock Plus 1.1] bar', 'start').type == 'header'
166
162 with pytest.raises(ParseError): 167 with pytest.raises(ParseError):
163 parse_line('[Adblock 1.1]') 168 # But the inside of the header needs to be right.
169 parse_line('[Adblock Minus 1.1]', 'start').type
170
171 with pytest.raises(ParseError):
172 # Really right!
173 parse_line('[Adblock 1.1]', 'start')
174
175 # Metadata-like lines are metadata.
176 assert parse_line('! Foo: bar', 'metadata').type == 'metadata'
177
178
179 def test_parse_metadata():
180 # Header-like lines are just filters.
181 assert parse_line('[Adblock 1.1]', 'metadata').type == 'filter'
182 # Metadata-like lines are metadata.
183 assert parse_line('! Foo: bar', 'metadata').type == 'metadata'
184
185
186 def test_parse_body():
187 # Header-like lines are just filters.
188 assert parse_line('[Adblock 1.1]', 'body').type == 'filter'
189 # Metadata-like lines are comments.
190 assert parse_line('! Foo: bar', 'body').type == 'comment'
191 # But there's an exception for the checksum.
192 assert parse_line('! Checksum: 42', 'body').type == 'metadata'
193
194
195 def test_parse_invalid_position():
196 with pytest.raises(ValueError):
197 parse_line('', 'nonsense')
164 198
165 199
166 def test_parse_filterlist(): 200 def test_parse_filterlist():
167 result = parse_filterlist(['[Adblock Plus 1.1]', 201 result = parse_filterlist(['[Adblock Plus 1.1]',
168 '! Last modified: 26 Jul 2018 02:10 UTC', 202 ' ! Last modified: 26 Jul 2018 02:10 UTC ',
169 '! Homepage : http://aaa.com/b', 203 '! Homepage : http://aaa.com/b',
170 '||example.com^', 204 '||example.com^',
171 '! Checksum: OaopkIiiAl77sSHk/VAWDA', 205 '! Checksum: OaopkIiiAl77sSHk/VAWDA',
172 '! Note: bla bla']) 206 '! Note: bla bla'])
173 207
174 assert next(result) == Header('Adblock Plus 1.1') 208 assert next(result) == Header('Adblock Plus 1.1')
175 assert next(result) == Metadata('Last modified', '26 Jul 2018 02:10 UTC') 209 # Check that trailing space is not stripped (like in ABP).
210 assert next(result) == Metadata('Last modified', '26 Jul 2018 02:10 UTC ')
176 assert next(result) == Metadata('Homepage', 'http://aaa.com/b') 211 assert next(result) == Metadata('Homepage', 'http://aaa.com/b')
177 assert next(result).type == 'filter' 212 assert next(result).type == 'filter'
178 assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA') 213 assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA')
179 assert next(result).type == 'comment' 214 assert next(result).type == 'comment'
180 215
181 with pytest.raises(StopIteration): 216 with pytest.raises(StopIteration):
182 next(result) 217 next(result)
183 218
184 219
185 def test_exception_timing(): 220 def test_exception_timing():
186 result = parse_filterlist(['! good line', '%bad line%']) 221 result = parse_filterlist(['! good line', '%bad line%'])
187 assert next(result) == Comment('good line') 222 assert next(result) == Comment('good line')
188 with pytest.raises(ParseError): 223 with pytest.raises(ParseError):
189 next(result) 224 next(result)
190 225
191 226
192 def test_parse_line_bytes(): 227 def test_parse_line_bytes():
193 line = parse_line(b'! \xc3\xbc') 228 line = parse_line(b'! \xc3\xbc')
194 assert line.text == '\xfc' 229 assert line.text == '\xfc'
OLDNEW
« no previous file with comments | « abp/filters/rpy.py ('k') | tests/test_rpy.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld