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
|
package service
import (
"encoding/json"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestDurationMarshal(t *testing.T) {
tests := []struct {
arg Duration
exp string
}{
{
Duration{time.Second},
"\"1s\"",
},
{
Duration{time.Minute},
"\"1m0s\"",
},
}
for _, test := range tests {
v, err := json.Marshal(test.arg)
require.NoError(t, err)
require.Equal(t, test.exp, string(v))
}
}
func TestDurationUnmarshal(t *testing.T) {
tests := []struct {
arg string
exp Duration
}{
{
"\"1s\"",
Duration{time.Second},
},
{
"\"1m0s\"",
Duration{time.Minute},
},
}
for _, test := range tests {
var v Duration
err := json.Unmarshal([]byte(test.arg), &v)
require.NoError(t, err)
require.Equal(t, test.exp.Duration, v.Duration)
}
}
|