-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_result.go
92 lines (78 loc) · 2.34 KB
/
test_result.go
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
package faket
import (
"fmt"
"path/filepath"
"sort"
"strings"
"github.com/prashantv/faket/internal/sliceutil"
)
// TestResult is the result of runnming a test against a fake [testing.TB].
type TestResult struct {
res *fakeTB
}
// Logs represents a list of logged entries.
type Logs []Log
// Log is a single log entry, along with caller information.
type Log struct {
Message string
CallerFile string
CallerLine int
CallerFunc string
// TBFunc is the testing.TB function that generated this log message.
TBFunc string
}
// Failed reports if a test failed.
func (r TestResult) Failed() bool {
return r.res.Failed()
}
// Panicked reports if a test panicked.
func (r TestResult) Panicked() bool {
return r.res.panicked
}
// Skipped reports if a test was skipped.
//
// If a test failed before it was skipped, then Failed takes precedence
// and Skipped returns false. To check if the test was skipped after a failure
// see [TestResult.FailedAndSkipped].
func (r TestResult) Skipped() bool {
// Above behaviour is for consistency with testing.TB, from SkipNow docs:
// > If a test fails (see Error, Errorf, Fail) and is then skipped,
// > it is still considered to have failed.
if r.Failed() {
return false
}
return r.res.Skipped()
}
// FailedAndSkipped reports if a test failed, and then was skipped.
//
// See [TestResult.Skipped] for more details for how this differs from using
// [TestResult.Failed] and [TestResult.Skipped].
func (r TestResult) FailedAndSkipped() bool {
return r.res.Failed() && r.res.Skipped()
}
// Helpers returns a list of functions that have called [testing.TB].Helper.
// The returned list is sorted by the full package+function.
func (r TestResult) Helpers() []string {
funcs := r.res.helperFuncs()
sort.Strings(funcs)
return funcs
}
// Logs returns a list of log entries logged by the test.
func (r TestResult) Logs() Logs {
return sliceutil.Map(r.res.logs, r.res.toLog)
}
// Messages returns a list of individual logs.
func (ls Logs) Messages() []string {
return sliceutil.Map(ls, func(l Log) string {
return l.Message
})
}
// String returns the log output, as it would be printed by `go test`
// with caller information.
func (ls Logs) String() string {
var buf strings.Builder
for _, l := range ls {
fmt.Fprintf(&buf, "%s:%d: %v\n", filepath.Base(l.CallerFile), l.CallerLine, l.Message)
}
return buf.String()
}