Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32a27ff

Browse files
committed
Add unit test for style.context() using a dict.
1 parent bbeb742 commit 32a27ff

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/matplotlib/style/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def use(name):
5656
name = [name]
5757

5858
for style in name:
59-
if not cbook.is_string_like(name):
59+
if not cbook.is_string_like(style):
6060
mpl.rcParams.update(style)
6161
continue
6262

lib/matplotlib/tests/test_style.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,48 @@ def test_context():
6767
# Check that this value is reset after the exiting the context.
6868
assert mpl.rcParams[PARAM] == 'gray'
6969

70+
def test_context_dict():
71+
ORIGINAL = 'gray'
72+
VALUE_OTHER = 'blue'
73+
mpl.rcParams[PARAM] = ORIGINAL
74+
with style.context({PARAM: VALUE_OTHER}):
75+
assert mpl.rcParams[PARAM] == VALUE_OTHER
76+
assert mpl.rcParams[PARAM] == ORIGINAL
77+
78+
def test_context_dictname1():
79+
# Test dict after style name where dict modifies the same parameter.
80+
ORIGINAL = 'gray'
81+
VALUE_OTHER = 'blue'
82+
mpl.rcParams[PARAM] = ORIGINAL
83+
with temp_style('test', DUMMY_SETTINGS):
84+
with style.context(['test', {PARAM: VALUE_OTHER}]):
85+
assert mpl.rcParams[PARAM] == VALUE_OTHER
86+
assert mpl.rcParams[PARAM] == ORIGINAL
87+
88+
def test_context_dictname2():
89+
# Test dict before style name where dict modifies the same parameter.
90+
ORIGINAL = 'gray'
91+
VALUE_OTHER = 'blue'
92+
mpl.rcParams[PARAM] = ORIGINAL
93+
with temp_style('test', DUMMY_SETTINGS):
94+
with style.context([{PARAM: VALUE_OTHER}, 'test']):
95+
assert mpl.rcParams[PARAM] == VALUE
96+
assert mpl.rcParams[PARAM] == ORIGINAL
97+
98+
def test_context_dictname3():
99+
# Test dict after style name where dict modifies the a different parameter.
100+
ORIGINAL = 'gray'
101+
PARAM_OTHER = 'text.usetex'
102+
VALUE_OTHER = True
103+
d = {PARAM_OTHER: VALUE_OTHER}
104+
mpl.rcParams[PARAM] = ORIGINAL
105+
mpl.rcParams[PARAM_OTHER] = (not VALUE_OTHER)
106+
with temp_style('test', DUMMY_SETTINGS):
107+
with style.context(['test', d]):
108+
assert mpl.rcParams[PARAM] == VALUE
109+
assert mpl.rcParams[PARAM_OTHER] == VALUE_OTHER
110+
assert mpl.rcParams[PARAM] == ORIGINAL
111+
assert mpl.rcParams[PARAM_OTHER] == (not VALUE_OTHER)
70112

71113
if __name__ == '__main__':
72114
from numpy import testing

0 commit comments

Comments
 (0)