Beispiel #1
0
    def test_maxlen(self):
        for value in ('foo', True, 123):
            with self.assert_raises(ValueError,
                                    'Too long (maximum length is 2)'):
                String(value, maxlen=2)

            s = String(value, maxlen=99)
            self.assertEqual(s, str(value))
Beispiel #2
0
    def test_minlen(self):
        for value in ('foo', True, 123):
            with self.assert_raises(ValueError,
                                    'Too short (minimum length is 5)'):
                String(value, minlen=5)

            s = String(value, minlen=1)
            self.assertEqual(s, str(value))
Beispiel #3
0
 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)
Beispiel #4
0
 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)
Beispiel #5
0
 def test_regex(self):
     assert String('f', regex='fo*') == 'f'
     assert String('fo', regex='fo*') == 'fo'
     assert String('foo', regex='fo*') == 'foo'
     with self.assert_raises(ValueError, 'Invalid value'):
         String('', regex='fo*')
     with self.assert_raises(ValueError, 'Invalid value'):
         String('f', regex='fo+')
     with self.assert_raises(RuntimeError, "Invalid regex: 'f[?'"):
         String('f', regex='f[?')
Beispiel #6
0
 def test_syntax(self):
     self.assertEqual(String('foo').syntax, 'string')
     self.assertEqual(
         String('foo', minlen=1).syntax, 'string (at least 1 character)')
     self.assertEqual(
         String('foo', minlen=2).syntax, 'string (at least 2 characters)')
     self.assertEqual(
         String('f', maxlen=1).syntax, 'string (at most 1 character)')
     self.assertEqual(
         String('f', maxlen=2).syntax, 'string (at most 2 characters)')
     self.assertEqual(
         String('f', minlen=1, maxlen=2).syntax, 'string (1-2 characters)')
     self.assertEqual(
         String('fo', minlen=2, maxlen=2).syntax, 'string (2 characters)')
Beispiel #7
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))
Beispiel #8
0
 def test_syntax(self):
     self.assertEqual(String('foo').syntax, 'string')
     self.assertEqual(
         String('foo', minlen=1).syntax, 'string (at least 1 character)')
     self.assertEqual(
         String('foo', minlen=2).syntax, 'string (at least 2 characters)')
     self.assertEqual(
         String('f', maxlen=1).syntax, 'string (at most 1 character)')
     self.assertEqual(
         String('f', maxlen=2).syntax, 'string (at most 2 characters)')
     self.assertEqual(
         String('f', minlen=1, maxlen=2).syntax, 'string (1-2 characters)')
     self.assertEqual(
         String('fo', minlen=2, maxlen=2).syntax, 'string (2 characters)')
     self.assertEqual(
         String('f', regex=r'fo*').syntax, "string (pattern=fo*)")
     self.assertEqual(
         String('f', regex=r'fo*', minlen=1).syntax,
         "string (pattern=fo*, at least 1 character)")
     self.assertEqual(
         String('f', regex=r'fo*', maxlen=3).syntax,
         "string (pattern=fo*, at most 3 characters)")
     self.assertEqual(
         String('f', regex=r'fo*', minlen=1, maxlen=3).syntax,
         "string (pattern=fo*, 1-3 characters)")