- time comparator fmt and documentation update

This commit is contained in:
Emir Pasic
2017-03-03 23:24:20 +01:00
parent 7d8a6c1533
commit 3389248bfc
2 changed files with 35 additions and 36 deletions
+15 -15
View File
@@ -53,21 +53,6 @@ func IntComparator(a, b interface{}) int {
}
}
// 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
}
}
// Int8Comparator provides a basic comparison on int8
func Int8Comparator(a, b interface{}) int {
aAsserted := a.(int8)
@@ -249,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
}
}
+20 -21
View File
@@ -31,27 +31,6 @@ func TestIntComparator(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 TestStringComparator(t *testing.T) {
// s1,s2,expected
@@ -75,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 {