示例#1
0
 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'))))
示例#2
0
    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))
示例#3
0
 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')
示例#4
0
    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))