 Issue 29465715:
  Fixes 4969 - Add parsing of filters  (Closed)
    
  
    Issue 29465715:
  Fixes 4969 - Add parsing of filters  (Closed) 
  | Index: tests/test_parser.py | 
| =================================================================== | 
| --- a/tests/test_parser.py | 
| +++ b/tests/test_parser.py | 
| @@ -12,29 +12,114 @@ | 
| # | 
| # 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_bad_option(): | 
| 
Vasily Kuznetsov
2017/07/28 18:57:49
Now we can also detect unknown options. Yaaay!
 
Vasily Kuznetsov
2017/08/02 16:21:17
I remove this test since we've agreed to remove th
 | 
| + with pytest.raises(ParseError): | 
| + parse_line('foo.com$script,foo') | 
| + with pytest.raises(ParseError): | 
| + parse_line('foo.com$~foo') | 
| def test_parse_comment(): | 
| line = parse_line('! Block foo') | 
| assert line.type == 'comment' | 
| assert line.text == 'Block foo' |