def foo(*, k: tc.map_of( (int, int), [tc.re("^[0-9]+$"), tc.re("^[0-9]+$")])): return functools.reduce( lambda r, t: r and str(t[0][0]) == t[1][0] and str(t[0][1]) == t[1] [1], k.items(), True)
def test_map_of_checkonly(): def probably_int(i): return i if random.random() > 0.25 else str(i) def probably_str(i): return str(i) if random.random() > 0.25 else i correct = 0 # test-check 20 random dictionaries with random violations: for i in range(20): mydict = dict() numbers = random.sample(range(1000000), 8) # are variable and all different for k in range(4): mydict[probably_int(numbers.pop())] = probably_str(numbers.pop()) correct += tc.map_of(int,str,checkonly=2)(mydict) assert correct > 0 and correct < 20 # should fail once about every 500000 runs
def test_map_of_checkonly(): def probably_int(i): return i if random.random() > 0.25 else str(i) def probably_str(i): return str(i) if random.random() > 0.25 else i correct = 0 # test-check 20 random dictionaries with random violations: for i in range(20): mydict = dict() numbers = random.sample(range(1000000), 8) # are variable and all different for k in range(4): mydict[probably_int(numbers.pop())] = probably_str(numbers.pop()) correct += tc.map_of(int, str, checkonly=2)(mydict, None) assert correct > 0 and correct < 20 # should fail once about every 500000 runs
def test_map_of_OrderedDict(): assert tc.map_of(int,str)(collections.OrderedDict())
def test_map_of_with_optional(): assert tc.map_of(int, tc.optional(str))({ 1: "foo", 2: None }) and \ tc.map_of(int, tc.optional(str)).check({ 1: "foo", 2: None }) assert not tc.map_of(int, tc.optional(str))({ None: "foo", 2: None }) and \ not tc.map_of(int, tc.optional(str)).check({ None: "foo", 2: None })
def foo(*, k: tc.map_of((int, int), [tc.re("^[0-9]+$"), tc.re("^[0-9]+$")])): return functools.reduce(lambda r, t: r and str(t[0][0]) == t[1][0] and str(t[0][1]) == t[1][1], k.items(), True)
def foo(x: tc.map_of(int, str)) -> tc.map_of(str, int): return { v: k for k, v in x.items() }
def test_map_of_OrderedDict(): assert tc.map_of(int, str)(collections.OrderedDict(), None)
def test_map_of_with_optional(): assert tc.map_of(int, tc.optional(str))({1: "foo", 2: None}, None) and \ tc.map_of(int, tc.optional(str)).check({1: "foo", 2: None}, None) assert not tc.map_of(int, tc.optional(str))({None: "foo", 2: None}, None) and \ not tc.map_of(int, tc.optional(str)).check({None: "foo", 2: None}, None)
def foo(x: tc.map_of(int, str)) -> tc.map_of(str, int): return {v: k for k, v in x.items()}