|
8 | 8 | import sys |
9 | 9 | import warnings |
10 | 10 |
|
| 11 | +from cycler import cycler, Cycler |
| 12 | + |
11 | 13 | import matplotlib as mpl |
12 | 14 | import matplotlib.pyplot as plt |
13 | 15 | from matplotlib.tests import assert_str_equal |
|
22 | 24 | validate_stringlist, |
23 | 25 | validate_bool, |
24 | 26 | validate_nseq_int, |
25 | | - validate_nseq_float) |
| 27 | + validate_nseq_float, |
| 28 | + validate_cycler) |
26 | 29 |
|
27 | 30 |
|
28 | 31 | mpl.rc('text', usetex=False) |
@@ -238,13 +241,14 @@ def test_Issue_1713(): |
238 | 241 | del os.environ['LANG'] |
239 | 242 | assert rc.get('timezone') == 'UTC' |
240 | 243 |
|
241 | | -if __name__ == '__main__': |
242 | | - nose.runmodule(argv=['-s', '--with-doctest'], exit=False) |
243 | | - |
244 | 244 |
|
245 | 245 | def _validation_test_helper(validator, arg, target): |
246 | 246 | res = validator(arg) |
247 | | - assert_equal(res, target) |
| 247 | + if not isinstance(target, Cycler): |
| 248 | + assert_equal(res, target) |
| 249 | + else: |
| 250 | + # Cyclers can't simply be asserted equal. They don't implement __eq__ |
| 251 | + assert_equal(list(res), list(target)) |
248 | 252 |
|
249 | 253 |
|
250 | 254 | def _validation_fail_helper(validator, arg, exception_type): |
@@ -293,8 +297,35 @@ def test_validators(): |
293 | 297 | for _ in ('aardvark', ('a', 1), |
294 | 298 | (1, 2, 3) |
295 | 299 | )) |
296 | | - } |
297 | | - |
| 300 | + }, |
| 301 | + {'validator': validate_cycler, |
| 302 | + 'success': (('cycler("color", "rgb")', |
| 303 | + cycler("color", 'rgb')), |
| 304 | + (cycler('linestyle', ['-', '--']), |
| 305 | + cycler('linestyle', ['-', '--'])), |
| 306 | + ("""(cycler("color", ["r", "g", "b"]) + |
| 307 | + cycler("mew", [2, 3, 5]))""", |
| 308 | + cycler("color", 'rgb') + cycler("mew", [2, 3, 5])), |
| 309 | + ), |
| 310 | + # This is *so* incredibly important: validate_cycler() eval's |
| 311 | + # an arbitrary string! I think I have it locked down enough, |
| 312 | + # and that is what this is testing. |
| 313 | + # TODO: Note that these tests are actually insufficient, as it may |
| 314 | + # be that they raised errors, but still did an action prior to |
| 315 | + # raising the exception. We should devise some additional tests |
| 316 | + # for that... |
| 317 | + 'fail': ((4, ValueError), # Gotta be a string or Cycler object |
| 318 | + ('cycler("bleh, [])', ValueError), # syntax error |
| 319 | + ('Cycler("linewidth", [1, 2, 3])', |
| 320 | + ValueError), # only 'cycler()' function is allowed |
| 321 | + ('1 + 2', ValueError), # doesn't produce a Cycler object |
| 322 | + ('os.system("echo Gotcha")', ValueError), # os not available |
| 323 | + ('import os', ValueError), # should not be able to import |
| 324 | + ('def badjuju(a): return a; badjuju(cycler("color", "rgb"))', |
| 325 | + ValueError), # Should not be able to define anything |
| 326 | + # even if it does return a cycler |
| 327 | + ) |
| 328 | + }, |
298 | 329 | ) |
299 | 330 |
|
300 | 331 | for validator_dict in validation_tests: |
@@ -331,3 +362,7 @@ def test_rcparams_reset_after_fail(): |
331 | 362 | pass |
332 | 363 |
|
333 | 364 | assert mpl.rcParams['text.usetex'] is False |
| 365 | + |
| 366 | + |
| 367 | +if __name__ == '__main__': |
| 368 | + nose.runmodule(argv=['-s', '--with-doctest'], exit=False) |
0 commit comments