def test_seq_of_checkonly(): almost_ints = list(range(1000)) + ["ha!"] + list(range(1001,2001)) non_gotchas = (tc.seq_of(int, checkonly=9)(almost_ints) + tc.seq_of(int, checkonly=10)(almost_ints) + tc.seq_of(int, checkonly=11)(almost_ints)) assert non_gotchas >= 2 # should fail about once every 40000 runs almost_ints = list(range(1000)) + 10*["ha!"] + list(range(1010,2001)) non_gotchas = (tc.seq_of(str, checkonly=8)(almost_ints) + tc.seq_of(str, checkonly=9)(almost_ints) + tc.seq_of(str, checkonly=10)(almost_ints)) assert non_gotchas <= 1 # should fail almost never
def test_seq_of_checkonly(): almost_ints = list(range(1000)) + ["ha!"] + list(range(1001, 2001)) non_gotchas = (tc.seq_of(int, checkonly=9)(almost_ints) + tc.seq_of(int, checkonly=10)(almost_ints) + tc.seq_of(int, checkonly=11)(almost_ints)) assert non_gotchas >= 2 # should fail about once every 40000 runs almost_ints = list(range(1000)) + 10 * ["ha!"] + list(range(1010, 2001)) non_gotchas = (tc.seq_of(str, checkonly=8)(almost_ints) + tc.seq_of(str, checkonly=9)(almost_ints) + tc.seq_of(str, checkonly=10)(almost_ints)) assert non_gotchas <= 1 # should fail almost never
def test_seq_of_str_with_simple_str(): assert not tc.seq_of(str)("A sequence of strings, but not a seq_of(str)")
def test_seq_of_UserList(): assert tc.seq_of(int)([4, 5]) assert tc.seq_of(int)(collections.UserList([4, 5]))
def test_seq_of_with_optional(): assert tc.seq_of(tc.optional(tc.re("^foo$")))(["foo", None, "foo"]) and \ tc.seq_of(tc.optional(tc.re("^foo$"))).check(["foo", None, "foo"]) assert not tc.seq_of(tc.optional(tc.re("^foo$")))(["123", None, "foo"]) and \ not tc.seq_of(tc.optional(tc.re("^foo$"))).check(["foo", None, "1234"])
def foo(x: tc.seq_of((tc.re("^[01]+$"), int))) -> bool: return functools.reduce(lambda r, e: r and int(e[0], 2) == e[1], x, True)
def foo_s(x: tc.seq_of(int)) -> tc.seq_of(float): return list(map(float, x))
def test_seq_of_str_with_simple_str(): assert not tc.seq_of(str)("A sequence of strings, but not a seq_of(str)", None)
def test_seq_of_UserList(): namespace = None assert tc.seq_of(int)([4, 5], namespace) assert tc.seq_of(int)(collections.UserList([4, 5]), namespace)
def test_seq_of_with_optional(): namespace = None assert tc.seq_of(tc.optional(tc.re("^foo$")))(["foo", None, "foo"], namespace) and \ tc.seq_of(tc.optional(tc.re("^foo$"))).check(["foo", None, "foo"], namespace) assert not tc.seq_of(tc.optional(tc.re("^foo$")))(["123", None, "foo"], namespace) and \ not tc.seq_of(tc.optional(tc.re("^foo$"))).check(["foo", None, "1234"], namespace)