File: t_test.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (93 lines) | stat: -rw-r--r-- 2,058 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2019, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

package tdutil_test

import (
	"strings"
	"testing"

	"github.com/maxatome/go-testdeep/helpers/tdutil"
	"github.com/maxatome/go-testdeep/internal/test"
)

func TestT(t *testing.T) {
	mockT := tdutil.NewT("hey!")

	if n := mockT.Name(); n != "hey!" {
		t.Errorf(`Test name is not correct, got: %s, expected: hey!`, n)
	}

	mockT.Log("Hey this is a log message!")

	buf := mockT.LogBuf()
	if !strings.HasSuffix(buf, "Hey this is a log message!\n") {
		t.Errorf(`LogBuf does not work as expected: "%s"`, buf)
	}
}

func TestFailNow(t *testing.T) {
	mockT := tdutil.NewT("hey!")

	test.IsFalse(t, mockT.CatchFailNow(func() {}))

	test.IsTrue(t, mockT.CatchFailNow(func() { mockT.FailNow() }))
	test.IsTrue(t, mockT.CatchFailNow(func() { mockT.Fatal("Ouch!") }))
	test.IsTrue(t, mockT.CatchFailNow(func() { mockT.Fatalf("Ouch!") }))

	// No FailNow() but panic()
	var (
		panicked, failNowOccurred bool
		panicParam                any
	)
	func() {
		defer func() { panicParam = recover() }()

		panicked = true
		failNowOccurred = mockT.CatchFailNow(func() { panic("Boom!") })
		panicked = false
	}()

	test.IsFalse(t, failNowOccurred)
	if test.IsTrue(t, panicked) {
		panicStr, ok := panicParam.(string)
		if test.IsTrue(t, ok) {
			test.EqualStr(t, panicStr, "Boom!")
		}
	}
}

func TestRun(t *testing.T) {
	for i, curTest := range []struct {
		fn       func(*testing.T)
		expected bool
	}{
		{
			fn:       func(*testing.T) {},
			expected: true,
		},
		{
			fn: func(t *testing.T) {
				t.Error("An error occurred!")
			},
			expected: false,
		},
	} {
		mockT := &tdutil.T{}

		var called bool
		res := mockT.Run("testname", func(t *testing.T) {
			called = true
			curTest.fn(t)
		})
		if !called {
			t.Errorf("Run#%d func not called", i)
		}
		if res != curTest.expected {
			t.Errorf("Run#%d returned %v ≠ expected %v", i, res, curTest.expected)
		}
	}
}