LEFT | RIGHT |
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 """Tests for abp.filters.blocks.""" | 16 """Tests for abp.filters.blocks.""" |
17 | 17 |
18 from __future__ import unicode_literals | 18 from __future__ import unicode_literals |
19 | 19 |
20 import json | 20 import json |
21 import os | 21 import os |
22 | 22 |
23 import pytest | 23 import pytest |
24 | 24 |
25 from abp.filters import (parse_line, parse_filterlist, SelectorType, | 25 from abp.filters import parse_filterlist, SelectorType, FilterAction |
26 FilterAction, ParseError) | 26 from abp.filters.blocks import to_blocks |
27 from abp.filters.blocks import to_blocks, FiltersBlock | |
28 | 27 |
29 DATA_PATH = os.path.join(os.path.dirname(__file__), 'data') | 28 DATA_PATH = os.path.join(os.path.dirname(__file__), 'data') |
30 | 29 |
31 | 30 |
32 @pytest.fixture() | 31 @pytest.fixture() |
33 def fl_lines(): | 32 def fl_lines(): |
34 with open(os.path.join(DATA_PATH, 'filterlist.txt')) as f: | 33 with open(os.path.join(DATA_PATH, 'filterlist.txt')) as f: |
35 return list(parse_filterlist(f)) | 34 return list(parse_filterlist(f)) |
36 | 35 |
37 | 36 |
38 @pytest.fixture() | 37 @pytest.fixture() |
39 def expected_blocks(): | 38 def expected_blocks(): |
40 with open(os.path.join(DATA_PATH, 'expected_blocks.json')) as f: | 39 with open(os.path.join(DATA_PATH, 'expected_blocks.json')) as f: |
41 return json.load(f) | 40 return json.load(f) |
42 | 41 |
43 | 42 |
44 def test_to_blocks(fl_lines): | 43 def test_to_blocks(fl_lines): |
45 blocks = list(to_blocks(fl_lines)) | 44 blocks = list(to_blocks(fl_lines)) |
46 assert len(blocks) == 2 | 45 assert len(blocks) == 2 |
47 block = blocks[0] | 46 block = blocks[0] |
48 assert block.foo == 'bar' | 47 assert block.variables['foo'] == 'bar' |
49 assert block.baz == 'some_tricky?variable=with&funny=chars#and-stuff' | 48 assert block.variables['baz'] == ('some_tricky?variable=with&funny=chars#' |
| 49 'and-stuff') |
50 assert block.description == 'Example block 1\nAnother comment' | 50 assert block.description == 'Example block 1\nAnother comment' |
51 # Don't test the filters thouroughly: filter parsing is tested elsewhere. | 51 # Don't test the filters thouroughly: filter parsing is tested elsewhere. |
52 assert len(block.filters) == 2 | 52 assert len(block.filters) == 2 |
53 assert block.filters[0].selector['type'] == SelectorType.URL_PATTERN | 53 assert block.filters[0].selector['type'] == SelectorType.URL_PATTERN |
54 assert block.filters[1].action == FilterAction.SHOW | 54 assert block.filters[1].action == FilterAction.SHOW |
55 | 55 |
56 | 56 |
57 def test_to_dict(fl_lines, expected_blocks): | 57 def test_to_dict(fl_lines, expected_blocks): |
58 blocks = [b.to_dict() for b in to_blocks(fl_lines)] | 58 blocks = [b.to_dict() for b in to_blocks(fl_lines)] |
59 print(json.dumps(blocks, indent=2)) | 59 print(json.dumps(blocks, indent=2)) |
60 assert blocks == expected_blocks | 60 assert blocks == expected_blocks |
61 | |
62 | |
63 @pytest.mark.parametrize('line', [ | |
64 '!:__dict__=foo', | |
65 '!:_var=bar', | |
66 '!:filters=stuff', | |
67 '!:description=he he', | |
68 ]) | |
69 def test_illegal_vars(line): | |
70 """Check that illegal variable names are not allowed.""" | |
71 with pytest.raises(ParseError): | |
72 FiltersBlock([parse_line(line)], []) | |
LEFT | RIGHT |