def test_default_filter_with_iterables(self): from stig.utils.usertypes import Tuple, Bool self.check_filter( filter_cls=SettingFilter, filter_names=('default', 'def'), items=({ 'id': 'a', 'default': Tuple('x', 'y', options=('x', 'y', 'z')), 'validate': Tuple.partial(options=('x', 'y', 'z')) }, { 'id': 'b', 'default': True, 'validate': Bool }, { 'id': 'c', 'default': 'foo' }, { 'id': 'd', 'default': 0, 'validate': int }, { 'id': 'e', 'default': Tuple('x', 'z', options=('x', 'y', 'z')), 'validate': Tuple.partial(options=('x', 'y', 'z')) }), test_cases=(('{name}', ('a', 'b', 'c', 'e')), ('!{name}', ('d', )), ('{name}=x,y', ('a', )), ('{name}!=x,y', ('b', 'c', 'd', 'e')), ('{name}=x', ()), ('{name}~x', ('a', 'e')), ('{name}~y', ('a', )), ('{name}~z', ('e', )), ('{name}!~z', ('a', 'b', 'c', 'd')), ('{name}~x,y', ('a', )), ('{name}~y,x', ('a', )), ('{name}!~y,x', ('b', 'c', 'd', 'e'))))
def test_aliases(self): aliases = {1: 'one', 2: 'two', 'too': 'two'} t = Tuple(1, 2, 'three', aliases=aliases) self.assertEqual(t, ('one', 'two', 'three')) self.assertEqual(t, Tuple(1, 'too', 'three', aliases=aliases)) self.assertEqual(t.aliases, {1: 'one', 2: 'two', 'too': 'two'}) self.assertEqual(t.aliases_inverse, {'one': [1], 'two': [2, 'too']})
def test_aliases(self): aliases = {1: 'one', 2: 'two', 'too': 'two'} t = Tuple(1, 2, 'three', aliases=aliases) self.assertEqual(t, ('one', 'two', 'three')) self.assertEqual(t, Tuple(1, 'too', 'three', aliases=aliases)) self.assertEqual(t.aliases, {1: 'one', 2: 'two', 'too': 'two'}) import sys if sys.hexversion < 0x03060000: # Python <= 3.6 dicts are not ordered yet inverse = t.aliases_inverse inverse['two'] = set(inverse['two']) self.assertEqual(inverse, {'one': [1], 'two': {2, 'too'}}) else: self.assertEqual(t.aliases_inverse, { 'one': [1], 'two': [2, 'too'] })
def test_separator(self): for sep, string, string_exp in ((', ', '1, 2 ,3 , 4', ('1, 2, 3, 4')), ('/', '1/2 / 3 /4', '1/2/3/4'), (' : ', '1:2 : 3: 4', '1 : 2 : 3 : 4')): t = Tuple(string, sep=sep) self.assertEqual(t, ('1', '2', '3', '4')) self.assertEqual(str(t), string_exp)
def test_issubclass(self): mt = multitype(Float, Option.partial(options=('foo', 'bar', 'baz')), Tuple.partial(sep=' / ', dedup=True)) for subcls in (Float, Option, Tuple): self.assertTrue(issubclass(subcls, mt)) for subcls in (String, Int, Path): self.assertFalse(issubclass(subcls, mt))
def test_invalid_type(self): mt = multitype(Bool.partial(true=('yes', ), false=('no', )), Float, Tuple.partial(options=('a', 'b', 'c'))) with self.assert_raises( ValueError, 'Not a boolean; Not a number; Invalid option: hi'): mt('hi') with self.assert_raises( ValueError, 'Not a boolean; Not a number; Invalid option: d'): mt('a', 'b', 'd')
def test_options(self): with self.assert_raises(ValueError, 'Invalid options: x, baz'): Tuple('x, foo, bar, baz', options=('foo', 'bar')) with self.assert_raises(ValueError, 'Invalid option: baz'): Tuple('foo', 'bar', 'baz', options=('foo', 'bar')) self.assertEqual(Tuple('foo, bar', options=('foo', 'bar')), ('foo', 'bar')) self.assertEqual(Tuple('bar', 'foo', options=('foo', 'bar')), ('bar', 'foo')) self.assertEqual(Tuple('foo', options=('foo', 'bar')), ('foo', )) self.assertEqual(Tuple('bar', options=('foo', 'bar')), ('bar', ))
def test_isinstance(self): mt = multitype(Int, Tuple.partial(options=('foo', 'bar', 'baz')), String.partial(minlen=1)) x = mt(49) self.assertTrue(isinstance(x, mt)) self.assertTrue(isinstance(x, Int)) self.assertFalse(isinstance(x, String)) self.assertFalse(isinstance(x, Tuple)) x = mt('asdf') self.assertTrue(isinstance(x, mt)) self.assertFalse(isinstance(x, Int)) self.assertTrue(isinstance(x, String)) self.assertFalse(isinstance(x, Tuple)) for value in (('foo', ), ('bar', 'baz')): x = mt(*value) self.assertTrue(isinstance(x, mt)) self.assertFalse(isinstance(x, Int)) self.assertFalse(isinstance(x, String)) self.assertTrue(isinstance(x, Tuple))
def test_mixed_values(self): t = Tuple(1, 2, '3, 4', 5, '6, 7') self.assertEqual(t, (1, 2, '3', '4', 5, '6', '7'))
def test_dedup(self): self.assertEqual(Tuple('foo', 'bar', 'bar', 'baz', 'foo', dedup=False), ('foo', 'bar', 'bar', 'baz', 'foo')) self.assertEqual(Tuple('foo, bar, bar, baz, foo', dedup=True), ('foo', 'bar', 'baz'))
def test_options_property(self): t = Tuple(3, 2, options=(1, 2, 3)) self.assertEqual(t.options, (1, 2, 3))
def test_asterisk_sets_all_options(self): self.assertEqual(Tuple('*', options=('foo', 'bar')), ('foo', 'bar'))
def test_syntax(self): self.assertEqual(Tuple('foo,bar,baz').syntax, '<OPTION>,<OPTION>,...') self.assertEqual( Tuple('foo|bar|baz', sep='|').syntax, '<OPTION>|<OPTION>|...')
def test_aliases(self): aliases = {1: 'one', 2: 'two'} t = Tuple(1, 2, 'three', aliases=aliases) self.assertEqual(t, ('one', 'two', 'three'))