Skip to content

Commit 1b68d05

Browse files
committed
Add 100% test coverage
Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
1 parent 7f38454 commit 1b68d05

File tree

2 files changed

+153
-3
lines changed

2 files changed

+153
-3
lines changed

interval.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// start time that is part of the interval and an end time which is excluded
1212
// from the interval.
1313
//
14-
// [----------i----------[
14+
// |----------i----------[
1515
// start end
1616
//
1717
// i.Include(start) == true
@@ -29,8 +29,12 @@ func NewInterval(start, end time.Time) Interval {
2929
return Interval{Start: start, End: end}
3030
}
3131

32+
const (
33+
stringFormat = "Interval{start: %s, end: %s, duration: %s}"
34+
)
35+
3236
func (i Interval) String() string {
33-
return fmt.Sprintf("Interval{start: %s, end: %s, duration: %s}", i.Start.Format(time.RFC3339), i.End.Format(time.RFC3339), i.Duration())
37+
return fmt.Sprintf(stringFormat, i.Start.Format(time.RFC3339), i.End.Format(time.RFC3339), i.Duration())
3438
}
3539

3640
func (i Interval) Duration() time.Duration {

interval_test.go

Lines changed: 147 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package timeutils
22

33
import (
4+
"fmt"
45
"math/rand"
6+
"strings"
57
"testing"
68
"time"
79
)
@@ -32,6 +34,17 @@ func TestNewIntervalPanic(t *testing.T) {
3234
_ = NewInterval(start, end)
3335
}
3436

37+
func TestIntervalString(t *testing.T) {
38+
start := time.Now()
39+
end := start.Add(time.Hour)
40+
41+
i := NewInterval(start, end)
42+
43+
if i.String() != fmt.Sprintf(stringFormat, i.Start.Format(time.RFC3339), i.End.Format(time.RFC3339), i.Duration()) {
44+
t.Errorf("Interval.String() output was not as expected")
45+
}
46+
}
47+
3548
func TestIntervalInclude(t *testing.T) {
3649
start := time.Now()
3750
end := start.Add(time.Hour)
@@ -255,6 +268,94 @@ func TestIntervalOverlap(t *testing.T) {
255268
}
256269
}
257270

271+
func TestIntervalContiguous(t *testing.T) {
272+
start := time.Now()
273+
end := start.Add(time.Hour)
274+
275+
i := NewInterval(start, end)
276+
277+
tests := []struct {
278+
name string
279+
input Interval
280+
expected bool
281+
}{
282+
{
283+
name: `Input equals interval`,
284+
input: NewInterval(start, end),
285+
expected: false,
286+
},
287+
{
288+
name: `Input before interval`,
289+
input: NewInterval(start.Add(-time.Hour), start.Add(-time.Second)),
290+
expected: false,
291+
},
292+
{
293+
name: `Input after interval`,
294+
input: NewInterval(end.Add(time.Second), end.Add(time.Hour)),
295+
expected: false,
296+
},
297+
{
298+
name: `Input's start negatively differs`,
299+
input: NewInterval(start.Add(-time.Second), end),
300+
expected: false,
301+
},
302+
{
303+
name: `Input's start positively differs`,
304+
input: NewInterval(start.Add(time.Second), end),
305+
expected: false,
306+
},
307+
{
308+
name: `Input's end negatively differs`,
309+
input: NewInterval(start, end.Add(-time.Second)),
310+
expected: false,
311+
},
312+
{
313+
name: `Input's start positively differs`,
314+
input: NewInterval(start, end.Add(time.Second)),
315+
expected: false,
316+
},
317+
{
318+
name: `Input's start before interval's start and end before interval's end`,
319+
input: NewInterval(start.Add(-time.Second), start.Add(time.Second)),
320+
expected: false,
321+
},
322+
{
323+
name: `Input's end before interval's end and end after interval's end`,
324+
input: NewInterval(end.Add(-time.Second), end.Add(time.Second)),
325+
expected: false,
326+
},
327+
{
328+
name: `Input is smaller`,
329+
input: NewInterval(start.Add(time.Second), end.Add(-time.Second)),
330+
expected: false,
331+
},
332+
{
333+
name: `Input is bigger`,
334+
input: NewInterval(start.Add(-time.Second), end.Add(time.Second)),
335+
expected: false,
336+
},
337+
{
338+
name: `Input is contiguous to interval's start`,
339+
input: NewInterval(start.Add(-time.Second), start),
340+
expected: true,
341+
},
342+
{
343+
name: `Input is contiguous to interval's end`,
344+
input: NewInterval(end, end.Add(time.Second)),
345+
expected: true,
346+
},
347+
}
348+
349+
for _, test := range tests {
350+
t.Run(test.name, func(t *testing.T) {
351+
actual := i.Contiguous(test.input)
352+
if actual != test.expected {
353+
t.Errorf("%s.Contiguous(%s) was expected to be %v, got %v", i, test.input, test.expected, actual)
354+
}
355+
})
356+
}
357+
}
358+
258359
func TestIntervalSub(t *testing.T) {
259360
start := time.Now()
260361
end := start.Add(time.Hour)
@@ -301,6 +402,20 @@ func TestIntervalSub(t *testing.T) {
301402
input: NewInterval(start, end.Add(time.Second)),
302403
expected: Intervals{},
303404
},
405+
{
406+
name: `Input's start before interval's start and end before interval's end`,
407+
input: NewInterval(start.Add(-time.Second), start.Add(time.Second)),
408+
expected: Intervals{
409+
NewInterval(start.Add(time.Second), end),
410+
},
411+
},
412+
{
413+
name: `Input's start before interval's end and end after interval's end`,
414+
input: NewInterval(end.Add(-time.Second), end.Add(time.Second)),
415+
expected: Intervals{
416+
NewInterval(start, end.Add(-time.Second)),
417+
},
418+
},
304419
{
305420
name: `Input is smaller`,
306421
input: NewInterval(start.Add(time.Second), end.Add(-time.Second)),
@@ -326,11 +441,39 @@ func TestIntervalSub(t *testing.T) {
326441
}
327442
}
328443

444+
func TestIntervalsString(t *testing.T) {
445+
start := time.Now()
446+
end := start.Add(time.Hour)
447+
448+
is := Intervals{
449+
NewInterval(start, end),
450+
NewInterval(start, end),
451+
NewInterval(start, end.Add(-time.Second)),
452+
NewInterval(start, end.Add(+time.Second)),
453+
NewInterval(start.Add(-time.Second), end),
454+
NewInterval(start.Add(+time.Second), end),
455+
NewInterval(start.Add(-time.Second), end.Add(-time.Second)),
456+
NewInterval(start.Add(+time.Second), end.Add(+time.Second)),
457+
NewInterval(start.Add(-time.Second), end.Add(+time.Second)),
458+
NewInterval(start.Add(+time.Second), end.Add(-time.Second)),
459+
}
460+
461+
var expected []string
462+
for _, i := range is {
463+
expected = append(expected, fmt.Sprintf(stringFormat, i.Start.Format(time.RFC3339), i.End.Format(time.RFC3339), i.Duration()))
464+
}
465+
466+
if is.String() != fmt.Sprintf("[%s]", strings.Join(expected, ", ")) {
467+
t.Errorf("Intervals.String() output was not as expected")
468+
}
469+
}
470+
329471
func TestIntervalsEqual(t *testing.T) {
330472
start := time.Now()
331473
end := start.Add(time.Hour)
332474

333475
is := Intervals{
476+
NewInterval(start, end),
334477
NewInterval(start, end),
335478
NewInterval(start, end.Add(-time.Second)),
336479
NewInterval(start, end.Add(+time.Second)),
@@ -350,6 +493,7 @@ func TestIntervalsEqual(t *testing.T) {
350493
{
351494
name: `Input equals interval`,
352495
input: Intervals{
496+
NewInterval(start, end),
353497
NewInterval(start, end),
354498
NewInterval(start, end.Add(-time.Second)),
355499
NewInterval(start, end.Add(+time.Second)),
@@ -365,6 +509,7 @@ func TestIntervalsEqual(t *testing.T) {
365509
{
366510
name: `Randomly shuffled`,
367511
input: Intervals{
512+
NewInterval(start, end),
368513
NewInterval(start.Add(+time.Second), end.Add(+time.Second)),
369514
NewInterval(start, end.Add(+time.Second)),
370515
NewInterval(start, end),
@@ -378,8 +523,9 @@ func TestIntervalsEqual(t *testing.T) {
378523
expected: true,
379524
},
380525
{
381-
name: `Randomly shuffled`,
526+
name: `Randomly shuffled #2`,
382527
input: Intervals{
528+
NewInterval(start, end),
383529
NewInterval(start, end),
384530
NewInterval(start, end.Add(-time.Minute)),
385531
NewInterval(start, end.Add(+time.Minute)),

0 commit comments

Comments
 (0)