templates.test_compute_exception

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    class Test(sp.Contract):
 7        def __init__(self):
 8            self.data.value = sp.int(0)
 9
10        @sp.offchain_view()
11        def other(self, x):
12            assert x < self.data.value
13            return x * x
14
15        @sp.offchain_view()
16        def square_exn(self, x):
17            sp.cast(x, int)
18            raise x * x
19
20
21if "main" in __name__:
22
23    @sp.add_test()
24    def test():
25        scenario = sp.test_scenario("Test", main)
26        c = main.Test()
27        scenario += c
28        scenario.compute(c.other(-5))
29        scenario.verify(sp.is_failing(c.other(5)))
30        scenario.verify(~sp.is_failing(c.other(-5)))
31        scenario.verify(
32            sp.catch_exception(c.other(5))
33            == sp.Some("Assert failure: params < self.data.value")
34        )
35        scenario.verify(
36            sp.catch_exception(c.other(5), t=sp.string)
37            == sp.Some("Assert failure: params < self.data.value")
38        )
39        scenario.verify(sp.catch_exception(c.other(-5), t=sp.string) == None)
40        scenario.verify(sp.catch_exception(c.other(-5), t=sp.int) == None)
41        scenario.verify(sp.catch_exception(c.square_exn(-5), t=sp.int) == sp.Some(25))