Debian Codegraph @

helpers/tdutil/t_test.go

raw
1 // Copyright (c) 2019, 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 tdutil_test
8
9 import (
10 "strings"
11 "testing"
12
13 "github.com/maxatome/go-testdeep/helpers/tdutil"
14 "github.com/maxatome/go-testdeep/internal/test"
15 )
16
17 func TestT(t *testing.T) {
18 mockT := tdutil.NewT("hey!")
19
20 if n := mockT.Name(); n != "hey!" {
21 t.Errorf(`Test name is not correct, got: %s, expected: hey!`, n)
22 }
23
24 mockT.Log("Hey this is a log message!")
25
26 buf := mockT.LogBuf()
27 if !strings.HasSuffix(buf, "Hey this is a log message!\n") {
28 t.Errorf(`LogBuf does not work as expected: "%s"`, buf)
29 }
30 }
31
32 func TestFailNow(t *testing.T) {
33 mockT := tdutil.NewT("hey!")
34
35 test.IsFalse(t, mockT.CatchFailNow(func() {}))
36
37 test.IsTrue(t, mockT.CatchFailNow(func() { mockT.FailNow() }))
38 test.IsTrue(t, mockT.CatchFailNow(func() { mockT.Fatal("Ouch!") }))
39 test.IsTrue(t, mockT.CatchFailNow(func() { mockT.Fatalf("Ouch!") }))
40
41 // No FailNow() but panic()
42 var (
43 panicked, failNowOccurred bool
44 panicParam any
45 )
46 func() {
47 defer func() { panicParam = recover() }()
48
49 panicked = true
50 failNowOccurred = mockT.CatchFailNow(func() { panic("Boom!") })
51 panicked = false
52 }()
53
54 test.IsFalse(t, failNowOccurred)
55 if test.IsTrue(t, panicked) {
56 panicStr, ok := panicParam.(string)
57 if test.IsTrue(t, ok) {
58 test.EqualStr(t, panicStr, "Boom!")
59 }
60 }
61 }
62
63 func TestRun(t *testing.T) {
64 for i, curTest := range []struct {
65 fn func(*testing.T)
66 expected bool
67 }{
68 {
69 fn: func(*testing.T) {},
70 expected: true,
71 },
72 {
73 fn: func(t *testing.T) {
74 t.Error("An error occurred!")
75 },
76 expected: false,
77 },
78 } {
79 mockT := &tdutil.T{}
80
81 var called bool
82 res := mockT.Run("testname", func(t *testing.T) {
83 called = true
84 curTest.fn(t)
85 })
86 if !called {
87 t.Errorf("Run#%d func not called", i)
88 }
89 if res != curTest.expected {
90 t.Errorf("Run#%d returned %v ≠ expected %v", i, res, curTest.expected)
91 }
92 }
93 }