Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: tests/test_rpy.py

Issue 30053555: Issue 7471 - Add an API for working with blocks of filters (Closed) Base URL: https://hg.adblockplus.org/python-abp
Patch Set: Refine the API and add documentation Created May 9, 2019, 11:13 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« abp/filters/rpy.py ('K') | « tests/test_filters_blocks.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 import sys
22 22
23 from abp.filters.rpy import tuple2dict, line2dict, lines2dicts 23 from abp.filters.rpy import line2dict, lines2dicts
24 24
25 25
26 _SAMPLE_TUPLE = namedtuple('tuple', 'foo,bar') 26 _SAMPLE_TUPLE = namedtuple('tuple', 'foo,bar')
27 27
28 _TEST_EXAMPLES = { 28 _TEST_EXAMPLES = {
29 'header': { 29 'header': {
30 'in': b'[Adblock Plus 2.0]', 30 'in': b'[Adblock Plus 2.0]',
31 'out': { 31 'out': {
32 b'type': b'Header', 32 b'type': b'Header',
33 b'version': b'Adblock Plus 2.0', 33 b'version': b'Adblock Plus 2.0',
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 for key, value in data.items(): 128 for key, value in data.items():
129 check_data_utf8(key) 129 check_data_utf8(key)
130 check_data_utf8(value) 130 check_data_utf8(value)
131 elif isinstance(data, (list, tuple)): 131 elif isinstance(data, (list, tuple)):
132 for item in data: 132 for item in data:
133 check_data_utf8(item) 133 check_data_utf8(item)
134 elif isinstance(data, type('')): 134 elif isinstance(data, type('')):
135 raise AssertionError('{} is str. Expected bytes.'.format(data)) 135 raise AssertionError('{} is str. Expected bytes.'.format(data))
136 136
137 137
138 @pytest.mark.parametrize('foo,bar', [
Vasily Kuznetsov 2019/05/09 11:18:59 The code that's tested here is now in `parser.py`
139 ('test_foo', 1),
140 ({'foofoo': 'test', 'foobar': 2}, [1, 2, 3]),
141 ((1,), [('a', True), ('b', False)]),
142 ])
143 def test_tuple2dict(foo, bar):
144 """Test that dicts are produced correctly from a named tuple."""
145 data = _SAMPLE_TUPLE(foo=foo, bar=bar)
146 exp = {'foo': foo, 'bar': bar, 'type': 'tuple'}
147
148 result = tuple2dict(data)
149
150 assert exp == result
151
152
153 @pytest.mark.skipif(sys.version.startswith('3.'), reason='Redundant on py3+.') 138 @pytest.mark.skipif(sys.version.startswith('3.'), reason='Redundant on py3+.')
154 @pytest.mark.parametrize('line_type', _TEST_EXAMPLES.keys()) 139 @pytest.mark.parametrize('line_type', _TEST_EXAMPLES.keys())
155 def test_line2dict_encoding(line_type): 140 def test_line2dict_encoding(line_type):
156 """Test that the resulting object has all strings encoded as utf-8. 141 """Test that the resulting object has all strings encoded as utf-8.
157 142
158 These tests will only be run on Python2.*. On Python3.*, these test 143 These tests will only be run on Python2.*. On Python3.*, these test
159 cases are covered by test_line2dict() below. 144 cases are covered by test_line2dict() below.
160 """ 145 """
161 data = line2dict(_TEST_EXAMPLES[line_type]['in']) 146 data = line2dict(_TEST_EXAMPLES[line_type]['in'])
162 check_data_utf8(data) 147 check_data_utf8(data)
(...skipping 27 matching lines...) Expand all
190 """Test that the API returns the correct result in the appropriate format. 175 """Test that the API returns the correct result in the appropriate format.
191 176
192 By default, lines2dicts() does not correctly parse headers and metadata. 177 By default, lines2dicts() does not correctly parse headers and metadata.
193 """ 178 """
194 tests = [t for t in _TEST_EXAMPLES.values() 179 tests = [t for t in _TEST_EXAMPLES.values()
195 if t['out'][b'type'] not in {b'Header', b'Metadata'}] 180 if t['out'][b'type'] not in {b'Header', b'Metadata'}]
196 ins = [ex['in'] for ex in tests] 181 ins = [ex['in'] for ex in tests]
197 outs = [ex['out'] for ex in tests] 182 outs = [ex['out'] for ex in tests]
198 183
199 assert lines2dicts(ins) == outs 184 assert lines2dicts(ins) == outs
OLDNEW
« abp/filters/rpy.py ('K') | « tests/test_filters_blocks.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld