-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
94 lines (83 loc) · 1.51 KB
/
types_test.go
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
package jsonrpc
import (
"bytes"
"encoding/json"
"math/big"
"reflect"
"strings"
"testing"
"text/template"
"github.com/stretchr/testify/assert"
"github.com/umbracle/ethgo"
)
func TestBasicTypes_Encode(t *testing.T) {
// decode basic types
cases := []struct {
obj interface{}
dec interface{}
res string
}{
{
argBig(*big.NewInt(10)),
&argBig{},
"0xa",
},
{
argUint64(10),
argUintPtr(0),
"0xa",
},
{
argBytes([]byte{0x1, 0x2}),
&argBytes{},
"0x0102",
},
}
for _, c := range cases {
res, err := json.Marshal(c.obj)
assert.NoError(t, err)
assert.Equal(t, strings.Trim(string(res), "\""), c.res)
assert.NoError(t, json.Unmarshal(res, c.dec))
}
}
func TestDecode_TxnArgs(t *testing.T) {
var (
addr = ethgo.Address{0x0}
num = argUint64(16)
hex = argBytes([]byte{0x01})
)
cases := []struct {
data string
res *txnArgs
}{
{
data: `{
"to": "{{.Addr}}",
"gas": "0x10",
"input": "0x01",
"value": "0x01"
}`,
res: &txnArgs{
To: &addr,
Gas: &num,
Input: &hex,
Value: &hex,
},
},
}
for _, c := range cases {
tmpl, err := template.New("test").Parse(c.data)
assert.NoError(t, err)
config := map[string]string{
"Addr": (ethgo.Address{}).String(),
"Hash": (ethgo.Hash{}).String(),
}
buffer := new(bytes.Buffer)
assert.NoError(t, tmpl.Execute(buffer, config))
r := &txnArgs{}
assert.NoError(t, json.Unmarshal(buffer.Bytes(), &r))
if !reflect.DeepEqual(r, c.res) {
t.Fatal("bad")
}
}
}