templates.test_mutez

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    class TestAddressComparison(sp.Contract):
 7        @sp.entrypoint
 8        def test(self, x):
 9            assert utils.mutez_to_nat(sp.amount) == x
10            assert utils.nat_to_mutez(x) == sp.amount
11
12        @sp.entrypoint
13        def test_diff(self, x, y):
14            sp.cast(x, sp.mutez)
15            sp.cast(y, sp.mutez)
16            r = x - y
17
18    class MyContract(sp.Contract):
19        def __init__(self):
20            self.data.result = None
21
22        @sp.entrypoint
23        def store(self, params):
24            self.data.result = sp.Some(params)
25
26        @sp.entrypoint
27        def decrease(self, params):
28            self.data.result = sp.Some(self.data.result.unwrap_some() - params)
29
30        @sp.entrypoint
31        def sub(self, params):
32            self.data.result = sp.sub_mutez(params.x, params.y)
33
34
35if "templates" not in __name__:
36
37    @sp.add_test()
38    def test():
39        scenario = sp.test_scenario("Test", [sp.utils, main])
40        c1 = main.TestAddressComparison()
41
42        scenario += c1
43
44        c1.test(10, _amount=sp.mutez(10))
45        c1.test(10000000, _amount=sp.tez(10))
46        c1.test_diff(x=sp.mutez(100), y=sp.mutez(50))
47        c1.test_diff(x=sp.mutez(70), y=sp.mutez(100), _valid=False)
48
49    @sp.add_test()
50    def test():
51        scenario = sp.test_scenario("Test2", [sp.utils, main])
52        c1 = main.MyContract()
53        scenario += c1
54        c1.store(sp.mutez(42))
55        c1.decrease(sp.mutez(22))
56        c1.decrease(sp.mutez(22), _valid=False)
57        c1.sub(sp.record(x=sp.mutez(1), y=sp.mutez(2)))
58        scenario.verify(c1.data.result == None)
59        c1.sub(sp.record(x=sp.mutez(100), y=sp.mutez(1)))
60        scenario.verify(c1.data.result == sp.Some(sp.mutez(99)))