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

Side by Side Diff: tests/test_parser.py

Issue 29873561: Issue 6920 - Only parse metadata from the top of the file (Closed)
Patch Set: Close metadata on everything but headers or comments Created Sept. 4, 2018, 8:22 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/parser.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
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 from __future__ import unicode_literals 16 from __future__ import unicode_literals
17 17
18 import pytest 18 import pytest
19 19
20 from abp.filters import ( 20 from abp.filters import (
21 parse_line, parse_filterlist, ParseError, 21 parse_line, parse_filterlist, ParseError,
22 SELECTOR_TYPE as ST, FILTER_ACTION as FA, FILTER_OPTION as OPT, 22 SELECTOR_TYPE as ST, FILTER_ACTION as FA, FILTER_OPTION as OPT,
23 ) 23 )
24 from abp.filters.parser import Comment, Metadata 24 from abp.filters.parser import Comment, Metadata, Header
25 25
26 26
27 def test_parse_empty(): 27 def test_parse_empty():
28 line = parse_line(' ') 28 line = parse_line(' ')
29 assert line.type == 'emptyline' 29 assert line.type == 'emptyline'
30 30
31 31
32 @pytest.mark.parametrize('filter_text, expected', { 32 @pytest.mark.parametrize('filter_text, expected', {
33 # Blocking filters with patterns and regexps and blocking exceptions. 33 # Blocking filters with patterns and regexps and blocking exceptions.
34 '*asdf*d**dd*': { 34 '*asdf*d**dd*': {
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 for attribute, expected_value in expected.items(): 140 for attribute, expected_value in expected.items():
141 assert getattr(parsed, attribute) == expected_value 141 assert getattr(parsed, attribute) == expected_value
142 142
143 143
144 def test_parse_comment(): 144 def test_parse_comment():
145 line = parse_line('! Block foo') 145 line = parse_line('! Block foo')
146 assert line.type == 'comment' 146 assert line.type == 'comment'
147 assert line.text == 'Block foo' 147 assert line.text == 'Block foo'
148 148
149 149
150 def test_parse_meta():
151 line = parse_line('! Homepage : http://aaa.com/b')
152 assert line.type == 'metadata'
153 assert line.key == 'Homepage'
154 assert line.value == 'http://aaa.com/b'
155
156
157 def test_parse_instruction(): 150 def test_parse_instruction():
158 line = parse_line('%include foo:bar/baz.txt%') 151 line = parse_line('%include foo:bar/baz.txt%')
159 assert line.type == 'include' 152 assert line.type == 'include'
160 assert line.target == 'foo:bar/baz.txt' 153 assert line.target == 'foo:bar/baz.txt'
161 154
162 155
163 def test_parse_bad_instruction(): 156 def test_parse_bad_instruction():
164 with pytest.raises(ParseError): 157 with pytest.raises(ParseError):
165 parse_line('%foo bar%') 158 parse_line('%foo bar%')
166 159
167 160
168 def test_parse_header():
169 line = parse_line('[Adblock Plus 1.1]')
170 assert line.type == 'header'
171 assert line.version == 'Adblock Plus 1.1'
172
173
174 def test_parse_bad_header(): 161 def test_parse_bad_header():
175 with pytest.raises(ParseError): 162 with pytest.raises(ParseError):
176 parse_line('[Adblock 1.1]') 163 parse_line('[Adblock 1.1]')
177 164
178 165
179 def test_parse_filterlist(): 166 def test_parse_filterlist():
180 result = parse_filterlist(['! foo', '! Title: bar']) 167 result = parse_filterlist(['[Adblock Plus 1.1]',
rhowell 2018/09/05 02:50:06 It might be good to include the 'Last modified' co
Sebastian Noack 2018/09/05 09:09:51 Done.
181 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')] 168 '! Homepage : http://aaa.com/b',
169 '||example.com^',
170 '! Checksum: OaopkIiiAl77sSHk/VAWDA',
171 '! Note: bla bla'])
172
173 assert next(result) == Header('Adblock Plus 1.1')
174 assert next(result) == Metadata('Homepage', 'http://aaa.com/b')
175 assert next(result).type == 'filter'
176 assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA')
177 assert next(result).type == 'comment'
178
179 with pytest.raises(StopIteration):
180 next(result)
182 181
183 182
184 def test_exception_timing(): 183 def test_exception_timing():
185 result = parse_filterlist(['! good line', '%bad line%']) 184 result = parse_filterlist(['! good line', '%bad line%'])
186 assert next(result) == Comment('good line') 185 assert next(result) == Comment('good line')
187 with pytest.raises(ParseError): 186 with pytest.raises(ParseError):
188 next(result) 187 next(result)
189 188
190 189
191 def test_parse_line_bytes(): 190 def test_parse_line_bytes():
192 line = parse_line(b'! \xc3\xbc') 191 line = parse_line(b'! \xc3\xbc')
193 assert line.text == '\xfc' 192 assert line.text == '\xfc'
OLDNEW
« no previous file with comments | « abp/filters/parser.py ('k') | tests/test_rpy.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld