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 line2dict(text, mode='body'): """Convert a filterlist line to a dictionary. All strings in the output dictionary will be UTF8 byte strings. This is necessary to prevent unicode encoding errors in rPython conversion layer. Parameters ---------- text: str The filter text we want to parse mode: str Parsing mode (see `abp.filters.parser.parse_line`). Returns ------- dict With the parsing results and all strings converted to utf8 byte strings. """ return strings2utf8(tuple2dict(parse_line(text, mode)))
def line2dict(text, mode='body'): """Convert a filterlist line to a dictionary. All strings in the output dictionary will be UTF8 byte strings. This is necessary to prevent unicode encoding errors in rPython conversion layer. Parameters ---------- text: str The filter text we want to parse mode: str Parsing mode (see `abp.filters.parser.parse_line`). Returns ------- dict With the parsing results and all strings converted to utf8 byte strings. """ return parse_line(text, mode).to_dict()
def test_parse_meta(): line = parse_line('! Homepage : http://aaa.com/b') assert line.type == 'metadata' assert line.key == 'Homepage' assert line.value == 'http://aaa.com/b'
def test_parse_comment(): line = parse_line('! Block foo') assert line.type == 'comment' assert line.text == 'Block foo'
def test_parse_bad_header(): with pytest.raises(ParseError): parse_line('[Adblock 1.1]')
def test_parse_metadata(): # Header-like lines are just filters. assert parse_line('[Adblock 1.1]', 'metadata').type == 'filter' # Metadata-like lines are metadata. assert parse_line('! Foo: bar', 'metadata').type == 'metadata'
def test_parse_bad_header(): with pytest.raises(ParseError): parse_line('[Adblock 1.1]')
def test_parse_bad_instruction(): with pytest.raises(ParseError): parse_line('%foo bar%')
def test_parse_meta(): line = parse_line('! Homepage : http://aaa.com/b') assert line.type == 'metadata' assert line.key == 'Homepage' assert line.value == 'http://aaa.com/b'
def test_parse_comment(): line = parse_line('! Block foo') assert line.type == 'comment' assert line.text == 'Block foo'
def test_parse_filter(): line = parse_line('||example.com/banner.gif') assert line.type == 'filter' assert line.expression == '||example.com/banner.gif'
def test_parse_empty(): line = parse_line(' ') assert line.type == 'emptyline'
def test_parse_line_bytes(): line = parse_line(b'! \xc3\xbc') assert line.text == '\xfc'
def test_parse_invalid_position(): with pytest.raises(ValueError): parse_line('', 'nonsense')
def test_parse_nonmeta(): line = parse_line('! WrongHeader: something') assert line.type == 'comment'
def test_parse_instruction(): line = parse_line('%include foo:bar/baz.txt%') assert line.type == 'include' assert line.target == 'foo:bar/baz.txt'
def test_parse_nonmeta(): line = parse_line('! WrongHeader: something') assert line.type == 'comment'
def test_parse_header(): line = parse_line('[Adblock Plus 1.1]') assert line.type == 'header' assert line.version == 'Adblock Plus 1.1'
def test_parse_instruction(): line = parse_line('%include foo:bar/baz.txt%') assert line.type == 'include' assert line.target == 'foo:bar/baz.txt'
def test_parse_empty(): line = parse_line(' ') assert line.type == 'emptyline'
def test_parse_bad_instruction(): with pytest.raises(ParseError): parse_line('%foo bar%')
def test_parse_header(): line = parse_line('[Adblock Plus 1.1]') assert line.type == 'header' assert line.version == 'Adblock Plus 1.1'
def test_parse_bad_instruction(): with pytest.raises(ParseError): parse_line('%include%')