| 1 | package time_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | . "github.com/mesos/mesos-go/api/v1/lib/time" |
| 8 | ) |
| 9 | |
| 10 | func TestParseDuration(t *testing.T) { |
| 11 | for i, tc := range []struct { |
| 12 | input string |
| 13 | output time.Duration |
| 14 | wantsError bool |
| 15 | }{ |
| 16 | {wantsError: true}, |
| 17 | {input: "1ns", output: time.Duration(1)}, |
| 18 | {input: "1.5ns", output: time.Duration(1)}, |
| 19 | {input: "1.9ns", output: time.Duration(1)}, |
| 20 | {input: "1us", output: 1 * time.Microsecond}, |
| 21 | {input: "1.5us", output: 1*time.Microsecond + 500}, |
| 22 | {input: "1ms", output: 1 * time.Millisecond}, |
| 23 | {input: "1secs", output: 1 * time.Second}, |
| 24 | {input: "1mins", output: 1 * time.Minute}, |
| 25 | {input: "1hrs", output: 1 * time.Hour}, |
| 26 | {input: "1days", output: 1 * time.Hour * 24}, |
| 27 | {input: "1weeks", output: 1 * time.Hour * 24 * 7}, |
| 28 | {input: "1", wantsError: true}, |
| 29 | {input: "1.0", wantsError: true}, |
| 30 | {input: "1.0.0ns", wantsError: true}, |
| 31 | {input: "a1ns", wantsError: true}, |
| 32 | {input: "ns", wantsError: true}, |
| 33 | {input: "1ms1ns", wantsError: true}, |
| 34 | } { |
| 35 | d, err := ParseDuration(tc.input) |
| 36 | if err != nil && tc.wantsError { |
| 37 | continue |
| 38 | } |
| 39 | if err != nil { |
| 40 | t.Errorf("test case %d failed: unexpected error %+v", i, err) |
| 41 | } else if tc.wantsError { |
| 42 | t.Errorf("test case %d failed: expected error for input %q", i, tc.input) |
| 43 | } else if tc.output != d { |
| 44 | t.Errorf("test case %d failed: expected output %v instead of %v", i, tc.output, d) |
| 45 | } |
| 46 | } |
| 47 | } |