| Index: tests/test_filter_block.py |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/tests/test_filter_block.py |
| @@ -0,0 +1,70 @@ |
| +# This file is part of Adblock Plus <https://adblockplus.org/>, |
| +# Copyright (C) 2006-present eyeo GmbH |
| +# |
| +# Adblock Plus is free software: you can redistribute it and/or modify |
| +# it under the terms of the GNU General Public License version 3 as |
| +# published by the Free Software Foundation. |
| +# |
| +# Adblock Plus is distributed in the hope that it will be useful, |
| +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| +# GNU General Public License for more details. |
| +# |
| +# You should have received a copy of the GNU General Public License |
| +# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + |
| +from __future__ import unicode_literals |
| + |
| +import pytest |
| + |
| +from abp.filters import parse_line, SelectorType, FilterAction, ParseError |
| +from abp.filters.blocks import to_blocks, FilterBlock |
| + |
| +BLOCKS = """ |
| +! Example block |
| +!:foo=bar |
| +!:baz=some_tricky?variable=with&funny=chars#and-stuff |
| +! Another comment |
| +||block.ing/filter$domain=foo.com|~bar.com |
| +white.list.ing#@#hiding.filter |
| +! Second block |
| +@@whateve.rs |
| +""" |
| + |
| +BLOCK_LINES = list(map(parse_line, BLOCKS.splitlines())) |
| + |
| + |
| +def test_to_blocks(): |
| + blocks = list(to_blocks(BLOCK_LINES)) |
| + assert len(blocks) == 2 |
| + block = blocks[0] |
| + assert block.foo == 'bar' |
| + assert block.baz == 'some_tricky?variable=with&funny=chars#and-stuff' |
| + assert block.description == 'Example block\nAnother comment' |
| + # Don't test the filters thouroughly: filter parsing is tested elsewhere. |
| + assert len(block.filters) == 2 |
| + assert block.filters[0].selector['type'] == SelectorType.URL_PATTERN |
| + assert block.filters[1].action == FilterAction.SHOW |
| + |
| + |
| +def test_asdict(): |
| + blocks = list(to_blocks(BLOCK_LINES)) |
| + d = blocks[0]._asdict() |
| + assert d['foo'] == 'bar' |
| + assert d['baz'] == 'some_tricky?variable=with&funny=chars#and-stuff' |
| + assert d['description'] == 'Example block\nAnother comment' |
| + assert len(d['filters']) == 2 |
| + assert d['filters'][0]['selector']['type'] == SelectorType.URL_PATTERN |
| + assert d['filters'][1]['action'] == FilterAction.SHOW |
| + |
| + |
| +@pytest.mark.parametrize('line', [ |
| + '!:__dict__=foo', |
| + '!:_var=bar', |
| + '!:filters=stuff', |
| + '!:description=he he', |
| +]) |
| +def test_illegal_vars(line): |
| + """Check that illegal variable names are not allowed.""" |
| + with pytest.raises(ParseError): |
| + FilterBlock([parse_line(line)], []) |