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

Delta Between Two Patch Sets: tests/test_parser.py

Issue 29880577: Issue 6877 - Only parse headers in the first line of the filter list (Closed)
Left Patch Set: Initial Created Sept. 14, 2018, 4:43 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « abp/filters/rpy.py ('k') | tests/test_rpy.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 with pytest.raises(ParseError): 162 # Header-line lines are headers.
163 parse_line('[Adblock 1.1]', mode='start') 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
167 with pytest.raises(ParseError):
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]',
202 ' ! Last modified: 26 Jul 2018 02:10 UTC ',
168 '! Homepage : http://aaa.com/b', 203 '! Homepage : http://aaa.com/b',
169 '||example.com^', 204 '||example.com^',
170 '! Checksum: OaopkIiiAl77sSHk/VAWDA', 205 '! Checksum: OaopkIiiAl77sSHk/VAWDA',
171 '! Note: bla bla']) 206 '! Note: bla bla'])
172 207
173 assert next(result) == Header('Adblock Plus 1.1') 208 assert next(result) == Header('Adblock Plus 1.1')
209 # Check that trailing space is not stripped (like in ABP).
210 assert next(result) == Metadata('Last modified', '26 Jul 2018 02:10 UTC ')
174 assert next(result) == Metadata('Homepage', 'http://aaa.com/b') 211 assert next(result) == Metadata('Homepage', 'http://aaa.com/b')
175 assert next(result).type == 'filter' 212 assert next(result).type == 'filter'
176 assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA') 213 assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA')
177 assert next(result).type == 'comment' 214 assert next(result).type == 'comment'
178 215
179 with pytest.raises(StopIteration): 216 with pytest.raises(StopIteration):
180 next(result) 217 next(result)
181 218
182 219
183 def test_exception_timing(): 220 def test_exception_timing():
184 result = parse_filterlist(['! good line', '%bad line%']) 221 result = parse_filterlist(['! good line', '%bad line%'])
185 assert next(result) == Comment('good line') 222 assert next(result) == Comment('good line')
186 with pytest.raises(ParseError): 223 with pytest.raises(ParseError):
187 next(result) 224 next(result)
188 225
189 226
190 def test_parse_line_bytes(): 227 def test_parse_line_bytes():
191 line = parse_line(b'! \xc3\xbc') 228 line = parse_line(b'! \xc3\xbc')
192 assert line.text == '\xfc' 229 assert line.text == '\xfc'
LEFTRIGHT

Powered by Google App Engine
This is Rietveld