-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from JosephWoodward/scrubber-support
Initial support for scrubbers via a fluent API
- Loading branch information
Showing
10 changed files
with
210 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package approvals_test | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
approvals "github.com/approvals/go-approval-tests" | ||
) | ||
|
||
func TestVerifyDoesNotAcceptSeveralVerifyOptions(t *testing.T) { | ||
scrubber1, _ := regexp.Compile("\\d{10}$") | ||
opts1 := approvals.Options().WithRegexScrubber(scrubber1, "<time>") | ||
opts2 := approvals.Options().WithRegexScrubber(scrubber1, "<time>") | ||
|
||
m := strings.NewReader("Hello World") | ||
|
||
defer func() { _ = recover() }() | ||
|
||
approvals.Verify(t, m, opts1, opts2) | ||
t.Errorf("Panic expected") | ||
} | ||
|
||
func TestVerifyMapWithRegexScrubber(t *testing.T) { | ||
scrubber, _ := regexp.Compile("\\d{10}$") | ||
opts := approvals.Options().WithRegexScrubber(scrubber, "<time>") | ||
|
||
m := map[string]string{ | ||
"dog": "bark", | ||
"cat": "meow", | ||
"time": fmt.Sprint(time.Now().Unix()), | ||
} | ||
approvals.VerifyMap(t, m, opts) | ||
} | ||
|
||
func TestVerifyArrayWithRegexScrubber(t *testing.T) { | ||
scrubber, _ := regexp.Compile("cat") | ||
opts := approvals.Options().WithRegexScrubber(scrubber, "person") | ||
|
||
xs := []string{"dog", "cat", "bird"} | ||
approvals.VerifyArray(t, xs, opts) | ||
} | ||
|
||
func TestVerifyJSONBytesWithRegexScrubber(t *testing.T) { | ||
scrubber, _ := regexp.Compile("Hello") | ||
opts := approvals.Options().WithRegexScrubber(scrubber, "Hi") | ||
|
||
jb := []byte("{ \"Greeting\": \"Hello\" }") | ||
approvals.VerifyJSONBytes(t, jb, opts) | ||
} | ||
|
||
func TestVerifyXMLBytesWithRegexScrubber(t *testing.T) { | ||
scrubber, _ := regexp.Compile("Hello") | ||
opts := approvals.Options().WithRegexScrubber(scrubber, "Hi") | ||
|
||
xmlb := []byte("<Test><Title>Hello World!</Title><Name>Peter Pan</Name><Age>100</Age></Test>") | ||
approvals.VerifyXMLBytes(t, xmlb, opts) | ||
} | ||
|
||
func TestVerifyStringWithRegexScrubber(t *testing.T) { | ||
scrubber, _ := regexp.Compile("\\d{10}$") | ||
opts := approvals.Options().WithRegexScrubber(scrubber, "<now>") | ||
|
||
s := fmt.Sprintf("The time is %v", time.Now().Unix()) | ||
approvals.VerifyString(t, s, opts) | ||
} | ||
|
||
func TestVerifyStringWithMultipleScrubbers(t *testing.T) { | ||
scrubber1, _ := regexp.Compile("\\d{10}$") | ||
scrubber2, _ := regexp.Compile("time") | ||
|
||
opts := approvals.Options(). | ||
WithRegexScrubber(scrubber1, "<now>"). | ||
WithRegexScrubber(scrubber2, "<future>") | ||
|
||
s := fmt.Sprintf("The time is %v", time.Now().Unix()) | ||
approvals.VerifyString(t, s, opts) | ||
} | ||
|
||
func TestVerifyAllWithRegexScrubber(t *testing.T) { | ||
scrubber, _ := regexp.Compile("Llewellyn") | ||
opts := approvals.Options().WithRegexScrubber(scrubber, "Walken") | ||
|
||
xs := []string{"Christopher", "Llewellyn"} | ||
approvals.VerifyAll(t, "uppercase", xs, func(x interface{}) string { return fmt.Sprintf("%s => %s", x, strings.ToUpper(x.(string))) }, opts) | ||
} |
5 changes: 5 additions & 0 deletions
5
testdata/scrubber_test.TestVerifyAllWithRegexScrubber.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
uppercase | ||
|
||
|
||
Christopher => CHRISTOPHER | ||
Walken => LLEWELLYN |
3 changes: 3 additions & 0 deletions
3
testdata/scrubber_test.TestVerifyArrayWithRegexScrubber.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[0]=dog | ||
[1]=person | ||
[2]=bird |
1 change: 1 addition & 0 deletions
1
testdata/scrubber_test.TestVerifyDoesNotAcceptSeveralVerifyOptions.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello World |
3 changes: 3 additions & 0 deletions
3
testdata/scrubber_test.TestVerifyJsonBytesWithRegexScrubber.approved.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"Greeting": "Hi" | ||
} |
3 changes: 3 additions & 0 deletions
3
testdata/scrubber_test.TestVerifyMapWithRegexScrubber.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[cat]=meow | ||
[dog]=bark | ||
[time]=<time> |
1 change: 1 addition & 0 deletions
1
testdata/scrubber_test.TestVerifyStringWithMultipleScrubbers.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The <future> is <now> |
1 change: 1 addition & 0 deletions
1
testdata/scrubber_test.TestVerifyStringWithRegexScrubber.approved.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The time is <now> |
5 changes: 5 additions & 0 deletions
5
testdata/scrubber_test.TestVerifyXMLBytesWithRegexScrubber.approved.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Test> | ||
<Title>Hi World!</Title> | ||
<Name>Peter Pan</Name> | ||
<Age>100</Age> | ||
</Test> |