From 4f99aafb7df6588c14b7fe14b09eca36f1dd90bc Mon Sep 17 00:00:00 2001 From: Ben Fuller Date: Mon, 15 Apr 2019 14:53:15 -0600 Subject: [PATCH] Time/MicroTime: add unit tests for comparison functions Co-authored-by: David Timm --- .../pkg/apis/meta/v1/micro_time_test.go | 116 ++++++++++++++++++ .../pkg/apis/meta/v1/time_test.go | 44 +++++++ 2 files changed, 160 insertions(+) diff --git a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/micro_time_test.go b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/micro_time_test.go index b08e3f236c5..fac8f56e76a 100644 --- a/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/micro_time_test.go +++ b/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/micro_time_test.go @@ -137,3 +137,119 @@ func TestMicroTimeProto(t *testing.T) { } } } + +func TestMicroTimeEqual(t *testing.T) { + t1 := NewMicroTime(time.Now()) + cases := []struct { + name string + x *MicroTime + y *MicroTime + result bool + }{ + {"nil =? nil", nil, nil, true}, + {"!nil =? !nil", &t1, &t1, true}, + {"nil =? !nil", nil, &t1, false}, + {"!nil =? nil", &t1, nil, false}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + result := c.x.Equal(c.y) + if result != c.result { + t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result) + } + }) + } +} + +func TestMicroTimeEqualTime(t *testing.T) { + t1 := NewMicroTime(time.Now()) + t2 := NewTime(t1.Time) + cases := []struct { + name string + x *MicroTime + y *Time + result bool + }{ + {"nil =? nil", nil, nil, true}, + {"!nil =? !nil", &t1, &t2, true}, + {"nil =? !nil", nil, &t2, false}, + {"!nil =? nil", &t1, nil, false}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + result := c.x.EqualTime(c.y) + if result != c.result { + t.Errorf("Failed equality test for '%v', '%v': expected %+v, got %+v", c.x, c.y, c.result, result) + } + }) + } +} + +func TestMicroTimeBefore(t *testing.T) { + t1 := NewMicroTime(time.Now()) + cases := []struct { + name string + x *MicroTime + y *MicroTime + }{ + {"nil