templates.test_effects

  1import smartpy as sp
  2
  3
  4@sp.module
  5def main():
  6    class Sub(sp.Contract):
  7        def __init__(self):
  8            self.data.a = sp.nat(0)
  9
 10        @sp.entrypoint
 11        def sub_incr(self):
 12            self.data.a = self.data.a + 1
 13
 14        @sp.entrypoint
 15        def sub_double(self):
 16            self.data.a = 2 * self.data.a
 17
 18    class Main(sp.Contract):
 19        def __init__(self, sub):
 20            self.private.sub = sub
 21            self.data.a = sp.nat(0)
 22
 23        @sp.private(with_storage="read-write")
 24        def incr(self, x):
 25            self.data.a = self.data.a + 1
 26            return x + 1
 27
 28        @sp.private(with_storage="read-write")
 29        def double(self, x):
 30            self.data.a = 2 * self.data.a
 31            return 2 * x
 32
 33        @sp.private(with_operations=True)
 34        def sub_incr(self, params):
 35            sub_incr = sp.contract(
 36                sp.unit, params.sub, entrypoint="sub_incr"
 37            ).unwrap_some()
 38            sp.transfer((), sp.tez(0), sub_incr)
 39            return params.x + 1
 40
 41        @sp.private(with_operations=True)
 42        def sub_double(self, params):
 43            sub_double = sp.contract(
 44                sp.unit, params.sub, entrypoint="sub_double"
 45            ).unwrap_some()
 46            sp.transfer((), sp.tez(0), sub_double)
 47            return 2 * params.x
 48
 49        @sp.private(with_storage="read-write", with_operations=True)
 50        def sub_incr_both(self, params):
 51            sub_incr = sp.contract(
 52                sp.unit, params.sub, entrypoint="sub_incr"
 53            ).unwrap_some()
 54            sp.transfer((), sp.tez(0), sub_incr)
 55            self.data.a = self.data.a + 1
 56            return params.x + 1
 57
 58        @sp.entrypoint
 59        def test_with_storage(self):
 60            self.data.a = 0
 61            assert self.incr(100) == 101
 62            assert self.data.a == 1
 63
 64            self.data.a = 10
 65            assert self.double(100) == 200
 66            assert self.data.a == 20
 67
 68            self.data.a = 10
 69            assert self.double(self.incr(100)) == 202
 70            assert self.data.a == 22
 71
 72            self.data.a = 10
 73            assert self.double(100) + self.incr(1000) == 1201
 74            assert self.data.a == 22
 75
 76            self.data.a = 10
 77            assert self.incr(1000) + self.double(100) == 1201
 78            assert self.data.a == 21
 79
 80        @sp.entrypoint
 81        def test_sub_incr(self):
 82            self.data.a = 0
 83            assert self.sub_incr(sp.record(x=100, sub=self.private.sub)) == 101
 84
 85        @sp.entrypoint
 86        def test_sub_incr_both(self):
 87            self.data.a = 0
 88            assert self.sub_incr_both(sp.record(x=100, sub=self.private.sub)) == 101
 89            assert self.data.a == 1
 90
 91        @sp.entrypoint
 92        def test_sub_incr_store(self):
 93            self.data.a = self.sub_incr_both(sp.record(x=100, sub=self.private.sub))
 94
 95
 96@sp.add_test()
 97def test():
 98    s = sp.test_scenario("Effects", main)
 99    sub = main.Sub()
100    s += sub
101    main_ = main.Main(sub.address)
102    s += main_
103    main_.test_with_storage()
104
105    s.verify(sub.data.a == 0)
106
107    main_.test_sub_incr()
108    s.verify(sub.data.a == 1)
109
110    main_.test_sub_incr_both()
111    s.verify(sub.data.a == 2)
112
113    main_.test_sub_incr_store()
114    s.verify(sub.data.a == 3)
115    s.verify(main_.data.a == 101)