|
7 | 7 | assert_raises, assert_true)
|
8 | 8 | from itertools import product, cycle, chain
|
9 | 9 | from operator import add, iadd, mul, imul
|
| 10 | +from collections import defaultdict |
10 | 11 |
|
11 | 12 |
|
12 | 13 | def _cycler_helper(c, length, keys, values):
|
@@ -300,3 +301,30 @@ def test_concat_fail():
|
300 | 301 | b = cycler('b', range(3))
|
301 | 302 | assert_raises(ValueError, concat, a, b)
|
302 | 303 | assert_raises(ValueError, a.concat, b)
|
| 304 | + |
| 305 | + |
| 306 | +def _by_key_helper(cy): |
| 307 | + res = cy.by_key() |
| 308 | + target = defaultdict(list) |
| 309 | + for sty in cy: |
| 310 | + for k, v in sty.items(): |
| 311 | + target[k].append(v) |
| 312 | + |
| 313 | + assert_equal(res, target) |
| 314 | + |
| 315 | + |
| 316 | +def test_by_key_add(): |
| 317 | + input_dict = dict(c=list('rgb'), lw=[1, 2, 3]) |
| 318 | + cy = cycler(c=input_dict['c']) + cycler(lw=input_dict['lw']) |
| 319 | + res = cy.by_key() |
| 320 | + assert_equal(res, input_dict) |
| 321 | + yield _by_key_helper, cy |
| 322 | + |
| 323 | + |
| 324 | +def test_by_key_mul(): |
| 325 | + input_dict = dict(c=list('rg'), lw=[1, 2, 3]) |
| 326 | + cy = cycler(c=input_dict['c']) * cycler(lw=input_dict['lw']) |
| 327 | + res = cy.by_key() |
| 328 | + assert_equal(input_dict['lw'] * len(input_dict['c']), |
| 329 | + res['lw']) |
| 330 | + yield _by_key_helper, cy |
0 commit comments