Skip to content

Commit ce488a7

Browse files
committed
! F make FileOptions and Scrubber public
closes #73
1 parent 667b58d commit ce488a7

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

date_scrubber.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ type DateScrubber struct {
1212
}
1313

1414
var (
15-
customScrubbers []SupportedFormat
15+
customScrubbers []SupportedFormat
1616
customScrubbersMutex sync.RWMutex
1717
)
1818

19-
func NewDateScrubber(pattern string) scrubber {
19+
func NewDateScrubber(pattern string) Scrubber {
2020
return CreateRegexScrubberWithLabeler(regexp.MustCompile(pattern), func(n int) string {
2121
return fmt.Sprintf(`[Date%d]`, n)
2222
})
@@ -71,7 +71,7 @@ func GetSupportedFormatsRegex() []string {
7171
// "No match found for %s.\n Feel free to add your date at https://github.com/approvals/ApprovalTests.Java/issues/112 \n Current supported formats are: %s",
7272
// formattedExample, Query.select(getSupportedFormats(), SupportedFormat::getRegex));
7373
// }
74-
func GetDateScrubberFor(formattedExample string) (scrubber, error) {
74+
func GetDateScrubberFor(formattedExample string) (Scrubber, error) {
7575
allFormats := getAllFormats()
7676
for _, pattern := range allFormats {
7777
scrubber := NewDateScrubber(pattern.Regex)
@@ -87,7 +87,7 @@ func GetDateScrubberFor(formattedExample string) (scrubber, error) {
8787
func getAllFormats() []SupportedFormat {
8888
customScrubbersMutex.RLock()
8989
defer customScrubbersMutex.RUnlock()
90-
90+
9191
allFormats := make([]SupportedFormat, 0, len(GetSupportedFormats())+len(customScrubbers))
9292
allFormats = append(allFormats, GetSupportedFormats()...)
9393
allFormats = append(allFormats, customScrubbers...)
@@ -108,24 +108,24 @@ func AddDateScrubber(example string, regex string, displayMessage ...bool) error
108108
if len(displayMessage) > 0 {
109109
showMessage = displayMessage[0]
110110
}
111-
111+
112112
compiled, err := regexp.Compile(regex)
113113
if err != nil {
114114
return fmt.Errorf("invalid regex pattern: %w", err)
115115
}
116-
116+
117117
if !compiled.MatchString(example) {
118118
return fmt.Errorf("regex pattern '%s' does not match example '%s'", regex, example)
119119
}
120-
120+
121121
customScrubbersMutex.Lock()
122122
defer customScrubbersMutex.Unlock()
123-
123+
124124
customScrubbers = append(customScrubbers, SupportedFormat{
125125
Regex: regex,
126126
Examples: []string{example},
127127
})
128-
128+
129129
if showMessage {
130130
fmt.Println("You are using a custom date scrubber. ")
131131
fmt.Println("If you think the format you want to scrub would be useful for others,please add it to ")
@@ -134,7 +134,7 @@ func AddDateScrubber(example string, regex string, displayMessage ...bool) error
134134
fmt.Println("To suppress this message, use")
135135
fmt.Printf("AddDateScrubber(\"%s\", \"%s\", false)\n", example, regex)
136136
}
137-
137+
138138
return nil
139139
}
140140

options.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ type VerifyOptions struct {
1212
fields map[string]interface{}
1313
}
1414

15-
type fileOptions struct {
15+
type FileOptions struct {
1616
fields map[string]interface{}
1717
}
1818

19-
func (v VerifyOptions) ForFile() fileOptions {
20-
return fileOptions{fields: v.fields}
19+
func (v VerifyOptions) ForFile() FileOptions {
20+
return FileOptions{fields: v.fields}
2121
}
2222

2323
// Deprecated: Use `ForFile().WithExtension(extension)` instead.
@@ -30,16 +30,16 @@ func (v VerifyOptions) GetExtension() string {
3030
return v.ForFile().GetExtension()
3131
}
3232

33-
func (f fileOptions) GetExtension() string {
33+
func (f FileOptions) GetExtension() string {
3434
ext := getField(f.fields, "extWithDot", ".txt")
3535
return ext.(string)
3636
}
3737

38-
func (f fileOptions) WithNamer(namer core.ApprovalNamerCreator) VerifyOptions {
38+
func (f FileOptions) WithNamer(namer core.ApprovalNamerCreator) VerifyOptions {
3939
return NewVerifyOptions(f.fields, "namer", namer)
4040
}
4141

42-
func (f fileOptions) GetNamer() core.ApprovalNamerCreator {
42+
func (f FileOptions) GetNamer() core.ApprovalNamerCreator {
4343
ext := getField(f.fields, "namer", getApprovalNameCreator())
4444
creator := ext.(core.ApprovalNamerCreator)
4545

@@ -58,7 +58,7 @@ func (v VerifyOptions) getField(key string, defaultValue interface{}) interface{
5858
return getField(v.fields, key, defaultValue)
5959
}
6060

61-
func (f fileOptions) getField(key string, defaultValue interface{}) interface{} {
61+
func (f FileOptions) getField(key string, defaultValue interface{}) interface{} {
6262
return getField(f.fields, key, defaultValue)
6363
}
6464

@@ -78,26 +78,26 @@ func Options() VerifyOptions {
7878
}
7979

8080
// WithScrubber allows you to 'scrub' data within your test input and replace it with a static placeholder
81-
func (v VerifyOptions) WithScrubber(scrub scrubber) VerifyOptions {
81+
func (v VerifyOptions) WithScrubber(scrub Scrubber) VerifyOptions {
8282
return NewVerifyOptions(v.fields, "scrubber", scrub)
8383
}
8484

8585
// AddScrubber allows you to 'scrub' data within your test input and replace it with a static placeholder
86-
func (v VerifyOptions) AddScrubber(scrubfn scrubber) VerifyOptions {
87-
scrub := CreateMultiScrubber(v.getField("scrubber", CreateNoopScrubber()).(scrubber), scrubfn)
86+
func (v VerifyOptions) AddScrubber(scrubfn Scrubber) VerifyOptions {
87+
scrub := CreateMultiScrubber(v.getField("scrubber", CreateNoopScrubber()).(Scrubber), scrubfn)
8888
return v.WithScrubber(scrub)
8989
}
9090

9191
// WithExtension overrides the default file extension (.txt) for approval files.
92-
func (f fileOptions) WithExtension(extensionWithDot string) VerifyOptions {
92+
func (f FileOptions) WithExtension(extensionWithDot string) VerifyOptions {
9393
if !strings.HasPrefix(extensionWithDot, ".") {
9494
extensionWithDot = "." + extensionWithDot
9595
}
9696
return NewVerifyOptions(f.fields, "extWithDot", extensionWithDot)
9797
}
9898

9999
// WithAdditionalInformation allows adding additional information to the file name for parameterized tests.
100-
func (f fileOptions) WithAdditionalInformation(info string) VerifyOptions {
100+
func (f FileOptions) WithAdditionalInformation(info string) VerifyOptions {
101101
return NewVerifyOptions(f.fields, "additionalInformation", strings.ReplaceAll(info, " ", "_"))
102102
}
103103

@@ -107,7 +107,7 @@ func (v VerifyOptions) Scrub(reader io.Reader) (io.Reader, error) {
107107
return nil, err
108108
}
109109

110-
scrub := v.getField("scrubber", CreateNoopScrubber()).(scrubber)
110+
scrub := v.getField("scrubber", CreateNoopScrubber()).(Scrubber)
111111
result := scrub(string(b))
112112

113113
return strings.NewReader(result), nil

scrubber.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strconv"
77
)
88

9-
type scrubber func(s string) string
9+
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
@@ -17,12 +17,12 @@ func (v VerifyOptions) WithRegexScrubber(regex *regexp.Regexp, replacer string)
1717
}
1818

1919
// CreateRegexScrubber allows you to create a scrubber that uses a regular expression to scrub data
20-
func CreateRegexScrubber(regex *regexp.Regexp, replacer string) scrubber {
20+
func CreateRegexScrubber(regex *regexp.Regexp, replacer string) Scrubber {
2121
return CreateRegexScrubberWithLabeler(regex, func(int) string { return replacer })
2222
}
2323

2424
// CreateRegexScrubberWithLabeler allows you to create a scrubber that uses a regular expression to scrub data
25-
func CreateRegexScrubberWithLabeler(regex *regexp.Regexp, replacer func(int) string) scrubber {
25+
func CreateRegexScrubberWithLabeler(regex *regexp.Regexp, replacer func(int) string) Scrubber {
2626
return func(s string) string {
2727
seen := map[string]int{}
2828
replacefn := func(s string) string {
@@ -37,19 +37,19 @@ func CreateRegexScrubberWithLabeler(regex *regexp.Regexp, replacer func(int) str
3737
}
3838
}
3939

40-
func CreateJSONScrubber(label string, valueMatcher *regexp.Regexp) scrubber {
40+
func CreateJSONScrubber(label string, valueMatcher *regexp.Regexp) Scrubber {
4141
return CreateRegexScrubberWithLabeler(regexp.MustCompile(fmt.Sprintf(`"%s": \d+`, label)), func(n int) string { return fmt.Sprintf(`"%s": "<%s_%d>"`, label, label, n) })
4242
}
4343

4444
// NoopScrubber is a scrubber that does nothing
45-
func CreateNoopScrubber() scrubber {
45+
func CreateNoopScrubber() Scrubber {
4646
return func(s string) string {
4747
return s
4848
}
4949
}
5050

5151
// CreateMultiScrubber allows you to chain multiple scrubbers together
52-
func CreateMultiScrubber(scrubbers ...scrubber) scrubber {
52+
func CreateMultiScrubber(scrubbers ...Scrubber) Scrubber {
5353
return func(s string) string {
5454
for _, scrubber := range scrubbers {
5555
s = scrubber(s)
@@ -58,7 +58,7 @@ func CreateMultiScrubber(scrubbers ...scrubber) scrubber {
5858
}
5959
}
6060

61-
func CreateGuidScrubber() scrubber {
61+
func CreateGuidScrubber() Scrubber {
6262
regex := regexp.MustCompile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}")
6363
return CreateRegexScrubberWithLabeler(regex, func(n int) string { return "guid_" + strconv.Itoa(n) })
6464
}

0 commit comments

Comments
 (0)