Skip to content

Commit ba07c45

Browse files
committed
- B Made VerifyOptions public. Closes #72
1 parent 10da66c commit ba07c45

File tree

4 files changed

+42
-41
lines changed

4 files changed

+42
-41
lines changed

approvals.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ var (
2727
// VerifyWithExtension(t, strings.NewReader("Hello"), ".json")
2828
//
2929
// Deprecated: Please use Verify with the Options() fluent syntax.
30-
func VerifyWithExtension(t core.Failable, reader io.Reader, extWithDot string, opts ...verifyOptions) {
30+
func VerifyWithExtension(t core.Failable, reader io.Reader, extWithDot string, opts ...VerifyOptions) {
3131
t.Helper()
3232
Verify(t, reader, alwaysOption(opts).ForFile().WithExtension(extWithDot))
3333
}
3434

3535
// Verify Example:
3636
//
3737
// Verify(t, strings.NewReader("Hello"))
38-
func Verify(t core.Failable, reader io.Reader, opts ...verifyOptions) {
38+
func Verify(t core.Failable, reader io.Reader, opts ...VerifyOptions) {
3939
t.Helper()
4040

4141
if len(opts) > 1 {
4242
panic("Please use fluent syntax for options, see documentation for more information")
4343
}
4444

45-
var opt verifyOptions
45+
var opt VerifyOptions
4646
if len(opts) > 0 {
4747
opt = opts[0]
4848
}
@@ -71,7 +71,7 @@ func Verify(t core.Failable, reader io.Reader, opts ...verifyOptions) {
7171

7272
// VerifyString stores the passed string into the received file and confirms
7373
// that it matches the approved local file. On failure, it will launch a reporter.
74-
func VerifyString(t core.Failable, s string, opts ...verifyOptions) {
74+
func VerifyString(t core.Failable, s string, opts ...VerifyOptions) {
7575
t.Helper()
7676
reader := strings.NewReader(s)
7777
Verify(t, reader, opts...)
@@ -80,7 +80,7 @@ func VerifyString(t core.Failable, s string, opts ...verifyOptions) {
8080
// VerifyXMLStruct Example:
8181
//
8282
// VerifyXMLStruct(t, xml)
83-
func VerifyXMLStruct(t core.Failable, obj interface{}, opts ...verifyOptions) {
83+
func VerifyXMLStruct(t core.Failable, obj interface{}, opts ...VerifyOptions) {
8484
t.Helper()
8585
options := alwaysOption(opts).ForFile().WithExtension(".xml")
8686
xmlContent, err := xml.MarshalIndent(obj, "", " ")
@@ -99,7 +99,7 @@ func VerifyXMLStruct(t core.Failable, obj interface{}, opts ...verifyOptions) {
9999
// VerifyXMLBytes Example:
100100
//
101101
// VerifyXMLBytes(t, []byte("<Test/>"))
102-
func VerifyXMLBytes(t core.Failable, bs []byte, opts ...verifyOptions) {
102+
func VerifyXMLBytes(t core.Failable, bs []byte, opts ...VerifyOptions) {
103103
t.Helper()
104104
type node struct {
105105
Attr []xml.Attr
@@ -121,7 +121,7 @@ func VerifyXMLBytes(t core.Failable, bs []byte, opts ...verifyOptions) {
121121
// VerifyJSONStruct Example:
122122
//
123123
// VerifyJSONStruct(t, json)
124-
func VerifyJSONStruct(t core.Failable, obj interface{}, opts ...verifyOptions) {
124+
func VerifyJSONStruct(t core.Failable, obj interface{}, opts ...VerifyOptions) {
125125
t.Helper()
126126
options := alwaysOption(opts).ForFile().WithExtension(".json")
127127
jsonb, err := json.MarshalIndent(obj, "", " ")
@@ -136,7 +136,7 @@ func VerifyJSONStruct(t core.Failable, obj interface{}, opts ...verifyOptions) {
136136
// VerifyJSONBytes Example:
137137
//
138138
// VerifyJSONBytes(t, []byte("{ \"Greeting\": \"Hello\" }"))
139-
func VerifyJSONBytes(t core.Failable, bs []byte, opts ...verifyOptions) {
139+
func VerifyJSONBytes(t core.Failable, bs []byte, opts ...VerifyOptions) {
140140
t.Helper()
141141
var obj map[string]interface{}
142142
err := json.Unmarshal(bs, &obj)
@@ -151,7 +151,7 @@ func VerifyJSONBytes(t core.Failable, bs []byte, opts ...verifyOptions) {
151151
// VerifyMap Example:
152152
//
153153
// VerifyMap(t, map[string][string] { "dog": "bark" })
154-
func VerifyMap(t core.Failable, m interface{}, opts ...verifyOptions) {
154+
func VerifyMap(t core.Failable, m interface{}, opts ...VerifyOptions) {
155155
t.Helper()
156156
outputText := utils.PrintMap(m)
157157
VerifyString(t, outputText, opts...)
@@ -160,7 +160,7 @@ func VerifyMap(t core.Failable, m interface{}, opts ...verifyOptions) {
160160
// VerifyArray Example:
161161
//
162162
// VerifyArray(t, []string{"dog", "cat"})
163-
func VerifyArray(t core.Failable, array interface{}, opts ...verifyOptions) {
163+
func VerifyArray(t core.Failable, array interface{}, opts ...VerifyOptions) {
164164
t.Helper()
165165
outputText := utils.PrintArray(array)
166166
VerifyString(t, outputText, opts...)
@@ -169,7 +169,7 @@ func VerifyArray(t core.Failable, array interface{}, opts ...verifyOptions) {
169169
// VerifyAll Example:
170170
//
171171
// VerifyAll(t, "uppercase", []string("dog", "cat"}, func(x interface{}) string { return strings.ToUpper(x.(string)) })
172-
func VerifyAll(t core.Failable, header string, collection interface{}, transform func(interface{}) string, opts ...verifyOptions) {
172+
func VerifyAll(t core.Failable, header string, collection interface{}, transform func(interface{}) string, opts ...VerifyOptions) {
173173
t.Helper()
174174
if len(header) != 0 {
175175
header = fmt.Sprintf("%s\n\n\n", header)

internal_documentation/architecture.go.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,21 @@ classDiagram
2727
+Report(approved string, received string) bool
2828
}
2929
30-
class verifyOptions {
30+
class VerifyOptions {
3131
-fields map
3232
+ForFile() fileOptions
33-
+WithScrubber(scrub scrubber) verifyOptions
34-
+AddScrubber(scrubfn scrubber) verifyOptions
33+
+WithScrubber(scrub scrubber) VerifyOptions
34+
+AddScrubber(scrubfn scrubber) VerifyOptions
3535
+Scrub(reader io.Reader) io.Reader, error
3636
}
3737
3838
class fileOptions {
3939
-fields map
40-
+WithExtension(extensionWithDot string) verifyOptions
40+
+WithExtension(extensionWithDot string) VerifyOptions
4141
+GetExtension() string
42-
+WithNamer(namer ApprovalNamerCreator) verifyOptions
42+
+WithNamer(namer ApprovalNamerCreator) VerifyOptions
4343
+GetNamer() ApprovalNamerCreator
44-
+WithAdditionalInformation(info string) verifyOptions
44+
+WithAdditionalInformation(info string) VerifyOptions
4545
}
4646
4747
class scrubber {
@@ -118,15 +118,15 @@ classDiagram
118118
119119
%% Relationships
120120
VerifyFunctions ..> Failable : uses
121-
VerifyFunctions ..> verifyOptions : uses
121+
VerifyFunctions ..> VerifyOptions : uses
122122
VerifyFunctions ..> ApprovalNamer : creates via
123123
VerifyFunctions ..> Reporter : uses
124124
125125
CombinationApprovals ..> Failable : uses
126126
CombinationApprovals ..> VerifyFunctions : calls
127127
128-
verifyOptions --> fileOptions : creates
129-
verifyOptions ..> scrubber : uses
128+
VerifyOptions --> fileOptions : creates
129+
VerifyOptions ..> scrubber : uses
130130
fileOptions ..> ApprovalNamer : creates
131131
132132
FirstWorkingReporter ..|> Reporter : implements
@@ -151,7 +151,7 @@ classDiagram
151151

152152
### Configuration
153153

154-
- **verifyOptions**: Main configuration object for verification operations
154+
- **VerifyOptions**: Main configuration object for verification operations
155155
- **fileOptions**: File-specific configuration options
156156
- **scrubber**: Function type for data sanitization/scrubbing
157157

@@ -178,7 +178,7 @@ classDiagram
178178
## Key Architectural Patterns
179179

180180
1. **Interface-based Design**: Core functionality is defined through interfaces (Failable, ApprovalNamer, Reporter)
181-
2. **Options Pattern**: Flexible configuration through verifyOptions and fileOptions
181+
2. **Options Pattern**: Flexible configuration through VerifyOptions and fileOptions
182182
3. **Strategy Pattern**: Different reporting strategies through Reporter interface
183183
4. **Chain of Responsibility**: FirstWorkingReporter tries reporters in sequence
184184
5. **Template Method**: templatedCustomNamer uses templates for file naming

options.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,30 @@ package approvals
33
import (
44
"io"
55
"strings"
6+
67
"github.com/approvals/go-approval-tests/core"
78
)
89

9-
// verifyOptions can be accessed via the approvals.Options() API enabling configuration of scrubbers
10-
type verifyOptions struct {
10+
// VerifyOptions can be accessed via the approvals.Options() API enabling configuration of scrubbers
11+
type VerifyOptions struct {
1112
fields map[string]interface{}
1213
}
1314

1415
type fileOptions struct {
1516
fields map[string]interface{}
1617
}
1718

18-
func (v verifyOptions) ForFile() fileOptions {
19+
func (v VerifyOptions) ForFile() fileOptions {
1920
return fileOptions{fields: v.fields}
2021
}
2122

2223
// Deprecated: Use `ForFile().WithExtension(extension)` instead.
23-
func (v verifyOptions) WithExtension(extension string) verifyOptions {
24+
func (v VerifyOptions) WithExtension(extension string) VerifyOptions {
2425
return v.ForFile().WithExtension(extension)
2526
}
2627

2728
// Deprecated: Use `ForFile().GetExtension()` instead.
28-
func (v verifyOptions) GetExtension() string {
29+
func (v VerifyOptions) GetExtension() string {
2930
return v.ForFile().GetExtension()
3031
}
3132

@@ -34,7 +35,7 @@ func (f fileOptions) GetExtension() string {
3435
return ext.(string)
3536
}
3637

37-
func (f fileOptions) WithNamer(namer core.ApprovalNamerCreator) verifyOptions {
38+
func (f fileOptions) WithNamer(namer core.ApprovalNamerCreator) VerifyOptions {
3839
return NewVerifyOptions(f.fields, "namer", namer)
3940
}
4041

@@ -53,7 +54,7 @@ func (f fileOptions) GetNamer() core.ApprovalNamerCreator {
5354
}
5455
}
5556

56-
func (v verifyOptions) getField(key string, defaultValue interface{}) interface{} {
57+
func (v VerifyOptions) getField(key string, defaultValue interface{}) interface{} {
5758
return getField(v.fields, key, defaultValue)
5859
}
5960

@@ -72,35 +73,35 @@ func getField(fields map[string]interface{}, key string, defaultValue interface{
7273
}
7374

7475
// Options enables providing individual Verify functions with customisations such as scrubbers
75-
func Options() verifyOptions {
76-
return verifyOptions{}
76+
func Options() VerifyOptions {
77+
return VerifyOptions{}
7778
}
7879

7980
// WithScrubber allows you to 'scrub' data within your test input and replace it with a static placeholder
80-
func (v verifyOptions) WithScrubber(scrub scrubber) verifyOptions {
81+
func (v VerifyOptions) WithScrubber(scrub scrubber) VerifyOptions {
8182
return NewVerifyOptions(v.fields, "scrubber", scrub)
8283
}
8384

8485
// AddScrubber allows you to 'scrub' data within your test input and replace it with a static placeholder
85-
func (v verifyOptions) AddScrubber(scrubfn scrubber) verifyOptions {
86+
func (v VerifyOptions) AddScrubber(scrubfn scrubber) VerifyOptions {
8687
scrub := CreateMultiScrubber(v.getField("scrubber", CreateNoopScrubber()).(scrubber), scrubfn)
8788
return v.WithScrubber(scrub)
8889
}
8990

9091
// WithExtension overrides the default file extension (.txt) for approval files.
91-
func (f fileOptions) WithExtension(extensionWithDot string) verifyOptions {
92+
func (f fileOptions) WithExtension(extensionWithDot string) VerifyOptions {
9293
if !strings.HasPrefix(extensionWithDot, ".") {
9394
extensionWithDot = "." + extensionWithDot
9495
}
9596
return NewVerifyOptions(f.fields, "extWithDot", extensionWithDot)
9697
}
9798

9899
// WithAdditionalInformation allows adding additional information to the file name for parameterized tests.
99-
func (f fileOptions) WithAdditionalInformation(info string) verifyOptions {
100+
func (f fileOptions) WithAdditionalInformation(info string) VerifyOptions {
100101
return NewVerifyOptions(f.fields, "additionalInformation", strings.ReplaceAll(info, " ", "_"))
101102
}
102103

103-
func (v verifyOptions) Scrub(reader io.Reader) (io.Reader, error) {
104+
func (v VerifyOptions) Scrub(reader io.Reader) (io.Reader, error) {
104105
b, err := io.ReadAll(reader)
105106
if err != nil {
106107
return nil, err
@@ -112,20 +113,20 @@ func (v verifyOptions) Scrub(reader io.Reader) (io.Reader, error) {
112113
return strings.NewReader(result), nil
113114
}
114115

115-
func NewVerifyOptions(fields map[string]interface{}, key string, value interface{}) verifyOptions {
116+
func NewVerifyOptions(fields map[string]interface{}, key string, value interface{}) VerifyOptions {
116117
// Make a copy of the fields map, but with the new key and value
117118
newFields := make(map[string]interface{}, len(fields))
118119
for k, v := range fields {
119120
newFields[k] = v
120121
}
121122
newFields[key] = value
122-
return verifyOptions{
123+
return VerifyOptions{
123124
fields: newFields,
124125
}
125126
}
126127

127-
func alwaysOption(opts []verifyOptions) verifyOptions {
128-
var v verifyOptions
128+
func alwaysOption(opts []VerifyOptions) VerifyOptions {
129+
var v VerifyOptions
129130
if len(opts) == 0 {
130131
v = Options()
131132
} else {

scrubber.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type scrubber func(s string) string
1010

1111
// Deprecated: WithRegexScrubber allows you to 'scrub' dynamic data such as timestamps within your test input
1212
// and replace it with a static placeholder
13-
func (v verifyOptions) WithRegexScrubber(regex *regexp.Regexp, replacer string) verifyOptions {
13+
func (v VerifyOptions) WithRegexScrubber(regex *regexp.Regexp, replacer string) VerifyOptions {
1414
return v.AddScrubber(func(s string) string {
1515
return regex.ReplaceAllString(s, replacer)
1616
})

0 commit comments

Comments
 (0)