def test_syntax(self): constructors = (Int.partial(min=-1, max=100), Option.partial(options=('foo', 'bar')), String) exp_syntax = ' or '.join( (Int.partial(min=-1, max=100).syntax, Option.partial(options=('foo', 'bar')).syntax, String.partial().syntax)) mt = multitype(*constructors) self.assertEqual(mt.syntax, exp_syntax) inst = mt('hello') self.assertEqual(inst.syntax, exp_syntax)
def test_typename(self): constructors = (Int.partial(min=-1, max=100), Option.partial(options=('foo', 'bar')), String) exp_typename = ' or '.join( (Int.partial(min=-1, max=100).typename, Option.partial(options=('foo', 'bar')).typename, String.partial().typename)) mt = multitype(*constructors) self.assertEqual(mt.typename, exp_typename) inst = mt('hello') self.assertEqual(inst.typename, exp_typename)
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))