Coverage for tests/test_stringfuture.py: 100%
59 statements
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 15:39 -0800
« prev ^ index » next coverage.py v7.6.7, created at 2024-11-22 15:39 -0800
1import time
3import appl
4from appl import CallFuture
5from appl import StringFuture as S
8def _check_str(s: S, target: str):
9 assert type(s) is S
10 assert str(s) == target
13def test_concat():
14 _check_str(S("ab") + S("cd"), "abcd")
15 _check_str(S("AP") + "PL", "APPL")
16 _check_str("AP" + S("PL"), "APPL")
17 s = ""
18 s += S("AP")
19 _check_str(s, "AP")
20 s += "PL"
21 _check_str(s, "APPL")
22 s += S("E")
23 _check_str(s, "APPLE")
26def test_cmp():
27 assert S("APPL") == "APPL"
28 assert "APPL" == S("APPL")
29 assert S("APPL") == S("APPL")
31 assert S("APPL") != "APPLE"
32 assert "APPL" != S("APPLE")
33 assert S("APPL") != S("APPLE")
35 assert "APPL" >= S("AAA")
36 assert S("APPL") >= S("AAA")
37 assert S("APPL") >= "AAA"
38 assert S("APPL") > "AAA"
39 assert S("APPL") <= "ZZZ"
40 assert S("APPL") < "ZZZ"
43def test_contains():
44 assert "AP" in S("APPL")
45 assert "AP" not in S("AAA")
48def test_getitem():
49 assert S("APPL")[1] == "P"
50 assert S("APPL")[1:] == "PPL"
51 assert S("A.P.P.L").split(".") == ["A", "P", "P", "L"]
54def test_format():
55 assert f"{S('A'):3}" == "A "
56 assert appl.format(1.234, ".2f") == "1.23"
57 assert isinstance(appl.format(1.234, ".2f"), S)
60def test_as_key():
61 d = {}
62 d[S("APPL")] = 1
63 assert d[S("APPL")] == 1
64 assert d["APPL"] == 1
67def test_future():
68 def run(t):
69 time.sleep(t)
70 return "a"
72 t0 = time.time()
73 n = 3
74 s = ""
75 for i in range(n):
76 s += S(CallFuture(run, t=0.2))
77 assert time.time() - t0 < 0.15
78 assert str(s) == "a" * n
79 assert time.time() - t0 < 0.4