| 70 | } |
| 71 | |
| 72 | func TestDuration_MarshalJSON(t *testing.T) { |
| 73 | cases := []struct { |
| 74 | name string |
| 75 | value interface{} |
| 76 | expectedJSON string |
| 77 | expectedYaml string |
| 78 | }{ |
| 79 | { |
| 80 | "simple", |
| 81 | Duration{5 * time.Second}, |
| 82 | `"5s"`, |
| 83 | "5s\n", |
| 84 | }, |
| 85 | { |
| 86 | "complex", |
| 87 | Duration{5*time.Second + time.Minute}, |
| 88 | `"1m5s"`, |
| 89 | "1m5s\n", |
| 90 | }, |
| 91 | { |
| 92 | "wrapped", |
| 93 | struct{ D Duration }{Duration{5*time.Second + time.Minute}}, |
| 94 | `{"D":"1m5s"}`, |
| 95 | "d: 1m5s\n", |
| 96 | }, |
| 97 | } |
| 98 | |
| 99 | for _, c := range cases { |
| 100 | c := c |
| 101 | t.Run(c.name, func(t *testing.T) { |
| 102 | t.Parallel() |
| 103 | data, err := json.Marshal(c.value) |
| 104 | assert.NoError(t, err) |
| 105 | assert.Equal(t, c.expectedJSON, string(data)) |
| 106 | data, _ = yaml.Marshal(c.value) |
| 107 | assert.Equal(t, c.expectedYaml, string(data)) |
| 108 | }) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestDuration_IsZero(t *testing.T) { |
| 113 | tests := []struct { |