def test_type_check_example(): assert t.type_check(5, 1) == None assert t.type_check(5, 1.3) == 'must be of type float' assert t.type_check('foo', 'bar') == None assert t.type_check(MyNumber(3), MyNumber(5)) == None assert t.type_check( 3, MyNumber(5)) == 'must be of type test.test_type_spec.MyNumber'
def test_type_check_callable(): assert t.type_check(5, Positive) == None assert t.type_check(5.9, Positive) == None assert t.type_check(-5, Positive) == 'must be positive' assert t.type_check(5, CallableNumber()) == None assert t.type_check(5.5, CallableNumber()) == 'must be of type int'
def test_type_check_dict(): assert t.type_check({}, {}) == None assert t.type_check([], {}) == 'must be of type dict' assert t.type_check({'foo': 1}, {}) == None assert t.type_check({'foo': 1}, {'foo': int}) == None assert t.type_check({'foo': 1.3}, {'foo': int}) == "{'foo': 'must be of type int'}"
def test_type_check_type(): assert t.type_check(5, int) == None assert t.type_check('foobar', int) == 'must be of type int' assert t.type_check(True, bool) == None assert t.type_check(False, bool) == None assert t.type_check(5, bool) == 'must be of type bool' assert t.type_check(Data(5), Data) == None assert t.type_check('foobar', Data) == 'must be of type test.test_type_spec.Data'
def test_type_check_list(): assert t.type_check([], []) == None assert t.type_check({}, []) == 'must be of type list' assert t.type_check([3], []) == None assert t.type_check([3], [int]) == None assert t.type_check([3, 4, 5], [int]) == None assert t.type_check( [3, 4, 5.2], [int]) == 'list index 2 is invalid - must be of type int'
def Offer(v): return type_check( v, { 'packId': str, 'type': Enum('est', 'rental'), 'timing': { 'purchaseAvailableSince': '2012-12-20T00:00:00.00Z', 'purchaseAvailableUntil': '2012-12-24T00:00:00.00Z' }, 'pricing': { 'upfrontPriceCents': 4900, 'currency': 'SEK' }, 'regions': [str] })
def Pack(v): return type_check(v, {'name': str, 'viewables': [str]})
def OfferUpdate(v): return type_check(v, {'disabled': bool})
def PackUpdate(v): return type_check(v, {'action': Enum('add', 'remove'), 'viewables': [str]})
def __call__(self, v): return t.type_check(v, int)
def Number(v): return t.type_check(v, t.AnyOf(int, float))
def test_type_check_none(): assert t.type_check(None, None) == None assert t.type_check(5, None) == 'must be None'
def test_type_check_custom_check(): assert t.type_check(MyNumber(5), MyNumber) == None assert t.type_check(MyNumber(5.3), MyNumber) == 'must be of type int'
def User(v): return type_check(v, {'userId': str, 'token': str})
def test_type_check_predicate(): even = lambda n: n % 2 == 0 assert t.type_check(6, even) == None assert t.type_check( 5, even) == 'must be of type test_type_check_predicate.<locals>.<lambda>'
def Partner(v): return type_check(v, {'id': str, 'backendSecret': str})
def type_check(my_number): return t.type_check(my_number.n, int)