示例#1
0
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
示例#2
0
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)))
示例#3
0
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()
示例#4
0
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'
示例#5
0
def test_parse_comment():
    line = parse_line('! Block foo')
    assert line.type == 'comment'
    assert line.text == 'Block foo'
示例#6
0
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'
示例#8
0
def test_parse_bad_header():
    with pytest.raises(ParseError):
        parse_line('[Adblock 1.1]')
示例#9
0
def test_parse_bad_instruction():
    with pytest.raises(ParseError):
        parse_line('%foo bar%')
示例#10
0
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'
示例#11
0
def test_parse_comment():
    line = parse_line('! Block foo')
    assert line.type == 'comment'
    assert line.text == 'Block foo'
示例#12
0
def test_parse_filter():
    line = parse_line('||example.com/banner.gif')
    assert line.type == 'filter'
    assert line.expression == '||example.com/banner.gif'
示例#13
0
def test_parse_empty():
    line = parse_line('    ')
    assert line.type == 'emptyline'
示例#14
0
def test_parse_line_bytes():
    line = parse_line(b'! \xc3\xbc')
    assert line.text == '\xfc'
示例#15
0
def test_parse_invalid_position():
    with pytest.raises(ValueError):
        parse_line('', 'nonsense')
示例#16
0
def test_parse_nonmeta():
    line = parse_line('! WrongHeader: something')
    assert line.type == 'comment'
示例#17
0
def test_parse_instruction():
    line = parse_line('%include foo:bar/baz.txt%')
    assert line.type == 'include'
    assert line.target == 'foo:bar/baz.txt'
示例#18
0
def test_parse_nonmeta():
    line = parse_line('! WrongHeader: something')
    assert line.type == 'comment'
示例#19
0
def test_parse_header():
    line = parse_line('[Adblock Plus 1.1]')
    assert line.type == 'header'
    assert line.version == 'Adblock Plus 1.1'
示例#20
0
def test_parse_instruction():
    line = parse_line('%include foo:bar/baz.txt%')
    assert line.type == 'include'
    assert line.target == 'foo:bar/baz.txt'
示例#21
0
def test_parse_empty():
    line = parse_line('    ')
    assert line.type == 'emptyline'
示例#22
0
def test_parse_bad_instruction():
    with pytest.raises(ParseError):
        parse_line('%foo bar%')
示例#23
0
def test_parse_header():
    line = parse_line('[Adblock Plus 1.1]')
    assert line.type == 'header'
    assert line.version == 'Adblock Plus 1.1'
示例#24
0
def test_parse_bad_instruction():
    with pytest.raises(ParseError):
        parse_line('%include%')