OLD | NEW |
(Empty) | |
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
| 16 """Functional tests for testing rPython integration.""" |
| 17 from __future__ import unicode_literals |
| 18 |
| 19 from collections import namedtuple |
| 20 import pytest |
| 21 import sys |
| 22 |
| 23 from abp.filters.rpy import tuple2dict, line2dict |
| 24 |
| 25 |
| 26 _SAMPLE_TUPLE = namedtuple('tuple', 'foo,bar') |
| 27 |
| 28 _TEST_EXAMPLES = { |
| 29 'header': { |
| 30 'in': b'[Adblock Plus 2.0]', |
| 31 'out': { |
| 32 b'type': b'Header', |
| 33 b'version': b'Adblock Plus 2.0', |
| 34 }, |
| 35 }, |
| 36 'metadata': { |
| 37 'in': b'! Title: Example list', |
| 38 'out': { |
| 39 b'type': b'Metadata', |
| 40 b'key': b'Title', |
| 41 b'value': b'Example list', |
| 42 }, |
| 43 }, |
| 44 'comment': { |
| 45 'in': b'! Comment', |
| 46 'out': { |
| 47 b'type': b'Comment', |
| 48 b'text': b'Comment', |
| 49 }, |
| 50 }, |
| 51 'empty': { |
| 52 'in': b'', |
| 53 'out': { |
| 54 b'type': b'EmptyLine', |
| 55 }, |
| 56 }, |
| 57 'include': { |
| 58 'in': b'%include www.test.py/filtelist.txt%', |
| 59 'out': { |
| 60 b'type': b'Include', |
| 61 b'target': b'www.test.py/filtelist.txt', |
| 62 }, |
| 63 }, |
| 64 'filter_single': { |
| 65 'in': b'foo.com##div#ad1', |
| 66 'out': { |
| 67 b'type': b'Filter', |
| 68 b'text': b'foo.com##div#ad1', |
| 69 b'selector': {b'type': b'css', b'value': b'div#ad1'}, |
| 70 b'action': b'hide', |
| 71 b'options': [(b'domain', [(b'foo.com', True)])], |
| 72 }, |
| 73 }, |
| 74 'filter_multiple': { |
| 75 'in': b'foo.com,bar.com##div#ad1', |
| 76 'out': { |
| 77 b'type': b'Filter', |
| 78 b'text': b'foo.com,bar.com##div#ad1', |
| 79 b'selector': {b'type': b'css', b'value': b'div#ad1'}, |
| 80 b'action': b'hide', |
| 81 b'options': [(b'domain', [(b'foo.com', True), (b'bar.com', |
| 82 True)])], |
| 83 }, |
| 84 }, |
| 85 } |
| 86 |
| 87 |
| 88 def check_data_utf8(data): |
| 89 """Check if all the strings in a dict are encoded as unicode. |
| 90 |
| 91 Parameters |
| 92 ---------- |
| 93 data: dict |
| 94 The dictionary to be checked |
| 95 |
| 96 Raises |
| 97 ------- |
| 98 AssertionError |
| 99 If any of the strings encountered are not unicode |
| 100 |
| 101 """ |
| 102 if isinstance(data, dict): |
| 103 for key, value in data.items(): |
| 104 check_data_utf8(key) |
| 105 check_data_utf8(value) |
| 106 elif isinstance(data, (list, tuple)): |
| 107 for item in data: |
| 108 check_data_utf8(item) |
| 109 elif isinstance(data, type('')): |
| 110 raise AssertionError('{} is str. Expected bytes.'.format(data)) |
| 111 |
| 112 |
| 113 @pytest.mark.parametrize('foo,bar', [ |
| 114 ('test_foo', 1), |
| 115 ({'foofoo': 'test', 'foobar': 2}, [1, 2, 3]), |
| 116 ((1,), [('a', True), ('b', False)]), |
| 117 ]) |
| 118 def test_tuple2dict(foo, bar): |
| 119 """Test that dicts are produced correctly from a named tuple.""" |
| 120 data = _SAMPLE_TUPLE(foo=foo, bar=bar) |
| 121 exp = {'foo': foo, 'bar': bar, 'type': 'tuple'} |
| 122 |
| 123 result = tuple2dict(data) |
| 124 |
| 125 assert exp == result |
| 126 |
| 127 |
| 128 @pytest.mark.skipif(sys.version.startswith('3.'), reason='Redundant on py3+.') |
| 129 @pytest.mark.parametrize('line_type', list(_TEST_EXAMPLES.keys())) |
| 130 def test_line2dict_encoding(line_type): |
| 131 """Test that the resulting object has all strings encoded as utf-8. |
| 132 |
| 133 These tests will only be run on Python2.*. On Python3.*, these test |
| 134 cases are covered by test_line2dict() below. |
| 135 """ |
| 136 data = line2dict(_TEST_EXAMPLES[line_type]['in']) |
| 137 check_data_utf8(data) |
| 138 |
| 139 |
| 140 @pytest.mark.parametrize('line_type', list(_TEST_EXAMPLES.keys())) |
| 141 def test_line2dict_format(line_type): |
| 142 """Test that the API result has the appropriate format. |
| 143 |
| 144 Checks for both keys and datatypes. |
| 145 """ |
| 146 data = line2dict(_TEST_EXAMPLES[line_type]['in']) |
| 147 |
| 148 assert data == _TEST_EXAMPLES[line_type]['out'] |
OLD | NEW |