def test_repr(self): group = RepeatingContainer([1, 2, 3]) self.assertEqual(repr(group), 'RepeatingContainer([1, 2, 3])') group = RepeatingContainer([1, 2]) group._keys = ['a', 'b'] self.assertEqual(repr(group), "RepeatingContainer({'a': 1, 'b': 2})")
def test_normalize_value(self): group = RepeatingContainer([2, 4]) result = group._normalize_value(5) self.assertEqual( result, (5, 5), msg='value is expanded to match number of _objs', ) result = group._normalize_value(RepeatingContainer([5, 6])) self.assertEqual( result, (5, 6), msg='compatible RepeatingContainers are unwrapped rather than expanded', ) other = RepeatingContainer([5, 6, 7]) result = group._normalize_value(other) self.assertIsInstance( result, tuple, msg='incompatible RepeatingContainers are expanded like other values', ) self.assertEqual(len(result), 2) equals_other = super(other.__class__, other).__eq__ self.assertTrue(equals_other(result[0])) self.assertTrue(equals_other(result[1])) group = RepeatingContainer([2, 4]) group._keys = ['foo', 'bar'] other = RepeatingContainer([8, 6]) other._keys = ['bar', 'foo'] # <- keys in different order result = group._normalize_value(other) self.assertEqual( result, (6, 8), # <- reordered to match `group` msg='result order should match key names, not _obj position', )
def test_expand_args_kwds(self): argsgroup = RepeatingContainer([2, 4]) kwdsgroup = RepeatingContainer([2, 4]) kwdsgroup._keys = ['foo', 'bar'] # Unwrap RepeatingContainer. result = argsgroup._expand_args_kwds(RepeatingContainer([5, 6])) expected = [ ((5,), {}), ((6,), {}), ] self.assertEqual(result, expected) # Expand int and unwrap RepeatingContainer. result = argsgroup._expand_args_kwds(1, RepeatingContainer([5, 6])) expected = [ ((1, 5), {}), ((1, 6), {}), ] self.assertEqual(result, expected) # Unwrap two RepeatingContainer. result = argsgroup._expand_args_kwds( x=RepeatingContainer([5, 6]), y=RepeatingContainer([7, 9]), ) expected = [ ((), {'x': 5, 'y': 7}), ((), {'x': 6, 'y': 9}), ] self.assertEqual(result, expected) # Kwdsgroup expansion. kwdgrp2 = RepeatingContainer([5, 6]) kwdgrp2._keys = ['foo', 'bar'] # Unwrap keyed RepeatingContainer. result = kwdsgroup._expand_args_kwds(kwdgrp2) expected = [ ((5,), {}), ((6,), {}), ] self.assertEqual(result, expected) # Unwrap keyed RepeatingContainer with keys in different order. kwdgrp_reverse = RepeatingContainer([6, 5]) kwdgrp_reverse._keys = ['bar', 'foo'] result = kwdsgroup._expand_args_kwds(kwdgrp_reverse) expected = [ ((5,), {}), ((6,), {}), ] self.assertEqual(result, expected) # Expand int and unwrap keyed RepeatingContainer. result = kwdsgroup._expand_args_kwds(1, kwdgrp2) expected = [ ((1, 5), {}), ((1, 6), {}), ] self.assertEqual(result, expected) # Sanity-check/quick integration test (all combinations). result = kwdsgroup._expand_args_kwds('a', RepeatingContainer({'foo': 'b', 'bar': 'c'}), x=1, y=RepeatingContainer({'bar': 4, 'foo': 2})) expected = [ (('a', 'b'), {'x': 1, 'y': 2}), (('a', 'c'), {'x': 1, 'y': 4}), ] self.assertEqual(result, expected)