1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
// Copyright (C) 2019-2021 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand. If not, see <https://www.gnu.org/licenses/>.
package abi
import (
"testing"
"github.com/algorand/go-algorand/test/partitiontest"
"github.com/stretchr/testify/require"
)
func TestJSONtoInterfaceValid(t *testing.T) {
partitiontest.PartitionTest(t)
var testCases = []struct {
input string
typeStr string
expected interface{}
}{
{
input: `[true, [0, 1, 2], 17]`,
typeStr: `(bool,byte[],uint64)`,
expected: []interface{}{
true,
[]interface{}{byte(0), byte(1), byte(2)},
uint64(17),
},
},
{
input: `[true, "AAEC", 17]`,
typeStr: `(bool,byte[],uint64)`,
expected: []interface{}{
true,
[]interface{}{byte(0), byte(1), byte(2)},
uint64(17),
},
},
{
input: `"AQEEBQEE"`,
typeStr: `byte[6]`,
expected: []interface{}{byte(1), byte(1), byte(4), byte(5), byte(1), byte(4)},
},
{
input: `[[0, [true, false], "utf-8"], [18446744073709551615, [false, true], "pistachio"]]`,
typeStr: `(uint64,bool[2],string)[]`,
expected: []interface{}{
[]interface{}{uint64(0), []interface{}{true, false}, "utf-8"},
[]interface{}{^uint64(0), []interface{}{false, true}, "pistachio"},
},
},
{
input: `[]`,
typeStr: `(uint64,bool[2],string)[]`,
expected: []interface{}{},
},
{
input: "[]",
typeStr: "()",
expected: []interface{}{},
},
{
input: "[65, 66, 67]",
typeStr: "string",
expected: "ABC",
},
{
input: "[]",
typeStr: "string",
expected: "",
},
{
input: "123.456",
typeStr: "ufixed64x3",
expected: uint64(123456),
},
{
input: `"optin"`,
typeStr: "string",
expected: "optin",
},
{
input: `"AAEC"`,
typeStr: "byte[3]",
expected: []interface{}{byte(0), byte(1), byte(2)},
},
{
input: `["uwu",["AAEC",12.34]]`,
typeStr: "(string,(byte[3],ufixed64x3))",
expected: []interface{}{"uwu", []interface{}{[]interface{}{byte(0), byte(1), byte(2)}, uint64(12340)}},
},
{
input: `[399,"should pass",[true,false,false,true]]`,
typeStr: "(uint64,string,bool[])",
expected: []interface{}{uint64(399), "should pass", []interface{}{true, false, false, true}},
},
}
for _, testCase := range testCases {
abiT, err := TypeOf(testCase.typeStr)
require.NoError(t, err, "fail to construct ABI type (%s): %v", testCase.typeStr, err)
res, err := abiT.UnmarshalFromJSON([]byte(testCase.input))
require.NoError(t, err, "fail to unmarshal JSON to interface: (%s): %v", testCase.input, err)
require.Equal(t, testCase.expected, res, "%v not matching with expected value %v", res, testCase.expected)
resEncoded, err := abiT.Encode(res)
require.NoError(t, err, "fail to encode %v to ABI bytes: %v", res, err)
resDecoded, err := abiT.Decode(resEncoded)
require.NoError(t, err, "fail to decode ABI bytes of %v: %v", res, err)
require.Equal(t, res, resDecoded, "ABI encode-decode round trip: %v not match with expected %v", resDecoded, res)
}
}
|