|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestAgeSet(t *testing.T) { |
| 10 | + testCases := []struct { |
| 11 | + in string |
| 12 | + err string // Contained in error text |
| 13 | + out int64 // Only checked if no error (natch) |
| 14 | + }{ |
| 15 | + {"", "1-5 digits", 0}, // # 0 |
| 16 | + {"123456s", "1-5 digits", 0}, |
| 17 | + {"01s", "octal", 0}, |
| 18 | + {"1S", "invalid unit", 0}, |
| 19 | + {"1t", "invalid unit", 0}, |
| 20 | + {"1xs", "invalid syntax", 0}, |
| 21 | + {"1234xs", "invalid syntax", 0}, |
| 22 | + {"x1234s", "invalid syntax", 0}, |
| 23 | + {"3000Y", "exceeds", 0}, |
| 24 | + {"16000M", "exceeds", 0}, // # 9 |
| 25 | + |
| 26 | + {"0s", "", 0 * second}, // # 10 |
| 27 | + {"1s", "", 1 * second}, |
| 28 | + {"59s", "", 59 * second}, |
| 29 | + {"60s", "", 1 * minute}, |
| 30 | + {"61s", "", 61 * second}, |
| 31 | + {"1m", "", 1 * minute}, |
| 32 | + {"59m", "", 59 * minute}, |
| 33 | + {"1h", "", 1 * hour}, |
| 34 | + {"23h", "", 23 * hour}, |
| 35 | + {"1D", "", 1 * day}, |
| 36 | + {"7D", "", 1 * week}, |
| 37 | + {"1W", "", 1 * week}, |
| 38 | + {"1Y", "", 1 * year}, |
| 39 | + } |
| 40 | + |
| 41 | + for ix, tc := range testCases { |
| 42 | + var a age |
| 43 | + err := a.Set(tc.in) |
| 44 | + if err != nil { |
| 45 | + if len(tc.err) == 0 { |
| 46 | + t.Error(ix, "Unexpected error", err) |
| 47 | + continue |
| 48 | + } |
| 49 | + if !strings.Contains(err.Error(), tc.err) { |
| 50 | + t.Errorf("%d Wrong error returned. Want '%s' got '%s'\n", ix, tc.err, err.Error()) |
| 51 | + continue |
| 52 | + } |
| 53 | + continue |
| 54 | + } |
| 55 | + if len(tc.err) > 0 { |
| 56 | + t.Error(ix, "Expected error", tc.err) |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + if tc.out != a.seconds { |
| 61 | + t.Error(ix, "Wrong value parsed. Expect", tc.out, "got", a.seconds) |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + if a.String() != tc.in { |
| 66 | + t.Error(ix, "Set value of", a.String(), "differs from input", tc.in) |
| 67 | + continue |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestAgeGreaterThan(t *testing.T) { |
| 73 | + var baby, twin, granny age |
| 74 | + baby.seconds = -3 |
| 75 | + twin.seconds = -3 // Equal ages are not greater than each other |
| 76 | + granny.seconds = 1 |
| 77 | + if !granny.gt(baby, false) { |
| 78 | + t.Error("Granny should be GT baby") |
| 79 | + } |
| 80 | + if baby.gt(twin, false) { |
| 81 | + t.Error("Baby should not be greater than twin") |
| 82 | + } |
| 83 | + if twin.gt(baby, false) { |
| 84 | + t.Error("Twin should not be greater than baby") |
| 85 | + } |
| 86 | + |
| 87 | + baby.seconds = -3*day + 11*hour |
| 88 | + twin.seconds = -3 * day |
| 89 | + baby.multiplier = day |
| 90 | + twin.multiplier = day |
| 91 | + |
| 92 | + if baby.gt(twin, true) { |
| 93 | + t.Error("Truncated baby should not be greater than twin") |
| 94 | + } |
| 95 | + if twin.gt(baby, true) { |
| 96 | + t.Error("Truncated twin should not be greater than baby") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestAgeLessThan(t *testing.T) { |
| 101 | + var baby, twin, granny age |
| 102 | + baby.seconds = -3 |
| 103 | + twin.seconds = -3 // Equal ages are not younger than each other |
| 104 | + granny.seconds = 1 |
| 105 | + if !baby.lt(granny) { |
| 106 | + t.Error("Baby should be younger than granny") |
| 107 | + } |
| 108 | + if baby.lt(twin) { |
| 109 | + t.Error("Baby should not be younger than twin") |
| 110 | + } |
| 111 | + if twin.lt(baby) { |
| 112 | + t.Error("Twin should not be younger than baby") |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestAgeSetFromTime(t *testing.T) { |
| 117 | + var baby, granny age |
| 118 | + |
| 119 | + base := time.Date(1000, 1, 1, 0, 0, 0, 0, time.UTC) |
| 120 | + year2000 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) |
| 121 | + year3000 := time.Date(3000, 1, 1, 0, 0, 0, 0, time.UTC) |
| 122 | + |
| 123 | + granny.setFromTime(base, year2000) |
| 124 | + baby.setFromTime(base, year3000) |
| 125 | + |
| 126 | + if granny.lt(baby) { |
| 127 | + t.Error("Granny", granny, "should not be younger than baby", baby) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestAgeCompactString(t *testing.T) { |
| 132 | + testCases := []struct { |
| 133 | + seconds int64 |
| 134 | + expect string |
| 135 | + }{ |
| 136 | + {86401 * 366, "1Y"}, |
| 137 | + {86401 * 99, "3M"}, |
| 138 | + {86401 * 7 * 2, "2W"}, |
| 139 | + {86401, "1D"}, |
| 140 | + {60*60 + 1, "1h"}, |
| 141 | + {121, "2m"}, |
| 142 | + {61, "1m"}, |
| 143 | + {59, "59s"}, |
| 144 | + {1, "1s"}, |
| 145 | + {0, "0s"}, |
| 146 | + {-5, "fut"}, |
| 147 | + } |
| 148 | + |
| 149 | + var a age |
| 150 | + for ix, tc := range testCases { |
| 151 | + a.seconds = tc.seconds |
| 152 | + s := a.compactString() |
| 153 | + if s != tc.expect { |
| 154 | + t.Error(ix, "Expect", tc.expect, "got", s) |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments