| 1 |
// Copyright (c) 2018, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"testing" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/internal/test" |
| 14 |
"github.com/maxatome/go-testdeep/td" |
| 15 |
) |
| 16 |
|
| 17 |
type ( |
| 18 |
MyTime time.Time |
| 19 |
MyTimeStr time.Time |
| 20 |
) |
| 21 |
|
| 22 |
func (t MyTimeStr) String() string { |
| 23 |
return "<<" + time.Time(t).Format(time.RFC3339Nano) + ">>" |
| 24 |
} |
| 25 |
|
| 26 |
func TestTruncTime(t *testing.T) { |
| 27 |
// |
| 28 |
// Monotonic |
| 29 |
now := time.Now() |
| 30 |
nowWithoutMono := now.Truncate(0) |
| 31 |
|
| 32 |
// If monotonic clock available, check without TruncTime() |
| 33 |
if now != nowWithoutMono { |
| 34 |
// OK now contains a monotonic part != 0, so fail coz "==" used inside |
| 35 |
checkError(t, now, nowWithoutMono, |
| 36 |
expectedError{ |
| 37 |
Message: mustBe("values differ"), |
| 38 |
Path: mustContain("DATA"), |
| 39 |
Next: ignoreExpectedError, |
| 40 |
}) |
| 41 |
} |
| 42 |
checkOK(t, now, td.TruncTime(nowWithoutMono)) |
| 43 |
|
| 44 |
// |
| 45 |
// time.Time |
| 46 |
gotDate := time.Date(2018, time.March, 9, 1, 2, 3, 4, time.UTC) |
| 47 |
|
| 48 |
// Time zone / location does not matter |
| 49 |
UTCp2 := time.FixedZone("UTC+2", 2) |
| 50 |
UTCm2 := time.FixedZone("UTC-2", 2) |
| 51 |
checkOK(t, gotDate, td.TruncTime(gotDate.In(UTCp2))) |
| 52 |
checkOK(t, gotDate, td.TruncTime(gotDate.In(UTCm2))) |
| 53 |
|
| 54 |
checkOK(t, gotDate.In(UTCm2), td.TruncTime(gotDate.In(UTCp2))) |
| 55 |
checkOK(t, gotDate.In(UTCp2), td.TruncTime(gotDate.In(UTCm2))) |
| 56 |
|
| 57 |
expDate := gotDate |
| 58 |
|
| 59 |
checkOK(t, gotDate, td.TruncTime(expDate)) |
| 60 |
checkOK(t, gotDate, td.TruncTime(expDate, time.Second)) |
| 61 |
checkOK(t, gotDate, td.TruncTime(expDate, time.Minute)) |
| 62 |
|
| 63 |
expDate = expDate.Add(time.Second) |
| 64 |
checkError(t, gotDate, td.TruncTime(expDate, time.Second), |
| 65 |
expectedError{ |
| 66 |
Message: mustBe("values differ"), |
| 67 |
Path: mustBe("DATA"), |
| 68 |
Got: mustBe("2018-03-09 01:02:03.000000004 +0000 UTC\n" + |
| 69 |
"truncated to:\n" + |
| 70 |
"2018-03-09 01:02:03 +0000 UTC"), |
| 71 |
Expected: mustBe("2018-03-09 01:02:04 +0000 UTC"), |
| 72 |
}) |
| 73 |
checkOK(t, gotDate, td.TruncTime(expDate, time.Minute)) |
| 74 |
|
| 75 |
checkError(t, gotDate, td.TruncTime(MyTime(gotDate)), |
| 76 |
expectedError{ |
| 77 |
Message: mustBe("type mismatch"), |
| 78 |
Path: mustBe("DATA"), |
| 79 |
Got: mustBe("time.Time"), |
| 80 |
Expected: mustBe("td_test.MyTime"), |
| 81 |
}) |
| 82 |
|
| 83 |
// |
| 84 |
// Type convertible to time.Time NOT implementing fmt.Stringer |
| 85 |
gotMyDate := MyTime(gotDate) |
| 86 |
expMyDate := MyTime(gotDate) |
| 87 |
|
| 88 |
checkOK(t, gotMyDate, td.TruncTime(expMyDate)) |
| 89 |
checkOK(t, gotMyDate, td.TruncTime(expMyDate, time.Second)) |
| 90 |
checkOK(t, gotMyDate, td.TruncTime(expMyDate, time.Minute)) |
| 91 |
|
| 92 |
expMyDate = MyTime(gotDate.Add(time.Second)) |
| 93 |
checkError(t, gotMyDate, td.TruncTime(expMyDate, time.Second), |
| 94 |
expectedError{ |
| 95 |
Message: mustBe("values differ"), |
| 96 |
Path: mustBe("DATA"), |
| 97 |
Got: mustBe("2018-03-09 01:02:03.000000004 +0000 UTC\n" + |
| 98 |
"truncated to:\n" + |
| 99 |
"2018-03-09 01:02:03 +0000 UTC"), |
| 100 |
Expected: mustBe("2018-03-09 01:02:04 +0000 UTC"), |
| 101 |
}) |
| 102 |
checkOK(t, gotMyDate, td.TruncTime(expMyDate, time.Minute)) |
| 103 |
|
| 104 |
checkError(t, MyTime(gotDate), td.TruncTime(gotDate), |
| 105 |
expectedError{ |
| 106 |
Message: mustBe("type mismatch"), |
| 107 |
Path: mustBe("DATA"), |
| 108 |
Got: mustBe("td_test.MyTime"), |
| 109 |
Expected: mustBe("time.Time"), |
| 110 |
}) |
| 111 |
|
| 112 |
// |
| 113 |
// Type convertible to time.Time implementing fmt.Stringer |
| 114 |
gotMyStrDate := MyTimeStr(gotDate) |
| 115 |
expMyStrDate := MyTimeStr(gotDate) |
| 116 |
|
| 117 |
checkOK(t, gotMyStrDate, td.TruncTime(expMyStrDate)) |
| 118 |
checkOK(t, gotMyStrDate, td.TruncTime(expMyStrDate, time.Second)) |
| 119 |
checkOK(t, gotMyStrDate, td.TruncTime(expMyStrDate, time.Minute)) |
| 120 |
|
| 121 |
expMyStrDate = MyTimeStr(gotDate.Add(time.Second)) |
| 122 |
checkError(t, gotMyStrDate, td.TruncTime(expMyStrDate, time.Second), |
| 123 |
expectedError{ |
| 124 |
Message: mustBe("values differ"), |
| 125 |
Path: mustBe("DATA"), |
| 126 |
Got: mustBe("<<2018-03-09T01:02:03.000000004Z>>\n" + |
| 127 |
"truncated to:\n" + |
| 128 |
"<<2018-03-09T01:02:03Z>>"), |
| 129 |
Expected: mustBe("<<2018-03-09T01:02:04Z>>"), |
| 130 |
}) |
| 131 |
checkOK(t, gotMyStrDate, td.TruncTime(expMyStrDate, time.Minute)) |
| 132 |
|
| 133 |
checkError(t, MyTimeStr(gotDate), td.TruncTime(gotDate), |
| 134 |
expectedError{ |
| 135 |
Message: mustBe("type mismatch"), |
| 136 |
Path: mustBe("DATA"), |
| 137 |
Got: mustBe("td_test.MyTimeStr"), |
| 138 |
Expected: mustBe("time.Time"), |
| 139 |
}) |
| 140 |
|
| 141 |
// |
| 142 |
// Bad usage |
| 143 |
checkError(t, "never tested", |
| 144 |
td.TruncTime("test"), |
| 145 |
expectedError{ |
| 146 |
Message: mustBe("bad usage of TruncTime operator"), |
| 147 |
Path: mustBe("DATA"), |
| 148 |
Summary: mustBe("usage: TruncTime(time.Time[, time.Duration]), 1st parameter must be time.Time or convertible to time.Time, but not string"), |
| 149 |
}) |
| 150 |
|
| 151 |
checkError(t, "never tested", |
| 152 |
td.TruncTime(1, 2, 3), |
| 153 |
expectedError{ |
| 154 |
Message: mustBe("bad usage of TruncTime operator"), |
| 155 |
Path: mustBe("DATA"), |
| 156 |
Summary: mustBe("usage: TruncTime(time.Time[, time.Duration]), too many parameters"), |
| 157 |
}) |
| 158 |
|
| 159 |
// Erroneous op |
| 160 |
test.EqualStr(t, td.TruncTime("test").String(), "TruncTime(<ERROR>)") |
| 161 |
} |
| 162 |
|
| 163 |
func TestTruncTimeTypeBehind(t *testing.T) { |
| 164 |
type MyTime time.Time |
| 165 |
|
| 166 |
equalTypes(t, td.TruncTime(time.Time{}), time.Time{}) |
| 167 |
equalTypes(t, td.TruncTime(MyTime{}), MyTime{}) |
| 168 |
|
| 169 |
// Erroneous op |
| 170 |
equalTypes(t, td.TruncTime("test"), nil) |
| 171 |
} |