コード例 #1
0
    def testByteString(self):
        c = schema.ByteStringConstraint(10)
        self.violates(c, "I'm str")
        self.conforms(c, b"I'm short")
        self.violates(c, b"I am toooooo long")
        self.conforms(c, b"a" * 10)
        self.violates(c, b"a" * 11)
        self.violates(c, 123)
        self.violates(c, Dummy())
        self.violates(c, None)

        c2 = schema.ByteStringConstraint(15, 10)
        self.violates(c2, b"too short")
        self.conforms(c2, b"long enough")
        self.violates(c2, b"this is too long")
        self.violates(c2, u"I am unicode")

        c3 = schema.ByteStringConstraint(regexp=b"needle")
        self.violates(c3, b"no present")
        self.conforms(c3, b"needle in a haystack")
        c4 = schema.ByteStringConstraint(regexp=b"[abc]+")
        self.violates(c4, b"spelled entirely without those letters")
        self.conforms(c4, b"add better cases")
        c5 = schema.ByteStringConstraint(regexp=re.compile(b'\d+\s\w+'))
        self.conforms(c5, b": 123 boo")
        self.violates(c5, b"more than 1  spaces")
        self.violates(c5, b"letters first 123")
コード例 #2
0
    def testRecursion(self):
        # we have to fiddle with PolyConstraint's innards
        value = schema.ChoiceOf(
            schema.ByteStringConstraint(),
            schema.IntegerConstraint(),
            # will add 'value' here
        )
        self.conforms(value, b"key")
        self.conforms(value, 123)
        self.violates(value, [])

        mapping = schema.TupleConstraint(schema.ByteStringConstraint(10),
                                         value)
        self.conforms(mapping, (b"name", b"key"))
        self.conforms(mapping, (b"name", 123))
        value.alternatives = value.alternatives + (mapping, )

        # but note that the constraint can still be applied
        self.conforms(mapping, (b"name", 123))
        self.conforms(mapping, (b"name", b"key"))
        self.conforms(mapping, (b"name", (b"key", b"value")))
        self.conforms(mapping, (b"name", (b"key", 123)))
        self.violates(mapping, (b"name", (b"key", [])))
        l = []
        l.append(l)
        self.violates(mapping, ("name", l))
コード例 #3
0
 def testTuple(self):
     c = schema.TupleConstraint(schema.ByteStringConstraint(10),
                                schema.ByteStringConstraint(100),
                                schema.IntegerConstraint())
     self.conforms(c, (b"hi", b"there buddy, you're number", 1))
     self.violates(c, "nope")
     self.violates(c, ("string", "string", "NaN"))
     self.violates(c, ("string that is too long", "string", 1))
     self.violates(c, ["Are tuples", "and lists the same?", 0])
コード例 #4
0
    def testNestedTuple(self):
        inner = schema.TupleConstraint(schema.ByteStringConstraint(10),
                                       schema.IntegerConstraint())
        outer = schema.TupleConstraint(schema.ByteStringConstraint(100), inner)

        self.conforms(inner, (b"hi", 2))
        self.conforms(outer, (b"long string here", (b"short", 3)))
        self.violates(outer, (b"long string here", (b"short", 3, b"extra")))
        self.violates(outer, (b"long string here", (b"too long string", 3)))

        outer2 = schema.TupleConstraint(inner, inner)
        self.conforms(outer2, ((b"hi", 1), (b"there", 2)))
        self.violates(outer2, (b"hi", 1, b"flat", 2))
コード例 #5
0
ファイル: test_schema.py プロジェクト: sajith/foolscap
    def testByteString(self):
        c = schema.ByteStringConstraint(10)
        self.conforms(c, b"I'm short")
        self.violates(c, b"I am too long")
        self.conforms(c, b"a" * 10)
        self.violates(c, b"a" * 11)
        self.violates(c, 123)
        self.violates(c, Dummy())
        self.violates(c, None)

        c2 = schema.ByteStringConstraint(15, 10)
        self.violates(c2, b"too short")
        self.conforms(c2, b"long enough")
        self.violates(c2, b"this is too long")
        self.violates(c2, u"I am unicode")
コード例 #6
0
 def testPoly(self):
     c = schema.PolyConstraint(schema.ByteStringConstraint(100),
                               schema.IntegerConstraint())
     self.conforms(c, b"string")
     self.conforms(c, 123)
     self.violates(c, u"unicode")
     self.violates(c, 123.4)
     self.violates(c, ["not", "a", "list"])
