forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_test.go
125 lines (103 loc) · 2.65 KB
/
json_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
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
124
125
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
type testJSON struct {
S []string `json:"stringSlice"`
I int `json:"intValue"`
}
type testJSONInvalid struct {
I []string `json:"intValue"`
S int `json:"stringSlice"`
}
func Test_marshalJSON(t *testing.T) {
t.Run("Successful JSON marshaling", func(t *testing.T) {
v := testJSON{
S: []string{"a", "b", "c"},
I: 7,
}
cf := map[string]interface{}{
"boolValue": false,
"intValue": 8,
}
actual, err := marshalWithCustomFields(&v, cf)
require.NoError(t, err)
expectedMap := map[string]interface{}{
"stringSlice": []string{"a", "b", "c"},
"intValue": 7,
"boolValue": false,
}
expected, err := json.Marshal(expectedMap)
require.NoError(t, err)
require.Equal(t, expected, actual)
})
t.Run("Failed JSON marshall", func(t *testing.T) {
// artificial example - pass smth which cannot be marshalled
jsonBytes, err := marshalWithCustomFields(make(chan int), map[string]interface{}{})
require.Error(t, err)
require.Nil(t, jsonBytes)
})
}
func Test_unmarshalJSON(t *testing.T) {
originalMap := map[string]interface{}{
"stringSlice": []string{"a", "b", "c"},
"intValue": 7,
"boolValue": false,
}
data, err := json.Marshal(originalMap)
require.NoError(t, err)
t.Run("Successful JSON unmarshalling", func(t *testing.T) {
v := new(testJSON)
cf := make(map[string]interface{})
err := unmarshalWithCustomFields(data, v, cf)
require.NoError(t, err)
expectedV := testJSON{
S: []string{"a", "b", "c"},
I: 7,
}
expectedEf := map[string]interface{}{
"boolValue": false,
}
require.Equal(t, expectedV, *v)
require.Equal(t, expectedEf, cf)
})
t.Run("Failed JSON unmarshalling", func(t *testing.T) {
cf := make(map[string]interface{})
// invalid JSON
err := unmarshalWithCustomFields([]byte("not JSON"), "", cf)
require.Error(t, err)
// unmarshallable value
err = unmarshalWithCustomFields(data, make(chan int), cf)
require.Error(t, err)
// incompatible structure of value
err = unmarshalWithCustomFields(data, new(testJSONInvalid), cf)
require.Error(t, err)
})
}
func Test_toMaps(t *testing.T) {
v := []interface{}{
struct {
S string
I int
}{"12", 5},
map[string]interface{}{
"a": "b",
},
}
maps, err := toMaps(v)
require.NoError(t, err)
require.Len(t, maps, 2)
require.Equal(t, []map[string]interface{}{
{"S": "12", "I": 5.},
{"a": "b"},
}, maps)
maps, err = toMaps([]interface{}{make(chan int)})
require.Error(t, err)
require.Empty(t, maps)
}