OSDN Git Service

fix related check to support that if-else statement include lock/unlock statement...
[bytom/equity.git] / compiler / compile_test.go
1 package compiler
2
3 import (
4         "encoding/hex"
5         "strings"
6         "testing"
7
8         chainjson "github.com/bytom/encoding/json"
9
10         "github.com/equity/compiler/equitytest"
11 )
12
13 func TestCompile(t *testing.T) {
14         cases := []struct {
15                 name     string
16                 contract string
17                 want     string
18         }{
19                 {
20                         "TrivialLock",
21                         equitytest.TrivialLock,
22                         "51",
23                 },
24                 {
25                         "LockWithPublicKey",
26                         equitytest.LockWithPublicKey,
27                         "ae7cac",
28                 },
29                 {
30                         "LockWithPublicKeyHash",
31                         equitytest.LockWithPKHash,
32                         "5279aa887cae7cac",
33                 },
34                 {
35                         "LockWith2of3Keys",
36                         equitytest.LockWith2of3Keys,
37                         "537a547a526bae71557a536c7cad",
38                 },
39                 {
40                         "LockToOutput",
41                         equitytest.LockToOutput,
42                         "00c3c251547ac1",
43                 },
44                 {
45                         "TradeOffer",
46                         equitytest.TradeOffer,
47                         "547a6413000000007b7b51547ac1631a000000547a547aae7cac",
48                 },
49                 {
50                         "EscrowedTransfer",
51                         equitytest.EscrowedTransfer,
52                         "537a641a000000537a7cae7cac6900c3c251557ac16328000000537a7cae7cac6900c3c251547ac1",
53                 },
54                 {
55                         "RevealPreimage",
56                         equitytest.RevealPreimage,
57                         "7caa87",
58                 },
59                 {
60                         "PriceChanger",
61                         equitytest.PriceChanger,
62                         "557a6432000000557a5479ae7cac6900c3c25100597a89587a89587a89587a89557a890274787e008901c07ec1633a000000007b537a51567ac1",
63                 },
64                 {
65                         "TestDefineVar",
66                         equitytest.TestDefineVar,
67                         "52797b937b7887916987",
68                 },
69                 {
70                         "TestSigIf",
71                         equitytest.TestSigIf,
72                         "53797b879169765379a00087641c00000052795279a0696321000000765279a069",
73                 },
74                 {
75                         "TestIfAndMultiClause",
76                         equitytest.TestIfAndMultiClause,
77                         "7b641f0000007087916976547aa00087641a000000765379a06963240000007b7bae7cac",
78                 },
79                 {
80                         "TestIfNesting",
81                         equitytest.TestIfNesting,
82                         "7b644400000054795279879169765579a00087643500000052795479a000876429000000765379a06952795579879169633a000000765479a06953797b8791635c0000007654798791695279a000876459000000527978a0697d8791",
83                 },
84         }
85         for _, c := range cases {
86                 t.Run(c.name, func(t *testing.T) {
87                         r := strings.NewReader(c.contract)
88                         compiled, err := Compile(r)
89                         if err != nil {
90                                 t.Fatal(err)
91                         }
92
93                         contract := compiled[len(compiled)-1]
94                         got := []byte(contract.Body)
95
96                         want, err := hex.DecodeString(c.want)
97                         if err != nil {
98                                 t.Fatal(err)
99                         }
100
101                         if string(got) != string(want) {
102                                 t.Errorf("%s got  %s\nwant %s", c.name, hex.EncodeToString(got), hex.EncodeToString(want))
103                         }
104                 })
105         }
106 }
107
108 func mustDecodeHex(h string) *chainjson.HexBytes {
109         bits, err := hex.DecodeString(h)
110         if err != nil {
111                 panic(err)
112         }
113         result := chainjson.HexBytes(bits)
114         return &result
115 }