9
9
10
10
import matplotlib as mpl
11
11
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
13
13
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 )
14
21
15
22
16
23
mpl .rc ('text' , usetex = False )
17
24
mpl .rc ('lines' , linewidth = 22 )
18
25
19
26
fname = os .path .join (os .path .dirname (__file__ ), 'test_rcparams.rc' )
20
27
21
- def test_rcparams ():
22
28
29
+ def test_rcparams ():
23
30
usetex = mpl .rcParams ['text.usetex' ]
24
31
linewidth = mpl .rcParams ['lines.linewidth' ]
25
32
@@ -55,7 +62,6 @@ def test_RcParams_class():
55
62
'font.weight' : 'normal' ,
56
63
'font.size' : 12 })
57
64
58
-
59
65
if six .PY3 :
60
66
expected_repr = """
61
67
RcParams({'font.cursive': ['Apple Chancery',
@@ -96,6 +102,7 @@ def test_RcParams_class():
96
102
assert ['font.cursive' , 'font.size' ] == sorted (rc .find_all ('i[vz]' ).keys ())
97
103
assert ['font.family' ] == list (six .iterkeys (rc .find_all ('family' )))
98
104
105
+
99
106
def test_Bug_2543 ():
100
107
# Test that it possible to add all values to itself / deepcopy
101
108
# This was not possible because validate_bool_maybe_none did not
@@ -116,7 +123,6 @@ def test_Bug_2543():
116
123
with mpl .rc_context ():
117
124
from copy import deepcopy
118
125
_deep_copy = deepcopy (mpl .rcParams )
119
- from matplotlib .rcsetup import validate_bool_maybe_none , validate_bool
120
126
# real test is that this does not raise
121
127
assert_true (validate_bool_maybe_none (None ) is None )
122
128
assert_true (validate_bool_maybe_none ("none" ) is None )
@@ -126,6 +132,7 @@ def test_Bug_2543():
126
132
mpl .rcParams ['svg.embed_char_paths' ] = False
127
133
assert_true (mpl .rcParams ['svg.fonttype' ] == "none" )
128
134
135
+
129
136
def test_Bug_2543_newer_python ():
130
137
# only split from above because of the usage of assert_raises
131
138
# as a context manager, which only works in 2.7 and above
@@ -141,5 +148,64 @@ def test_Bug_2543_newer_python():
141
148
mpl .rcParams ['svg.fonttype' ] = True
142
149
143
150
if __name__ == '__main__' :
144
- import nose
145
151
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