templates.test_modules6

 1import smartpy as sp
 2
 3
 4@sp.module
 5def M2():
 6    m1 = {"a": 42}
 7    m2 = sp.cast({"a": 42}, sp.map[sp.string, sp.int])
 8    bm1 = sp.big_map({"a": 42})
 9    bm2 = sp.cast(sp.big_map({"a": 42}), sp.big_map[sp.string, sp.int])
10    bm3 = sp.cast(sp.big_map(), sp.big_map[sp.string, sp.int])
11    bm4 = sp.cast(sp.big_map({}), sp.big_map[sp.string, sp.int])
12
13    class C(sp.Contract):
14        def __init__(self):
15            self.data.a = 0
16            self.data.x = 0
17            self.private.c = 42
18
19        @sp.private(with_storage="read-write")
20        def f(self, x):
21            assert utils.nat_to_tez(0) == sp.tez(0)
22            self.data.a = 1000
23            return x + 2
24
25        @sp.onchain_view
26        def view_on(self, x):
27            _ = self.private.c
28            _ = self.f(42)
29            return x + 1
30
31        @sp.offchain_view
32        def view_off(self, x):
33            _ = self.f(42)
34            self.data.x = 42
35            return x + 1
36
37        @sp.entrypoint
38        def ep(self):
39            assert self.f(2) == 4
40
41
42@sp.add_test()
43def test():
44    s = sp.test_scenario("Test", [sp.utils, M2])
45    c = M2.C()
46    s += c
47    c.ep()
48    s.verify(c.view_off(1) == 2)