github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/equity/compiler/compile_test.go (about) 1 package compiler 2 3 import ( 4 "encoding/hex" 5 "strings" 6 "testing" 7 8 chainjson "github.com/bytom/bytom/encoding/json" 9 10 "github.com/bytom/bytom/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 "TestAssignVar", 71 equitytest.TestAssignVar, 72 "7b7b9387", 73 }, 74 { 75 "TestSigIf", 76 equitytest.TestSigIf, 77 "53797b879169765379a09161641c00000052795279a0696321000000765279a069", 78 }, 79 { 80 "TestIfAndMultiClause", 81 equitytest.TestIfAndMultiClause, 82 "7b641f0000007087916976547aa09161641a000000765379a06963240000007b7bae7cac", 83 }, 84 { 85 "TestIfNesting", 86 equitytest.TestIfNesting, 87 "7b644400000054795279879169765579a09161643500000052795479a091616429000000765379a06952795579879169633a000000765479a06953797b8791635c0000007654798791695279a091616459000000527978a0697d8791", 88 }, 89 { 90 "TestConstantMath", 91 equitytest.TestConstantMath, 92 "765779577a935a93887c0431323330aa887c06737472696e67aa887c91697b011493879a", 93 }, 94 } 95 for _, c := range cases { 96 t.Run(c.name, func(t *testing.T) { 97 r := strings.NewReader(c.contract) 98 compiled, err := Compile(r) 99 if err != nil { 100 t.Fatal(err) 101 } 102 103 contract := compiled[len(compiled)-1] 104 got := []byte(contract.Body) 105 106 want, err := hex.DecodeString(c.want) 107 if err != nil { 108 t.Fatal(err) 109 } 110 111 if string(got) != string(want) { 112 t.Errorf("%s got %s\nwant %s", c.name, hex.EncodeToString(got), hex.EncodeToString(want)) 113 } 114 }) 115 } 116 } 117 118 func mustDecodeHex(h string) *chainjson.HexBytes { 119 bits, err := hex.DecodeString(h) 120 if err != nil { 121 panic(err) 122 } 123 result := chainjson.HexBytes(bits) 124 return &result 125 }