| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 # This file is part of Adblock Plus <https://adblockplus.org/>, | 1 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 2 # Copyright (C) 2006-present eyeo GmbH | 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # | 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify | 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 | 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. | 6 # published by the Free Software Foundation. |
| 7 # | 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, | 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. | 11 # GNU General Public License for more details. |
| 12 # | 12 # |
| 13 # You should have received a copy of the GNU General Public License | 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/>. | 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 | 15 |
| 16 """Functional tests for testing rPython integration.""" | 16 """Functional tests for testing rPython integration.""" |
| 17 from __future__ import unicode_literals | 17 from __future__ import unicode_literals |
| 18 | 18 |
| 19 from collections import namedtuple | 19 from collections import namedtuple |
| 20 import pytest | 20 import pytest |
| 21 import sys | |
| 21 | 22 |
| 22 from abp.filters.rpy import ( | 23 from abp.filters.rpy import tuple2dict, line2dict |
| 23 tuple2dict, line2dict, | 24 |
| 24 ) | |
| 25 | 25 |
| 26 _SAMPLE_TUPLE = namedtuple('tuple', 'foo,bar') | 26 _SAMPLE_TUPLE = namedtuple('tuple', 'foo,bar') |
| 27 | 27 |
| 28 _ENCODED_STR_TYPE = type(''.encode('utf-8')) | 28 _TEST_EXAMPLES = { |
| 29 | |
| 30 _EXPECTED_TYPES = { | |
| 31 'header': { | 29 'header': { |
| 32 'type': _ENCODED_STR_TYPE, | 30 'in': b'[Adblock Plus 2.0]', |
| 33 'version': _ENCODED_STR_TYPE, | 31 'out': { |
| 32 b'type': b'Header', | |
| 33 b'version': b'Adblock Plus 2.0', | |
| 34 }, | |
| 34 }, | 35 }, |
| 35 'metadata': { | 36 'metadata': { |
| 36 'type': _ENCODED_STR_TYPE, | 37 'in': b'! Title: Example list', |
| 37 'key': _ENCODED_STR_TYPE, | 38 'out': { |
| 38 'value': _ENCODED_STR_TYPE, | 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 }, | |
| 39 }, | 50 }, |
| 40 'empty': { | 51 'empty': { |
| 41 'type': _ENCODED_STR_TYPE, | 52 'in': b'', |
| 42 }, | 53 'out': { |
| 43 'comment': { | 54 b'type': b'EmptyLine', |
| 44 'type': _ENCODED_STR_TYPE, | 55 }, |
| 45 'text': _ENCODED_STR_TYPE, | |
| 46 }, | 56 }, |
| 47 'include': { | 57 'include': { |
| 48 'type': _ENCODED_STR_TYPE, | 58 'in': b'%include www.test.py/filtelist.txt%', |
| 49 'target': _ENCODED_STR_TYPE, | 59 'out': { |
| 60 b'type': b'Include', | |
| 61 b'target': b'www.test.py/filtelist.txt', | |
| 62 }, | |
| 50 }, | 63 }, |
| 51 'filter': { | 64 'filter_single': { |
| 52 'type': _ENCODED_STR_TYPE, | 65 'in': b'foo.com##div#ad1', |
| 53 'text': _ENCODED_STR_TYPE, | 66 'out': { |
| 54 'selector': dict, | 67 b'type': b'Filter', |
| 55 'action': _ENCODED_STR_TYPE, | 68 b'text': b'foo.com##div#ad1', |
| 56 'options': list, | 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 }, | |
| 57 }, | 84 }, |
| 58 } | 85 } |
| 59 | |
| 60 | |
| 61 def check_correct_datatypes(data, expected_types): | |
|
Vasily Kuznetsov
2018/08/08 14:32:50
What do you think about checking generated dicts a
Tudor Avram
2018/08/09 12:55:02
Done.
| |
| 62 """Check if the resulting data has the correct keys and datatypes. | |
| 63 | |
| 64 Parameters | |
| 65 ---------- | |
| 66 data: dict | |
| 67 The dictonary produced by the API | |
| 68 | |
| 69 expected_types: dict | |
| 70 The expected keys and datatypes associated with them. | |
| 71 | |
| 72 Raises | |
| 73 ------- | |
| 74 AssertionError | |
| 75 If any of the types/ keys don't correspond | |
| 76 | |
| 77 """ | |
| 78 def encode_fn(x): | |
| 79 return x.encode('utf-8') | |
| 80 | |
| 81 data_keys = sorted(list(data.keys())) | |
| 82 expected_keys = sorted(list(map(encode_fn, expected_types.keys()))) | |
| 83 | |
| 84 if data_keys != expected_keys: | |
| 85 raise AssertionError('Invalid dict format. Got keys {0}, ' | |
| 86 'expected {1}'.format(data_keys, expected_keys)) | |
| 87 | |
| 88 for key in data: | |
| 89 if not isinstance(data[key], expected_types[key.decode('utf-8')]): | |
| 90 raise AssertionError( | |
| 91 'Invalid dict format. {0} should be {1}. Got {2}'.format( | |
| 92 key, | |
| 93 expected_types[key.decode('utf-8')], | |
| 94 type(data[key]), | |
| 95 ), | |
| 96 ) | |
| 97 | 86 |
| 98 | 87 |
| 99 def check_data_utf8(data): | 88 def check_data_utf8(data): |
| 100 """Check if all the strings in a dict are encoded as unicode. | 89 """Check if all the strings in a dict are encoded as unicode. |
| 101 | 90 |
| 102 Parameters | 91 Parameters |
| 103 ---------- | 92 ---------- |
| 104 data: dict | 93 data: dict |
| 105 The dictionary to be checked | 94 The dictionary to be checked |
| 106 | 95 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 129 def test_tuple2dict(foo, bar): | 118 def test_tuple2dict(foo, bar): |
| 130 """Test that dicts are produced correctly from a named tuple.""" | 119 """Test that dicts are produced correctly from a named tuple.""" |
| 131 data = _SAMPLE_TUPLE(foo=foo, bar=bar) | 120 data = _SAMPLE_TUPLE(foo=foo, bar=bar) |
| 132 exp = {'foo': foo, 'bar': bar, 'type': 'tuple'} | 121 exp = {'foo': foo, 'bar': bar, 'type': 'tuple'} |
| 133 | 122 |
| 134 result = tuple2dict(data) | 123 result = tuple2dict(data) |
| 135 | 124 |
| 136 assert exp == result | 125 assert exp == result |
| 137 | 126 |
| 138 | 127 |
| 139 @pytest.mark.parametrize('filter_text', [ | 128 @pytest.mark.skipif(sys.version.startswith('3.'), reason='Redundant on py3+.') |
| 140 'abc$image', | 129 @pytest.mark.parametrize('line_type', _TEST_EXAMPLES.keys()) |
| 141 '\u0432\u0435\u0431\u0441\u0430\u0439.\u0440\u0444$domain=\xfcber.de', | 130 def test_line2dict_encoding(line_type): |
| 142 ]) | 131 """Test that the resulting object has all strings encoded as utf-8. |
| 143 def test_line2dict_encoding(filter_text): | 132 |
| 144 """Test that the resulting object has all strings encoded as utf-8.""" | 133 These tests will only be run on Python2.*. On Python3.*, these test |
| 145 data = line2dict(filter_text.encode('utf-8')) | 134 cases are covered by test_line2dict() below. |
| 135 """ | |
| 136 data = line2dict(_TEST_EXAMPLES[line_type]['in']) | |
| 146 check_data_utf8(data) | 137 check_data_utf8(data) |
| 147 | 138 |
| 148 | 139 |
| 149 @pytest.mark.parametrize('line,expected', [ | 140 @pytest.mark.parametrize('line_type', list(_TEST_EXAMPLES.keys())) |
| 150 ('[Adblock Plus 2.0]', _EXPECTED_TYPES['header']), | 141 def test_line2dict_format(line_type): |
| 151 ('! Title: Example list', _EXPECTED_TYPES['metadata']), | |
| 152 ('! Comment', _EXPECTED_TYPES['comment']), | |
| 153 ('abc.com,cdf.com##div#ad1', _EXPECTED_TYPES['filter']), | |
| 154 ('%include www.test.py/filtelist.txt%', _EXPECTED_TYPES['include']), | |
| 155 ('', _EXPECTED_TYPES['empty']), | |
| 156 ]) | |
| 157 def test_line2dict_format(line, expected): | |
| 158 """Test that the API result has the appropriate format. | 142 """Test that the API result has the appropriate format. |
| 159 | 143 |
| 160 Checks for both keys and datatypes. | 144 Checks for both keys and datatypes. |
| 161 """ | 145 """ |
| 162 data = line2dict(line) | 146 data = line2dict(_TEST_EXAMPLES[line_type]['in']) |
| 163 check_correct_datatypes(data, expected) | 147 |
| 148 assert data == _TEST_EXAMPLES[line_type]['out'] | |
| LEFT | RIGHT |