Skip to content

Commit

Permalink
Merge pull request #3 from cappuccinotm/must-helper
Browse files Browse the repository at this point in the history
provide helper function for panicking when error occurred
  • Loading branch information
Semior001 authored Jul 29, 2022
2 parents 08e34eb + c892e89 commit a0b8378
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 227 deletions.
18 changes: 18 additions & 0 deletions operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,21 @@ type boundary struct {
tm time.Time
typ boundaryType
}

// MustRanges is a helper that accepts the result of function, that returns
// ranges and panics, if err is returned.
func MustRanges(r []Range, err error) []Range {
if err != nil {
panic(err)
}
return r
}

// MustRange is a helper that accepts the result of function, that returns
// a single range and panics, if err is returned.
func MustRange(r Range, err error) Range {
if err != nil {
panic(err)
}
return r
}
113 changes: 70 additions & 43 deletions operations_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
package trn

import (
"errors"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestMustRange(t *testing.T) {
assert.Panics(t, func() { MustRange(Range{}, errors.New("some error")) })

assert.NotPanics(t, func() {
rn := Range{st: time.Now(), dur: 5 * time.Minute}
res := MustRange(rn, nil)
assert.Equal(t, rn, res)
})
}

func TestMustRanges(t *testing.T) {
assert.Panics(t, func() { MustRanges([]Range{}, errors.New("some error")) })

assert.NotPanics(t, func() {
now := time.Now()
rngs := []Range{
{st: now, dur: 5 * time.Minute},
{st: now.Add(5 * time.Minute), dur: 7 * time.Minute},
{st: now.Add(10 * time.Minute), dur: 18 * time.Minute},
}
res := MustRanges(rngs, nil)
assert.Equal(t, rngs, res)
})
}

func TestMergeOverlappingRanges(t *testing.T) {
tests := []struct {
name string
Expand All @@ -14,83 +41,83 @@ func TestMergeOverlappingRanges(t *testing.T) {
{
name: "ranges don't overlap",
args: []Range{
MustBetween(tm(13, 0), tm(14, 0)),
MustBetween(tm(15, 0), tm(16, 0)),
MustRange(Between(tm(13, 0), tm(14, 0))),
MustRange(Between(tm(15, 0), tm(16, 0))),
},
want: []Range{
MustBetween(tm(13, 0), tm(14, 0)),
MustBetween(tm(15, 0), tm(16, 0)),
MustRange(Between(tm(13, 0), tm(14, 0))),
MustRange(Between(tm(15, 0), tm(16, 0))),
},
},
{
name: "ranges intersect",
args: []Range{
MustBetween(tm(13, 0), tm(14, 0)),
MustBetween(tm(13, 30), tm(15, 0)),
MustRange(Between(tm(13, 0), tm(14, 0))),
MustRange(Between(tm(13, 30), tm(15, 0))),
},
want: []Range{
MustBetween(tm(13, 0), tm(15, 0)),
MustRange(Between(tm(13, 0), tm(15, 0))),
},
},
{
name: "one range eternally overlaps the other",
args: []Range{
MustBetween(tm(13, 0), tm(15, 0)),
MustBetween(tm(13, 30), tm(14, 30)),
MustRange(Between(tm(13, 0), tm(15, 0))),
MustRange(Between(tm(13, 30), tm(14, 30))),
},
want: []Range{
MustBetween(tm(13, 0), tm(15, 0)),
MustRange(Between(tm(13, 0), tm(15, 0))),
},
},
{
name: "boundaries of two ranges are equal",
args: []Range{
MustBetween(tm(13, 0), tm(13, 15)),
MustBetween(tm(13, 15), tm(13, 30)),
MustRange(Between(tm(13, 0), tm(13, 15))),
MustRange(Between(tm(13, 15), tm(13, 30))),
},
want: []Range{
MustBetween(tm(13, 0), tm(13, 30)),
MustRange(Between(tm(13, 0), tm(13, 30))),
},
},
{
name: "complex test",
args: []Range{
// next three ranges must be merged (last two are within the first one)
MustBetween(tm(19, 0), tm(19, 30)),
MustBetween(tm(19, 1), tm(19, 15)),
MustBetween(tm(19, 8), tm(19, 17)),
MustRange(Between(tm(19, 0), tm(19, 30))),
MustRange(Between(tm(19, 1), tm(19, 15))),
MustRange(Between(tm(19, 8), tm(19, 17))),
// next second range must be removed (end of first = end of second)
MustBetween(tm(15, 0), tm(15, 30)),
MustBetween(tm(15, 16), tm(15, 30)),
MustRange(Between(tm(15, 0), tm(15, 30))),
MustRange(Between(tm(15, 16), tm(15, 30))),
// next two ranges must NOT be merged
MustBetween(tm(12, 0), tm(12, 15)),
MustBetween(tm(12, 30), tm(12, 45)),
MustRange(Between(tm(12, 0), tm(12, 15))),
MustRange(Between(tm(12, 30), tm(12, 45))),
// next two ranges must be merged (end of the first = start of the second)
MustBetween(tm(13, 0), tm(13, 15)),
MustBetween(tm(13, 15), tm(13, 30)),
MustRange(Between(tm(13, 0), tm(13, 15))),
MustRange(Between(tm(13, 15), tm(13, 30))),
// next two ranges must be merged
MustBetween(tm(14, 0), tm(14, 16)),
MustBetween(tm(14, 15), tm(14, 30)),
MustRange(Between(tm(14, 0), tm(14, 16))),
MustRange(Between(tm(14, 15), tm(14, 30))),
// next second range must be removed (start of first = start of second)
MustBetween(tm(16, 0), tm(16, 30)),
MustBetween(tm(16, 0), tm(16, 16)),
MustRange(Between(tm(16, 0), tm(16, 30))),
MustRange(Between(tm(16, 0), tm(16, 16))),
// next second range must be removed (ranges are equal)
MustBetween(tm(17, 0), tm(17, 30)),
MustBetween(tm(17, 0), tm(17, 30)),
MustRange(Between(tm(17, 0), tm(17, 30))),
MustRange(Between(tm(17, 0), tm(17, 30))),
// next second range must be removed
MustBetween(tm(18, 0), tm(18, 30)),
MustBetween(tm(18, 1), tm(18, 15)),
MustRange(Between(tm(18, 0), tm(18, 30))),
MustRange(Between(tm(18, 1), tm(18, 15))),
},
want: []Range{
MustBetween(tm(12, 0), tm(12, 15)),
MustBetween(tm(12, 30), tm(12, 45)),
MustBetween(tm(13, 0), tm(13, 30)),
MustBetween(tm(14, 0), tm(14, 30)),
MustBetween(tm(15, 0), tm(15, 30)),
MustBetween(tm(16, 0), tm(16, 30)),
MustBetween(tm(17, 0), tm(17, 30)),
MustBetween(tm(18, 0), tm(18, 30)),
MustBetween(tm(19, 0), tm(19, 30)),
MustRange(Between(tm(12, 0), tm(12, 15))),
MustRange(Between(tm(12, 30), tm(12, 45))),
MustRange(Between(tm(13, 0), tm(13, 30))),
MustRange(Between(tm(14, 0), tm(14, 30))),
MustRange(Between(tm(15, 0), tm(15, 30))),
MustRange(Between(tm(16, 0), tm(16, 30))),
MustRange(Between(tm(17, 0), tm(17, 30))),
MustRange(Between(tm(18, 0), tm(18, 30))),
MustRange(Between(tm(19, 0), tm(19, 30))),
},
},
}
Expand All @@ -117,11 +144,11 @@ func TestIntersection(t *testing.T) {
{
name: "intersection",
args: []Range{
MustBetween(tm(13, 0), tm(19, 0)),
MustBetween(tm(15, 0), tm(17, 0)),
MustBetween(tm(16, 0), tm(21, 0)),
MustRange(Between(tm(13, 0), tm(19, 0))),
MustRange(Between(tm(15, 0), tm(17, 0))),
MustRange(Between(tm(16, 0), tm(21, 0))),
},
want: MustBetween(tm(16, 0), tm(17, 0)),
want: MustRange(Between(tm(16, 0), tm(17, 0))),
},
}
for _, tt := range tests {
Expand Down
36 changes: 4 additions & 32 deletions range.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package trn

import (
"fmt"
"time"
"strings"
"strconv"
"strings"
"time"
)

const defaultRangeFmt = "2006-01-02 15:04:05.999999999 -0700 MST"
Expand Down Expand Up @@ -157,12 +157,11 @@ func (r Range) Truncate(bounds Range) Range {
// ---XXX---
// ----YYY--
return Range{st: bounds.st, dur: r.End().Sub(bounds.st)}
case r.st.After(bounds.st) && r.End().After(bounds.End()):
default:
// r.st.After(bounds.st) && r.End().After(bounds.End())
// ---XXX---
// --YYY----
return Range{st: r.st, dur: bounds.End().Sub(r.st)}
default:
panic("trn: should never happen")
}
}

Expand Down Expand Up @@ -204,33 +203,6 @@ func (r Range) flipValidRanges(ranges []Range) []Range {
return res
}

// MustSplit does the same as Split, but panics in case of any error.
func (r Range) MustSplit(duration time.Duration, interval time.Duration) []Range {
rngs, err := r.Split(duration, interval)
if err != nil {
panic(err)
}
return rngs
}

// MustStratify does the same as Stratify, but panics in case of any error.
func (r Range) MustStratify(duration time.Duration, interval time.Duration) []Range {
rngs, err := r.Stratify(duration, interval)
if err != nil {
panic(err)
}
return rngs
}

// MustBetween does the same as Between, but panics, instead of returning error.
func MustBetween(start, end time.Time, opts ...Option) Range {
rng, err := Between(start, end, opts...)
if err != nil {
panic(err)
}
return rng
}

// Error describes any error appeared in this package.
type Error string

Expand Down
Loading

0 comments on commit a0b8378

Please sign in to comment.