|
7 | 7 |
|
8 | 8 | from paths_cli.parsing.core import * |
9 | 9 |
|
| 10 | +class MockNamedObject: |
| 11 | + # used in the tests for Parser._parse_dict and Parser.register_object |
| 12 | + def __init__(self, data): |
| 13 | + self.data = data |
| 14 | + self.name = None |
| 15 | + |
| 16 | + def named(self, name): |
| 17 | + self.name = name |
| 18 | + return self |
| 19 | + |
| 20 | +def mock_named_object_factory(dct): |
| 21 | + return MockNamedObject(**dct) |
| 22 | + |
| 23 | + |
10 | 24 | class TestParameter: |
11 | 25 | def setup(self): |
12 | 26 | self.loader = Mock( |
@@ -180,40 +194,101 @@ def test_call(self): |
180 | 194 |
|
181 | 195 |
|
182 | 196 | class TestParser: |
| 197 | + def setup(self): |
| 198 | + self.parser = Parser( |
| 199 | + {'foo': mock_named_object_factory}, |
| 200 | + 'foo_parser' |
| 201 | + ) |
| 202 | + |
| 203 | + def _mock_register_obj(self): |
| 204 | + obj = "bar" |
| 205 | + self.parser.all_objs.append(obj) |
| 206 | + self.parser.named_objs['foo'] = obj |
| 207 | + |
183 | 208 | def test_parse_str(self): |
184 | 209 | # parse_str should load a known object with the input name |
185 | | - pytest.skip() |
| 210 | + self._mock_register_obj() |
| 211 | + assert self.parser._parse_str('foo') == "bar" |
186 | 212 |
|
187 | 213 | def test_parse_str_error(self): |
188 | 214 | # if parse_str is given a name that is not known, an InputError |
189 | 215 | # should be raised |
190 | | - pytest.skip() |
| 216 | + self._mock_register_obj() |
| 217 | + with pytest.raises(InputError, match="Unable to find"): |
| 218 | + self.parser._parse_str('baz') |
191 | 219 |
|
192 | | - def test_parse_dict(self): |
| 220 | + @pytest.mark.parametrize('named', [True, False]) |
| 221 | + def test_parse_dict(self, named): |
193 | 222 | # parse_dct should create the object from the input dict |
194 | | - pytest.skip() |
| 223 | + input_dict = {'type': 'foo', 'data': "qux"} |
| 224 | + if named: |
| 225 | + input_dict['name'] = 'bar' |
| 226 | + |
| 227 | + obj = self.parser._parse_dict(input_dict) |
| 228 | + assert obj.data == "qux" |
| 229 | + name = {True: 'bar', False: None}[named] |
| 230 | + assert obj.name == name |
195 | 231 |
|
196 | 232 | def test_register_object_named(self): |
197 | 233 | # when registered, a named object should register with the all_objs |
198 | 234 | # list and with the named_objs dict |
199 | | - pytest.skip() |
| 235 | + obj = MockNamedObject('foo') |
| 236 | + assert obj.name is None |
| 237 | + assert self.parser.all_objs == [] |
| 238 | + assert self.parser.named_objs == {} |
| 239 | + obj = self.parser.register_object(obj, 'bar') |
| 240 | + assert obj.name == 'bar' |
| 241 | + assert self.parser.all_objs == [obj] |
| 242 | + assert self.parser.named_objs == {'bar': obj} |
200 | 243 |
|
201 | 244 | def test_register_object_unnamed(self): |
202 | 245 | # when registered, an unnamed object should register with the |
203 | 246 | # all_objs list and leave the named_objs dict unchanged |
204 | | - pytest.skip() |
| 247 | + obj = MockNamedObject('foo') |
| 248 | + assert obj.name is None |
| 249 | + assert self.parser.all_objs == [] |
| 250 | + assert self.parser.named_objs == {} |
| 251 | + obj = self.parser.register_object(obj, None) |
| 252 | + assert obj.name is None |
| 253 | + assert self.parser.all_objs == [obj] |
| 254 | + assert self.parser.named_objs == {} |
205 | 255 |
|
206 | 256 | def test_register_object_duplicate(self): |
207 | 257 | # if an attempt is made to register an object with a name that is |
208 | 258 | # already in use, an InputError should be raised, and the object |
209 | 259 | # should not register with either all_objs or named_objs |
210 | | - pytest.skip() |
| 260 | + obj = MockNamedObject('foo').named('bar') |
| 261 | + self.parser.named_objs['bar'] = obj |
| 262 | + self.parser.all_objs.append(obj) |
| 263 | + obj2 = MockNamedObject('baz') |
| 264 | + with pytest.raises(InputError, match="already exists"): |
| 265 | + self.parser.register_object(obj2, 'bar') |
| 266 | + |
| 267 | + assert self.parser.named_objs == {'bar': obj} |
| 268 | + assert self.parser.all_objs == [obj] |
| 269 | + assert obj2.name is None |
211 | 270 |
|
212 | 271 | def test_register_builder(self): |
213 | | - pytest.skip() |
| 272 | + # a new builder can be registered and used, if it has a new name |
| 273 | + assert len(self.parser.type_dispatch) == 1 |
| 274 | + assert 'bar' not in self.parser.type_dispatch |
| 275 | + self.parser.register_builder(lambda dct: 10, 'bar') |
| 276 | + assert len(self.parser.type_dispatch) == 2 |
| 277 | + assert 'bar' in self.parser.type_dispatch |
| 278 | + input_dict = {'type': 'bar'} |
| 279 | + assert self.parser(input_dict) == 10 |
214 | 280 |
|
215 | 281 | def test_register_builder_duplicate(self): |
216 | | - pytest.skip() |
| 282 | + # if an attempt is made to registered a builder with a name that is |
| 283 | + # already in use, a RuntimeError is raised |
| 284 | + orig = self.parser.type_dispatch['foo'] |
| 285 | + # TODO: this should be an error; need to figure out how to avoid |
| 286 | + # duplication |
| 287 | + # with pytest.raises(RuntimeError, match="already registered"): |
| 288 | + with pytest.warns(UserWarning, match="already registered"): |
| 289 | + self.parser.register_builder(lambda dct: 10, 'foo') |
| 290 | + |
| 291 | + assert self.parser.type_dispatch['foo'] is orig |
217 | 292 |
|
218 | 293 | @pytest.mark.parametrize('input_type', ['str', 'dict']) |
219 | 294 | def test_parse(self, input_type): |
|
0 commit comments