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

Side by Side Diff: tests/test_parser.py

Issue 29465715: Fixes 4969 - Add parsing of filters (Closed)
Patch Set: Address review comments on patch set 2 Created July 28, 2017, 6:52 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
« abp/filters/parser.py ('K') | « abp/filters/parser.py ('k') | no next file » | 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-2017 eyeo GmbH 2 # Copyright (C) 2006-2017 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 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 def test_parse_filter(): 32 @pytest.mark.parametrize('filter_text, expected', {
30 line = parse_line('||example.com/banner.gif') 33 # Blocking filters with patterns and regexps and blocking exceptions.
31 assert line.type == 'filter' 34 '*asdf*d**dd*': {
32 assert line.expression == '||example.com/banner.gif' 35 'selector': {'type': ST.URL_PATTERN, 'value': '*asdf*d**dd*'},
36 'action': FA.BLOCK,
37 },
38 '@@|*asd|f*d**dd*|': {
39 'selector': {'type': ST.URL_PATTERN, 'value': '|*asd|f*d**dd*|'},
40 'action': FA.ALLOW,
41 },
42 '/ddd|f?a[s]d/': {
43 'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'},
44 'action': FA.BLOCK,
45 },
46 '@@/ddd|f?a[s]d/': {
47 'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'},
48 'action': FA.ALLOW,
49 },
50 # Blocking filters with some options.
51 'bla$match-case,~script,domain=foo.com|~bar.com,sitekey=foo': {
52 'selector': {'type': ST.URL_PATTERN, 'value': 'bla'},
53 'action': FA.BLOCK,
54 'options': [
55 (OPT.MATCH_CASE, True),
56 (OPT.SCRIPT, False),
57 (OPT.DOMAIN, [('foo.com', True), ('bar.com', False)]),
58 (OPT.SITEKEY, ['foo']),
59 ],
60 },
61 '@@http://bla$~script,~other,sitekey=foo|bar': {
62 'selector': {'type': ST.URL_PATTERN, 'value': 'http://bla'},
63 'action': FA.ALLOW,
64 'options': [
65 (OPT.SCRIPT, False),
66 (OPT.OTHER, False),
67 (OPT.SITEKEY, ['foo', 'bar']),
68 ],
69 },
70 # Element hiding filters and exceptions.
71 '##ddd': {
72 'selector': {'type': ST.CSS, 'value': 'ddd'},
73 'action': FA.HIDE,
74 'options': [],
75 },
76 '#@#body > div:first-child': {
77 'selector': {'type': ST.CSS, 'value': 'body > div:first-child'},
78 'action': FA.SHOW,
79 'options': [],
80 },
81 'foo,~bar##ddd': {
82 'options': [(OPT.DOMAIN, [('foo', True), ('bar', False)])],
83 },
84 # Element hiding emulation filters (extended CSS).
85 'foo,~bar#?#:-abp-properties(abc)': {
86 'selector': {'type': ST.XCSS, 'value': ':-abp-properties(abc)'},
87 'action': FA.HIDE,
88 'options': [(OPT.DOMAIN, [('foo', True), ('bar', False)])],
89 },
90 'foo.com#?#aaa :-abp-properties(abc) bbb': {
91 'selector': {
92 'type': ST.XCSS,
93 'value': 'aaa :-abp-properties(abc) bbb'
94 },
95 },
96 '#?#:-abp-properties(|background-image: url(data:*))': {
97 'selector': {
98 'type': ST.XCSS,
99 'value': ':-abp-properties(|background-image: url(data:*))'
100 },
101 'options': [],
102 },
103 }.items())
104 def test_parse_filters(filter_text, expected):
105 """Parametric test for filter parsing"""
106 parsed = parse_line(filter_text)
107 assert parsed.type == 'filter'
108 assert parsed.text == filter_text
109 for attribute, expected_value in expected.items():
110 assert getattr(parsed, attribute) == expected_value
111
112
113 def test_parse_bad_option():
Vasily Kuznetsov 2017/07/28 18:57:49 Now we can also detect unknown options. Yaaay!
Vasily Kuznetsov 2017/08/02 16:21:17 I remove this test since we've agreed to remove th
114 with pytest.raises(ParseError):
115 parse_line('foo.com$script,foo')
116 with pytest.raises(ParseError):
117 parse_line('foo.com$~foo')
33 118
34 119
35 def test_parse_comment(): 120 def test_parse_comment():
36 line = parse_line('! Block foo') 121 line = parse_line('! Block foo')
37 assert line.type == 'comment' 122 assert line.type == 'comment'
38 assert line.text == 'Block foo' 123 assert line.text == 'Block foo'
39 124
40 125
41 def test_parse_meta(): 126 def test_parse_meta():
42 line = parse_line('! Homepage : http://aaa.com/b') 127 line = parse_line('! Homepage : http://aaa.com/b')
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 def test_parse_filterlist(): 160 def test_parse_filterlist():
76 result = parse_filterlist(['! foo', '! Title: bar']) 161 result = parse_filterlist(['! foo', '! Title: bar'])
77 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')] 162 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')]
78 163
79 164
80 def test_exception_timing(): 165 def test_exception_timing():
81 result = parse_filterlist(['! good line', '%bad line%']) 166 result = parse_filterlist(['! good line', '%bad line%'])
82 assert next(result) == Comment('good line') 167 assert next(result) == Comment('good line')
83 with pytest.raises(ParseError): 168 with pytest.raises(ParseError):
84 next(result) 169 next(result)
OLDNEW
« abp/filters/parser.py ('K') | « abp/filters/parser.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld