10 func TestUnmarshalDuration(t *testing.T) {
11 successCases := []string{
12 `1000`, // this is an "integer"
18 for _, c := range successCases {
20 err := json.Unmarshal([]byte(c), &dur)
22 t.Errorf("unexpected error %v", err)
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)
31 negativeCases := []string{
36 for _, c := range negativeCases {
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)
47 err := json.Unmarshal([]byte("null"), &dur)
49 t.Errorf("unexpected error %v", err)
52 if dur.Duration != 0 {
53 t.Errorf(`Duration.UnmarshalJSON("null") = %v want 0`, dur.Duration)
57 func TestMarshalDuration(t *testing.T) {
59 Duration: time.Second,
61 b, err := json.Marshal(dur)
63 t.Errorf("unexpected error %v", err)
66 got, err := strconv.Atoi(string(b))
72 t.Errorf("wanted %d, got %d", want, got)