|
| 1 | +// +build go1.7 |
| 2 | + |
| 3 | +package protocol |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestFormatTime(t *testing.T) { |
| 11 | + cases := map[string]struct { |
| 12 | + formatName string |
| 13 | + expectedOutput string |
| 14 | + input time.Time |
| 15 | + }{ |
| 16 | + "UnixTest1": { |
| 17 | + formatName: UnixTimeFormatName, |
| 18 | + expectedOutput: "946845296", |
| 19 | + input: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC), |
| 20 | + }, |
| 21 | + "ISO8601Test1": { |
| 22 | + formatName: ISO8601TimeFormatName, |
| 23 | + expectedOutput: "2000-01-02T20:34:56Z", |
| 24 | + input: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC), |
| 25 | + }, |
| 26 | + "RFC822Test1": { |
| 27 | + formatName: RFC822TimeFormatName, |
| 28 | + expectedOutput: "Sun, 02 Jan 2000 20:34:56 GMT", |
| 29 | + input: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC), |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + for name, c := range cases { |
| 34 | + t.Run(name, func(t *testing.T) { |
| 35 | + if v, _ := FormatTime(c.formatName, c.input); v != c.expectedOutput { |
| 36 | + t.Errorf("input %v \n and output: %v, \n don't match for %s format ", c.input, c.expectedOutput, c.formatName) |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestParseTime(t *testing.T) { |
| 43 | + |
| 44 | + // Assert equals for input and output works for a precision upto three decimal places |
| 45 | + cases := map[string]struct { |
| 46 | + formatName, input string |
| 47 | + expectedOutput time.Time |
| 48 | + }{ |
| 49 | + "UnixTest1": { |
| 50 | + formatName: UnixTimeFormatName, |
| 51 | + input: "946845296.123", |
| 52 | + expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC), |
| 53 | + }, |
| 54 | + "UnixTest2": { |
| 55 | + formatName: UnixTimeFormatName, |
| 56 | + input: "946845296.12344", |
| 57 | + expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC), |
| 58 | + }, |
| 59 | + "UnixTest3": { |
| 60 | + formatName: UnixTimeFormatName, |
| 61 | + input: "946845296.1229999", |
| 62 | + expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC), |
| 63 | + }, |
| 64 | + "ISO8601Test1": { |
| 65 | + formatName: ISO8601TimeFormatName, |
| 66 | + input: "2000-01-02T20:34:56.123Z", |
| 67 | + expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123e9, time.UTC), |
| 68 | + }, |
| 69 | + "ISO8601Test2": { |
| 70 | + formatName: ISO8601TimeFormatName, |
| 71 | + input: "2000-01-02T20:34:56.123456789Z", |
| 72 | + expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, .123456789e9, time.UTC), |
| 73 | + }, |
| 74 | + "RFC822Test1": { |
| 75 | + formatName: RFC822TimeFormatName, |
| 76 | + input: "Sun, 2 Jan 2000 20:34:56 GMT", |
| 77 | + expectedOutput: time.Date(2000, time.January, 2, 20, 34, 56, 0, time.UTC), |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for name, c := range cases { |
| 82 | + t.Run(name, func(t *testing.T) { |
| 83 | + timeVal, err := ParseTime(c.formatName, c.input) |
| 84 | + if err != nil { |
| 85 | + t.Errorf("unable to parse time, %v", err) |
| 86 | + } |
| 87 | + if timeVal.UTC() != c.expectedOutput { |
| 88 | + t.Errorf("input: %v \n and output time %v,\n don't match for %s format ", c.input, c.expectedOutput, c.formatName) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments