templates.test_convert

 1import smartpy as sp
 2
 3
 4@sp.module
 5def main():
 6    class MyContract(sp.Contract):
 7        def __init__(self):
 8            self.data.x = sp.cast(
 9                sp.record(a=1, b=2), sp.record(a=sp.int, b=sp.int).layout(("a", "b"))
10            )
11            self.data.y = sp.cast(
12                sp.variant.A(42), sp.variant(A=sp.int, B=sp.int).layout(("A", "B"))
13            )
14
15        @sp.entrypoint
16        def test_record(self):
17            x = sp.cast(
18                sp.record(c=3, d=4), sp.record(c=sp.int, d=sp.int).layout(("c", "d"))
19            )
20            self.data.x = sp.convert(x)
21            assert self.data.x == sp.record(a=3, b=4)
22
23        @sp.entrypoint
24        def test_pair(self):
25            self.data.x = sp.convert((5, 6))
26            assert self.data.x == sp.record(a=5, b=6)
27
28        @sp.entrypoint
29        def test_variant(self):
30            y = sp.cast(
31                sp.variant.C(43), sp.variant(C=sp.int, D=sp.int).layout(("C", "D"))
32            )
33            self.data.y = sp.convert(y)
34            assert self.data.y == sp.variant.A(43)
35
36
37@sp.add_test()
38def test():
39    scenario = sp.test_scenario("Test_Convert", main)
40
41    c = main.MyContract()
42    scenario += c
43
44    c.test_record()
45    c.test_pair()
46    c.test_variant()