コード例 #1
0
    def test6(self):
        "Test OneOf validator."

        msg = 'Validation failed for OneOf %s!'

        choices = ('clockwise', 'anticlockwise')
        OneOf = validators.OneOf(choices)
        for c in choices:
            assert OneOf(c) == 1, msg % c
        for c in ('a', 'b', 'c'):
            assert OneOf(c) == 0, msg % c

        OneOf = validators.OneOf('clockwise', 'anticlockwise')
        for c in choices:
            assert OneOf(c) == 1, msg % c
        for c in ('a', 'b', 'c'):
            assert OneOf(c) == 0, msg % c

        try:
            validators.OneOf(choices, 'bongo')
            raise AssertionError("OneOf failed to detect bad arguments")
        except ValueError:
            pass

        import re
        V = [
            validators.OneOf('scalar', 'vector', 'matrix'),
            validators.OneOf('scalar', re.compile(r'^vector(?:|\W.*)$'),
                             'matrix'),
        ]
        for t, x in (
            ('scalar', (True, True)),
            ('vector', (True, True)),
            ('vector[0]', (False, True)),
            ('vector1', (False, False)),
            ('matrix', (True, True)),
            ('matrix1', (False, False)),
            ('matrix[0]', (False, False)),
        ):
            r = (V[0].test(t), V[1].test(t))
            self.assertEqual(
                x, r,
                "\n!!!!! regex OneOf validation failed for %r\n%sexpected=%r != result=%r"
                % (t, 6 * ' ', x, r))
コード例 #2
0
    def test6(self):
        "Test OneOf validator."

        msg = 'Validation failed for OneOf %s!'

        choices = ('clockwise', 'anticlockwise')
        OneOf = validators.OneOf(choices)
        for c in choices:
            assert OneOf(c) == 1, msg % c
        for c in ('a', 'b', 'c'):
            assert OneOf(c) == 0, msg % c

        OneOf = validators.OneOf('clockwise', 'anticlockwise')
        for c in choices:
            assert OneOf(c) == 1, msg % c
        for c in ('a', 'b', 'c'):
            assert OneOf(c) == 0, msg % c

        try:
            validators.OneOf(choices, 'bongo')
            raise AssertionError, "OneOf failed to detect bad arguments"
        except ValueError:
            pass
コード例 #3
0
    def test8(self):
        "test Sequence of validator"

        msg = 'Validation failed for SequenceOf %s!'

        v=validators.SequenceOf(validators.OneOf(('eps','pdf','png','gif','jpg','tif')),lo=1,hi=3,emptyOK=0)
        for c in (['png'],('eps',),('eps','pdf')):
            assert v(c), msg % str(c)
        v._lo = 2
        for c in ([],(),('eps'),('eps','pdf','a'),['eps','pdf','png','gif']):
            assert not v(c), msg % str(c)
        v._emptyOK=1
        for c in ([],(),('eps','pdf')):
            assert v(c), msg % str(c)