| 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 |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"reflect" |
| 12 |
"time" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/types" |
| 16 |
) |
| 17 |
|
| 18 |
type tdTruncTime struct { |
| 19 |
tdExpectedType |
| 20 |
expectedTime time.Time |
| 21 |
trunc time.Duration |
| 22 |
} |
| 23 |
|
| 24 |
var _ TestDeep = &tdTruncTime{} |
| 25 |
|
| 26 |
// summary(TruncTime): compares time.Time (or assignable) values after |
| 27 |
// truncating them |
| 28 |
// input(TruncTime): struct(time.Time),ptr(todo) |
| 29 |
|
| 30 |
// TruncTime operator compares [time.Time] (or assignable) values |
| 31 |
// after truncating them to the optional trunc duration. See |
| 32 |
// [time.Time.Truncate] for details about the truncation. |
| 33 |
// |
| 34 |
// If trunc is missing, it defaults to 0. |
| 35 |
// |
| 36 |
// During comparison, location does not matter as [time.Time.Equal] |
| 37 |
// method is used behind the scenes: a time instant in two different |
| 38 |
// locations is the same time instant. |
| 39 |
// |
| 40 |
// Whatever the trunc value is, the monotonic clock is stripped |
| 41 |
// before the comparison against expectedTime. |
| 42 |
// |
| 43 |
// gotDate := time.Date(2018, time.March, 9, 1, 2, 3, 999999999, time.UTC). |
| 44 |
// In(time.FixedZone("UTC+2", 2)) |
| 45 |
// |
| 46 |
// expected := time.Date(2018, time.March, 9, 1, 2, 3, 0, time.UTC) |
| 47 |
// |
| 48 |
// td.Cmp(t, gotDate, td.TruncTime(expected)) // fails, ns differ |
| 49 |
// td.Cmp(t, gotDate, td.TruncTime(expected, time.Second)) // succeeds |
| 50 |
// |
| 51 |
// TypeBehind method returns the [reflect.Type] of expectedTime. |
| 52 |
func TruncTime(expectedTime any, trunc ...time.Duration) TestDeep { |
| 53 |
const usage = "(time.Time[, time.Duration])" |
| 54 |
|
| 55 |
t := tdTruncTime{ |
| 56 |
tdExpectedType: tdExpectedType{ |
| 57 |
base: newBase(3), |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
if len(trunc) > 1 { |
| 62 |
t.err = ctxerr.OpTooManyParams("TruncTime", usage) |
| 63 |
return &t |
| 64 |
} |
| 65 |
|
| 66 |
if len(trunc) == 1 { |
| 67 |
t.trunc = trunc[0] |
| 68 |
} |
| 69 |
|
| 70 |
vval := reflect.ValueOf(expectedTime) |
| 71 |
|
| 72 |
t.expectedType = vval.Type() |
| 73 |
if t.expectedType == types.Time { |
| 74 |
t.expectedTime = expectedTime.(time.Time).Truncate(t.trunc) |
| 75 |
return &t |
| 76 |
} |
| 77 |
if !t.expectedType.ConvertibleTo(types.Time) { // 1.17 ok as time.Time is a struct |
| 78 |
t.err = ctxerr.OpBad("TruncTime", "usage: TruncTime%s, 1st parameter must be time.Time or convertible to time.Time, but not %T", |
| 79 |
usage, expectedTime) |
| 80 |
return &t |
| 81 |
} |
| 82 |
|
| 83 |
t.expectedTime = vval.Convert(types.Time). |
| 84 |
Interface().(time.Time).Truncate(t.trunc) |
| 85 |
return &t |
| 86 |
} |
| 87 |
|
| 88 |
func (t *tdTruncTime) Match(ctx ctxerr.Context, got reflect.Value) *ctxerr.Error { |
| 89 |
if t.err != nil { |
| 90 |
return ctx.CollectError(t.err) |
| 91 |
} |
| 92 |
|
| 93 |
err := t.checkType(ctx, got) |
| 94 |
if err != nil { |
| 95 |
return ctx.CollectError(err) |
| 96 |
} |
| 97 |
|
| 98 |
gotTime, err := getTime(ctx, got, got.Type() != types.Time) |
| 99 |
if err != nil { |
| 100 |
return ctx.CollectError(err) |
| 101 |
} |
| 102 |
gotTimeTrunc := gotTime.Truncate(t.trunc) |
| 103 |
|
| 104 |
if gotTimeTrunc.Equal(t.expectedTime) { |
| 105 |
return nil |
| 106 |
} |
| 107 |
|
| 108 |
// Fail |
| 109 |
if ctx.BooleanError { |
| 110 |
return ctxerr.BooleanError |
| 111 |
} |
| 112 |
|
| 113 |
var gotRawStr, gotTruncStr string |
| 114 |
if t.expectedType != types.Time && |
| 115 |
t.expectedType.Implements(types.FmtStringer) { |
| 116 |
gotRawStr = got.Interface().(fmt.Stringer).String() |
| 117 |
gotTruncStr = reflect.ValueOf(gotTimeTrunc).Convert(t.expectedType). |
| 118 |
Interface().(fmt.Stringer).String() |
| 119 |
} else { |
| 120 |
gotRawStr = gotTime.String() |
| 121 |
gotTruncStr = gotTimeTrunc.String() |
| 122 |
} |
| 123 |
|
| 124 |
return ctx.CollectError(&ctxerr.Error{ |
| 125 |
Message: "values differ", |
| 126 |
Got: types.RawString(gotRawStr + "\ntruncated to:\n" + gotTruncStr), |
| 127 |
Expected: t, |
| 128 |
}) |
| 129 |
} |
| 130 |
|
| 131 |
func (t *tdTruncTime) String() string { |
| 132 |
if t.err != nil { |
| 133 |
return t.stringError() |
| 134 |
} |
| 135 |
|
| 136 |
if t.expectedType.Implements(types.FmtStringer) { |
| 137 |
return reflect.ValueOf(t.expectedTime).Convert(t.expectedType). |
| 138 |
Interface().(fmt.Stringer).String() |
| 139 |
} |
| 140 |
return t.expectedTime.String() |
| 141 |
} |