forked from matplotlib/cycler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cycler.py
150 lines (107 loc) · 4.56 KB
/
test_cycler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from __future__ import (absolute_import, division, print_function)
import six
from six.moves import zip, range
from cycler import cycler, Cycler
from nose.tools import assert_equal, assert_raises
from itertools import product
from operator import add, iadd, mul, imul
def _cycler_helper(c, length, keys, values):
assert_equal(len(c), length)
assert_equal(len(c), len(list(c)))
assert_equal(c.keys, set(keys))
for k, vals in zip(keys, values):
for v, v_target in zip(c, vals):
assert_equal(v[k], v_target)
def _cycles_equal(c1, c2):
assert_equal(list(c1), list(c2))
def test_creation():
c = cycler('c', 'rgb')
yield _cycler_helper, c, 3, ['c'], [['r', 'g', 'b']]
c = cycler('c', list('rgb'))
yield _cycler_helper, c, 3, ['c'], [['r', 'g', 'b']]
def test_compose():
c1 = cycler('c', 'rgb')
c2 = cycler('lw', range(3))
c3 = cycler('lw', range(15))
# addition
yield _cycler_helper, c1+c2, 3, ['c', 'lw'], [list('rgb'), range(3)]
yield _cycler_helper, c2+c1, 3, ['c', 'lw'], [list('rgb'), range(3)]
yield _cycles_equal, c2+c1, c1+c2
# miss-matched add lengths
assert_raises(ValueError, add, c1, c3)
assert_raises(ValueError, add, c3, c1)
# multiplication
target = zip(*product(list('rgb'), range(3)))
yield (_cycler_helper, c1 * c2, 9, ['c', 'lw'], target)
target = zip(*product(range(3), list('rgb')))
yield (_cycler_helper, c2 * c1, 9, ['lw', 'c'], target)
target = zip(*product(range(15), list('rgb')))
yield (_cycler_helper, c3 * c1, 45, ['lw', 'c'], target)
def test_inplace():
c1 = cycler('c', 'rgb')
c2 = cycler('lw', range(3))
c2 += c1
yield _cycler_helper, c2, 3, ['c', 'lw'], [list('rgb'), range(3)]
c3 = cycler('c', 'rgb')
c4 = cycler('lw', range(3))
c3 *= c4
target = zip(*product(list('rgb'), range(3)))
yield (_cycler_helper, c3, 9, ['c', 'lw'], target)
def test_constructor():
c1 = cycler('c', 'rgb')
c2 = cycler('ec', c1)
yield _cycler_helper, c1+c2, 3, ['c', 'ec'], [['r', 'g', 'b']]*2
c3 = cycler('c', c1)
yield _cycler_helper, c3+c2, 3, ['c', 'ec'], [['r', 'g', 'b']]*2
def test_failures():
c1 = cycler('c', 'rgb')
c2 = cycler('c', c1)
assert_raises(ValueError, add, c1, c2)
assert_raises(ValueError, iadd, c1, c2)
assert_raises(ValueError, mul, c1, c2)
assert_raises(ValueError, imul, c1, c2)
c3 = cycler('ec', c1)
assert_raises(ValueError, cycler, 'c', c2 + c3)
def test_simplify():
c1 = cycler('c', 'rgb')
c2 = cycler('ec', c1)
for c in [c1 * c2, c2 * c1, c1 + c2]:
yield _cycles_equal, c, c.simplify()
def test_multiply():
c1 = cycler('c', 'rgb')
yield _cycler_helper, 2*c1, 6, ['c'], ['rgb'*2]
c2 = cycler('ec', c1)
c3 = c1 * c2
yield _cycles_equal, 2*c3, c3*2
def test_mul_fails():
c1 = cycler('c', 'rgb')
assert_raises(TypeError, mul, c1, 2.0)
assert_raises(TypeError, mul, c1, 'a')
assert_raises(TypeError, mul, c1, [])
def test_getitem():
c1 = cycler('lw', range(15))
widths = list(range(15))
for slc in (slice(None, None, None),
slice(None, None, -1),
slice(1, 5, None),
slice(0, 5, 2)):
yield _cycles_equal, c1[slc], cycler('lw', widths[slc])
def test_fail_getime():
c1 = cycler('lw', range(15))
assert_raises(ValueError, Cycler.__getitem__, c1, 0)
assert_raises(ValueError, Cycler.__getitem__, c1, [0, 1])
def _repr_tester_helper(rpr_func, cyc, target_repr):
test_repr = getattr(cyc, rpr_func)()
assert_equal(six.text_type(test_repr),
six.text_type(target_repr))
def test_repr():
c = cycler('c', 'rgb')
c2 = cycler('lw', range(3))
c_sum_rpr = "(cycler('c', ['r', 'g', 'b']) + cycler('lw', [0, 1, 2]))"
c_prod_rpr = "(cycler('c', ['r', 'g', 'b']) * cycler('lw', [0, 1, 2]))"
yield _repr_tester_helper, '__repr__', c + c2, c_sum_rpr
yield _repr_tester_helper, '__repr__', c * c2, c_prod_rpr
sum_html = "<table><th>'c'</th><th>'lw'</th><tr><td>'r'</td><td>0</td></tr><tr><td>'g'</td><td>1</td></tr><tr><td>'b'</td><td>2</td></tr></table>"
prod_html = "<table><th>'c'</th><th>'lw'</th><tr><td>'r'</td><td>0</td></tr><tr><td>'r'</td><td>1</td></tr><tr><td>'r'</td><td>2</td></tr><tr><td>'g'</td><td>0</td></tr><tr><td>'g'</td><td>1</td></tr><tr><td>'g'</td><td>2</td></tr><tr><td>'b'</td><td>0</td></tr><tr><td>'b'</td><td>1</td></tr><tr><td>'b'</td><td>2</td></tr></table>"
yield _repr_tester_helper, '_repr_html_', c + c2, sum_html
yield _repr_tester_helper, '_repr_html_', c * c2, prod_html