forked from 0xPolygonID/c-polygonid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
87 lines (76 loc) · 1.93 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
package c_polygonid
import (
"encoding/json"
"math/big"
"testing"
"github.com/stretchr/testify/require"
)
func TestJsonNumber_UnmarshalJSON(t *testing.T) {
in := `123`
var jn = new(big.Int)
err := ((*jsonNumber)(jn)).UnmarshalJSON([]byte(in))
require.NoError(t, err)
require.Equal(t, "123", jn.String())
var jn2 struct {
Jn *jsonNumber
}
in2 := `{"jn":123}`
err = json.Unmarshal([]byte(in2), &jn2)
require.NoError(t, err)
require.Equal(t, "123", ((*big.Int)(jn2.Jn)).String())
var jn3 struct {
Jn *jsonNumber
}
in3 := `{"jn":"123"}`
err = json.Unmarshal([]byte(in3), &jn3)
require.NoError(t, err)
require.Equal(t, "123", ((*big.Int)(jn3.Jn)).String())
var jn4 struct {
Jn *jsonNumber
}
in4 := `{"jn":"0x123"}`
err = json.Unmarshal([]byte(in4), &jn4)
require.NoError(t, err)
require.Equal(t, "291", ((*big.Int)(jn4.Jn)).String())
var jn5 struct {
Jn *jsonNumber
}
in5 := `{"jn":"0X123"}`
err = json.Unmarshal([]byte(in5), &jn5)
require.NoError(t, err)
require.Equal(t, "291", ((*big.Int)(jn5.Jn)).String())
in6 := `123.4`
var jn6 = new(big.Int)
err = json.Unmarshal([]byte(in6), (*jsonNumber)(jn6))
require.EqualError(t, err, "invalid integer number format")
var jn7 = new(big.Int)
err = ((*jsonNumber)(jn7)).UnmarshalJSON(nil)
require.EqualError(t, err, "empty input")
}
func TestJsonByte_UnmarshalJSON(t *testing.T) {
testCases := []struct {
in []byte
want byte
err string
}{
{[]byte(`123`), 123, ""},
{[]byte(`"123"`), 123, ""},
{[]byte(`"0xab"`), 171, ""},
{[]byte(`"0xa"`), 10, ""},
{[]byte(`"0xabc"`), 172,
"strconv.ParseUint: parsing \"abc\": value out of range"},
{[]byte(`"0B101011"`), 43, ""},
}
for _, tc := range testCases {
t.Run(string(tc.in), func(t *testing.T) {
var jb jsonByte
err := json.Unmarshal(tc.in, &jb)
if tc.err != "" {
require.EqualError(t, err, tc.err)
return
}
require.NoError(t, err)
require.Equal(t, jsonByte(tc.want), jb)
})
}
}