8000 feat: support MatchStandaloneYAML (#128) · gkampitakis/go-snaps@2957128 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2957128

Browse files
authored
feat: support MatchStandaloneYAML (#128)
1 parent 66ada09 commit 2957128

15 files changed

+628
-4
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [MatchJSON](#matchjson)
2323
- [MatchStandaloneJSON](#matchstandalonejson) `New`
2424
- [MatchYAML](#matchyaml) `New`
25+
- [MatchStandaloneYAML](#matchstandaloneyaml) `New`
2526
- [Matchers](#matchers)
2627
- [match.Any](#matchany)
2728
- [match.Custom](#matchcustom)
@@ -167,6 +168,22 @@ func TestYAML(t *testing.T) {
167168
}
168169
```
169170

171+
## MatchStandaloneYAML
172+
173+
`MatchStandaloneYAML` will create snapshots on separate files as opposed to `MatchYAML` which adds multiple snapshots inside the same file.
174+
175+
```go
176+
func TestSimple(t *testing.T) {
177+
snaps.MatchStandaloneYAML(t, "user: \"mock-user\"\nage: 10\nemail: \"mock@email.com\"")
178+
snaps.MatchStandaloneYAML(t, User{10, "mock-email"})
179+
}
180+
```
181+
182+
`go-snaps` saves the snapshots in `__snapshots__` directory and the file
183+
name is the `t.Name()` plus a number plus the extension `.snap.yaml`.
184+
185+
So for the above example the snapshot file name will be `./__snapshots__/TestSimple_1.snap.yaml` and `./__snapshots__/TestSimple_2.snap.yaml`.
186+
170187
### Matchers
171188

172189
`MatchJSON`'s and `MatchYAML`'s third argument can accept a list of matchers. Matchers are functions that can act
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
user: mock-user
2+
age: 10
3+
nested:
4+
now:
5+
- <Any value>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
user: mock-user
2+
age: <less than 5 age>
3+
email: mock@email.com
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: mock-name
2+
email: mock-user@email.com
3+
keys:
4+
- 1
5+
- 2
6+
- 3
7+
- 4
8+
- 5
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data: <Type:uint64>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
metadata: <Type:map[string]interface {}>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mock-0: value
2+
mock-1: 2
3+
mock-2:
4+
msg: Hello World
5+
mock-3: 10.4
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: mock-name
2+
email: mock@email.com
3+
keys:
4+
- 1
5+
- 2
6+
- 3
7+
- 4
8+
- 5
9+
tags: []
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package examples
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
8+
"github.com/gkampitakis/go-snaps/match"
9+
"github.com/gkampitakis/go-snaps/snaps"
10+
)
11+
12+
func TestMatchStandaloneYAML(t *testing.T) {
13+
t.Run("should make a yaml object snapshot", func(t *testing.T) {
14+
m := map[string]any{
15+
"mock-0": "value",
16+
"mock-1": 2,
17+
"mock-2": struct{ Msg string }{"Hello World"},
18+
"mock-3": float32(10.4),
19+
}
20+
21+
snaps.MatchStandaloneYAML(t, m)
22+
})
23+
24+
t.Run("should marshal struct", func(t *testing.T) {
25+
type User struct {
26+
Name string `yaml:"name"`
27+
Email string `yaml:"email"`
28+
Keys []int `yaml:"keys"`
29+
Tags []string `yaml:"tags"`
30+
}
31+
32+
u := User{
33+
Name: "mock-name",
34+
Email: "mock@email.com",
35+
Keys: []int{1, 2, 3, 4, 5},
36+
}
37+
38+
snaps.MatchStandaloneYAML(t, u)
39+
})
40+
41+
t.Run("matchers", func(t *testing.T) {
42+
t.Run("Custom matcher", func(t *testing.T) {
43+
t.Run("struct marshalling", func(t *testing.T) {
44+
type User struct {
45+
Name string `yaml:"name"`
46+
Email string `yaml:"email"`
47+
Keys []int `yaml:"keys"`
48+
}
49+
50+
u := User{
51+
Name: "mock-name",
52+
Email: "mock-user@email.com",
53+
Keys: []int{1, 2, 3, 4, 5},
54+
}
55+
56+
snaps.MatchStandaloneYAML(t, u, match.Custom("$.keys", func(val any) (any, error) {
57+
keys, ok := val.([]any)
58+
if !ok {
59+
return nil, fmt.Errorf("expected []any but got %T", val)
60+
}
61+
62+
if len(keys) > 5 {
63+
return nil, fmt.Errorf("expected less than 5 keys")
64+
}
65+
66+
return val, nil
67+
}))
68+
})
69+
70+
t.Run("YAML string validation", func(t *testing.T) {
71+
value := `user: mock-user
72+
age: 2
73+
email: mock@email.com`
74+
75+
snaps.MatchStandaloneYAML(
76+
t,
77+
value,
78+
match.Custom("$.age", func(val any) (any, error) {
79+
if valInt, ok := val.(uint64); !ok || valInt >= 5 {
80+
return nil, fmt.Errorf("expecting number less than 5")
81+
}
82+
83+
return "<less than 5 age>", nil
84+
}),
85+
)
86+
})
87+
})
88+
89+
t.Run("Any matcher", func(t *testing.T) {
90+
t.Run("should ignore fields", func(t *testing.T) {
91+
value := fmt.Sprintf(`user: mock-user
92+
age: 10
93+
nested:
94+
now:
95+
- "%s"`, time.Now())
96+
snaps.MatchStandaloneYAML(t, value, match.Any("$.nested.now[0]"))
97+
})
98+
})
99+
100+
t.Run("Type matcher", func(t *testing.T) {
101+
t.Run("should create snapshot with type placeholder", func(t *testing.T) {
102+
snaps.MatchStandaloneYAML(t, `data: 10`, match.Type[uint64]("$.data"))
103+
snaps.MatchStandaloneYAML(
104+
t,
105+
`metadata:
106+
timestamp: 1687108093142
107+
`,
108+
match.Type[map[string]any]("$.metadata"),
109+
)
110+
})
111+
})
112+
})
113+
}

snaps/matchJSON_test.go

Lines 65CE changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func TestMatchJSON(t *testing.T) {
172172
})
173173

174174
t.Run("if snaps.Update(false) should skip creating snapshot", func(t *testing.T) {
175-
setupSnapshot(t, fileName, false)
175+
setupSnapshot(t, jsonFilename, false)
176176

177177
mockT := test.NewMockTestingT(t)
178178
mockT.MockError = func(args ...any) {

0 commit comments

Comments
 (0)
0