-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathoptions.go
More file actions
137 lines (112 loc) · 3.87 KB
/
options.go
File metadata and controls
137 lines (112 loc) · 3.87 KB
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
package approvals
import (
"io"
"strings"
"github.com/approvals/go-approval-tests/core"
)
// VerifyOptions can be accessed via the approvals.Options() API enabling configuration of scrubbers
type VerifyOptions struct {
fields map[string]interface{}
}
type FileOptions struct {
fields map[string]interface{}
}
func (v VerifyOptions) ForFile() FileOptions {
return FileOptions{fields: v.fields}
}
// Deprecated: Use `ForFile().WithExtension(extension)` instead.
func (v VerifyOptions) WithExtension(extension string) VerifyOptions {
return v.ForFile().WithExtension(extension)
}
// Deprecated: Use `ForFile().GetExtension()` instead.
func (v VerifyOptions) GetExtension() string {
return v.ForFile().GetExtension()
}
func (f FileOptions) GetExtension() string {
ext := getField(f.fields, "extWithDot", ".txt")
return ext.(string)
}
func (f FileOptions) WithNamer(namer core.ApprovalNamerCreator) VerifyOptions {
return NewVerifyOptions(f.fields, "namer", namer)
}
func (f FileOptions) GetNamer() core.ApprovalNamerCreator {
ext := getField(f.fields, "namer", getApprovalNameCreator())
creator := ext.(core.ApprovalNamerCreator)
return func(t core.Failable) core.ApprovalNamer {
namer := creator(t)
templated, ok := namer.(*templatedCustomNamer)
if ok {
templated.additionalInformation = f.getField("additionalInformation", "").(string)
return templated
}
return namer
}
}
func (v VerifyOptions) getField(key string, defaultValue interface{}) interface{} {
return getField(v.fields, key, defaultValue)
}
func (f FileOptions) getField(key string, defaultValue interface{}) interface{} {
return getField(f.fields, key, defaultValue)
}
func getField(fields map[string]interface{}, key string, defaultValue interface{}) interface{} {
if fields == nil {
return defaultValue
}
if value, ok := fields[key]; ok {
return value
}
return defaultValue
}
// Options enables providing individual Verify functions with customisations such as scrubbers
func Options() VerifyOptions {
return VerifyOptions{}
}
// WithScrubber allows you to 'scrub' data within your test input and replace it with a static placeholder
func (v VerifyOptions) WithScrubber(scrub Scrubber) VerifyOptions {
return NewVerifyOptions(v.fields, "scrubber", scrub)
}
// AddScrubber allows you to 'scrub' data within your test input and replace it with a static placeholder
func (v VerifyOptions) AddScrubber(scrubfn Scrubber) VerifyOptions {
scrub := CreateMultiScrubber(v.getField("scrubber", CreateNoopScrubber()).(Scrubber), scrubfn)
return v.WithScrubber(scrub)
}
// WithExtension overrides the default file extension (.txt) for approval files.
func (f FileOptions) WithExtension(extensionWithDot string) VerifyOptions {
if !strings.HasPrefix(extensionWithDot, ".") {
extensionWithDot = "." + extensionWithDot
}
return NewVerifyOptions(f.fields, "extWithDot", extensionWithDot)
}
// WithAdditionalInformation allows adding additional information to the file name for parameterized tests.
func (f FileOptions) WithAdditionalInformation(info string) VerifyOptions {
return NewVerifyOptions(f.fields, "additionalInformation", strings.ReplaceAll(info, " ", "_"))
}
func (v VerifyOptions) Scrub(reader io.Reader) (io.Reader, error) {
b, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
scrub := v.getField("scrubber", CreateNoopScrubber()).(Scrubber)
result := scrub(string(b))
return strings.NewReader(result), nil
}
func NewVerifyOptions(fields map[string]interface{}, key string, value interface{}) VerifyOptions {
// Make a copy of the fields map, but with the new key and value
newFields := make(map[string]interface{}, len(fields))
for k, v := range fields {
newFields[k] = v
}
newFields[key] = value
return VerifyOptions{
fields: newFields,
}
}
func alwaysOption(opts []VerifyOptions) VerifyOptions {
var v VerifyOptions
if len(opts) == 0 {
v = Options()
} else {
v = opts[0]
}
return v
}