File: check.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 (160 lines) | stat: -rw-r--r-- 3,153 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) 2018, 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 test

import (
	"strings"
	"testing"
	"unicode"

	"github.com/davecgh/go-spew/spew"

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

// EqualErrorMessage prints a test error message of the form:
//
//	Message
//	Failed test
//	       got: got_value
//	  expected: expected_value
func EqualErrorMessage(t *testing.T, got, expected any,
	args ...any) {
	t.Helper()

	testName := tdutil.BuildTestName(args...)
	if testName != "" {
		testName = " '" + testName + "'"
	}

	t.Errorf(`Failed test%s
	     got: %v
	expected: %v`,
		testName, got, expected)
}

func spewIfNeeded(s string) string {
	for _, chr := range s {
		if !unicode.IsPrint(chr) {
			return strings.TrimRight(spew.Sdump(s), "\n")
		}
	}
	return s
}

// EqualStr checks that got equals expected.
func EqualStr(t *testing.T, got, expected string, args ...any) bool {
	if got == expected {
		return true
	}

	t.Helper()
	EqualErrorMessage(t, spewIfNeeded(got), spewIfNeeded(expected), args...)
	return false
}

// EqualInt checks that got equals expected.
func EqualInt(t *testing.T, got, expected int, args ...any) bool {
	if got == expected {
		return true
	}

	t.Helper()
	EqualErrorMessage(t, got, expected, args...)
	return false
}

// EqualBool checks that got equals expected.
func EqualBool(t *testing.T, got, expected bool, args ...any) bool {
	if got == expected {
		return true
	}

	t.Helper()
	EqualErrorMessage(t, got, expected, args...)
	return false
}

// IsTrue checks that got is true.
func IsTrue(t *testing.T, got bool, args ...any) bool {
	if got {
		return true
	}

	t.Helper()
	EqualErrorMessage(t, false, true, args...)
	return false
}

// IsFalse checks that got is false.
func IsFalse(t *testing.T, got bool, args ...any) bool {
	if !got {
		return true
	}

	t.Helper()
	EqualErrorMessage(t, true, false, args...)
	return false
}

// CheckPanic checks that fn() panics and that the panic() arg is a
// string that contains contains.
func CheckPanic(t *testing.T, fn func(), contains string) bool {
	t.Helper()

	var (
		panicked   bool
		panicParam any
	)

	func() {
		defer func() { panicParam = recover() }()
		panicked = true
		fn()
		panicked = false
	}()

	if !panicked {
		t.Error("panic() did not occur")
		return false
	}

	panicStr, ok := panicParam.(string)
	if !ok {
		t.Errorf("panic() occurred but recover()d %T type (%v) instead of string",
			panicParam, panicParam)
		return false
	}

	if !strings.Contains(panicStr, contains) {
		t.Errorf("panic() string `%s'\ndoes not contain `%s'", panicStr, contains)
		return false
	}
	return true
}

// NoError checks that err is nil.
func NoError(t *testing.T, err error, args ...any) bool {
	if err == nil {
		return true
	}

	t.Helper()
	EqualErrorMessage(t, err, nil, args...)
	return false
}

// Error checks that err is non-nil.
func Error(t *testing.T, err error, args ...any) bool {
	if err != nil {
		return true
	}

	t.Helper()
	EqualErrorMessage(t, err, "<non-nil>", args...)
	return false
}