コード例 #7
0
    def testList(self):
        l = schema.ListOf(schema.ByteStringConstraint(10))
        self.conforms(l, [b"one", b"two", b"three"])
        self.violates(l, (b"can't", b"fool", b"me"))
        self.violates(l, [b"but", b"perspicacity", b"is too long"])
        self.violates(l, [0, b"numbers", b"allowed"])
        self.conforms(l, [b"short", b"sweet"])

        l2 = schema.ListOf(schema.ByteStringConstraint(10), 3)
        self.conforms(l2, [b"the number", b"shall be", b"three"])
        self.violates(l2, [b"five", b"is", b"...", b"right", b"out"])

        l3 = schema.ListOf(schema.ByteStringConstraint(10), None)
        self.conforms(l3, [b"long"] * 35)
        self.violates(l3, [b"number", 1, b"rule", b"is", 0, b"numbers"])

        l4 = schema.ListOf(schema.ByteStringConstraint(10), 3, 3)
        self.conforms(l4, [b"three", b"is", b"good"])
        self.violates(l4, [b"but", b"four", b"is", b"bad"])
        self.violates(l4, [b"two", b"too"])
コード例 #8
0
    def testDict(self):
        d = schema.DictOf(schema.ByteStringConstraint(10),
                          schema.IntegerConstraint(),
                          maxKeys=4)

        self.conforms(d, {b"a": 1, b"b": 2})
        self.conforms(d, {b"foo": 123, b"bar": 345, b"blah": 456, b"yar": 789})
        self.violates(d, None)
        self.violates(d, 12)
        self.violates(d, [b"nope"])
        self.violates(d, (b"nice", b"try"))
        self.violates(d, {1: 2, 3: 4})
        self.violates(d, {b"a": b"b"})
        self.violates(d, {b"a": 1, b"b": 2, b"c": 3, b"d": 4, b"toomuch": 5})
コード例 #9
0
    def testMakeConstraint(self):
        make = IConstraint
        c = make(int)
        self.check(c, schema.IntegerConstraint)
        self.assertIn(c.maxBytes, (-1, 1024))

        c = make(bytes)
        self.check(c, schema.ByteStringConstraint)
        self.assertEqual(c.maxLength, None)

        c = make(schema.ByteStringConstraint(2000))
        self.check(c, schema.ByteStringConstraint)
        self.assertEqual(c.maxLength, 2000)

        c = make(str)
        self.check(c, schema.StringConstraint)
        self.assertEqual(c.maxLength, None)

        self.check(make(bool), schema.BooleanConstraint)
        self.check(make(float), schema.NumberConstraint)

        self.check(make(schema.NumberConstraint()), schema.NumberConstraint)
        c = make((int, bytes))
        self.check(c, schema.TupleConstraint)
        self.check(c.constraints[0], schema.IntegerConstraint)
        self.check(c.constraints[1], schema.ByteStringConstraint)

        c = make(common.RIHelper)
        self.check(c, RemoteInterfaceConstraint)
        self.assertEqual(c.interface, common.RIHelper)

        c = make(common.IFoo)
        self.check(c, LocalInterfaceConstraint)
        self.assertEqual(c.interface, common.IFoo)

        c = make(Referenceable)
        self.check(c, RemoteInterfaceConstraint)
        self.assertEqual(c.interface, None)
コード例 #10
0
    def testMakeConstraint(self):
        make = IConstraint
        c = make(int)
        self.check(c, schema.IntegerConstraint)
        self.failUnlessEqual(c.maxBytes, -1)

        c = make(str)
        self.check(c, schema.ByteStringConstraint)
        self.failUnlessEqual(c.maxLength, None)

        c = make(schema.ByteStringConstraint(2000))
        self.check(c, schema.ByteStringConstraint)
        self.failUnlessEqual(c.maxLength, 2000)

        c = make(unicode)
        self.check(c, schema.UnicodeConstraint)
        self.failUnlessEqual(c.maxLength, None)

        self.check(make(bool), schema.BooleanConstraint)
        self.check(make(float), schema.NumberConstraint)

        self.check(make(schema.NumberConstraint()), schema.NumberConstraint)
        c = make((int, str))
        self.check(c, schema.TupleConstraint)
        self.check(c.constraints[0], schema.IntegerConstraint)
        self.check(c.constraints[1], schema.ByteStringConstraint)

        c = make(common.RIHelper)
        self.check(c, RemoteInterfaceConstraint)
        self.failUnlessEqual(c.interface, common.RIHelper)

        c = make(common.IFoo)
        self.check(c, LocalInterfaceConstraint)
        self.failUnlessEqual(c.interface, common.IFoo)

        c = make(Referenceable)
        self.check(c, RemoteInterfaceConstraint)
        self.failUnlessEqual(c.interface, None)