MCPcopy Create free account
hub / github.com/prometheus/common / TestTimeJSON

Function TestTimeJSON

model/time_test.go:451–481  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

449}
450
451func TestTimeJSON(t *testing.T) {
452 tests := []struct {
453 in Time
454 out string
455 }{
456 {Time(1), `0.001`},
457 {Time(-1), `-0.001`},
458 {Time(1001), `1.001`},
459 {Time(-1001), `-1.001`},
460 {Time(123000), `123`},
461 {Time(123100), `123.1`},
462 {Time(123010), `123.01`},
463 }
464
465 for i, test := range tests {
466 t.Run(strconv.Itoa(i), func(t *testing.T) {
467 b, err := test.in.MarshalJSON()
468 require.NoErrorf(t, err, "Error marshaling time: %v", err)
469
470 if string(b) != test.out {
471 t.Errorf("Mismatch in marshal expected=%s actual=%s", test.out, b)
472 }
473
474 var tm Time
475 err = tm.UnmarshalJSON(b)
476 require.NoErrorf(t, err, "Error Unmarshaling time: %v", err)
477
478 require.Truef(t, test.in.Equal(tm), "Mismatch after Unmarshal expected=%v actual=%v", test.in, tm)
479 })
480 }
481}
482
483func BenchmarkParseDuration(b *testing.B) {
484 const data = "30s"

Callers

nothing calls this directly

Calls 4

UnmarshalJSONMethod · 0.95
TimeTypeAlias · 0.85
MarshalJSONMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected