|
8 | 8 | from contextlib import contextmanager
|
9 | 9 |
|
10 | 10 | from nose import SkipTest
|
11 |
| -from nose.tools import assert_raises |
| 11 | +from nose.tools import assert_raises, assert_equal |
12 | 12 | from nose.plugins.attrib import attr
|
13 | 13 |
|
14 | 14 | import matplotlib as mpl
|
15 | 15 | from matplotlib import style
|
16 |
| -from matplotlib.style.core import USER_LIBRARY_PATHS, STYLE_EXTENSION |
| 16 | +from matplotlib.style.core import (USER_LIBRARY_PATHS, |
| 17 | + STYLE_EXTENSION, |
| 18 | + BASE_LIBRARY_PATH, |
| 19 | + flatten_inheritance_dict, get_style_dict) |
17 | 20 |
|
18 | 21 | from matplotlib.externals import six
|
19 | 22 |
|
|
25 | 28 | @contextmanager
|
26 | 29 | def temp_style(style_name, settings=None):
|
27 | 30 | """Context manager to create a style sheet in a temporary directory."""
|
28 |
| - settings = DUMMY_SETTINGS |
| 31 | + if not settings: |
| 32 | + settings = DUMMY_SETTINGS |
29 | 33 | temp_file = '%s.%s' % (style_name, STYLE_EXTENSION)
|
30 | 34 |
|
31 | 35 | # Write style settings to file in the temp directory.
|
@@ -137,6 +141,148 @@ def test_context_with_badparam():
|
137 | 141 | assert mpl.rcParams[PARAM] == other_value
|
138 | 142 |
|
139 | 143 |
|
| 144 | +def test_get_style_dict(): |
| 145 | + style_dict = get_style_dict('bmh') |
| 146 | + assert(isinstance(style_dict, dict)) |
| 147 | + |
| 148 | + |
| 149 | +def test_get_style_dict_from_lib(): |
| 150 | + style_dict = get_style_dict('bmh') |
| 151 | + assert_equal(style_dict['lines.linewidth'], 2.0) |
| 152 | + |
| 153 | + |
| 154 | +def test_get_style_dict_from_file(): |
| 155 | + style_dict = get_style_dict(os.path.join(BASE_LIBRARY_PATH, |
| 156 | + 'bmh.mplstyle')) |
| 157 | + assert_equal(style_dict['lines.linewidth'], 2.0) |
| 158 | + |
| 159 | + |
| 160 | +def test_parent_stylesheet(): |
| 161 | + parent_value = 'blue' |
| 162 | + parent = {PARAM: parent_value} |
| 163 | + child = {'style': parent} |
| 164 | + with style.context(child): |
| 165 | + assert_equal(mpl.rcParams[PARAM], parent_value) |
| 166 | + |
| 167 | + |
| 168 | +def test_parent_stylesheet_children_override(): |
| 169 | + parent_value = 'blue' |
| 170 | + child_value = 'gray' |
| 171 | + parent = {PARAM: parent_value} |
| 172 | + child = {'style': parent, PARAM: child_value} |
| 173 | + with style.context(child): |
| 174 | + assert_equal(mpl.rcParams[PARAM], child_value) |
| 175 | + |
| 176 | + |
| 177 | +def test_grandparent_stylesheet(): |
| 178 | + grandparent_value = 'blue' |
| 179 | + grandparent = {PARAM: grandparent_value} |
| 180 | + parent = {'style': grandparent} |
| 181 | + child = {'style': parent} |
| 182 | + with style.context(child): |
| 183 | + assert_equal(mpl.rcParams[PARAM], grandparent_value) |
| 184 | + |
| 185 | + |
| 186 | +def test_parent_stylesheet_from_string(): |
| 187 | + parent_param = 'lines.linewidth' |
| 188 | + parent_value = 2.0 |
| 189 | + parent = {parent_param: parent_value} |
| 190 | + child = {'style': ['parent']} |
| 191 | + with temp_style('parent', settings=parent): |
| 192 | + with style.context(child): |
| 193 | + assert_equal(mpl.rcParams[parent_param], parent_value) |
| 194 | + |
| 195 | + |
| 196 | +def test_parent_stylesheet_brothers(): |
| 197 | + parent_param = PARAM |
| 198 | + parent_value1 = 'blue' |
| 199 | + parent_value2 = 'gray' |
| 200 | + parent1 = {parent_param: parent_value1} |
| 201 | + parent2 = {parent_param: parent_value2} |
| 202 | + child = {'style': [parent1, parent2]} |
| 203 | + with style.context(child): |
| 204 | + assert_equal(mpl.rcParams[parent_param], parent_value2) |
| 205 | + |
| 206 | + |
| 207 | +# Dictionnary flattening function tests |
| 208 | +def test_empty_dict(): |
| 209 | + child = {} |
| 210 | + flattened = flatten_inheritance_dict(child, 'parents') |
| 211 | + assert_equal(flattened, child) |
| 212 | + |
| 213 | + |
| 214 | +def test_no_parent(): |
| 215 | + child = {'my-key': 'my-value'} |
| 216 | + flattened = flatten_inheritance_dict(child, 'parents') |
| 217 | + assert_equal(flattened, child) |
| 218 | + # Verify that flatten_inheritance_dict always returns a copy. |
| 219 | + assert(flattened is not child) |
| 220 | + |
| 221 | + |
| 222 | +def test_non_list_raises(): |
| 223 | + child = {'parents': 'parent-value'} |
| 224 | + assert_raises(ValueError, flatten_inheritance_dict, child, |
| 225 | + 'parents') |
| 226 | + |
| 227 | + |
| 228 | +def test_child_with_no_unique_values(): |
| 229 | + parent = {'a': 1} |
| 230 | + child = {'parents': [parent]} |
| 231 | + flattened = flatten_inheritance_dict(child, 'parents') |
| 232 | + assert_equal(flattened, parent) |
| 233 | + |
| 234 | + |
| 235 | +def test_child_overrides_parent_value(): |
| 236 | + parent = {'a': 'old-value'} |
| 237 | + child = {'parents': [parent], 'a': 'new-value'} |
| 238 | + flattened = flatten_inheritance_dict(child, 'parents') |
| 239 | + assert_equal(flattened, {'a': 'new-value'}) |
| 240 | + |
| 241 | + |
| 242 | +def test_parents_with_distinct_values(): |
| 243 | + child = {'parents': [{'a': 1}, {'b': 2}]} |
| 244 | + flattened = flatten_inheritance_dict(child, 'parents') |
| 245 | + assert_equal(flattened, {'a': 1, 'b': 2}) |
| 246 | + |
| 247 | + |
| 248 | +def test_later_parent_overrides_former(): |
| 249 | + child = {'parents': [{'a': 1}, {'a': 2}]} |
| 250 | + flattened = flatten_inheritance_dict(child, 'parents') |
| 251 | + assert_equal(flattened, {'a': 2}) |
| 252 | + |
| 253 | + |
| 254 | +def test_grandparent(): |
| 255 | + grandparent = {'a': 1} |
| 256 | + parent = {'parents': [grandparent]} |
| 257 | + child = {'parents': [parent]} |
| 258 | + flattened = flatten_inheritance_dict(child, 'parents') |
| 259 | + assert_equal(flattened, grandparent) |
| 260 | + |
| 261 | + |
| 262 | +def test_custom_expand_parent(): |
| 263 | + parent_map = {'a-pointer': {'a': 1}, 'b-pointer': {'b': 2}} |
| 264 | + |
| 265 | + def expand_parent(key): |
| 266 | + return parent_map[key] |
| 267 | + |
| 268 | + child = {'parents': ['a-pointer', 'b-pointer']} |
| 269 | + flattened = flatten_inheritance_dict(child, 'parents', |
| 270 | + expand_parent=expand_parent) |
| 271 | + assert_equal(flattened, {'a': 1, 'b': 2}) |
| 272 | + |
| 273 | + |
| 274 | +def test_circular_parents(): |
| 275 | + parent_map = {'a-pointer': {'parents': ['b-pointer']}, |
| 276 | + 'b-pointer': {'parents': ['a-pointer']}} |
| 277 | + |
| 278 | + def expand_parent(key): |
| 279 | + return parent_map[key] |
| 280 | + |
| 281 | + child = {'parents': ['a-pointer']} |
| 282 | + assert_raises(RuntimeError, flatten_inheritance_dict, child, |
| 283 | + 'parents', expand_parent=expand_parent) |
| 284 | + |
| 285 | + |
140 | 286 | if __name__ == '__main__':
|
141 | 287 | from numpy import testing
|
142 | 288 | testing.run_module_suite()
|
0 commit comments