MCPcopy Create free account
hub / github.com/chain/Core / TestUnmarshalDuration

Function TestUnmarshalDuration

encoding/json/duration_test.go:10–55  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

8)
9
10func TestUnmarshalDuration(t *testing.T) {
11 successCases := []string{
12 `1000`, // this is an "integer"
13 `"1000ms"`,
14 `"1000000000ns"`,
15 `"1s"`,
16 }
17
18 for _, c := range successCases {
19 var dur Duration
20 err := json.Unmarshal([]byte(c), &dur)
21 if err != nil {
22 t.Errorf("unexpected error %v", err)
23 }
24
25 var want float64 = 1 // all of our inputs equal 1 second
26 if got := dur.Seconds(); got != want {
27 t.Errorf("Duration.UnmarshalJSON(%q) = %f want %f", c, got, want)
28 }
29 }
30
31 negativeCases := []string{
32 `-1000`,
33 `"-1000ms"`,
34 }
35
36 for _, c := range negativeCases {
37 var dur Duration
38 wantErr := "invalid json.Duration: Duration cannot be less than 0"
39 err := json.Unmarshal([]byte(c), &dur)
40 if err.Error() != wantErr {
41 t.Errorf("wanted error %s, got %s", wantErr, err)
42 }
43 }
44
45 // Null case
46 var dur Duration
47 err := json.Unmarshal([]byte("null"), &dur)
48 if err != nil {
49 t.Errorf("unexpected error %v", err)
50 }
51
52 if dur.Duration != 0 {
53 t.Errorf(`Duration.UnmarshalJSON("null") = %v want 0`, dur.Duration)
54 }
55}
56
57func TestMarshalDuration(t *testing.T) {
58 dur := Duration{

Callers

nothing calls this directly

Calls 1

ErrorMethod · 0.45

Tested by

no test coverage detected