Skip to content

Commit ed04d93

Browse files
committed
tests now depend on python version to check cases with bytes args
1 parent 0a1473e commit ed04d93

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

lib/matplotlib/tests/test_rcparams.py

+36-20
Original file line numberDiff line numberDiff line change
@@ -333,29 +333,45 @@ def generate_validator_testcases(valid):
333333
),
334334
'fail': (('aardvark', ValueError),
335335
)
336-
},
337-
{'validator': _validate_linestyle, # NB: case-insensitive
338-
'success': (('-', '-'), ('solid', 'solid'),
339-
('--', '--'), ('dashed', 'dashed'),
340-
('-.', '-.'), ('dashdot', 'dashdot'),
341-
(':', ':'), ('dotted', 'dotted'),
342-
('', ''), (' ', ' '),
343-
('None', 'none'), ('none', 'none'),
344-
('DoTtEd', 'dotted'),
345-
(['1.23', '4.56'], (None, [1.23, 4.56])),
346-
([1.23, 456], (None, [1.23, 456.0])),
347-
([1, 2, 3, 4], (None, [1.0, 2.0, 3.0, 4.0])),
348-
),
349-
'fail': (('aardvark', ValueError), # not a valid string
350-
((None, [1, 2]), ValueError), # (offset, dashes) is not OK
351-
((0, [1, 2]), ValueError), # idem
352-
((-1, [1, 2]), ValueError), # idem
353-
([1, 2, 3], ValueError), # not a sequence of even length
354-
(1.23, ValueError) # not a sequence
355-
)
356336
}
357337
)
358338

339+
# The behavior of _validate_linestyle depends on the version of Python.
340+
# ASCII-compliant bytes arguments should pass on Python 2 because of the
341+
# automatic conversion between bytes and strings. Python 3 does not
342+
# perform such a conversion, so the same cases should raise an exception.
343+
#
344+
# Common cases:
345+
ls_test = {'validator': _validate_linestyle,
346+
'success': (('-', '-'), ('solid', 'solid'),
347+
('--', '--'), ('dashed', 'dashed'),
348+
('-.', '-.'), ('dashdot', 'dashdot'),
349+
(':', ':'), ('dotted', 'dotted'),
350+
('', ''), (' ', ' '),
351+
('None', 'none'), ('none', 'none'),
352+
('DoTtEd', 'dotted'), # case-insensitive
353+
(['1.23', '4.56'], (None, [1.23, 4.56])),
354+
([1.23, 456], (None, [1.23, 456.0])),
355+
([1, 2, 3, 4], (None, [1.0, 2.0, 3.0, 4.0])),
356+
),
357+
'fail': (('aardvark', ValueError), # not a valid string
358+
('dotted'.encode('utf-16'), ValueError), # even on PY2
359+
((None, [1, 2]), ValueError), # (offset, dashes) != OK
360+
((0, [1, 2]), ValueError), # idem
361+
((-1, [1, 2]), ValueError), # idem
362+
([1, 2, 3], ValueError), # sequence with odd length
363+
(1.23, ValueError), # not a sequence
364+
)
365+
}
366+
# Add some cases of bytes arguments that Python 2 can convert silently:
367+
ls_bytes_args = (b'dotted', 'dotted'.encode('ascii'))
368+
if six.PY3:
369+
ls_test['fail'] += tuple((arg, ValueError) for arg in ls_bytes_args)
370+
else:
371+
ls_test['success'] += tuple((arg, 'dotted') for arg in ls_bytes_args)
372+
# Update the validation test sequence.
373+
validation_tests += (ls_test,)
374+
359375
for validator_dict in validation_tests:
360376
validator = validator_dict['validator']
361377
if valid:

0 commit comments

Comments
 (0)