templates.test_runtime_errors

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    sender = sp.sender
 7    source = sp.source
 8    x1 = sp.mutez(1) - sp.mutez(2)
 9    x2 = {}[42]
10    x3 = {}.get(42, error="oops")
11
12    class C1(sp.Contract):
13        @sp.entrypoint
14        def ep(self):
15            pass
16
17    class C2(sp.Contract):
18        pass
19
20    class Caller(sp.Contract):
21        @sp.entrypoint
22        def drain(self, dest, amount):
23            c = sp.contract(sp.unit, dest).unwrap_some()
24            sp.transfer((), amount, c)
25
26
27def run_test(label, f):
28    try:
29        f()
30    except Exception as e:
31        print(label)
32        print(e)
33
34
35@sp.add_test()
36def test():
37    s = sp.test_scenario("s", main)
38    run_test("l001", lambda: s.compute(sender))
39    run_test("l002", lambda: s.compute(source))
40    run_test("l003", lambda: s.compute(main.x1))
41    run_test("l004", lambda: s.compute(main.x2))
42    run_test("l005", lambda: s.compute(main.x3))
43
44    caller = main.Caller()
45    s += caller
46
47    c1 = main.C1()
48    s += c1
49    run_test(
50        "l006", lambda: caller.drain(sp.record(dest=c1.address, amount=sp.mutez(1000)))
51    )
52
53    c2 = main.C2()
54    s += c2
55    run_test(
56        "l007", lambda: caller.drain(sp.record(dest=c2.address, amount=sp.mutez(0)))
57    )
58
59    run_test("l008", lambda: c1.ep(_exception="blah", _valid=True))
60    run_test("l009", lambda: s.verify(False))
def run_test(label, f):
28def run_test(label, f):
29    try:
30        f()
31    except Exception as e:
32        print(label)
33        print(e)