Left: | ||
Right: |
OLD | NEW |
---|---|
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 |
21 from abp.filters.parser import Comment, Metadata | 21 from abp.filters.parser import InvalidLine, Comment, Metadata, ParseError |
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 '||example.com/banner.gif$image,~match-case,domain=abc.com|~def.org': { |
31 assert line.type == 'filter' | 31 'selector': { |
32 assert line.expression == '||example.com/banner.gif' | 32 'type': 'url-pattern', |
33 'value': '||example.com/banner.gif', | |
34 }, | |
35 'action': 'block', | |
36 'options': { | |
37 'match-case': False, | |
38 'types-none': True, | |
39 'types-include': ['image'], | |
40 'domains-none': True, | |
41 'domains-include': ['abc.com'], | |
42 'domains-exclude': ['def.org'], | |
43 }, | |
44 }, | |
45 '/ab?c\\.com/$image': { | |
46 'selector': {'type': 'url-regexp', 'value': 'ab?c\\.com'}, | |
47 'action': 'block', | |
48 'options': { | |
49 'types-none': True, | |
50 'types-include': ['image'], | |
51 }, | |
52 }, | |
53 'abc$~image': { | |
54 'selector': {'type': 'url-pattern', 'value': 'abc'}, | |
55 'action': 'block', | |
56 'options': { | |
57 'types-exclude': ['image'], | |
58 }, | |
59 }, | |
60 '@@||example.com/good.gif': { | |
61 'selector': {'type': 'url-pattern', 'value': '||example.com/good.gif'}, | |
62 'action': 'allow', | |
63 'options': {}, | |
64 }, | |
65 '@@/ab?c\\.com/': { | |
66 'selector': {'type': 'url-regexp', 'value': 'ab?c\\.com'}, | |
67 'action': 'allow', | |
68 'options': {}, | |
69 }, | |
70 'abc.com,cdf.com##div#ad1': { | |
71 'selector': {'type': 'css', 'value': 'div#ad1'}, | |
72 'action': 'hide', | |
73 'options': { | |
74 'domains-none': True, | |
75 'domains-include': ['abc.com', 'cdf.com'], | |
76 }, | |
77 }, | |
78 '#@#div#ad1': { | |
79 'selector': {'type': 'css', 'value': 'div#ad1'}, | |
80 'action': 'show', | |
81 'options': {}, | |
82 }, | |
83 'abc.com,~cdf.abc.com#@##ad1': { | |
84 'selector': {'type': 'css', 'value': '#ad1'}, | |
85 'action': 'show', | |
86 'options': { | |
87 'domains-none': True, | |
88 'domains-include': ['abc.com'], | |
89 'domains-exclude': ['cdf.abc.com'], | |
90 }, | |
91 }, | |
92 # Exception with a site key. | |
93 '@@||abc.com/ad?$subdocument,sitekey=foo': { | |
94 'selector': {'type': 'url-pattern', 'value': '||abc.com/ad?'}, | |
95 'action': 'allow', | |
96 'options': { | |
97 'types-none': True, | |
98 'types-include': ['subdocument'], | |
99 'sitekeys': ['foo'] | |
100 }, | |
101 }, | |
102 # Element hiding filter using old (a.k.a. simple) syntax. | |
103 'abc.com#div(foo)(name=bar)(value=baz)': { | |
104 'selector': { | |
105 'type': 'abp-simple', | |
106 'value': 'div(foo)(name=bar)(value=baz)', | |
107 }, | |
108 'action': 'hide', | |
109 'options': { | |
110 'domains-none': True, | |
111 'domains-include': ['abc.com'], | |
112 }, | |
113 }, | |
114 # Exclude all types with "x|~x" trick. | |
115 'x$image,~image': {'options': {'types-none': True}}, | |
116 # More tricky combos of type flags. | |
117 'x$~image,image': {'options': {}}, | |
118 'x$~image,image,~image': {'options': {'types-exclude': ['image']}}, | |
119 'x$image,~image,image': { | |
120 'options': {'types-none': True, 'types-include': ['image']}, | |
121 }, | |
122 }.items()) | |
123 def test_parse_filters(filter_text, expected): | |
124 """Parametric test for filter parsing""" | |
125 parsed = parse_line(filter_text) | |
126 assert parsed.type == 'filter' | |
127 assert parsed.text == filter_text | |
128 for attribute, expected_value in expected.items(): | |
129 assert getattr(parsed, attribute) == expected_value | |
33 | 130 |
34 | 131 |
35 def test_parse_comment(): | 132 def test_parse_comment(): |
36 line = parse_line('! Block foo') | 133 line = parse_line('! Block foo') |
37 assert line.type == 'comment' | 134 assert line.type == 'comment' |
38 assert line.text == 'Block foo' | 135 assert line.text == 'Block foo' |
39 | 136 |
40 | 137 |
41 def test_parse_meta(): | 138 def test_parse_meta(): |
42 line = parse_line('! Homepage : http://aaa.com/b') | 139 line = parse_line('! Homepage : http://aaa.com/b') |
(...skipping 27 matching lines...) Expand all Loading... | |
70 def test_parse_bad_header(): | 167 def test_parse_bad_header(): |
71 with pytest.raises(ParseError): | 168 with pytest.raises(ParseError): |
72 parse_line('[Adblock 1.1]') | 169 parse_line('[Adblock 1.1]') |
73 | 170 |
74 | 171 |
75 def test_parse_filterlist(): | 172 def test_parse_filterlist(): |
76 result = parse_filterlist(['! foo', '! Title: bar']) | 173 result = parse_filterlist(['! foo', '! Title: bar']) |
77 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')] | 174 assert list(result) == [Comment('foo'), Metadata('Title', 'bar')] |
78 | 175 |
79 | 176 |
80 def test_exception_timing(): | 177 def test_invalid_lines(): |
mathias
2017/07/26 20:37:15
This test vanishing seems unrelated as well.
Vasily Kuznetsov
2017/07/27 12:20:58
`parse_filterlist` doesn't throw exceptions anymor
| |
81 result = parse_filterlist(['! good line', '%bad line%']) | 178 result = parse_filterlist([ |
82 assert next(result) == Comment('good line') | 179 '[Bad header]', |
83 with pytest.raises(ParseError): | 180 '! Good comment', |
84 next(result) | 181 '%Bad instruction%', |
182 ]) | |
183 assert list(result) == [ | |
184 InvalidLine('[Bad header]', 'Malformed header'), | |
185 Comment('Good comment'), | |
186 InvalidLine('%Bad instruction%', 'Unrecognized instruction'), | |
187 ] | |
OLD | NEW |