OSDN Git Service

optimise equity commandline (#36)
[bytom/equity.git] / compiler / equitytest / equity_test.go
1 package equitytest
2
3 import (
4         "bufio"
5         "encoding/hex"
6         "os"
7         "path/filepath"
8         "testing"
9
10         "github.com/equity/compiler"
11 )
12
13 func TestCompileContract(t *testing.T) {
14         cases := []struct {
15                 pathFile string
16                 want     string
17         }{
18                 {
19                         "./LockPosition",
20                         "cd9f697b7bae7cac6900c3c251547ac1",
21                 },
22                 {
23                         "./RepayCollateral",
24                         "557a641f0000007bcda069007b7b51547ac16951c3c251547ac1632a0000007bcd9f6900c3c251567ac1",
25                 },
26                 {
27                         "./LoanCollateral",
28                         "567a64650000007bcda06900c3537ac2547a5100597989587a89577a89557a89537a8901747e2a557a641f0000007bcda069007b7b51547ac16951c3c251547ac1632a0000007bcd9f6900c3c251567ac189008901c07ec16951c3c251547ac163700000007bcd9f6900c3c251577ac1",
29                 },
30                 {
31                         "./FixedLimitCollect",
32                         "597a642f0200005479cda069c35b797ca153795579a19a695a790400e1f5059653790400e1f505967c00a07c00a09a69c35b797c9f9161644d010000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec169515b79c2515d79c16952c35c7994c251005d79895c79895b79895a79895979895879895779895679895579890274787e008901c07ec169632a020000005b79c2547951005e79895d79895c79895b7989597989587989537a894caa587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac890274787e008901c07ec16951c3c2515d79c169633b020000547acd9f69587a587aae7cac",
33                 },
34                 {
35                         "./FixedLimitProfit",
36                         "587a649e0000005479cd9f6959790400e1f5059653790400e1f505967800a07800a09a5c7956799f9a6955797b957c96c37800a052797ba19a69c3787c9f91616487000000005b795479515b79c1695178c2515d79c16952c3527994c251005d79895c79895b79895a79895979895879895779895679890274787e008901c07ec1696399000000005b795479515b79c16951c3c2515d79c16963aa000000557acd9f69577a577aae7cac",
37                 },
38         }
39
40         for _, c := range cases {
41                 contractName := filepath.Base(c.pathFile)
42                 t.Run(contractName, func(t *testing.T) {
43                         absPathFile, err := filepath.Abs(c.pathFile)
44                         if err != nil {
45                                 t.Fatal(err)
46                         }
47
48                         if _, err := os.Stat(absPathFile); err != nil {
49                                 t.Fatal(err)
50                         }
51
52                         inputFile, err := os.Open(absPathFile)
53                         if err != nil {
54                                 t.Fatal(err)
55                         }
56                         defer inputFile.Close()
57
58                         inputReader := bufio.NewReader(inputFile)
59                         contracts, err := compiler.Compile(inputReader)
60                         if err != nil {
61                                 t.Fatal(err)
62                         }
63
64                         contract := contracts[len(contracts)-1]
65                         got := hex.EncodeToString(contract.Body)
66                         if got != c.want {
67                                 t.Errorf("%s got %s\nwant %s", contractName, got, c.want)
68                         }
69                 })
70         }
71 }