-
Notifications
You must be signed in to change notification settings - Fork 6
/
hook_game_test.go
134 lines (113 loc) · 2.79 KB
/
hook_game_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
126
127
128
129
130
131
132
133
134
package itchio
import (
"encoding/json"
"testing"
"time"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
)
func Test_GameHook(t *testing.T) {
ref := Game{
ID: 123,
InPressSystem: true,
Platforms: Platforms{
Linux: ArchitecturesAll,
Windows: ArchitecturesAll,
},
}
marshalledTraits := []byte(`{
"id": 123,
"traits": ["in_press_system", "p_linux", "p_windows"]
}`)
marshalledSane := []byte(`{
"id": 123,
"inPressSystem": true,
"platforms": {"linux": "all", "windows": "all"}
}`)
{
intermediateTraits := make(map[string]interface{})
err := json.Unmarshal(marshalledTraits, &intermediateTraits)
assert.NoError(t, err)
var decodedTraits Game
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
DecodeHook: GameHookFunc,
WeaklyTypedInput: true,
Result: &decodedTraits,
})
assert.NoError(t, err)
err = dec.Decode(intermediateTraits)
assert.NoError(t, err)
assert.EqualValues(t, ref, decodedTraits)
}
{
intermediateSane := make(map[string]interface{})
err := json.Unmarshal(marshalledSane, &intermediateSane)
assert.NoError(t, err)
var decodedSane Game
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
DecodeHook: GameHookFunc,
WeaklyTypedInput: true,
Result: &decodedSane,
})
assert.NoError(t, err)
err = dec.Decode(intermediateSane)
assert.NoError(t, err)
assert.EqualValues(t, ref, decodedSane)
}
// -------------
bs, err := json.Marshal(ref)
assert.NoError(t, err)
var unmarshalled Game
err = json.Unmarshal(bs, &unmarshalled)
assert.NoError(t, err)
assert.EqualValues(t, ref, unmarshalled)
// ------------
}
func Test_GameHookNested(t *testing.T) {
type Res struct {
Games []*Game `json:"games"`
}
marshalledSane := []byte(`{"games": [{
"id": 123,
"inPressSystem": true,
"platforms": {"linux": "all", "windows": "all"}
}]}`)
intermediate := make(map[string]interface{})
err := json.Unmarshal(marshalledSane, &intermediate)
tmust(t, err)
var res Res
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
Result: &res,
DecodeHook: mapstructure.ComposeDecodeHookFunc(
mapstructure.StringToTimeHookFunc(time.RFC3339Nano),
GameHookFunc,
),
WeaklyTypedInput: true,
})
tmust(t, err)
err = dec.Decode(intermediate)
tmust(t, err)
assert.EqualValues(t, Res{
Games: []*Game{
&Game{
ID: 123,
InPressSystem: true,
Platforms: Platforms{
Linux: "all",
Windows: "all",
},
},
},
}, res)
}
// tmust shows a complete error stack and fails a test immediately
// if err is non-nil
func tmust(t *testing.T, err error) {
if err != nil {
t.Helper()
t.Errorf("%+v", err)
t.FailNow()
}
}