github.com/aeternity/aepp-sdk-go/v4@v4.0.1/cmd/contract_test.go (about) 1 package cmd 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/spf13/cobra" 10 ) 11 12 const contractSimpleStorage = "contract SimpleStorage =\n record state = { data : int }\n function init(value : int) : state = { data = value }\n function get() : int = state.data\n stateful function set(value : int) = put(state{data = value})" 13 const contractSimpleStorageBytecode = "cb_+QYYRgKg+HOI9x+n5+MOEpnQ/zO+GoibqhQxGO4bgnvASx0vzB75BKX5AUmgOoWULXtHOgf10E7h2cFqXOqxa3kc6pKJYRpEw/nlugeDc2V0uMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAoP//////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC4YAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///////////////////////////////////////////jJoEnsSQdsAgNxJqQzA+rc5DsuLDKUV7ETxQp+ItyJgJS3g2dldLhgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA///////////////////////////////////////////uEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA+QKLoOIjHWzfyTkW3kyzqYV79lz0D8JW9KFJiz9+fJgMGZNEhGluaXS4wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACg//////////////////////////////////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALkBoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEA//////////////////////////////////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYD//////////////////////////////////////////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAuQFEYgAAj2IAAMKRgICAUX9J7EkHbAIDcSakMwPq3OQ7LiwylFexE8UKfiLciYCUtxRiAAE5V1CAgFF/4iMdbN/JORbeTLOphXv2XPQPwlb0oUmLP358mAwZk0QUYgAA0VdQgFF/OoWULXtHOgf10E7h2cFqXOqxa3kc6pKJYRpEw/nlugcUYgABG1dQYAEZUQBbYAAZWWAgAZCBUmAgkANgAFmQgVKBUllgIAGQgVJgIJADYAOBUpBZYABRWVJgAFJgAPNbYACAUmAA81tgAFFRkFZbYCABUVGQUIOSUICRUFCAWZCBUllgIAGQgVJgIJADYAAZWWAgAZCBUmAgkANgAFmQgVKBUllgIAGQgVJgIJADYAOBUoFSkFCQVltgIAFRUVlQgJFQUGAAUYFZkIFSkFBgAFJZkFCQVltQUFlQUGIAAMpWhTMuMS4wHchc+w==" 14 const contractSimpleStorageInitCalldata = "cb_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACDiIx1s38k5Ft5Ms6mFe/Zc9A/CVvShSYs/fnyYDBmTRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACo7j+li" 15 const contractSimpleStorageErr = "contract SimpleStorage =\n record state = { data : int }\n function init(value : int) : state = { data = value }\n function get() : int = state.data\n function set(value : int) = put(state{data = value})" 16 17 func writeTestContractFile(source string) (tempdir string, path string, err error) { 18 tempdir, err = ioutil.TempDir("", "aepp-sdk-go") 19 if err != nil { 20 return "", "", err 21 } 22 path = filepath.Join(tempdir, "testcontract.aes") 23 err = ioutil.WriteFile(path, []byte(source), 0666) 24 if err != nil { 25 return "", "", err 26 } 27 28 return 29 } 30 31 func Test_compileFunc(t *testing.T) { 32 emptyCmd := cobra.Command{} 33 34 type args struct { 35 cmd *cobra.Command 36 source string 37 } 38 tests := []struct { 39 name string 40 args args 41 wantErr bool 42 }{ 43 { 44 name: "Simple storage should compile", 45 args: args{ 46 cmd: &emptyCmd, 47 source: contractSimpleStorage, 48 }, 49 wantErr: false, 50 }, 51 { 52 name: "Simple storage with syntax error", 53 args: args{ 54 cmd: &emptyCmd, 55 source: contractSimpleStorageErr, 56 }, 57 wantErr: true, 58 }, 59 } 60 for _, tt := range tests { 61 tempdir, path, err := writeTestContractFile(tt.args.source) 62 if err != nil { 63 t.Fatal(err) 64 } 65 defer os.RemoveAll(tempdir) 66 67 t.Run(tt.name, func(t *testing.T) { 68 if err := compileFunc(tt.args.cmd, []string{path}); (err != nil) != tt.wantErr { 69 t.Errorf("compileFunc() error = %v, wantErr %v", err, tt.wantErr) 70 } 71 }) 72 } 73 } 74 75 func Test_encodeCalldataFunc(t *testing.T) { 76 emptyCmd := cobra.Command{} 77 78 type args struct { 79 cmd *cobra.Command 80 args []string 81 source string 82 } 83 tests := []struct { 84 name string 85 args args 86 wantErr bool 87 }{ 88 { 89 name: "1 function argument", 90 args: args{ 91 cmd: &emptyCmd, 92 args: []string{"init", "42"}, 93 source: contractSimpleStorage, 94 }, 95 }, 96 } 97 for _, tt := range tests { 98 tempdir, path, err := writeTestContractFile(tt.args.source) 99 if err != nil { 100 t.Fatal(err) 101 } 102 defer os.RemoveAll(tempdir) 103 104 t.Run(tt.name, func(t *testing.T) { 105 a := append([]string{path}, tt.args.args...) 106 if err := encodeCalldataFunc(tt.args.cmd, a); (err != nil) != tt.wantErr { 107 t.Errorf("encodeCalldataFunc() error = %v, wantErr %v", err, tt.wantErr) 108 } 109 }) 110 } 111 } 112 113 func Test_decodeCalldataFunc(t *testing.T) { 114 type args struct { 115 cmd *cobra.Command 116 args []string 117 } 118 // Write source file for Decode with source file test 119 tempdir, path, err := writeTestContractFile(contractSimpleStorage) 120 if err != nil { 121 t.Fatal(err) 122 } 123 defer os.RemoveAll(tempdir) 124 125 tests := []struct { 126 name string 127 args args 128 wantErr bool 129 }{ 130 { 131 name: "Decode with bytecode", 132 args: args{ 133 cmd: &cobra.Command{}, 134 args: []string{contractSimpleStorageBytecode, contractSimpleStorageInitCalldata}, 135 }, 136 wantErr: false, 137 }, 138 { 139 name: "Decode with source file", 140 args: args{ 141 cmd: &cobra.Command{}, 142 args: []string{path, contractSimpleStorageInitCalldata}, 143 }, 144 wantErr: false, 145 }, 146 } 147 148 for _, tt := range tests { 149 t.Run(tt.name, func(t *testing.T) { 150 if err := decodeCalldataFunc(tt.args.cmd, tt.args.args); (err != nil) != tt.wantErr { 151 t.Errorf("decodeCalldataFunc() error = %v, wantErr %v", err, tt.wantErr) 152 } 153 }) 154 } 155 }