templates.test_modify

  1import smartpy as sp
  2
  3
  4@sp.module
  5def main():
  6    t2: type = sp.record(a=sp.nat, b=sp.int, c=sp.bool, d=sp.string).layout(
  7        ("a", ("b", ("c", "d")))
  8    )
  9    t3: type = sp.record(a=sp.nat, b=sp.int, c=sp.bool, d=sp.string).layout(
 10        ((("a", "b"), "c"), "d")
 11    )
 12    t4: type = sp.record(a=sp.nat, b=sp.int, c=sp.bool, d=sp.string).layout(
 13        (("a", "b"), ("c", "d"))
 14    )
 15
 16    class C1(sp.Contract):
 17        def __init__(self):
 18            self.data.x1 = sp.record(a=sp.nat(0), b=sp.int(1), c=True, d="abc")
 19            self.data.x2 = sp.record(a=sp.nat(0), b=sp.int(1), c=True, d="abc")
 20            self.data.x3 = sp.record(a=sp.nat(0), b=sp.int(1), c=True, d="abc")
 21            self.data.x4 = sp.record(a=sp.nat(0), b=sp.int(1), c=True, d="abc")
 22            self.data.y = (sp.nat(0), sp.int(1), True, "abc")
 23            self.data.z = sp.record(
 24                a=sp.nat(0), b=sp.int(1), c=True, d=sp.record(e=1, f="x")
 25            )
 26
 27        @sp.entrypoint
 28        def ep1(self):
 29            sp.cast(self.data.x2, t2)
 30            sp.cast(self.data.x3, t3)
 31            sp.cast(self.data.x4, t4)
 32            with sp.modify_record(self.data.x1) as x1:
 33                assert abs(x1.b) == x1.a + 1
 34            with sp.modify_record(self.data.x2) as x2:
 35                assert abs(x2.b) == x2.a + 1
 36                x2.d = "xyz"
 37            with sp.modify_record(self.data.x3) as x3:
 38                assert abs(x3.b) == x3.a + 1
 39                x3.d = "xyz"
 40            with sp.modify_record(self.data.x4) as x4:
 41                assert abs(x4.b) == x4.a + 1
 42                x4.d = "xyz"
 43
 44        @sp.entrypoint
 45        def ep2(self):
 46            with sp.modify_record(self.data.x1) as data:
 47                assert abs(data.b) == data.a + 1
 48                data.d = "xyz"
 49
 50        @sp.entrypoint
 51        def ep4(self):
 52            with sp.modify_record(self.data.x1) as data:
 53                _ = data.a
 54
 55        @sp.entrypoint
 56        def ep5(self):
 57            with sp.modify_record(self.data.x1) as data:
 58                assert abs(data.b) == data.a + 1
 59                data.d = "xyz"
 60            self.data.x1.a += 5
 61
 62        @sp.entrypoint
 63        def ep5(self, alice):
 64            with sp.modify_record(self.data.x1) as data:
 65                sp.send(alice, sp.tez(0))
 66                data.d = "xyz"
 67            self.data.x1.a += 5
 68
 69        @sp.entrypoint
 70        def ep6(self):
 71            with sp.modify_record(self.data.z) as outter:
 72                with sp.modify_record(outter.d) as inner:
 73                    outter.b = 100
 74                    inner.e = 2
 75                    inner.f = "y"
 76
 77        @sp.entrypoint
 78        def ep7(self):
 79            assert self.data.z.d.e == 2
 80            with sp.modify_record(self.data.z.d) as z_d:
 81                assert z_d.e == 2
 82                z_d.e = 3
 83                z_d.f = "z"
 84                assert z_d.e == 3
 85                z_d.e = 4
 86                assert z_d.e == 4
 87            assert self.data.z.d.e == 4
 88
 89    class C2(sp.Contract):
 90        def __init__(self):
 91            self.data.a = sp.nat(0)
 92            self.data.b = sp.int(1)
 93            self.data.c = True
 94            self.data.d = "abc"
 95
 96        @sp.entrypoint
 97        def ep1(self):
 98            with sp.modify_record(self.data) as data:
 99                assert abs(data.b) == data.a + 1
100                data.d = "xyz"
101
102        @sp.entrypoint
103        def ep2(self):
104            with sp.modify_record(self.data) as data:
105                data.d = "abc"
106
107
108@sp.add_test()
109def test():
110    alice = sp.test_account("Alice")
111    s = sp.test_scenario("Match", main)
112    c1 = main.C1()
113    s += c1
114    c1.ep1()
115    c1.ep2()
116    c1.ep4()
117    c1.ep5()
118    c1.ep6()
119    c1.ep7()
120
121    s += main.C2()