Merge pull request #48 from emirpasic/development

Development
pull/50/merge
Emir Pasic 7 years ago committed by GitHub
commit bbaf5d1c57

@ -1253,7 +1253,7 @@ This takes a while, so test within sub-packages:
Biggest contribution towards this library is to use it and give us feedback for further improvements and additions.
For direct contributions, _pull request_ into master or ask to become a contributor.
For direct contributions, _pull request_ into development branch or ask to become a contributor.
Coding style:

@ -4,6 +4,8 @@
package utils
import "time"
// Comparator will make type assertion (see IntComparator for example),
// which will panic if a or b are not of the asserted type.
//
@ -232,3 +234,18 @@ func RuneComparator(a, b interface{}) int {
return 0
}
}
// TimeComparator provides a basic comparison on time.Time
func TimeComparator(a, b interface{}) int {
aAsserted := a.(time.Time)
bAsserted := b.(time.Time)
switch {
case aAsserted.After(bAsserted):
return 1
case aAsserted.Before(bAsserted):
return -1
default:
return 0
}
}

@ -6,6 +6,7 @@ package utils
import (
"testing"
"time"
)
func TestIntComparator(t *testing.T) {
@ -53,6 +54,26 @@ func TestStringComparator(t *testing.T) {
}
}
func TestTimeComparator(t *testing.T) {
now := time.Now()
// i1,i2,expected
tests := [][]interface{}{
{now, now, 0},
{now.Add(24 * 7 * 2 * time.Hour), now, 1},
{now, now.Add(24 * 7 * 2 * time.Hour), -1},
}
for _, test := range tests {
actual := TimeComparator(test[0], test[1])
expected := test[2]
if actual != expected {
t.Errorf("Got %v expected %v", actual, expected)
}
}
}
func TestCustomComparator(t *testing.T) {
type Custom struct {

Loading…
Cancel
Save