generated from xmidt-org/.go-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcustomDuration_test.go
94 lines (88 loc) · 2.04 KB
/
customDuration_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
// SPDX-FileCopyrightText: 2022 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package ancla
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestUnmarshalJSON(t *testing.T) {
type test struct {
Duration CustomDuration
}
tests := []struct {
description string
input []byte
expectedDuration CustomDuration
errExpected bool
}{
{
description: "Int success",
input: []byte(`{"duration":50}`),
expectedDuration: CustomDuration(50 * time.Second),
},
{
description: "String success",
input: []byte(`{"duration":"5m"}`),
expectedDuration: CustomDuration(5 * time.Minute),
},
{
description: "String failure",
input: []byte(`{"duration":"2r"}`),
errExpected: true,
},
{
description: "Object failure",
input: []byte(`{"duration":{"key":"val"}}`),
errExpected: true,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
cd := test{}
err := json.Unmarshal(tc.input, &cd)
assert.Equal(tc.expectedDuration, cd.Duration)
if !tc.errExpected {
assert.NoError(err)
return
}
assert.Error(err)
})
}
}
func TestMarshalJSON(t *testing.T) {
type test struct {
Duration CustomDuration
}
tests := []struct {
description string
input test
expectedOutput []byte
errExpected bool
}{
{
description: "Int success",
input: test{Duration: CustomDuration(50 * time.Second)},
expectedOutput: []byte(`{"Duration":"50s"}`),
},
{
description: "String success",
input: test{Duration: CustomDuration(5 * time.Minute)},
expectedOutput: []byte(`{"Duration":"5m0s"}`),
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
assert := assert.New(t)
output, err := json.Marshal(tc.input)
assert.Equal(tc.expectedOutput, output)
if !tc.errExpected {
assert.NoError(err)
return
}
assert.Error(err)
})
}
}