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

Skip to content

Commit 1c2bc58

Browse files
committed
TST : added tests for some validation functions
1 parent 30eaa0b commit 1c2bc58

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

lib/matplotlib/tests/test_rcparams.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@
99

1010
import matplotlib as mpl
1111
from matplotlib.tests import assert_str_equal
12-
from nose.tools import assert_true, assert_raises
12+
from nose.tools import assert_true, assert_raises, assert_equal
1313
import nose
14+
from itertools import chain
15+
import numpy as np
16+
from matplotlib.rcsetup import (validate_bool_maybe_none,
17+
validate_stringlist,
18+
validate_bool,
19+
validate_nseq_int,
20+
validate_nseq_float)
1421

1522

1623
mpl.rc('text', usetex=False)
1724
mpl.rc('lines', linewidth=22)
1825

1926
fname = os.path.join(os.path.dirname(__file__), 'test_rcparams.rc')
2027

21-
def test_rcparams():
2228

29+
def test_rcparams():
2330
usetex = mpl.rcParams['text.usetex']
2431
linewidth = mpl.rcParams['lines.linewidth']
2532

@@ -55,7 +62,6 @@ def test_RcParams_class():
5562
'font.weight': 'normal',
5663
'font.size': 12})
5764

58-
5965
if six.PY3:
6066
expected_repr = """
6167
RcParams({'font.cursive': ['Apple Chancery',
@@ -96,6 +102,7 @@ def test_RcParams_class():
96102
assert ['font.cursive', 'font.size'] == sorted(rc.find_all('i[vz]').keys())
97103
assert ['font.family'] == list(six.iterkeys(rc.find_all('family')))
98104

105+
99106
def test_Bug_2543():
100107
# Test that it possible to add all values to itself / deepcopy
101108
# This was not possible because validate_bool_maybe_none did not
@@ -116,7 +123,6 @@ def test_Bug_2543():
116123
with mpl.rc_context():
117124
from copy import deepcopy
118125
_deep_copy = deepcopy(mpl.rcParams)
119-
from matplotlib.rcsetup import validate_bool_maybe_none, validate_bool
120126
# real test is that this does not raise
121127
assert_true(validate_bool_maybe_none(None) is None)
122128
assert_true(validate_bool_maybe_none("none") is None)
@@ -126,6 +132,7 @@ def test_Bug_2543():
126132
mpl.rcParams['svg.embed_char_paths'] = False
127133
assert_true(mpl.rcParams['svg.fonttype'] == "none")
128134

135+
129136
def test_Bug_2543_newer_python():
130137
# only split from above because of the usage of assert_raises
131138
# as a context manager, which only works in 2.7 and above
@@ -141,5 +148,64 @@ def test_Bug_2543_newer_python():
141148
mpl.rcParams['svg.fonttype'] = True
142149

143150
if __name__ == '__main__':
144-
import nose
145151
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)
152+
153+
154+
def _validation_test_helper(validator, arg, target):
155+
res = validator(arg)
156+
assert_equal(res, target)
157+
158+
159+
def _validation_fail_helper(validator, arg, exception_type):
160+
with assert_raises(exception_type):
161+
validator(arg)
162+
163+
164+
def test_validators():
165+
validation_tests = (
166+
{'validator': validate_bool,
167+
'success': chain(((_, True) for _ in
168+
('t', 'y', 'yes', 'on', 'true', '1', 1, True)),
169+
((_, False) for _ in
170+
('f', 'n', 'no', 'off', 'false', '0', 0, False))),
171+
'fail': ((_, ValueError)
172+
for _ in ('aardvark', 2, -1, [], ))},
173+
{'validator': validate_stringlist,
174+
'success': (('', []),
175+
('a,b', ['a', 'b']),
176+
('aardvark', ['aardvark']),
177+
('aardvark, ', ['aardvark']),
178+
('aardvark, ,', ['aardvark']),
179+
(['a', 'b'], ['a', 'b']),
180+
(('a', 'b'), ['a', 'b']),
181+
((1, 2), ['1', '2'])),
182+
'fail': ((dict(), AssertionError),
183+
(1, AssertionError),)
184+
},
185+
{'validator': validate_nseq_int(2),
186+
'success': ((_, [1, 2])
187+
for _ in ('1, 2', [1.5, 2.5], [1, 2],
188+
(1, 2), np.array((1, 2)))),
189+
'fail': ((_, ValueError)
190+
for _ in ('aardvark', ('a', 1),
191+
(1, 2, 3)
192+
))
193+
},
194+
{'validator': validate_nseq_float(2),
195+
'success': ((_, [1.5, 2.5])
196+
for _ in ('1.5, 2.5', [1.5, 2.5], [1.5, 2.5],
197+
(1.5, 2.5), np.array((1.5, 2.5)))),
198+
'fail': ((_, ValueError)
199+
for _ in ('aardvark', ('a', 1),
200+
(1, 2, 3)
201+
))
202+
}
203+
204+
)
205+
206+
for validator_dict in validation_tests:
207+
validator = validator_dict['validator']
208+
for arg, target in validator_dict['success']:
209+
yield _validation_test_helper, validator, arg, target
210+
for arg, error_type in validator_dict['fail']:
211+
yield _validation_fail_helper, validator, arg, error_type

0 commit comments

Comments
 (0)