1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package strfmt
16
17 import (
18 "testing"
19 "time"
20
21 "github.com/stretchr/testify/assert"
22 )
23
24 func TestDuration(t *testing.T) {
25 pp := Duration(0)
26
27 err := pp.UnmarshalText([]byte("0ms"))
28 assert.NoError(t, err)
29 err = pp.UnmarshalText([]byte("yada"))
30 assert.Error(t, err)
31
32 orig := "2ms"
33 b := []byte(orig)
34 bj := []byte("\"" + orig + "\"")
35
36 err = pp.UnmarshalText(b)
37 assert.NoError(t, err)
38
39 err = pp.UnmarshalText([]byte("three week"))
40 assert.Error(t, err)
41
42 err = pp.UnmarshalText([]byte("9999999999999999999999999999999999999999999999999999999 weeks"))
43 assert.Error(t, err)
44
45 txt, err := pp.MarshalText()
46 assert.NoError(t, err)
47 assert.Equal(t, orig, string(txt))
48
49 err = pp.UnmarshalJSON(bj)
50 assert.NoError(t, err)
51 assert.EqualValues(t, orig, pp.String())
52
53 err = pp.UnmarshalJSON([]byte("yada"))
54 assert.Error(t, err)
55
56 err = pp.UnmarshalJSON([]byte(`"12 parsecs"`))
57 assert.Error(t, err)
58
59 err = pp.UnmarshalJSON([]byte(`"12 y"`))
60 assert.Error(t, err)
61
62 b, err = pp.MarshalJSON()
63 assert.NoError(t, err)
64 assert.Equal(t, bj, b)
65 }
66
67 func testDurationParser(t *testing.T, toParse string, expected time.Duration) {
68 r, e := ParseDuration(toParse)
69 assert.NoError(t, e)
70 assert.Equal(t, expected, r)
71 }
72
73 func TestDurationParser_Failed(t *testing.T) {
74 _, e := ParseDuration("45 wekk")
75 assert.Error(t, e)
76 }
77
78 func TestIsDuration_Failed(t *testing.T) {
79 e := IsDuration("45 weeekks")
80 assert.False(t, e)
81 }
82
83 func TestDurationParser(t *testing.T) {
84 testcases := map[string]time.Duration{
85
86
87 "1ns": 1 * time.Nanosecond,
88 "1us": 1 * time.Microsecond,
89 "1µs": 1 * time.Microsecond,
90 "1ms": 1 * time.Millisecond,
91 "1s": 1 * time.Second,
92 "1m": 1 * time.Minute,
93 "1h": 1 * time.Hour,
94 "1hr": 1 * time.Hour,
95 "1d": 24 * time.Hour,
96 "1w": 7 * 24 * time.Hour,
97 "1wk": 7 * 24 * time.Hour,
98
99
100 "1nanoseconds": 1 * time.Nanosecond,
101 "1nanos": 1 * time.Nanosecond,
102 "1microseconds": 1 * time.Microsecond,
103 "1micros": 1 * time.Microsecond,
104 "1millis": 1 * time.Millisecond,
105 "1milliseconds": 1 * time.Millisecond,
106 "1second": 1 * time.Second,
107 "1sec": 1 * time.Second,
108 "1min": 1 * time.Minute,
109 "1minute": 1 * time.Minute,
110 "1hour": 1 * time.Hour,
111 "1day": 24 * time.Hour,
112 "1week": 7 * 24 * time.Hour,
113
114
115 "1 ns": 1 * time.Nanosecond,
116 "1 us": 1 * time.Microsecond,
117 "1 µs": 1 * time.Microsecond,
118 "1 ms": 1 * time.Millisecond,
119 "1 s": 1 * time.Second,
120 "1 m": 1 * time.Minute,
121 "1 h": 1 * time.Hour,
122 "1 hr": 1 * time.Hour,
123 "1 d": 24 * time.Hour,
124 "1 w": 7 * 24 * time.Hour,
125 "1 wk": 7 * 24 * time.Hour,
126
127
128 "1 nanoseconds": 1 * time.Nanosecond,
129 "1 nanos": 1 * time.Nanosecond,
130 "1 microseconds": 1 * time.Microsecond,
131 "1 micros": 1 * time.Microsecond,
132 "1 millis": 1 * time.Millisecond,
133 "1 milliseconds": 1 * time.Millisecond,
134 "1 second": 1 * time.Second,
135 "1 sec": 1 * time.Second,
136 "1 min": 1 * time.Minute,
137 "1 minute": 1 * time.Minute,
138 "1 hour": 1 * time.Hour,
139 "1 day": 24 * time.Hour,
140 "1 week": 7 * 24 * time.Hour,
141 }
142
143 for str, dur := range testcases {
144 testDurationParser(t, str, dur)
145 }
146 }
147 func TestIsDuration_Caveats(t *testing.T) {
148
149 e := IsDuration("45 weeks")
150 assert.True(t, e)
151
152
153 e = IsDuration("45 weekz")
154 assert.True(t, e)
155
156
157 e = IsDuration("12 hours")
158 assert.True(t, e)
159
160
161 e = IsDuration("12 minutes")
162 assert.True(t, e)
163
164
165 e = IsDuration("12 phours")
166 assert.False(t, e)
167
168 }
169
170 func TestDeepCopyDuration(t *testing.T) {
171 dur := Duration(42)
172 in := &dur
173
174 out := new(Duration)
175 in.DeepCopyInto(out)
176 assert.Equal(t, in, out)
177
178 out2 := in.DeepCopy()
179 assert.Equal(t, in, out2)
180
181 var inNil *Duration
182 out3 := inNil.DeepCopy()
183 assert.Nil(t, out3)
184 }
185
View as plain text