| 1 | package duration |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestParse(t *testing.T) { |
| 12 | type args struct { |
| 13 | d string |
| 14 | } |
| 15 | var ( |
| 16 | noError = func(e error) bool { return e == nil } |
| 17 | newMatchFn = func(expected error) func(e error) bool { |
| 18 | return func(e error) bool { |
| 19 | return errors.Is(e, expected) |
| 20 | } |
| 21 | } |
| 22 | ) |
| 23 | |
| 24 | tests := []struct { |
| 25 | name string |
| 26 | args args |
| 27 | want *Duration |
| 28 | errorMatchFn func(e error) bool |
| 29 | }{ |
| 30 | { |
| 31 | name: "invalid-duration-1", |
| 32 | args: args{d: "T0S"}, |
| 33 | want: nil, |
| 34 | errorMatchFn: newMatchFn(ErrUnexpectedInput), |
| 35 | }, |
| 36 | { |
| 37 | name: "invalid-duration-2", |
| 38 | args: args{d: "P-T0S"}, |
| 39 | want: nil, |
| 40 | errorMatchFn: newMatchFn(ErrUnexpectedInput), |
| 41 | }, |
| 42 | { |
| 43 | name: "invalid-duration-3", |
| 44 | args: args{d: "PT0SP0D"}, |
| 45 | want: nil, |
| 46 | errorMatchFn: newMatchFn(ErrUnexpectedInput), |
| 47 | }, |
| 48 | { |
| 49 | name: "period-only", |
| 50 | args: args{d: "P4Y"}, |
| 51 | want: &Duration{ |
| 52 | Years: 4, |
| 53 | }, |
| 54 | errorMatchFn: noError, |
| 55 | }, |
| 56 | { |
| 57 | name: "time-only-decimal", |
| 58 | args: args{d: "PT2.5S"}, |
| 59 | want: &Duration{ |
| 60 | Seconds: 2.5, |
| 61 | }, |
| 62 | errorMatchFn: noError, |
| 63 | }, |
| 64 | { |
| 65 | name: "full", |
| 66 | args: args{d: "P3Y6M4DT12H30M5.5S"}, |
| 67 | want: &Duration{ |
| 68 | Years: 3, |
| 69 | Months: 6, |
| 70 | Days: 4, |
| 71 | Hours: 12, |
| 72 | Minutes: 30, |
| 73 | Seconds: 5.5, |
| 74 | }, |
| 75 | errorMatchFn: noError, |
| 76 | }, |
| 77 | { |
| 78 | name: "negative", |
| 79 | args: args{d: "-PT5M"}, |
| 80 | want: &Duration{ |
| 81 | Minutes: 5, |
| 82 | Negative: true, |
| 83 | }, |
| 84 | errorMatchFn: noError, |
| 85 | }, |
| 86 | { |
| 87 | name: "no unit after prefix P", |
| 88 | args: args{d: "P6"}, |
| 89 | want: nil, |
| 90 | errorMatchFn: newMatchFn(ErrIncompleteExpr), |
| 91 | }, |
| 92 | { |
| 93 | name: "no unit after valid sub-prefix", |
| 94 | args: args{d: "P7Y4"}, |
| 95 | want: nil, |
| 96 | errorMatchFn: newMatchFn(ErrIncompleteExpr), |
| 97 | }, |
| 98 | } |
| 99 | for _, tt := range tests { |
| 100 | t.Run(tt.name, func(t *testing.T) { |
| 101 | got, err := Parse(tt.args.d) |
| 102 | if !tt.errorMatchFn(err) { |
| 103 | t.Errorf("error %q doesn't match the expected", err) |
| 104 | return |
| 105 | } |
| 106 | if !reflect.DeepEqual(got, tt.want) { |
| 107 | t.Errorf("Parse() got = %v, want %v", got, tt.want) |
| 108 | } |
| 109 | }) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | func TestFromTimeDuration(t *testing.T) { |
| 114 | tests := []struct { |
| 115 | give time.Duration |
| 116 | want *Duration |
| 117 | }{ |
| 118 | { |
| 119 | give: 0, |
| 120 | want: &Duration{}, |
| 121 | }, |
| 122 | { |
| 123 | give: time.Minute * 94, |
| 124 | want: &Duration{ |
| 125 | Hours: 1, |
| 126 | Minutes: 34, |
| 127 | }, |
| 128 | }, |
| 129 | { |
| 130 | give: -time.Second * 10, |
| 131 | want: &Duration{ |
| 132 | Seconds: 10, |
| 133 | Negative: true, |
| 134 | }, |
| 135 | }, |
| 136 | } |
| 137 | for _, tt := range tests { |
| 138 | t.Run(tt.give.String(), func(t *testing.T) { |
| 139 | got := FromTimeDuration(tt.give) |
| 140 | if !reflect.DeepEqual(got, tt.want) { |
| 141 | t.Errorf("Format() got = %s, want %s", got, tt.want) |
| 142 | } |
| 143 | }) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | func TestFormat(t *testing.T) { |
| 148 | tests := []struct { |
| 149 | give time.Duration |
| 150 | want string |
| 151 | }{ |
| 152 | { |
| 153 | give: 0, |
| 154 | want: "PT0S", |
| 155 | }, |
| 156 | { |
| 157 | give: time.Minute * 94, |
| 158 | want: "PT1H34M", |
| 159 | }, |
| 160 | { |
| 161 | give: time.Hour * 72, |
| 162 | want: "P3D", |
| 163 | }, |
| 164 | { |
| 165 | give: time.Hour * 26, |
| 166 | want: "P1DT2H", |
| 167 | }, |
| 168 | { |
| 169 | give: time.Second * 465461651, |
| 170 | want: "P14Y9M3DT12H54M11S", |
| 171 | }, |
| 172 | { |
| 173 | give: -time.Hour * 99544, |
| 174 | want: "-P11Y4M1W4D", |
| 175 | }, |
| 176 | { |
| 177 | give: -time.Second * 10, |
| 178 | want: "-PT10S", |
| 179 | }, |
| 180 | } |
| 181 | for _, tt := range tests { |
| 182 | t.Run(tt.want, func(t *testing.T) { |
| 183 | got := Format(tt.give) |
| 184 | if !reflect.DeepEqual(got, tt.want) { |
| 185 | t.Errorf("Format() got = %s, want %s", got, tt.want) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | func TestDuration_ToTimeDuration(t *testing.T) { |
| 192 | type fields struct { |
| 193 | Years float64 |
| 194 | Months float64 |
| 195 | Weeks float64 |
| 196 | Days float64 |
| 197 | Hours float64 |
| 198 | Minutes float64 |
| 199 | Seconds float64 |
| 200 | Negative bool |
| 201 | } |
| 202 | tests := []struct { |
| 203 | name string |
| 204 | fields fields |
| 205 | want time.Duration |
| 206 | }{ |
| 207 | { |
| 208 | name: "seconds", |
| 209 | fields: fields{ |
| 210 | Seconds: 33.3, |
| 211 | }, |
| 212 | want: time.Second*33 + time.Millisecond*300, |
| 213 | }, |
| 214 | { |
| 215 | name: "hours, minutes, and seconds", |
| 216 | fields: fields{ |
| 217 | Hours: 2, |
| 218 | Minutes: 33, |
| 219 | Seconds: 17, |
| 220 | }, |
| 221 | want: time.Hour*2 + time.Minute*33 + time.Second*17, |
| 222 | }, |
| 223 | { |
| 224 | name: "days", |
| 225 | fields: fields{ |
| 226 | Days: 2, |
| 227 | }, |
| 228 | want: time.Hour * 24 * 2, |
| 229 | }, |
| 230 | { |
| 231 | name: "weeks", |
| 232 | fields: fields{ |
| 233 | Weeks: 1, |
| 234 | }, |
| 235 | want: time.Hour * 24 * 7, |
| 236 | }, |
| 237 | { |
| 238 | name: "fractional weeks", |
| 239 | fields: fields{ |
| 240 | Weeks: 12.5, |
| 241 | }, |
| 242 | want: time.Hour*24*7*12 + time.Hour*84, |
| 243 | }, |
| 244 | { |
| 245 | name: "negative", |
| 246 | fields: fields{ |
| 247 | Hours: 2, |
| 248 | Negative: true, |
| 249 | }, |
| 250 | want: -time.Hour * 2, |
| 251 | }, |
| 252 | } |
| 253 | for _, tt := range tests { |
| 254 | t.Run(tt.name, func(t *testing.T) { |
| 255 | duration := &Duration{ |
| 256 | Years: tt.fields.Years, |
| 257 | Months: tt.fields.Months, |
| 258 | Weeks: tt.fields.Weeks, |
| 259 | Days: tt.fields.Days, |
| 260 | Hours: tt.fields.Hours, |
| 261 | Minutes: tt.fields.Minutes, |
| 262 | Seconds: tt.fields.Seconds, |
| 263 | Negative: tt.fields.Negative, |
| 264 | } |
| 265 | if got := duration.ToTimeDuration(); got != tt.want { |
| 266 | t.Errorf("ToTimeDuration() = %v, want %v", got, tt.want) |
| 267 | } |
| 268 | }) |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | func TestDuration_String(t *testing.T) { |
| 273 | duration, err := Parse("P3Y6M4DT12H30M5.5S") |
| 274 | if err != nil { |
| 275 | t.Fatal(err) |
| 276 | } |
| 277 | |
| 278 | if duration.String() != "P3Y6M4DT12H30M5.5S" { |
| 279 | t.Errorf("expected: %s, got: %s", "P3Y6M4DT12H30M5.5S", duration.String()) |
| 280 | } |
| 281 | |
| 282 | duration.Seconds = 33.3333 |
| 283 | |
| 284 | if duration.String() != "P3Y6M4DT12H30M33.3333S" { |
| 285 | t.Errorf("expected: %s, got: %s", "P3Y6M4DT12H30M33.3333S", duration.String()) |
| 286 | } |
| 287 | |
| 288 | smallDuration, err := Parse("PT0.0000000000001S") |
| 289 | if err != nil { |
| 290 | t.Fatal(err) |
| 291 | } |
| 292 | |
| 293 | if smallDuration.String() != "PT0.0000000000001S" { |
| 294 | t.Errorf("expected: %s, got: %s", "PT0.0000000000001S", smallDuration.String()) |
| 295 | } |
| 296 | |
| 297 | negativeDuration, err := Parse("-PT2H5M") |
| 298 | if err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | |
| 302 | if negativeDuration.String() != "-PT2H5M" { |
| 303 | t.Errorf("expected: %s, got: %s", "-PT2H5M", negativeDuration.String()) |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | func TestDuration_MarshalJSON(t *testing.T) { |
| 308 | td, err := Parse("P3Y6M4DT12H30M5.5S") |
| 309 | if err != nil { |
| 310 | t.Fatal(err) |
| 311 | } |
| 312 | |
| 313 | jsonVal, err := json.Marshal(struct { |
| 314 | Dur *Duration `json:"d"` |
| 315 | }{Dur: td}) |
| 316 | if err != nil { |
| 317 | t.Errorf("did not expect error: %s", err.Error()) |
| 318 | } |
| 319 | if string(jsonVal) != `{"d":"P3Y6M4DT12H30M5.5S"}` { |
| 320 | t.Errorf("expected: %s, got: %s", `{"d":"P3Y6M4DT12H30M5.5S"}`, string(jsonVal)) |
| 321 | } |
| 322 | |
| 323 | jsonVal, err = json.Marshal(struct { |
| 324 | Dur Duration `json:"d"` |
| 325 | }{Dur: *td}) |
| 326 | if err != nil { |
| 327 | t.Errorf("did not expect error: %s", err.Error()) |
| 328 | } |
| 329 | if string(jsonVal) != `{"d":"P3Y6M4DT12H30M5.5S"}` { |
| 330 | t.Errorf("expected: %s, got: %s", `{"d":"P3Y6M4DT12H30M5.5S"}`, string(jsonVal)) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | func TestDuration_UnmarshalJSON(t *testing.T) { |
| 335 | jsonStr := ` |
| 336 | { |
| 337 | "d": "P3Y6M4DT12H30M5.5S" |
| 338 | } |
| 339 | ` |
| 340 | expected, err := Parse("P3Y6M4DT12H30M5.5S") |
| 341 | if err != nil { |
| 342 | t.Fatal(err) |
| 343 | } |
| 344 | |
| 345 | var durStructPtr struct { |
| 346 | Dur *Duration `json:"d"` |
| 347 | } |
| 348 | err = json.Unmarshal([]byte(jsonStr), &durStructPtr) |
| 349 | if err != nil { |
| 350 | t.Errorf("did not expect error: %s", err.Error()) |
| 351 | } |
| 352 | if !reflect.DeepEqual(durStructPtr.Dur, expected) { |
| 353 | t.Errorf("JSON Unmarshal ptr got = %s, want %s", durStructPtr.Dur, expected) |
| 354 | } |
| 355 | |
| 356 | var durStruct struct { |
| 357 | Dur Duration `json:"d"` |
| 358 | } |
| 359 | err = json.Unmarshal([]byte(jsonStr), &durStruct) |
| 360 | if err != nil { |
| 361 | t.Errorf("did not expect error: %s", err.Error()) |
| 362 | } |
| 363 | if !reflect.DeepEqual(durStruct.Dur, *expected) { |
| 364 | t.Errorf("JSON Unmarshal ptr got = %s, want %s", &(durStruct.Dur), expected) |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | func TestDuration_MarshalText(t *testing.T) { |
| 369 | const orig = "P3Y6M4DT12H30M5.5S" |
| 370 | td, err := Parse(orig) |
| 371 | if err != nil { |
| 372 | t.Fatal(err) |
| 373 | } |
| 374 | |
| 375 | text, err := td.MarshalText() |
| 376 | if err != nil { |
| 377 | t.Errorf("did not expect error: %s", err) |
| 378 | } |
| 379 | if string(text) != orig { |
| 380 | t.Errorf("expected: %s, got: %s", orig, text) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | func TestDuration_UnmarshalText(t *testing.T) { |
| 385 | const orig = `P3Y6M4DT12H30M5.5S` |
| 386 | expected, err := Parse(orig) |
| 387 | if err != nil { |
| 388 | t.Fatal(err) |
| 389 | } |
| 390 | |
| 391 | var dur Duration |
| 392 | err = dur.UnmarshalText([]byte(orig)) |
| 393 | if err != nil { |
| 394 | t.Errorf("did not expect error: %s", err.Error()) |
| 395 | } |
| 396 | if !reflect.DeepEqual(dur, *expected) { |
| 397 | t.Errorf("Text Unmarshal ptr got = %s, want %s", &dur, expected) |
| 398 | } |
| 399 | |
| 400 | dur = Duration{} |
| 401 | err = (&dur).UnmarshalText([]byte(orig)) |
| 402 | if err != nil { |
| 403 | t.Errorf("did not expect error: %s", err) |
| 404 | } |
| 405 | if !reflect.DeepEqual(dur, *expected) { |
| 406 | t.Errorf("Text Unmarshal ptr got = %s, want %s", &dur, expected) |
| 407 | } |
| 408 | } |