Debian Codegraph @

td/utils_test.go

raw
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 "testing"
13 "time"
14
15 "github.com/maxatome/go-testdeep/internal/ctxerr"
16 "github.com/maxatome/go-testdeep/internal/dark"
17 "github.com/maxatome/go-testdeep/internal/test"
18 )
19
20 func TestGetTime(t *testing.T) {
21 type MyTime time.Time
22
23 oneTime := time.Date(2018, 7, 14, 12, 11, 10, 0, time.UTC)
24
25 // OK cases
26 for idx, curTest := range []struct {
27 ParamGot any
28 ParamMustConvert bool
29 ExpectedTime time.Time
30 }{
31 {
32 ParamGot: oneTime,
33 ExpectedTime: oneTime,
34 },
35 {
36 ParamGot: MyTime(oneTime),
37 ParamMustConvert: true,
38 ExpectedTime: oneTime,
39 },
40 } {
41 testName := fmt.Sprintf("Test #%d: ", idx)
42
43 tm, err := getTime(newContext(nil),
44 reflect.ValueOf(curTest.ParamGot), curTest.ParamMustConvert)
45
46 if !tm.Equal(curTest.ExpectedTime) {
47 test.EqualErrorMessage(t, tm, curTest.ExpectedTime,
48 testName+"time")
49 }
50
51 if err != nil {
52 test.EqualErrorMessage(t, err, "no error",
53 testName+"should NOT return an error")
54 }
55 }
56
57 // Simulate error return from dark.GetInterface
58 oldGetInterface := dark.GetInterface
59 defer func() { dark.GetInterface = oldGetInterface }()
60 dark.GetInterface = func(val reflect.Value, force bool) (any, bool) {
61 return nil, false
62 }
63
64 // Error cases
65 for idx, ctx := range []ctxerr.Context{newContext(nil), newBooleanContext()} {
66 testName := fmt.Sprintf("Test #%d: ", idx)
67
68 tm, err := getTime(ctx, reflect.ValueOf(oneTime), false)
69
70 if !tm.Equal(time.Time{}) {
71 test.EqualErrorMessage(t, tm, time.Time{}, testName+"time")
72 }
73
74 if err == nil {
75 test.EqualErrorMessage(t, "no error", err,
76 testName+"should return an error")
77 }
78 }
79 }