-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgolden_master_test.go
115 lines (96 loc) · 3.03 KB
/
golden_master_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
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
package golden_test
import (
"errors"
"github.com/franiglesias/golden"
"github.com/franiglesias/golden/internal/helper"
"github.com/franiglesias/golden/internal/vfs"
"strings"
"testing"
)
/*
TestGoldenMaster needs the same setup as TestVerify. Check it for documentation.
*/
func TestGoldenMaster(t *testing.T) {
var fs *vfs.MemFs
var gld golden.Golden
var tSpy helper.TSpy
setUp := func(t *testing.T) {
fs = vfs.NewMemFs()
gld = *golden.NewUsingFs(fs)
tSpy = helper.TSpy{
T: t,
}
}
t.Run("should create a golden master snapshot", func(t *testing.T) {
setUp(t)
f := func(args ...any) any {
title := args[0].(string)
part := args[1].(string)
span := args[2].(int)
return border(title, part, span)
}
titles := []any{"Title", "Subtitle"}
parts := []any{"*", "#"}
times := []any{1, 2}
gld.Master(&tSpy, f, golden.Combine(titles, parts, times))
})
t.Run("should manage the error", func(t *testing.T) {
setUp(t)
f := func(args ...any) any {
result, err := division(args[0].(float64), args[1].(float64))
if err != nil {
return err.Error()
}
return result
}
dividend := []any{1.0, 2.0}
divisor := []any{0.0, -1.0, 1.0, 2.0}
values := golden.Combine(dividend, divisor)
gld.Master(&tSpy, f, values)
vfs.AssertSnapshotWasCreated(t, fs, "testdata/TestGoldenMaster/should_manage_the_error.snap.json")
vfs.AssertSnapShotContains(t, fs, "testdata/TestGoldenMaster/should_manage_the_error.snap.json", "division by 0")
})
t.Run("should support custom name", func(t *testing.T) {
setUp(t)
f := func(args ...any) any {
result, err := division(args[0].(float64), args[1].(float64))
if err != nil {
return err.Error()
}
return result
}
dividend := []any{1.0, 2.0}
divisor := []any{0.0, -1.0, 1.0, 2.0}
gld.Master(&tSpy, f, golden.Combine(dividend, divisor), golden.Snapshot("combinations"))
vfs.AssertSnapshotWasCreated(t, fs, "testdata/combinations.snap.json")
vfs.AssertSnapShotContains(t, fs, "testdata/combinations.snap.json", "division by 0")
})
t.Run("should support approval", func(t *testing.T) {
setUp(t)
f := func(args ...any) any {
result, err := division(args[0].(float64), args[1].(float64))
if err != nil {
return err.Error()
}
return result
}
dividend := []any{1.0, 2.0}
divisor := []any{0.0, -1.0, 1.0, 2.0}
gld.Master(&tSpy, f, golden.Combine(dividend, divisor), golden.WaitApproval())
vfs.AssertSnapshotWasCreated(t, fs, "testdata/TestGoldenMaster/should_support_approval.snap.json")
vfs.AssertSnapShotContains(t, fs, "testdata/TestGoldenMaster/should_support_approval.snap.json", "division by 0")
helper.AssertFailedTest(t, &tSpy)
})
}
func border(title string, part string, span int) string {
width := span*2 + len(title) + 2
top := strings.Repeat(part, width)
body := part + strings.Repeat(" ", span) + title + strings.Repeat(" ", span) + part
return top + "\n" + body + "\n" + top + "\n"
}
func division(a float64, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by 0")
}
return a / b, nil
}