1package core 2 3import ( 4 "testing" 5 "time" 6) 7 8func newTestRecord(s int, e int) Record { 9 start := time.Now().Add(time.Duration(s) * time.Minute) 10 end := time.Now().Add(time.Duration(e) * time.Minute) 11 return Record{Start: start, End: &end} 12} 13 14func newTestRecTracked(s int) Record { 15 start := time.Now().Add(time.Duration(s) * time.Minute) 16 return Record{Start: start} 17} 18 19func checkConsistent(t *testing.T, expect, result []*Record) { 20 sameLen := len(result) == len(expect) 21 sameContent := true 22 23 if sameLen { 24 for i := range result { 25 if expect[i] != result[i] { 26 sameContent = false 27 } 28 } 29 } 30 31 if !(sameLen && sameContent) { 32 t.Errorf("should collide with :\n") 33 for _, r := range expect { 34 t.Errorf("%v\n", r) 35 } 36 t.Errorf("while collides return :\n") 37 for _, r := range result { 38 t.Errorf("%v\n", r) 39 } 40 } 41 42} 43 44func TestCollides(t *testing.T) { 45 savedRec := newTestRecord(-60, -1) 46 allRecs := []*Record{&savedRec} 47 savedRecTracked := newTestRecTracked(-60) 48 allRecsTracked := []*Record{&savedRecTracked} 49 50 // rec1 starts and end after savedRec 51 rec1 := newTestRecord(-1, 0) 52 53 if collide, collidingRecs := collides(rec1, allRecs); collide && len(collidingRecs) == 0 { 54 t.Error("records should not collide") 55 } 56 57 // rec2 starts in savedRec, ends after 58 rec2 := newTestRecord(-30, 1) 59 60 if collide, collidingRecs := collides(rec2, allRecs); !collide { 61 t.Error("records should collide") 62 } else { 63 checkConsistent(t, allRecs, collidingRecs) 64 } 65 66 // rec3 start before savedRec, ends inside 67 rec3 := newTestRecord(-75, -30) 68 69 if collide, collidingRecs := collides(rec3, allRecs); !collide { 70 t.Error("records should collide") 71 } else { 72 checkConsistent(t, allRecs, collidingRecs) 73 } 74 75 // rec4 starts and ends before savedRec 76 rec4 := newTestRecord(-75, -70) 77 78 if collide, collidingRecs := collides(rec4, allRecs); collide && len(collidingRecs) == 0 { 79 t.Error("records should not collide") 80 } 81 82 // rec5 starts and ends inside savedRec 83 rec5 := newTestRecord(-40, -20) 84 85 if collide, collidingRecs := collides(rec5, allRecs); !collide { 86 t.Error("records should collide") 87 } else { 88 checkConsistent(t, allRecs, collidingRecs) 89 } 90 91 // rec6 starts before and ends after savedRec 92 rec6 := newTestRecord(-70, 10) 93 94 if collide, collidingRecs := collides(rec6, allRecs); !collide { 95 t.Error("records should collide") 96 } else { 97 checkConsistent(t, allRecs, collidingRecs) 98 } 99 100 // rec7 starts and ends at the same time as savedRec 101 rec7 := newTestRecord(-60, -1) 102 103 if collide, collidingRecs := collides(rec7, allRecs); !collide { 104 t.Error("records should collide") 105 } else { 106 checkConsistent(t, allRecs, collidingRecs) 107 } 108 109 // rec7 starts at the same time as savedRecTracked 110 rec8 := newTestRecord(-60, -1) 111 112 if collide, collidingRecs := collides(rec8, allRecsTracked); !collide { 113 t.Error("records should collide") 114 } else { 115 checkConsistent(t, allRecsTracked, collidingRecs) 116 } 117 118 // rec9 ends at the time savedRecTracked ends 119 rec9 := newTestRecord(-80, -60) 120 121 if collide, collidingRecs := collides(rec9, allRecsTracked); !collide { 122 t.Error("records should collide") 123 } else { 124 checkConsistent(t, allRecsTracked, collidingRecs) 125 } 126 127 // rec10 ends after savedRecTracked starts 128 rec10 := newTestRecord(-80, -50) 129 130 if collide, collidingRecs := collides(rec10, allRecsTracked); !collide { 131 t.Error("records should collide") 132 } else { 133 checkConsistent(t, allRecsTracked, collidingRecs) 134 } 135 136 // rec11 ends before savedRecTracked starts 137 rec11 := newTestRecord(-80, -70) 138 139 if collide, collidingRecs := collides(rec11, allRecsTracked); collide && len(collidingRecs) == 0 { 140 t.Error("records should not collide") 141 } 142} 143 144func TestFormatDuration(t *testing.T) { 145 146 tt := []struct { 147 Duration time.Duration 148 Expected string 149 ExpectedDec string 150 ExpectedBoth string 151 }{ 152 { 153 Duration: time.Duration(12 * time.Second), 154 Expected: "0h 0min", 155 ExpectedDec: "0.0h", 156 ExpectedBoth: "0h 0min 0.0h", 157 }, 158 { 159 Duration: time.Duration(60 * time.Minute), 160 Expected: "1h 0min", 161 ExpectedDec: "1.0h", 162 ExpectedBoth: "1h 0min 1.0h", 163 }, 164 { 165 Duration: time.Duration(24 * time.Minute), 166 Expected: "0h 24min", 167 ExpectedDec: "0.4h", 168 ExpectedBoth: "0h 24min 0.4h", 169 }, 170 { 171 Duration: time.Duration((60*8 + 24) * time.Minute), 172 Expected: "8h 24min", 173 ExpectedDec: "8.4h", 174 ExpectedBoth: "8h 24min 8.4h", 175 }, 176 { 177 Duration: time.Duration((60*8+24)*time.Minute + 12*time.Second), 178 Expected: "8h 24min", 179 ExpectedDec: "8.4h", 180 ExpectedBoth: "8h 24min 8.4h", 181 }, 182 { 183 Duration: time.Duration(0 * time.Second), 184 Expected: "0h 0min", 185 ExpectedDec: "0.0h", 186 ExpectedBoth: "0h 0min 0.0h", 187 }, 188 } 189 190 formatter := Formatter{} 191 192 //Default Case 193 for _, test := range tt { 194 strFormat := formatter.FormatDuration(test.Duration) 195 if strFormat != test.Expected { 196 t.Fatalf("format error: %s != %s", strFormat, test.Expected) 197 } 198 } 199 //Decimal Hours true 200 formatter.useDecimalHours = "On" 201 for _, test := range tt { 202 strFormat := formatter.FormatDuration(test.Duration) 203 if strFormat != test.ExpectedDec { 204 t.Fatalf("format error: %s != %s", strFormat, test.ExpectedDec) 205 } 206 } 207 //Decimal Hours both 208 formatter.useDecimalHours = "Both" 209 for _, test := range tt { 210 strFormat := formatter.FormatDuration(test.Duration) 211 if strFormat != test.ExpectedBoth { 212 t.Fatalf("format error: %s != %s", strFormat, test.ExpectedBoth) 213 } 214 } 215} 216