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: remove all interpretation and keep only parsing, add support for element hiding emulation filters, … Created July 27, 2017, 7:16 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 parse_line, parse_filterlist, ParseError, ST, FA
21 from abp.filters.parser import Comment, Metadata 21 from abp.filters.parser import Comment, Metadata
22 22
23 23
24 def test_parse_empty(): 24 def test_parse_empty():
25 line = parse_line(' ') 25 line = parse_line(' ')
26 assert line.type == 'emptyline' 26 assert line.type == 'emptyline'
27 27
28 28
29 def test_parse_filter(): 29 @pytest.mark.parametrize('filter_text, expected', {
30 line = parse_line('||example.com/banner.gif') 30 # Blocking filters with patterns and regexps and blocking exceptions.
31 assert line.type == 'filter' 31 '*asdf*d**dd*': {
32 assert line.expression == '||example.com/banner.gif' 32 'selector': {'type': ST.URL_PATTERN, 'value': '*asdf*d**dd*'},
33 'action': FA.BLOCK,
34 },
35 '@@|*asd|f*d**dd*|': {
36 'selector': {'type': ST.URL_PATTERN, 'value': '|*asd|f*d**dd*|'},
37 'action': FA.ALLOW,
38 },
39 '/ddd|f?a[s]d/': {
40 'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'},
41 'action': FA.BLOCK,
42 },
43 '@@/ddd|f?a[s]d/': {
44 'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'},
45 'action': FA.ALLOW,
46 },
47 # Blocking filters with some options.
48 'bla$match-case,~script,domain=foo.com|~bar.com,sitekey=foo': {
49 'selector': {'type': ST.URL_PATTERN, 'value': 'bla'},
50 'action': FA.BLOCK,
51 'options': [
52 ('match-case', True),
53 ('script', False),
54 ('domains', [('foo.com', True), ('bar.com', False)]),
55 ('sitekeys', ['foo']),
56 ],
57 },
58 '@@http://bla$~script,~other,sitekey=foo|bar': {
59 'selector': {'type': ST.URL_PATTERN, 'value': 'http://bla'},
60 'action': FA.ALLOW,
61 'options': [
62 ('script', False),
63 ('other', False),
64 ('sitekeys', ['foo', 'bar']),
65 ],
66 },
67 # Element hiding filters and exceptions.
68 '##ddd': {
69 'selector': {'type': ST.CSS, 'value': 'ddd'},
70 'action': FA.HIDE,
71 'options': [],
72 },
73 '#@#body > div:first-child': {
74 'selector': {'type': ST.CSS, 'value': 'body > div:first-child'},
75 'action': FA.SHOW,
76 'options': [],
77 },
78 'foo,~bar##ddd': {
79 'options': [('domains', [('foo', True), ('bar', False)])],
80 },
81 # Element hiding emulation filters (extended CSS).
82 'foo,~bar#?#:-abp-properties(abc)': {
83 'selector': {'type': ST.XCSS, 'value': ':-abp-properties(abc)'},
84 'action': FA.HIDE,
85 'options': [('domains', [('foo', True), ('bar', False)])],
86 },
87 'foo.com#?#aaa :-abp-properties(abc) bbb': {
88 'selector': {
89 'type': ST.XCSS,
90 'value': 'aaa :-abp-properties(abc) bbb'
91 },
92 },
93 '#?#:-abp-properties(|background-image: url(data:*))': {
94 'selector': {
95 'type': ST.XCSS,
96 'value': ':-abp-properties(|background-image: url(data:*))'
97 },
98 'options': [],
99 },
100 }.items())
101 def test_parse_filters(filter_text, expected):
102 """Parametric test for filter parsing"""
103 parsed = parse_line(filter_text)
104 assert parsed.type == 'filter'
105 assert parsed.text == filter_text
106 for attribute, expected_value in expected.items():
107 assert getattr(parsed, attribute) == expected_value
33 108
34 109
35 def test_parse_comment(): 110 def test_parse_comment():
36 line = parse_line('! Block foo') 111 line = parse_line('! Block foo')
37 assert line.type == 'comment' 112 assert line.type == 'comment'
38 assert line.text == 'Block foo' 113 assert line.text == 'Block foo'
39 114
40 115
41 def test_parse_meta(): 116 def test_parse_meta():
42 line = parse_line('! Homepage : http://aaa.com/b') 117 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(): 150 def test_parse_filterlist():
76 result = parse_filterlist(['! foo', '! Title: bar']) 151 result = parse_filterlist(['! foo', '! Title: bar'])
77 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')] 152 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')]
78 153
79 154
80 def test_exception_timing(): 155 def test_exception_timing():
81 result = parse_filterlist(['! good line', '%bad line%']) 156 result = parse_filterlist(['! good line', '%bad line%'])
82 assert next(result) == Comment('good line') 157 assert next(result) == Comment('good line')
83 with pytest.raises(ParseError): 158 with pytest.raises(ParseError):
84 next(result) 159 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