-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathscripts.go
96 lines (85 loc) · 2.31 KB
/
scripts.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
93
94
95
96
// Copyright (c) The Test Authors
// SPDX-License-Identifier: MPL-2.0
package test
import (
"fmt"
"strings"
)
func run(posts ...PostScript) string {
s := new(strings.Builder)
for _, post := range posts {
s.WriteString("↪ PostScript | ")
s.WriteString(post.Label())
s.WriteString(" ↷\n")
s.WriteString(post.Content())
s.WriteString("\n")
}
return s.String()
}
// A PostScript is used to annotate a test failure with additional information.
//
// Can be useful in large e2e style test cases, where adding additional context
// beyond an assertion helps in debugging.
type PostScript interface {
// Label should categorize what is in Content.
Label() string
// Content contains extra contextual information for debugging a test failure.
Content() string
}
type script struct {
label string
content string
}
func (s *script) Label() string {
return strings.TrimSpace(s.label)
}
func (s *script) Content() string {
return "\t" + strings.TrimSpace(s.content)
}
// Sprintf appends a Sprintf-string as an annotation to the output of a test case failure.
func Sprintf(msg string, args ...any) Setting {
return func(s *Settings) {
s.postScripts = append(s.postScripts, &script{
label: "annotation",
content: fmt.Sprintf(msg, args...),
})
}
}
// Sprint appends a Sprint-string as an annotation to the output of a test case failure.
func Sprint(args ...any) Setting {
return func(s *Settings) {
s.postScripts = append(s.postScripts, &script{
label: "annotation",
content: strings.TrimSpace(fmt.Sprintln(args...)),
})
}
}
// Values adds formatted key-val mappings as an annotation to the output of a test case failure.
func Values(vals ...any) Setting {
b := new(strings.Builder)
n := len(vals)
for i := 0; i < n-1; i += 2 {
s := fmt.Sprintf("\t%#v => %#v\n", vals[i], vals[i+1])
b.WriteString(s)
}
if n%2 != 0 {
s := fmt.Sprintf("\t%v => <MISSING ARG>", vals[n-1])
b.WriteString(s)
}
content := b.String()
return func(s *Settings) {
s.postScripts = append(s.postScripts, &script{
label: "mapping",
content: content,
})
}
}
// Func adds the string produced by f as an annotation to the output of a test case failure.
func Func(f func() string) Setting {
return func(s *Settings) {
s.postScripts = append(s.postScripts, &script{
label: "function",
content: f(),
})
}
}