|
12 | 12 | from numpy.testing.utils import (assert_array_equal, assert_approx_equal, |
13 | 13 | assert_array_almost_equal) |
14 | 14 | from nose.tools import raises, assert_raises |
| 15 | +import pytest |
15 | 16 |
|
16 | 17 | import matplotlib.cbook as cbook |
17 | 18 | import matplotlib.colors as mcolors |
@@ -343,65 +344,61 @@ def test_sanitize_sequence(): |
343 | 344 | assert k == cbook.sanitize_sequence(k) |
344 | 345 |
|
345 | 346 |
|
346 | | -def _kwarg_norm_helper(inp, expected, kwargs_to_norm, warn_count=0): |
347 | | - |
| 347 | +fail_mapping = ( |
| 348 | + ({'a': 1}, {'forbidden': ('a')}), |
| 349 | + ({'a': 1}, {'required': ('b')}), |
| 350 | + ({'a': 1, 'b': 2}, {'required': ('a'), 'allowed': ()}) |
| 351 | +) |
| 352 | + |
| 353 | +warn_passing_mapping = ( |
| 354 | + ({'a': 1, 'b': 2}, {'a': 1}, {'alias_mapping': {'a': ['b']}}, 1), |
| 355 | + ({'a': 1, 'b': 2}, {'a': 1}, |
| 356 | + {'alias_mapping': {'a': ['b']}, 'allowed': ('a',)}, 1), |
| 357 | + ({'a': 1, 'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['a', 'b']}}, 1), |
| 358 | + ({'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'c': 3}, |
| 359 | + {'alias_mapping': {'a': ['b']}, 'required': ('a', )}, 1), |
| 360 | +) |
| 361 | + |
| 362 | +pass_mapping = ( |
| 363 | + ({'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {}), |
| 364 | + ({'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['a', 'b']}}), |
| 365 | + ({'b': 2}, {'a': 2}, |
| 366 | + {'alias_mapping': {'a': ['b']}, 'forbidden': ('b', )}), |
| 367 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, |
| 368 | + {'required': ('a', ), 'allowed': ('c', )}), |
| 369 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, |
| 370 | + {'required': ('a', 'c'), 'allowed': ('c', )}), |
| 371 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, |
| 372 | + {'required': ('a', 'c'), 'allowed': ('a', 'c')}), |
| 373 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, |
| 374 | + {'required': ('a', 'c'), 'allowed': ()}), |
| 375 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c')}), |
| 376 | + ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'allowed': ('a', 'c')}), |
| 377 | +) |
| 378 | + |
| 379 | + |
| 380 | +@pytest.mark.parametrize('inp, kwargs_to_norm', fail_mapping) |
| 381 | +def test_normalize_kwargs_fail(inp, kwargs_to_norm): |
| 382 | + with pytest.raises(TypeError): |
| 383 | + cbook.normalize_kwargs(inp, **kwargs_to_norm) |
| 384 | + |
| 385 | + |
| 386 | +@pytest.mark.parametrize('inp, expected, kwargs_to_norm, warn_count', |
| 387 | + warn_passing_mapping) |
| 388 | +def test_normalize_kwargs_warn(inp, expected, kwargs_to_norm, warn_count): |
348 | 389 | with warnings.catch_warnings(record=True) as w: |
349 | 390 | warnings.simplefilter("always") |
350 | 391 | assert expected == cbook.normalize_kwargs(inp, **kwargs_to_norm) |
351 | 392 | assert len(w) == warn_count |
352 | 393 |
|
353 | 394 |
|
354 | | -def _kwarg_norm_fail_helper(inp, kwargs_to_norm): |
355 | | - assert_raises(TypeError, cbook.normalize_kwargs, inp, **kwargs_to_norm) |
356 | | - |
357 | | - |
358 | | -def test_normalize_kwargs(): |
359 | | - fail_mapping = ( |
360 | | - ({'a': 1}, {'forbidden': ('a')}), |
361 | | - ({'a': 1}, {'required': ('b')}), |
362 | | - ({'a': 1, 'b': 2}, {'required': ('a'), 'allowed': ()}) |
363 | | - ) |
364 | | - |
365 | | - for inp, kwargs in fail_mapping: |
366 | | - yield _kwarg_norm_fail_helper, inp, kwargs |
367 | | - |
368 | | - warn_passing_mapping = ( |
369 | | - ({'a': 1, 'b': 2}, {'a': 1}, {'alias_mapping': {'a': ['b']}}, 1), |
370 | | - ({'a': 1, 'b': 2}, {'a': 1}, {'alias_mapping': {'a': ['b']}, |
371 | | - 'allowed': ('a',)}, 1), |
372 | | - ({'a': 1, 'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['a', 'b']}}, 1), |
373 | | - |
374 | | - ({'a': 1, 'b': 2, 'c': 3}, {'a': 1, 'c': 3}, |
375 | | - {'alias_mapping': {'a': ['b']}, 'required': ('a', )}, 1), |
376 | | - |
377 | | - ) |
378 | | - |
379 | | - for inp, exp, kwargs, wc in warn_passing_mapping: |
380 | | - yield _kwarg_norm_helper, inp, exp, kwargs, wc |
381 | | - |
382 | | - pass_mapping = ( |
383 | | - ({'a': 1, 'b': 2}, {'a': 1, 'b': 2}, {}), |
384 | | - ({'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['a', 'b']}}), |
385 | | - ({'b': 2}, {'a': 2}, {'alias_mapping': {'a': ['b']}, |
386 | | - 'forbidden': ('b', )}), |
387 | | - |
388 | | - ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', ), |
389 | | - 'allowed': ('c', )}), |
390 | | - |
391 | | - ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c'), |
392 | | - 'allowed': ('c', )}), |
393 | | - ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c'), |
394 | | - 'allowed': ('a', 'c')}), |
395 | | - ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c'), |
396 | | - 'allowed': ()}), |
397 | | - |
398 | | - ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'required': ('a', 'c')}), |
399 | | - ({'a': 1, 'c': 3}, {'a': 1, 'c': 3}, {'allowed': ('a', 'c')}), |
400 | | - |
401 | | - ) |
402 | | - |
403 | | - for inp, exp, kwargs in pass_mapping: |
404 | | - yield _kwarg_norm_helper, inp, exp, kwargs |
| 395 | +@pytest.mark.parametrize('inp, expected, kwargs_to_norm', |
| 396 | + pass_mapping) |
| 397 | +def test_normalize_kwargs_pass(inp, expected, kwargs_to_norm): |
| 398 | + with warnings.catch_warnings(record=True) as w: |
| 399 | + warnings.simplefilter("always") |
| 400 | + assert expected == cbook.normalize_kwargs(inp, **kwargs_to_norm) |
| 401 | + assert len(w) == 0 |
405 | 402 |
|
406 | 403 |
|
407 | 404 | def test_to_prestep(): |
|
0 commit comments