| 1 |
// Copyright (c) 2019-2026, 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 |
"encoding/json" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 15 |
"github.com/maxatome/go-testdeep/internal/test" |
| 16 |
"github.com/maxatome/go-testdeep/td" |
| 17 |
) |
| 18 |
|
| 19 |
// Starting go1.27, the first file of the stack trace is not |
| 20 |
// types_test.go but module_path/td/types_test.go. So as a "/" is |
| 21 |
// detected in file name, go-testdeep automatically add "This is how |
| 22 |
// we got here" paragraph. |
| 23 |
var thisIsHowWeGotHere = func(line int) string { return "" } |
| 24 |
|
| 25 |
func TestSetlocation(t *testing.T) { |
| 26 |
//nolint: gocritic |
| 27 |
//line types_test.go:10 |
| 28 |
tt := tdutil.NewT("test") |
| 29 |
ok := td.Cmp(tt, 12, 13) |
| 30 |
if !ok { |
| 31 |
test.EqualStr(t, tt.LogBuf(), ` types_test.go:11: Failed test |
| 32 |
DATA: values differ |
| 33 |
got: 12 |
| 34 |
expected: 13 |
| 35 |
`+thisIsHowWeGotHere(11)) |
| 36 |
} else { |
| 37 |
t.Error("Cmp returned true!") |
| 38 |
} |
| 39 |
|
| 40 |
//nolint: gocritic |
| 41 |
//line types_test.go:20 |
| 42 |
tt = tdutil.NewT("test") |
| 43 |
ok = td.Cmp(tt, |
| 44 |
12, |
| 45 |
td.Any(13, 14, 15)) |
| 46 |
if !ok { |
| 47 |
test.EqualStr(t, tt.LogBuf(), ` types_test.go:21: Failed test |
| 48 |
DATA: comparing with Any |
| 49 |
got: 12 |
| 50 |
expected: Any(13, |
| 51 |
14, |
| 52 |
15) |
| 53 |
[under operator Any at types_test.go:23] |
| 54 |
`+thisIsHowWeGotHere(21)) |
| 55 |
} else { |
| 56 |
t.Error("Cmp returned true!") |
| 57 |
} |
| 58 |
|
| 59 |
//nolint: gocritic |
| 60 |
//line types_test.go:30 |
| 61 |
tt = tdutil.NewT("test") |
| 62 |
ok = td.CmpAny(tt, |
| 63 |
12, |
| 64 |
[]any{13, 14, 15}) |
| 65 |
if !ok { |
| 66 |
test.EqualStr(t, tt.LogBuf(), ` types_test.go:31: Failed test |
| 67 |
DATA: comparing with Any |
| 68 |
got: 12 |
| 69 |
expected: Any(13, |
| 70 |
14, |
| 71 |
15) |
| 72 |
`+thisIsHowWeGotHere(31)) |
| 73 |
} else { |
| 74 |
t.Error("CmpAny returned true!") |
| 75 |
} |
| 76 |
|
| 77 |
//nolint: gocritic |
| 78 |
//line types_test.go:40 |
| 79 |
tt = tdutil.NewT("test") |
| 80 |
ttt := td.NewT(tt) |
| 81 |
ok = ttt.Cmp( |
| 82 |
12, |
| 83 |
td.Any(13, 14, 15)) |
| 84 |
if !ok { |
| 85 |
test.EqualStr(t, tt.LogBuf(), ` types_test.go:42: Failed test |
| 86 |
DATA: comparing with Any |
| 87 |
got: 12 |
| 88 |
expected: Any(13, |
| 89 |
14, |
| 90 |
15) |
| 91 |
[under operator Any at types_test.go:44] |
| 92 |
`+thisIsHowWeGotHere(42)) |
| 93 |
} else { |
| 94 |
t.Error("Cmp returned true!") |
| 95 |
} |
| 96 |
|
| 97 |
//nolint: gocritic |
| 98 |
//line types_test.go:50 |
| 99 |
tt = tdutil.NewT("test") |
| 100 |
ttt = td.NewT(tt) |
| 101 |
ok = ttt.Any( |
| 102 |
12, |
| 103 |
[]any{13, 14, 15}) |
| 104 |
if !ok { |
| 105 |
test.EqualStr(t, tt.LogBuf(), ` types_test.go:52: Failed test |
| 106 |
DATA: comparing with Any |
| 107 |
got: 12 |
| 108 |
expected: Any(13, |
| 109 |
14, |
| 110 |
15) |
| 111 |
`+thisIsHowWeGotHere(52)) |
| 112 |
} else { |
| 113 |
t.Error("Cmp returned true!") |
| 114 |
} |
| 115 |
|
| 116 |
//line /a/full/path/types_test.go:50 |
| 117 |
tt = tdutil.NewT("test") |
| 118 |
ttt = td.NewT(tt) |
| 119 |
ok = ttt.Any( |
| 120 |
12, |
| 121 |
[]any{13, 14, 15}) |
| 122 |
if !ok { |
| 123 |
test.EqualStr(t, tt.LogBuf(), ` types_test.go:52: Failed test |
| 124 |
DATA: comparing with Any |
| 125 |
got: 12 |
| 126 |
expected: Any(13, |
| 127 |
14, |
| 128 |
15) |
| 129 |
This is how we got here: |
| 130 |
TestSetlocation() /a/full/path/types_test.go:52 |
| 131 |
`) // at least one '/' in file name → "This is how we got here" |
| 132 |
} else { |
| 133 |
t.Error("Cmp returned true!") |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
func TestError(t *testing.T) { |
| 138 |
test.NoError(t, td.Re(`x`).Error()) |
| 139 |
test.Error(t, td.Re(123).Error()) |
| 140 |
} |
| 141 |
|
| 142 |
func TestMarshalJSON(t *testing.T) { |
| 143 |
op := td.String("foo") |
| 144 |
|
| 145 |
_, err := json.Marshal(op) |
| 146 |
if test.Error(t, err) { |
| 147 |
test.IsTrue(t, strings.HasSuffix(err.Error(), "String TestDeep operator cannot be json.Marshal'led")) |
| 148 |
} |
| 149 |
} |