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: Test 'Last modified' case Created Sept. 5, 2018, 9:09 a.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]',
181 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')] 168 '! Last modified: 26 Jul 2018 02:10 UTC',
169 '! Homepage : http://aaa.com/b',
170 '||example.com^',
171 '! Checksum: OaopkIiiAl77sSHk/VAWDA',
172 '! Note: bla bla'])
173
174 assert next(result) == Header('Adblock Plus 1.1')
175 assert next(result).type == 'comment'
176 assert next(result) == Metadata('Homepage', 'http://aaa.com/b')
177 assert next(result).type == 'filter'
178 assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA')
179 assert next(result).type == 'comment'
180
181 with pytest.raises(StopIteration):
182 next(result)
182 183
183 184
184 def test_exception_timing(): 185 def test_exception_timing():
185 result = parse_filterlist(['! good line', '%bad line%']) 186 result = parse_filterlist(['! good line', '%bad line%'])
186 assert next(result) == Comment('good line') 187 assert next(result) == Comment('good line')
187 with pytest.raises(ParseError): 188 with pytest.raises(ParseError):
188 next(result) 189 next(result)
189 190
190 191
191 def test_parse_line_bytes(): 192 def test_parse_line_bytes():
192 line = parse_line(b'! \xc3\xbc') 193 line = parse_line(b'! \xc3\xbc')
193 assert line.text == '\xfc' 194 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