diff --git a/utils/comparator.go b/utils/comparator.go index b5f0f79..6a9afbf 100644 --- a/utils/comparator.go +++ b/utils/comparator.go @@ -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 + } +} diff --git a/utils/comparator_test.go b/utils/comparator_test.go index 00d028f..40efbd3 100644 --- a/utils/comparator_test.go +++ b/utils/comparator_test.go @@ -31,20 +31,22 @@ func TestIntComparator(t *testing.T) { } } +func TestStringComparator(t *testing.T) { -func TestTimeComparator(t *testing.T) { - - now := time.Now() - - // i1,i2,expected + // s1,s2,expected tests := [][]interface{}{ - {now, now, 0}, - {now.Add(24 * 7 * 2 * time.Hour), now, 1}, - {now, now.Add(24 * 7 * 2 * time.Hour), -1}, + {"a", "a", 0}, + {"a", "b", -1}, + {"b", "a", 1}, + {"aa", "aab", -1}, + {"", "", 0}, + {"a", "", 1}, + {"", "a", -1}, + {"", "aaaaaaa", -1}, } for _, test := range tests { - actual := TimeComparator(test[0], test[1]) + actual := StringComparator(test[0], test[1]) expected := test[2] if actual != expected { t.Errorf("Got %v expected %v", actual, expected) @@ -52,22 +54,19 @@ func TestTimeComparator(t *testing.T) { } } -func TestStringComparator(t *testing.T) { +func TestTimeComparator(t *testing.T) { - // s1,s2,expected + now := time.Now() + + // i1,i2,expected tests := [][]interface{}{ - {"a", "a", 0}, - {"a", "b", -1}, - {"b", "a", 1}, - {"aa", "aab", -1}, - {"", "", 0}, - {"a", "", 1}, - {"", "a", -1}, - {"", "aaaaaaa", -1}, + {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 := StringComparator(test[0], test[1]) + actual := TimeComparator(test[0], test[1]) expected := test[2] if actual != expected { t.Errorf("Got %v expected %v", actual, expected)