コード例 #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
ファイル: rpy.py プロジェクト: adblockplus/python-abp
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
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
def test_parse_bad_header():
    with pytest.raises(ParseError):
        parse_line('[Adblock 1.1]')
コード例 #7
0
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
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
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
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
def test_parse_comment():
    line = parse_line('! Block foo')
    assert line.type == 'comment'
    assert line.text == 'Block foo'
コード例 #12
0
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
def test_parse_filter():
    line = parse_line('||example.com/banner.gif')
    assert line.type == 'filter'
    assert line.expression == '||example.com/banner.gif'
コード例 #13
0
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
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
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
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
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
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
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
def test_parse_bad_instruction():
    with pytest.raises(ParseError):
        parse_line('%foo bar%')
コード例 #23
0
ファイル: test_parser.py プロジェクト: mjhennig/python-abp
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%')