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))
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))
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])