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

Unified Diff: tests/test_parser.py

Issue 29465715: Fixes 4969 - Add parsing of filters (Closed)
Patch Set: Created June 14, 2017, 5:32 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« setup.py ('K') | « setup.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/test_parser.py
===================================================================
--- a/tests/test_parser.py
+++ b/tests/test_parser.py
@@ -12,29 +12,126 @@
#
# 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, parse_filterlist, ParseError
-from abp.filters.parser import Comment, Metadata
+from abp.filters import parse_line, parse_filterlist
+from abp.filters.parser import InvalidLine, Comment, Metadata, ParseError
def test_parse_empty():
line = parse_line(' ')
assert line.type == 'emptyline'
-def test_parse_filter():
- line = parse_line('||example.com/banner.gif')
- assert line.type == 'filter'
- assert line.expression == '||example.com/banner.gif'
+@pytest.mark.parametrize('filter_text, expected', {
+ '||example.com/banner.gif$image,~match-case,domain=abc.com|~def.org': {
+ 'selector': {
+ 'type': 'url-pattern',
+ 'value': '||example.com/banner.gif',
+ },
+ 'action': 'block',
+ 'options': {
+ 'match-case': False,
+ 'types-none': True,
+ 'types-include': ['image'],
+ 'domains-none': True,
+ 'domains-include': ['abc.com'],
+ 'domains-exclude': ['def.org'],
+ },
+ },
+ '/ab?c\\.com/$image': {
+ 'selector': {'type': 'url-regexp', 'value': 'ab?c\\.com'},
+ 'action': 'block',
+ 'options': {
+ 'types-none': True,
+ 'types-include': ['image'],
+ },
+ },
+ 'abc$~image': {
+ 'selector': {'type': 'url-pattern', 'value': 'abc'},
+ 'action': 'block',
+ 'options': {
+ 'types-exclude': ['image'],
+ },
+ },
+ '@@||example.com/good.gif': {
+ 'selector': {'type': 'url-pattern', 'value': '||example.com/good.gif'},
+ 'action': 'allow',
+ 'options': {},
+ },
+ '@@/ab?c\\.com/': {
+ 'selector': {'type': 'url-regexp', 'value': 'ab?c\\.com'},
+ 'action': 'allow',
+ 'options': {},
+ },
+ 'abc.com,cdf.com##div#ad1': {
+ 'selector': {'type': 'css', 'value': 'div#ad1'},
+ 'action': 'hide',
+ 'options': {
+ 'domains-none': True,
+ 'domains-include': ['abc.com', 'cdf.com'],
+ },
+ },
+ '#@#div#ad1': {
+ 'selector': {'type': 'css', 'value': 'div#ad1'},
+ 'action': 'show',
+ 'options': {},
+ },
+ 'abc.com,~cdf.abc.com#@##ad1': {
+ 'selector': {'type': 'css', 'value': '#ad1'},
+ 'action': 'show',
+ 'options': {
+ 'domains-none': True,
+ 'domains-include': ['abc.com'],
+ 'domains-exclude': ['cdf.abc.com'],
+ },
+ },
+ # Exception with a site key.
+ '@@||abc.com/ad?$subdocument,sitekey=foo': {
+ 'selector': {'type': 'url-pattern', 'value': '||abc.com/ad?'},
+ 'action': 'allow',
+ 'options': {
+ 'types-none': True,
+ 'types-include': ['subdocument'],
+ 'sitekeys': ['foo']
+ },
+ },
+ # Element hiding filter using old (a.k.a. simple) syntax.
+ 'abc.com#div(foo)(name=bar)(value=baz)': {
+ 'selector': {
+ 'type': 'abp-simple',
+ 'value': 'div(foo)(name=bar)(value=baz)',
+ },
+ 'action': 'hide',
+ 'options': {
+ 'domains-none': True,
+ 'domains-include': ['abc.com'],
+ },
+ },
+ # Exclude all types with "x|~x" trick.
+ 'x$image,~image': {'options': {'types-none': True}},
+ # More tricky combos of type flags.
+ 'x$~image,image': {'options': {}},
+ 'x$~image,image,~image': {'options': {'types-exclude': ['image']}},
+ 'x$image,~image,image': {
+ 'options': {'types-none': True, 'types-include': ['image']},
+ },
+}.items())
+def test_parse_filters(filter_text, expected):
+ """Parametric test for filter parsing"""
+ parsed = parse_line(filter_text)
+ assert parsed.type == 'filter'
+ assert parsed.text == filter_text
+ for attribute, expected_value in expected.items():
+ assert getattr(parsed, attribute) == expected_value
def test_parse_comment():
line = parse_line('! Block foo')
assert line.type == 'comment'
assert line.text == 'Block foo'
@@ -72,13 +169,19 @@
parse_line('[Adblock 1.1]')
def test_parse_filterlist():
result = parse_filterlist(['! foo', '! Title: bar'])
assert list(result) == [Comment('foo'), Metadata('Title', 'bar')]
-def test_exception_timing():
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
- result = parse_filterlist(['! good line', '%bad line%'])
- assert next(result) == Comment('good line')
- with pytest.raises(ParseError):
- next(result)
+def test_invalid_lines():
+ result = parse_filterlist([
+ '[Bad header]',
+ '! Good comment',
+ '%Bad instruction%',
+ ])
+ assert list(result) == [
+ InvalidLine('[Bad header]', 'Malformed header'),
+ Comment('Good comment'),
+ InvalidLine('%Bad instruction%', 'Unrecognized instruction'),
+ ]
« setup.py ('K') | « setup.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld