|
2 | 2 | unicode_literals) |
3 | 3 | import itertools |
4 | 4 | from weakref import ref |
| 5 | +import warnings |
5 | 6 |
|
6 | 7 | from matplotlib.externals import six |
7 | 8 |
|
@@ -309,6 +310,67 @@ def dummy(self): |
309 | 310 | pass |
310 | 311 |
|
311 | 312 |
|
| 313 | +def _kwarg_norm_helper(inp, expected, kwargs_to_norm, warn_count=0): |
| 314 | + |
| 315 | + with warnings.catch_warnings(record=True) as w: |
| 316 | + warnings.simplefilter("always") |
| 317 | + assert expected == cbook.normalize_kwargs(inp, **kwargs_to_norm) |
| 318 | + assert len(w) == warn_count |
| 319 | + |
| 320 | + |
| 321 | +def _kwarg_norm_fail_helper(inp, kwargs_to_norm): |
| 322 | + assert_raises(TypeError, cbook.normalize_kwargs, inp, **kwargs_to_norm) |
| 323 | + |
| 324 | + |
| 325 | +def test_normalize_kwargs(): |
| 326 | + fail_mapping = ( |
| 327 | + ({'a': 1}, {'forbidden': ('a')}), |
| 328 | + ({'a': 1}, {'required': ('b')}), |
| 329 | + ({'a': 1, 'b': 2}, {'required': ('a'), 'allowed': ()}) |
| 330 | + ) |
| 331 | + |
| 332 | + for inp, kwargs in fail_mapping: |
| 333 | + yield _kwarg_norm_fail_helper, inp, kwargs |
| 334 | + |
| 335 | + warn_passing_mapping = ( |
| 336 | + ({'a': 1, 'b': 2}, {'a': 1}, {'alias_mapping': {'a': ['b']}}, 1), |
| 337 | + ({'a': 1, 'b': 2}, {'a': 1}, {'alias_mapping': {'a': ['b']}, |
| 338 | + 'allowed': ('a',)}, 1), |
| 339 | + ({'a': 1, 'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['a', 'b']}}, 1), |
| 340 | + |
| 341 | + ({'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'c': 3}, |
| 342 | + {'alias_mapping': {'a': ['b']}, 'required': ('a', )}, 1), |
| 343 | + |
| 344 | + ) |
| 345 | + |
| 346 | + for inp, exp, kwargs, wc in warn_passing_mapping: |
| 347 | + yield _kwarg_norm_helper, inp, exp, kwargs, wc |
| 348 | + |
| 349 | + pass_mapping = ( |
| 350 | + ({'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {}), |
| 351 | + ({'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['a', 'b']}}), |
| 352 | + ({'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['b']}, |
| 353 | + 'forbidden': ('b', )}), |
| 354 | + |
| 355 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', ), |
| 356 | + 'allowed': ('c', )}), |
| 357 | + |
| 358 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c'), |
| 359 | + 'allowed': ('c', )}), |
| 360 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c'), |
| 361 | + 'allowed': ('a', 'c')}), |
| 362 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c'), |
| 363 | + 'allowed': ()}), |
| 364 | + |
| 365 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c')}), |
| 366 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'allowed': ('a', 'c')}), |
| 367 | + |
| 368 | + ) |
| 369 | + |
| 370 | + for inp, exp, kwargs in pass_mapping: |
| 371 | + yield _kwarg_norm_helper, inp, exp, kwargs |
| 372 | + |
| 373 | + |
312 | 374 | def test_to_prestep(): |
313 | 375 | x = np.arange(4) |
314 | 376 | y1 = np.arange(4) |
|
0 commit comments