Example #1
0
    def testUnbounded(self):
        big = schema.StringConstraint(None)
        self.assertUnboundedSize(big)
        self.assertDepth(big, 1)
        self.conforms(big, "blah blah blah blah blah" * 1024)
        self.violates(big, 123)

        bag = schema.TupleConstraint(schema.IntegerConstraint(), big)
        self.assertUnboundedSize(bag)
        self.assertDepth(bag, 2)

        polybag = schema.PolyConstraint(schema.IntegerConstraint(), bag)
        self.assertUnboundedSize(polybag)
        self.assertDepth(polybag, 2)
Example #2
0
    def testRecursion(self):
        # we have to fiddle with PolyConstraint's innards
        value = schema.ChoiceOf(
            schema.StringConstraint(),
            schema.IntegerConstraint(),
            # will add 'value' here
        )
        self.assertSize(value, 1065)
        self.assertDepth(value, 1)
        self.conforms(value, "key")
        self.conforms(value, 123)
        self.violates(value, [])

        mapping = schema.TupleConstraint(schema.StringConstraint(10), value)
        self.assertSize(mapping, 72 + 75 + 1065)
        self.assertDepth(mapping, 2)
        self.conforms(mapping, ("name", "key"))
        self.conforms(mapping, ("name", 123))
        value.alternatives = value.alternatives + (mapping, )

        self.assertUnboundedSize(value)
        self.assertUnboundedDepth(value)
        self.assertUnboundedSize(mapping)
        self.assertUnboundedDepth(mapping)

        # but note that the constraint can still be applied
        self.conforms(mapping, ("name", 123))
        self.conforms(mapping, ("name", "key"))
        self.conforms(mapping, ("name", ("key", "value")))
        self.conforms(mapping, ("name", ("key", 123)))
        self.violates(mapping, ("name", ("key", [])))
        l = []
        l.append(l)
        self.violates(mapping, ("name", l))
Example #3
0
 def testLargeInteger(self):
     c = schema.IntegerConstraint(64)
     self.assertSize(c, INTSIZE + 64)
     self.assertDepth(c, 1)
     self.conforms(c, 123)
     self.violates(c, "123")
     self.violates(c, None)
     self.conforms(c, 2**512 - 1)
     self.violates(c, 2**512)
     self.conforms(c, -2**512 + 1)
     self.violates(c, -2**512)
Example #4
0
 def testTuple(self):
     c = schema.TupleConstraint(schema.StringConstraint(10),
                                schema.StringConstraint(100),
                                schema.IntegerConstraint())
     self.conforms(c, ("hi", "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])
     self.assertSize(c, 72 + 75 + 165 + 73)
     self.assertDepth(c, 2)
Example #5
0
 def testInteger(self):
     # s_int32_t
     c = schema.IntegerConstraint()
     self.assertSize(c, INTSIZE)
     self.assertDepth(c, 1)
     self.conforms(c, 123)
     self.violates(c, 2**64)
     self.conforms(c, 0)
     self.conforms(c, 2**31 - 1)
     self.violates(c, 2**31)
     self.conforms(c, -2**31)
     self.violates(c, -2**31 - 1)
     self.violates(c, "123")
     self.violates(c, Dummy())
     self.violates(c, None)
Example #6
0
    def testDict(self):
        d = schema.DictOf(schema.StringConstraint(10),
                          schema.IntegerConstraint(),
                          maxKeys=4)

        self.assertDepth(d, 2)
        self.conforms(d, {"a": 1, "b": 2})
        self.conforms(d, {"foo": 123, "bar": 345, "blah": 456, "yar": 789})
        self.violates(d, None)
        self.violates(d, 12)
        self.violates(d, ["nope"])
        self.violates(d, ("nice", "try"))
        self.violates(d, {1: 2, 3: 4})
        self.violates(d, {"a": "b"})
        self.violates(d, {"a": 1, "b": 2, "c": 3, "d": 4, "toomuch": 5})
Example #7
0
    def testNestedTuple(self):
        inner = schema.TupleConstraint(schema.StringConstraint(10),
                                       schema.IntegerConstraint())
        self.assertSize(inner, 72 + 75 + 73)
        self.assertDepth(inner, 2)
        outer = schema.TupleConstraint(schema.StringConstraint(100), inner)
        self.assertSize(outer, 72 + 165 + 72 + 75 + 73)
        self.assertDepth(outer, 3)

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

        outer2 = schema.TupleConstraint(inner, inner)
        self.assertSize(outer2, 72 + 2 * (72 + 75 + 73))
        self.assertDepth(outer2, 3)
        self.conforms(outer2, (("hi", 1), ("there", 2)))
        self.violates(outer2, ("hi", 1, "flat", 2))
Example #8
0
 def testPoly(self):
     c = schema.PolyConstraint(schema.StringConstraint(100),
                               schema.IntegerConstraint())
     self.assertSize(c, 165)
     self.assertDepth(c, 1)