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
|
// SPDX-License-Identifier: GPL-3.0-or-later
package web
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestDuration_MarshalYAML(t *testing.T) {
tests := map[string]struct {
d Duration
want string
}{
"1 second": {d: Duration(time.Second), want: "1"},
"1.5 seconds": {d: Duration(time.Second + time.Millisecond*500), want: "1.5"},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
bs, err := yaml.Marshal(&test.d)
require.NoError(t, err)
assert.Equal(t, test.want, strings.TrimSpace(string(bs)))
})
}
}
func TestDuration_MarshalJSON(t *testing.T) {
tests := map[string]struct {
d Duration
want string
}{
"1 second": {d: Duration(time.Second), want: "1"},
"1.5 seconds": {d: Duration(time.Second + time.Millisecond*500), want: "1.5"},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
bs, err := json.Marshal(&test.d)
require.NoError(t, err)
assert.Equal(t, test.want, strings.TrimSpace(string(bs)))
})
}
}
func TestDuration_UnmarshalYAML(t *testing.T) {
tests := map[string]struct {
input any
}{
"duration": {input: "300ms"},
"string int": {input: "1"},
"string float": {input: "1.1"},
"int": {input: 2},
"float": {input: 2.2},
}
var zero Duration
for name, test := range tests {
name = fmt.Sprintf("%s (%v)", name, test.input)
t.Run(name, func(t *testing.T) {
data, err := yaml.Marshal(test.input)
require.NoError(t, err)
var d Duration
require.NoError(t, yaml.Unmarshal(data, &d))
assert.NotEqual(t, zero.String(), d.String())
})
}
}
func TestDuration_UnmarshalJSON(t *testing.T) {
tests := map[string]struct {
input any
}{
"duration": {input: "300ms"},
"string int": {input: "1"},
"string float": {input: "1.1"},
"int": {input: 2},
"float": {input: 2.2},
}
var zero Duration
type duration struct {
D Duration `json:"d"`
}
type input struct {
D any `json:"d"`
}
for name, test := range tests {
name = fmt.Sprintf("%s (%v)", name, test.input)
t.Run(name, func(t *testing.T) {
input := input{D: test.input}
data, err := yaml.Marshal(input)
require.NoError(t, err)
var d duration
require.NoError(t, yaml.Unmarshal(data, &d))
assert.NotEqual(t, zero.String(), d.D.String())
})
}
}
|