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

Delta Between Two Patch Sets: tests/test_parser.py

Issue 29465715: Fixes 4969 - Add parsing of filters (Closed)
Left Patch Set: remove all interpretation and keep only parsing, add support for element hiding emulation filters, … Created July 27, 2017, 7:16 p.m.
Right Patch Set: Rebase to 1f5d7ead9bff Created Oct. 24, 2017, 3:58 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/parser.py ('k') | no next file » | 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-2017 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 parse_line, parse_filterlist, ParseError, ST, FA 20 from abp.filters import (
21 parse_line, parse_filterlist, ParseError,
22 SELECTOR_TYPE as ST, FILTER_ACTION as FA, FILTER_OPTION as OPT,
23 )
21 from abp.filters.parser import Comment, Metadata 24 from abp.filters.parser import Comment, Metadata
22 25
23 26
24 def test_parse_empty(): 27 def test_parse_empty():
25 line = parse_line(' ') 28 line = parse_line(' ')
26 assert line.type == 'emptyline' 29 assert line.type == 'emptyline'
27 30
28 31
29 @pytest.mark.parametrize('filter_text, expected', { 32 @pytest.mark.parametrize('filter_text, expected', {
30 # Blocking filters with patterns and regexps and blocking exceptions. 33 # Blocking filters with patterns and regexps and blocking exceptions.
(...skipping 11 matching lines...) Expand all
42 }, 45 },
43 '@@/ddd|f?a[s]d/': { 46 '@@/ddd|f?a[s]d/': {
44 'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'}, 47 'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'},
45 'action': FA.ALLOW, 48 'action': FA.ALLOW,
46 }, 49 },
47 # Blocking filters with some options. 50 # Blocking filters with some options.
48 'bla$match-case,~script,domain=foo.com|~bar.com,sitekey=foo': { 51 'bla$match-case,~script,domain=foo.com|~bar.com,sitekey=foo': {
49 'selector': {'type': ST.URL_PATTERN, 'value': 'bla'}, 52 'selector': {'type': ST.URL_PATTERN, 'value': 'bla'},
50 'action': FA.BLOCK, 53 'action': FA.BLOCK,
51 'options': [ 54 'options': [
52 ('match-case', True), 55 (OPT.MATCH_CASE, True),
53 ('script', False), 56 (OPT.SCRIPT, False),
54 ('domains', [('foo.com', True), ('bar.com', False)]), 57 (OPT.DOMAIN, [('foo.com', True), ('bar.com', False)]),
55 ('sitekeys', ['foo']), 58 (OPT.SITEKEY, ['foo']),
56 ], 59 ],
57 }, 60 },
58 '@@http://bla$~script,~other,sitekey=foo|bar': { 61 '@@http://bla$~script,~other,sitekey=foo|bar': {
59 'selector': {'type': ST.URL_PATTERN, 'value': 'http://bla'}, 62 'selector': {'type': ST.URL_PATTERN, 'value': 'http://bla'},
60 'action': FA.ALLOW, 63 'action': FA.ALLOW,
61 'options': [ 64 'options': [
62 ('script', False), 65 (OPT.SCRIPT, False),
63 ('other', False), 66 (OPT.OTHER, False),
64 ('sitekeys', ['foo', 'bar']), 67 (OPT.SITEKEY, ['foo', 'bar']),
65 ], 68 ],
66 }, 69 },
67 # Element hiding filters and exceptions. 70 # Element hiding filters and exceptions.
68 '##ddd': { 71 '##ddd': {
69 'selector': {'type': ST.CSS, 'value': 'ddd'}, 72 'selector': {'type': ST.CSS, 'value': 'ddd'},
70 'action': FA.HIDE, 73 'action': FA.HIDE,
71 'options': [], 74 'options': [],
72 }, 75 },
73 '#@#body > div:first-child': { 76 '#@#body > div:first-child': {
74 'selector': {'type': ST.CSS, 'value': 'body > div:first-child'}, 77 'selector': {'type': ST.CSS, 'value': 'body > div:first-child'},
75 'action': FA.SHOW, 78 'action': FA.SHOW,
76 'options': [], 79 'options': [],
77 }, 80 },
78 'foo,~bar##ddd': { 81 'foo,~bar##ddd': {
79 'options': [('domains', [('foo', True), ('bar', False)])], 82 'options': [(OPT.DOMAIN, [('foo', True), ('bar', False)])],
80 }, 83 },
81 # Element hiding emulation filters (extended CSS). 84 # Element hiding emulation filters (extended CSS).
82 'foo,~bar#?#:-abp-properties(abc)': { 85 'foo,~bar#?#:-abp-properties(abc)': {
83 'selector': {'type': ST.XCSS, 'value': ':-abp-properties(abc)'}, 86 'selector': {'type': ST.XCSS, 'value': ':-abp-properties(abc)'},
84 'action': FA.HIDE, 87 'action': FA.HIDE,
85 'options': [('domains', [('foo', True), ('bar', False)])], 88 'options': [(OPT.DOMAIN, [('foo', True), ('bar', False)])],
86 }, 89 },
87 'foo.com#?#aaa :-abp-properties(abc) bbb': { 90 'foo.com#?#aaa :-abp-properties(abc) bbb': {
88 'selector': { 91 'selector': {
89 'type': ST.XCSS, 92 'type': ST.XCSS,
90 'value': 'aaa :-abp-properties(abc) bbb' 93 'value': 'aaa :-abp-properties(abc) bbb'
91 }, 94 },
92 }, 95 },
93 '#?#:-abp-properties(|background-image: url(data:*))': { 96 '#?#:-abp-properties(|background-image: url(data:*))': {
94 'selector': { 97 'selector': {
95 'type': ST.XCSS, 98 'type': ST.XCSS,
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 def test_parse_filterlist(): 153 def test_parse_filterlist():
151 result = parse_filterlist(['! foo', '! Title: bar']) 154 result = parse_filterlist(['! foo', '! Title: bar'])
152 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')] 155 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')]
153 156
154 157
155 def test_exception_timing(): 158 def test_exception_timing():
156 result = parse_filterlist(['! good line', '%bad line%']) 159 result = parse_filterlist(['! good line', '%bad line%'])
157 assert next(result) == Comment('good line') 160 assert next(result) == Comment('good line')
158 with pytest.raises(ParseError): 161 with pytest.raises(ParseError):
159 next(result) 162 next(result)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld