|
12 | 12 | frommatplotlibimport_api,_c_internal_utils |
13 | 13 | importmatplotlib.pyplotasplt |
14 | 14 | importmatplotlib.colorsasmcolors |
| 15 | +importmatplotlib.patheffectsaspath_effects |
| 16 | +frommatplotlib.testing.decoratorsimportcheck_figures_equal |
| 17 | + |
15 | 18 | importnumpyasnp |
16 | 19 | frommatplotlib.rcsetupimport ( |
17 | 20 | validate_bool, |
|
27 | 30 | validate_int, |
28 | 31 | validate_markevery, |
29 | 32 | validate_stringlist, |
| 33 | +validate_path_effects, |
30 | 34 | _validate_linestyle, |
31 | | -_listify_validator) |
| 35 | +_listify_validator, |
| 36 | + ) |
32 | 37 |
|
33 | 38 |
|
34 | 39 | deftest_rcparams(tmpdir): |
@@ -628,3 +633,62 @@ def test_rcparams_legend_loc_from_file(tmpdir, value): |
628 | 633 |
|
629 | 634 | withmpl.rc_context(fname=rc_path): |
630 | 635 | assertmpl.rcParams["legend.loc"]==value |
| 636 | + |
| 637 | +ped= [('Normal', {}), |
| 638 | + ('Stroke', {'offset': (1,2)}), |
| 639 | + ('withStroke', {'linewidth':4,'foreground':'w'})] |
| 640 | + |
| 641 | +pel= [path_effects.Normal(), |
| 642 | +path_effects.Stroke((1,2)), |
| 643 | +path_effects.withStroke(linewidth=4,foreground='w')] |
| 644 | + |
| 645 | + |
| 646 | +@pytest.mark.parametrize("value", [pel,ped],ids=["func","dict"]) |
| 647 | +deftest_path_effects(value): |
| 648 | +assertvalidate_path_effects(value)==value |
| 649 | +forvinvalue: |
| 650 | +assertvalidate_path_effects(value)==value |
| 651 | + |
| 652 | + |
| 653 | +deftest_path_effects_string(): |
| 654 | +"""test list of dicts properly parsed""" |
| 655 | +pstr="('Normal', ), " |
| 656 | +pstr+="('Stroke', {'offset': (1, 2)})," |
| 657 | +pstr+="('withStroke', {'linewidth': 4, 'foreground': 'w'})" |
| 658 | +assertvalidate_path_effects(pstr)==ped |
| 659 | + |
| 660 | + |
| 661 | +@pytest.mark.parametrize("fdict, flist", |
| 662 | + [([ped[0]], [pel[0]]), |
| 663 | + ([ped[1]], [pel[1]]), |
| 664 | + ([ped[2]], [ped[2]]), |
| 665 | + (ped,pel)], |
| 666 | +ids=['function','args','kwargs','all']) |
| 667 | +@check_figures_equal() |
| 668 | +deftest_path_effects_picture(fig_test,fig_ref,fdict,flist): |
| 669 | +withmpl.rc_context({'path.effects':fdict}): |
| 670 | +fig_test.subplots().plot([1,2,3]) |
| 671 | + |
| 672 | +withmpl.rc_context({'path.effects':flist}): |
| 673 | +fig_ref.subplots().plot([1,2,3]) |
| 674 | + |
| 675 | + |
| 676 | +@pytest.mark.parametrize("s, msg", [ |
| 677 | + ([1,2,3],"Expected a list of patheffects .*"), |
| 678 | + (("Happy", ),r".* \'Happy\' is not a valid value for path\.effects\.function.*"), |
| 679 | + (("Normal", [1,2,3]),r"Expected a dictionary .*"),]) |
| 680 | +deftest_path_effect_errors(s,msg): |
| 681 | +withpytest.raises(ValueError,match=msg): |
| 682 | +mpl.rcParams['path.effects']=s |
| 683 | + |
| 684 | + |
| 685 | +deftest_path_effects_from_file(tmpdir): |
| 686 | +# rcParams['legend.loc'] should be settable from matplotlibrc. |
| 687 | +# if any of these are not allowed, an exception will be raised. |
| 688 | +# test for gh issue #22338 |
| 689 | +rc_path=tmpdir.join("matplotlibrc") |
| 690 | +rc_path.write("path.effects: ('Normal', {}), ('withStroke', {'linewidth': 2})") |
| 691 | + |
| 692 | +withmpl.rc_context(fname=rc_path): |
| 693 | +assertisinstance(mpl.rcParams["path.effects"][0],path_effects.Normal) |
| 694 | +assertisinstance(mpl.rcParams["path.effects"][1],path_effects.withStroke) |