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: Rebase to 1f5d7ead9bff Created Oct. 24, 2017, 3:58 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
« no previous file with comments | « abp/filters/parser.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,107 @@
#
# 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 import (
+ parse_line, parse_filterlist, ParseError,
+ SELECTOR_TYPE as ST, FILTER_ACTION as FA, FILTER_OPTION as OPT,
+)
from abp.filters.parser import Comment, Metadata
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', {
+ # Blocking filters with patterns and regexps and blocking exceptions.
+ '*asdf*d**dd*': {
+ 'selector': {'type': ST.URL_PATTERN, 'value': '*asdf*d**dd*'},
+ 'action': FA.BLOCK,
+ },
+ '@@|*asd|f*d**dd*|': {
+ 'selector': {'type': ST.URL_PATTERN, 'value': '|*asd|f*d**dd*|'},
+ 'action': FA.ALLOW,
+ },
+ '/ddd|f?a[s]d/': {
+ 'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'},
+ 'action': FA.BLOCK,
+ },
+ '@@/ddd|f?a[s]d/': {
+ 'selector': {'type': ST.URL_REGEXP, 'value': 'ddd|f?a[s]d'},
+ 'action': FA.ALLOW,
+ },
+ # Blocking filters with some options.
+ 'bla$match-case,~script,domain=foo.com|~bar.com,sitekey=foo': {
+ 'selector': {'type': ST.URL_PATTERN, 'value': 'bla'},
+ 'action': FA.BLOCK,
+ 'options': [
+ (OPT.MATCH_CASE, True),
+ (OPT.SCRIPT, False),
+ (OPT.DOMAIN, [('foo.com', True), ('bar.com', False)]),
+ (OPT.SITEKEY, ['foo']),
+ ],
+ },
+ '@@http://bla$~script,~other,sitekey=foo|bar': {
+ 'selector': {'type': ST.URL_PATTERN, 'value': 'http://bla'},
+ 'action': FA.ALLOW,
+ 'options': [
+ (OPT.SCRIPT, False),
+ (OPT.OTHER, False),
+ (OPT.SITEKEY, ['foo', 'bar']),
+ ],
+ },
+ # Element hiding filters and exceptions.
+ '##ddd': {
+ 'selector': {'type': ST.CSS, 'value': 'ddd'},
+ 'action': FA.HIDE,
+ 'options': [],
+ },
+ '#@#body > div:first-child': {
+ 'selector': {'type': ST.CSS, 'value': 'body > div:first-child'},
+ 'action': FA.SHOW,
+ 'options': [],
+ },
+ 'foo,~bar##ddd': {
+ 'options': [(OPT.DOMAIN, [('foo', True), ('bar', False)])],
+ },
+ # Element hiding emulation filters (extended CSS).
+ 'foo,~bar#?#:-abp-properties(abc)': {
+ 'selector': {'type': ST.XCSS, 'value': ':-abp-properties(abc)'},
+ 'action': FA.HIDE,
+ 'options': [(OPT.DOMAIN, [('foo', True), ('bar', False)])],
+ },
+ 'foo.com#?#aaa :-abp-properties(abc) bbb': {
+ 'selector': {
+ 'type': ST.XCSS,
+ 'value': 'aaa :-abp-properties(abc) bbb'
+ },
+ },
+ '#?#:-abp-properties(|background-image: url(data:*))': {
+ 'selector': {
+ 'type': ST.XCSS,
+ 'value': ':-abp-properties(|background-image: url(data:*))'
+ },
+ 'options': [],
+ },
+}.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'
« no previous file with comments | « abp/filters/parser.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld