forked from approvals/go-approval-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrubber_test.go
88 lines (67 loc) · 2.52 KB
/
scrubber_test.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
package approvals_test
import (
"fmt"
"regexp"
"strings"
"testing"
"time"
approvals "github.com/customerio/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)
}