mirror of https://github.com/k3s-io/k3s
Time/MicroTime: add unit tests for comparison functions
Co-authored-by: David Timm <dtimm@pivotal.io>k3s-v1.15.3
parent
a3cc460431
commit
4f99aafb7d
|
@ -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 <? nil", nil, nil},
|
||||||
|
{"!nil <? !nil", &t1, &t1},
|
||||||
|
{"nil <? !nil", nil, &t1},
|
||||||
|
{"!nil <? nil", &t1, nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
result := c.x.Before(c.y)
|
||||||
|
if result {
|
||||||
|
t.Errorf("Failed before test for '%v', '%v': expected false, got %+v", c.x, c.y, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestMicroTimeBeforeTime(t *testing.T) {
|
||||||
|
t1 := NewMicroTime(time.Now())
|
||||||
|
t2 := NewTime(t1.Time)
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
x *MicroTime
|
||||||
|
y *Time
|
||||||
|
}{
|
||||||
|
{"nil <? nil", nil, nil},
|
||||||
|
{"!nil <? !nil", &t1, &t2},
|
||||||
|
{"nil <? !nil", nil, &t2},
|
||||||
|
{"!nil <? nil", &t1, nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
result := c.x.BeforeTime(c.y)
|
||||||
|
if result {
|
||||||
|
t.Errorf("Failed before test for '%v', '%v': expected false, got %+v", c.x, c.y, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMicroTimeIsZero(t *testing.T) {
|
||||||
|
t1 := NewMicroTime(time.Now())
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
x *MicroTime
|
||||||
|
result bool
|
||||||
|
}{
|
||||||
|
{"nil =? 0", nil, true},
|
||||||
|
{"!nil =? 0", &t1, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
result := c.x.IsZero()
|
||||||
|
if result != c.result{
|
||||||
|
t.Errorf("Failed equality test for '%v': expected %+v, got %+v", c.x, c.result, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -195,3 +195,47 @@ func TestTimeEqual(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTimeBefore(t *testing.T) {
|
||||||
|
t1 := NewTime(time.Now())
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
x *Time
|
||||||
|
y *Time
|
||||||
|
}{
|
||||||
|
{"nil <? nil", nil, nil},
|
||||||
|
{"!nil <? !nil", &t1, &t1},
|
||||||
|
{"nil <? !nil", nil, &t1},
|
||||||
|
{"!nil <? nil", &t1, nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
result := c.x.Before(c.y)
|
||||||
|
if result {
|
||||||
|
t.Errorf("Failed equality test for '%v', '%v': expected false, got %+v", c.x, c.y, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimeIsZero(t *testing.T) {
|
||||||
|
t1 := NewTime(time.Now())
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
x *Time
|
||||||
|
result bool
|
||||||
|
}{
|
||||||
|
{"nil =? 0", nil, true},
|
||||||
|
{"!nil =? 0", &t1, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
result := c.x.IsZero()
|
||||||
|
if result != c.result{
|
||||||
|
t.Errorf("Failed equality test for '%v': expected %+v, got %+v", c.x, c.result, result)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue