| 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 |
"bytes" |
| 11 |
"reflect" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/maxatome/go-testdeep/internal/ctxerr" |
| 15 |
"github.com/maxatome/go-testdeep/internal/test" |
| 16 |
"github.com/maxatome/go-testdeep/internal/trace" |
| 17 |
) |
| 18 |
|
| 19 |
func TestStripTrace(t *testing.T) { |
| 20 |
check := func(got, expected trace.Stack) { |
| 21 |
got = stripTrace(got) |
| 22 |
if !reflect.DeepEqual(got, expected) { |
| 23 |
t.Helper() |
| 24 |
t.Errorf("\n got: %#v\nexpected: %#v", got, expected) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
check(nil, nil) |
| 29 |
|
| 30 |
s := trace.Stack{ |
| 31 |
{Package: "test", Func: "A"}, |
| 32 |
} |
| 33 |
check(s, s) |
| 34 |
|
| 35 |
s = trace.Stack{ |
| 36 |
{Package: "test", Func: "A"}, |
| 37 |
{Package: "test", Func: "TestSimple"}, |
| 38 |
} |
| 39 |
check(s, s) |
| 40 |
|
| 41 |
// inside testing.Cleanup() call |
| 42 |
s = trace.Stack{ |
| 43 |
{Package: "test", Func: "TestCleanup.func2"}, |
| 44 |
{Package: "testing", Func: "(*common).Cleanup.func1"}, |
| 45 |
{Package: "testing", Func: "(*common).runCleanup"}, |
| 46 |
{Package: "testing", Func: "tRunner.func2"}, |
| 47 |
} |
| 48 |
check(s, s[:1]) |
| 49 |
|
| 50 |
// |
| 51 |
// td |
| 52 |
// |
| 53 |
// td.(*T).Run() call |
| 54 |
s = trace.Stack{ |
| 55 |
{Package: "test", Func: "A"}, |
| 56 |
{Package: "test", Func: "TestSubtestTd.func1"}, |
| 57 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).Run.func1"}, |
| 58 |
} |
| 59 |
check(s, s[:2]) |
| 60 |
|
| 61 |
// td.(*T).RunAssertRequire() call |
| 62 |
s = trace.Stack{ |
| 63 |
{Package: "test", Func: "A"}, |
| 64 |
{Package: "test", Func: "TestSubtestTd.func1"}, |
| 65 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).RunAssertRequire.func1"}, |
| 66 |
} |
| 67 |
check(s, s[:2]) |
| 68 |
|
| 69 |
// |
| 70 |
// tdhttp |
| 71 |
// |
| 72 |
// tdhttp.(*TestAPI).Run() call |
| 73 |
s = trace.Stack{ |
| 74 |
{Package: "test", Func: "A"}, |
| 75 |
{Package: "test", Func: "TestSubtestTd.func1"}, |
| 76 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdhttp", Func: "(*TestAPI).Run.func1"}, |
| 77 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).Run.func1"}, |
| 78 |
} |
| 79 |
check(s, s[:2]) |
| 80 |
|
| 81 |
// |
| 82 |
// tdsuite |
| 83 |
// |
| 84 |
// tdsuite.Run() call → TestSuite(*td.T) |
| 85 |
s = trace.Stack{ |
| 86 |
{Package: "test", Func: "A"}, |
| 87 |
{Package: "test", Func: "Suite.TestSuite"}, |
| 88 |
{Package: "reflect", Func: "Value.call"}, |
| 89 |
{Package: "reflect", Func: "Value.Call"}, |
| 90 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run.func2"}, |
| 91 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).Run.func1"}, |
| 92 |
} |
| 93 |
check(s, s[:2]) |
| 94 |
|
| 95 |
// tdsuite.Run() call → TestSuite(assert, require *td.T) |
| 96 |
s = trace.Stack{ |
| 97 |
{Package: "test", Func: "A"}, |
| 98 |
{Package: "test", Func: "Suite.TestSuite"}, |
| 99 |
{Package: "reflect", Func: "Value.call"}, |
| 100 |
{Package: "reflect", Func: "Value.Call"}, |
| 101 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run.func1"}, |
| 102 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).RunAssertRequire.func1"}, |
| 103 |
} |
| 104 |
check(s, s[:2]) |
| 105 |
|
| 106 |
// tdsuite.Run() call → Suite.Setup() |
| 107 |
s = trace.Stack{ |
| 108 |
{Package: "test", Func: "A"}, |
| 109 |
{Package: "test", Func: "Suite.Setup"}, |
| 110 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run"}, |
| 111 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "Run"}, |
| 112 |
{Package: "test", Func: "TestSuiteSetup"}, |
| 113 |
} |
| 114 |
check(s, append(s[:2:2], s[4])) |
| 115 |
|
| 116 |
// tdsuite.Run() call → Suite.PreTest() |
| 117 |
s = trace.Stack{ |
| 118 |
{Package: "test", Func: "A"}, |
| 119 |
{Package: "test", Func: "Suite.PreTest"}, |
| 120 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run.func2"}, |
| 121 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).Run.func1"}, |
| 122 |
} |
| 123 |
check(s, s[:2]) |
| 124 |
|
| 125 |
// tdsuite.Run() call → Suite.PostTest() |
| 126 |
s = trace.Stack{ |
| 127 |
{Package: "test", Func: "A"}, |
| 128 |
{Package: "test", Func: "Suite.PostTest"}, |
| 129 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run.func2.1"}, |
| 130 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run.func2"}, |
| 131 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).Run.func1"}, |
| 132 |
} |
| 133 |
check(s, s[:2]) |
| 134 |
|
| 135 |
// tdsuite.Run() call → Suite.BetweenTests() |
| 136 |
s = trace.Stack{ |
| 137 |
{Package: "test", Func: "A"}, |
| 138 |
{Package: "test", Func: "Suite.BetweenTests"}, |
| 139 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run"}, |
| 140 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "Run"}, |
| 141 |
{Package: "test", Func: "TestSuiteBetweenTests"}, |
| 142 |
} |
| 143 |
check(s, append(s[:2:2], s[4])) |
| 144 |
|
| 145 |
// tdsuite.Run() call → Suite.Destroy() |
| 146 |
s = trace.Stack{ |
| 147 |
{Package: "test", Func: "A"}, |
| 148 |
{Package: "test", Func: "Suite.Destroy"}, |
| 149 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run.func1"}, |
| 150 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run"}, |
| 151 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "Run"}, |
| 152 |
{Package: "test", Func: "TestSuiteDestroy"}, |
| 153 |
} |
| 154 |
check(s, append(s[:2:2], s[5])) |
| 155 |
|
| 156 |
// Improbable cases |
| 157 |
s = trace.Stack{ |
| 158 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "Run"}, |
| 159 |
{Package: "test", Func: "TestSuiteDestroy"}, |
| 160 |
} |
| 161 |
check(s, s[:1]) |
| 162 |
|
| 163 |
s = trace.Stack{ |
| 164 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "y"}, |
| 165 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "x"}, |
| 166 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "Run"}, |
| 167 |
{Package: "test", Func: "TestSuiteDestroy"}, |
| 168 |
} |
| 169 |
check(s, s[:1]) |
| 170 |
|
| 171 |
s = trace.Stack{ |
| 172 |
{Package: "test", Func: "Suite.TestXxx"}, |
| 173 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "Run"}, |
| 174 |
{Package: "test", Func: "TestSuiteDestroy"}, |
| 175 |
} |
| 176 |
check(s, append(s[:1:1], s[2])) |
| 177 |
|
| 178 |
s = trace.Stack{ |
| 179 |
{Package: "reflect", Func: "Value.call"}, |
| 180 |
{Package: "reflect", Func: "Value.Call"}, |
| 181 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run.func1"}, |
| 182 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).RunAssertRequire.func1"}, |
| 183 |
} |
| 184 |
check(s, nil) |
| 185 |
|
| 186 |
s = trace.Stack{ |
| 187 |
{Package: "test", Func: "A"}, |
| 188 |
{Package: "test", Func: "Suite.TestSuite"}, |
| 189 |
{Package: "github.com/maxatome/go-testdeep/helpers/tdsuite", Func: "run.func1"}, |
| 190 |
{Package: "github.com/maxatome/go-testdeep/td", Func: "(*T).RunAssertRequire.func1"}, |
| 191 |
} |
| 192 |
check(s, s[:2]) |
| 193 |
} |
| 194 |
|
| 195 |
func TestFormatError(t *testing.T) { |
| 196 |
err := &ctxerr.Error{ |
| 197 |
Context: newContext(nil), |
| 198 |
Message: "test error message", |
| 199 |
Summary: ctxerr.NewSummary("test error summary"), |
| 200 |
} |
| 201 |
|
| 202 |
nonStringName := bytes.NewBufferString("zip!") |
| 203 |
|
| 204 |
for _, fatal := range []bool{false, true} { |
| 205 |
// |
| 206 |
// Without args |
| 207 |
ttt := test.NewTestingT() |
| 208 |
ttt.CatchFatal(func() { formatError(ttt, fatal, err) }) |
| 209 |
test.EqualStr(t, ttt.LastMessage(), `Failed test |
| 210 |
DATA: test error message |
| 211 |
test error summary`) |
| 212 |
test.EqualBool(t, ttt.IsFatal, fatal) |
| 213 |
|
| 214 |
// |
| 215 |
// With one arg |
| 216 |
ttt = test.NewTestingT() |
| 217 |
ttt.CatchFatal(func() { formatError(ttt, fatal, err, "foo bar!") }) |
| 218 |
test.EqualStr(t, ttt.LastMessage(), `Failed test 'foo bar!' |
| 219 |
DATA: test error message |
| 220 |
test error summary`) |
| 221 |
test.EqualBool(t, ttt.IsFatal, fatal) |
| 222 |
|
| 223 |
ttt = test.NewTestingT() |
| 224 |
ttt.CatchFatal(func() { formatError(ttt, fatal, err, nonStringName) }) |
| 225 |
test.EqualStr(t, ttt.LastMessage(), `Failed test 'zip!' |
| 226 |
DATA: test error message |
| 227 |
test error summary`) |
| 228 |
test.EqualBool(t, ttt.IsFatal, fatal) |
| 229 |
|
| 230 |
// |
| 231 |
// With several args & Printf format |
| 232 |
ttt = test.NewTestingT() |
| 233 |
ttt.CatchFatal(func() { formatError(ttt, fatal, err, "hello %d!", 123) }) |
| 234 |
test.EqualStr(t, ttt.LastMessage(), `Failed test 'hello 123!' |
| 235 |
DATA: test error message |
| 236 |
test error summary`) |
| 237 |
test.EqualBool(t, ttt.IsFatal, fatal) |
| 238 |
|
| 239 |
// |
| 240 |
// With several args & Printf format + Flatten |
| 241 |
ttt = test.NewTestingT() |
| 242 |
ttt.CatchFatal(func() { |
| 243 |
formatError(ttt, fatal, err, "hello %s → %d/%d!", "bob", Flatten([]int{123, 125})) |
| 244 |
}) |
| 245 |
test.EqualStr(t, ttt.LastMessage(), `Failed test 'hello bob → 123/125!' |
| 246 |
DATA: test error message |
| 247 |
test error summary`) |
| 248 |
test.EqualBool(t, ttt.IsFatal, fatal) |
| 249 |
|
| 250 |
// |
| 251 |
// With several args without Printf format |
| 252 |
ttt = test.NewTestingT() |
| 253 |
ttt.CatchFatal(func() { formatError(ttt, fatal, err, "hello ", "world! ", 123) }) |
| 254 |
test.EqualStr(t, ttt.LastMessage(), `Failed test 'hello world! 123' |
| 255 |
DATA: test error message |
| 256 |
test error summary`) |
| 257 |
test.EqualBool(t, ttt.IsFatal, fatal) |
| 258 |
|
| 259 |
// |
| 260 |
// With several args without Printf format + Flatten |
| 261 |
ttt = test.NewTestingT() |
| 262 |
ttt.CatchFatal(func() { formatError(ttt, fatal, err, "hello ", "world! ", Flatten([]int{123, 125})) }) |
| 263 |
test.EqualStr(t, ttt.LastMessage(), `Failed test 'hello world! 123 125' |
| 264 |
DATA: test error message |
| 265 |
test error summary`) |
| 266 |
test.EqualBool(t, ttt.IsFatal, fatal) |
| 267 |
|
| 268 |
ttt = test.NewTestingT() |
| 269 |
ttt.CatchFatal(func() { formatError(ttt, fatal, err, nonStringName, "hello ", "world! ", 123) }) |
| 270 |
test.EqualStr(t, ttt.LastMessage(), `Failed test 'zip!hello world! 123' |
| 271 |
DATA: test error message |
| 272 |
test error summary`) |
| 273 |
test.EqualBool(t, ttt.IsFatal, fatal) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func TestS(t *testing.T) { |
| 278 |
for i, curTest := range []struct { |
| 279 |
params []any |
| 280 |
expected string |
| 281 |
}{ |
| 282 |
{ |
| 283 |
params: []any{}, |
| 284 |
expected: "", |
| 285 |
}, |
| 286 |
{ |
| 287 |
params: []any{"pipo", "bingo"}, |
| 288 |
expected: "pipobingo", |
| 289 |
}, |
| 290 |
{ |
| 291 |
params: []any{"pipo %d %s", 42, "bingo"}, |
| 292 |
expected: "pipo 42 bingo", |
| 293 |
}, |
| 294 |
{ |
| 295 |
params: []any{"pipo %d"}, |
| 296 |
expected: "pipo %d", |
| 297 |
}, |
| 298 |
{ |
| 299 |
params: []any{"pipo %", 42}, |
| 300 |
expected: "pipo %42", |
| 301 |
}, |
| 302 |
{ |
| 303 |
params: []any{42, 666}, |
| 304 |
expected: "42 666", |
| 305 |
}, |
| 306 |
} { |
| 307 |
test.EqualStr(t, S(curTest.params...), curTest.expected, "#%d", i) |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
func TestCmp(t *testing.T) { |
| 312 |
tt := test.NewTestingTB(t.Name()) |
| 313 |
test.IsTrue(t, Cmp(tt, 1, 1)) |
| 314 |
test.IsFalse(t, tt.Failed()) |
| 315 |
|
| 316 |
tt = test.NewTestingTB(t.Name()) |
| 317 |
test.IsFalse(t, Cmp(tt, 1, 2)) |
| 318 |
test.IsTrue(t, tt.Failed()) |
| 319 |
} |
| 320 |
|
| 321 |
func TestCmpDeeply(t *testing.T) { |
| 322 |
tt := test.NewTestingTB(t.Name()) |
| 323 |
test.IsTrue(t, CmpDeeply(tt, 1, 1)) |
| 324 |
test.IsFalse(t, tt.Failed()) |
| 325 |
|
| 326 |
tt = test.NewTestingTB(t.Name()) |
| 327 |
test.IsFalse(t, CmpDeeply(tt, 1, 2)) |
| 328 |
test.IsTrue(t, tt.Failed()) |
| 329 |
} |