Skip to content

Commit

Permalink
chore(docs): minor improvements (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
gkampitakis committed Jan 14, 2024
1 parent a66844e commit 080bbd2
Show file tree
Hide file tree
Showing 21 changed files with 131 additions and 125 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ JSON will be saved in snapshot in pretty format for more readability and determi
`MatchJSON`'s third argument can accept a list of matchers. Matchers are functions that can act
as property matchers and test values.

You can pass a path of the property you want to match and test.
You can pass the path of the property you want to match and test.

The path syntax is a series of keys separated by a dot. The dot and colon can be escaped with `\`.
_More information about the supported path syntax from [gjson](https://github.com/tidwall/gjson/blob/v1.17.0/SYNTAX.md)._

Currently `go-snaps` has two build in matchers
Currently `go-snaps` has three build in matchers

- `match.Any`
- `match.Custom`
Expand Down Expand Up @@ -145,7 +145,7 @@ match.Any("user.name").
Custom matcher allows you to bring your own validation and placeholder value

```go
match.Custom("user.age", func(val interface{}) (interface{}, error) {
match.Custom("user.age", func(val any) (any, error) {
age, ok := val.(float64)
if !ok {
return nil, fmt.Errorf("expected number but got %T", val)
Expand All @@ -162,8 +162,8 @@ bool // for JSON booleans
float64 // for JSON numbers
string // for JSON string literals
nil // for JSON null
map[string]interface{} // for JSON objects
[]interface{} // for JSON arrays
map[string]any // for JSON objects
[]any // for JSON arrays
```

If Custom matcher returns an error the snapshot test will fail with that error.
Expand Down Expand Up @@ -201,7 +201,7 @@ You can see more [examples](./examples/matchJSON_test.go#L96).

- the directory where snapshots are stored, _relative or absolute path_
- the filename where snapshots are stored
- programmatically control whether to update snapshots. _You can find an example usage at [examples](./examples/examples_test.go:14)_
- programmatically control whether to update snapshots. _You can find an example usage at [examples](/examples/examples_test.go#13)_

```go
t.Run("snapshot tests", func(t *testing.T) {
Expand Down Expand Up @@ -326,7 +326,7 @@ Snapshots have the form

```txt
[TestSimple/should_make_a_map_snapshot - 1]
map[string]interface {}{
map[string]interface{}{
"mock-0": "value",
"mock-1": int(2),
"mock-2": func() {...},
Expand Down
12 changes: 6 additions & 6 deletions examples/matchJSON_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (m *myMatcher) JSON(s []byte) ([]byte, []match.MatcherError) {

func TestMatchJSON(t *testing.T) {
t.Run("should make a json object snapshot", func(t *testing.T) {
m := map[string]interface{}{
m := map[string]any{
"mock-0": "value",
"mock-1": 2,
"mock-2": struct{ Msg string }{"Hello World"},
Expand Down Expand Up @@ -108,10 +108,10 @@ func TestMatchers(t *testing.T) {
Keys: []int{1, 2, 3, 4, 5},
}

snaps.MatchJSON(t, u, match.Custom("keys", func(val interface{}) (interface{}, error) {
keys, ok := val.([]interface{})
snaps.MatchJSON(t, u, match.Custom("keys", func(val any) (any, error) {
keys, ok := val.([]any)
if !ok {
return nil, fmt.Errorf("expected []interface{} but got %T", val)
return nil, fmt.Errorf("expected []any but got %T", val)
}

if len(keys) > 5 {
Expand All @@ -126,7 +126,7 @@ func TestMatchers(t *testing.T) {
t.Run("JSON string validation", func(t *testing.T) {
value := `{"user":"mock-user","age":2,"email":"mock@email.com"}`

snaps.MatchJSON(t, value, match.Custom("age", func(val interface{}) (interface{}, error) {
snaps.MatchJSON(t, value, match.Custom("age", func(val any) (any, error) {
if valInt, ok := val.(float64); !ok || valInt >= 5 {
return nil, fmt.Errorf("expecting number less than 5")
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestMatchers(t *testing.T) {
snaps.MatchJSON(
t,
`{"metadata":{"timestamp":"1687108093142"}}`,
match.Type[map[string]interface{}]("metadata"),
match.Type[map[string]any]("metadata"),
)
})
}
6 changes: 3 additions & 3 deletions examples/matchSnapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestMatchSnapshot(t *testing.T) {
})

t.Run("should make a map snapshot", func(t *testing.T) {
m := map[string]interface{}{
m := map[string]any{
"mock-0": "value",
"mock-1": 2,
"mock-2": func() {},
Expand Down Expand Up @@ -94,7 +94,7 @@ func TestMatchSnapshot(t *testing.T) {
func TestSimpleTable(t *testing.T) {
type testCases struct {
description string
input interface{}
input any
}

for _, scenario := range []testCases{
Expand All @@ -108,7 +108,7 @@ func TestSimpleTable(t *testing.T) {
},
{
description: "map",
input: map[string]interface{}{
input: map[string]any{
"test": func() {},
},
},
Expand Down
2 changes: 1 addition & 1 deletion examples/parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestParallel(t *testing.T) {
type testCases struct {
description string
input interface{}
input any
}

value := 10
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require (
github.com/gkampitakis/ciinfo v0.3.0
github.com/gkampitakis/go-diff v1.3.2
github.com/kr/pretty v0.3.1
github.com/maruel/natural v1.1.0
github.com/maruel/natural v1.1.1
github.com/tidwall/gjson v1.17.0
github.com/tidwall/pretty v1.2.1
github.com/tidwall/sjson v1.2.5
)

require (
github.com/kr/text v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/maruel/natural v1.1.0 h1:2z1NgP/Vae+gYrtC0VuvrTJ6U35OuyUqDdfluLqMWuQ=
github.com/maruel/natural v1.1.0/go.mod h1:eFVhYCcUOfZFxXoDZam8Ktya72wa79fNC3lc/leA0DQ=
github.com/maruel/natural v1.1.1 h1:Hja7XhhmvEFhcByqDoHz9QZbkWey+COd9xWfCfn1ioo=
github.com/maruel/natural v1.1.1/go.mod h1:v+Rfd79xlw1AgVBjbO0BEQmptqb5HvL/k9GRHB7ZKEg=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.0 h1:/Jocvlh98kcTfpN2+JzGQWQcqrPQwDrVEMApx/M5ZwM=
github.com/tidwall/gjson v1.17.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
Expand Down
23 changes: 14 additions & 9 deletions internal/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@ import (
type MockTestingT struct {
MockHelper func()
MockName func() string
MockSkip func(args ...interface{})
MockSkipf func(format string, args ...interface{})
MockSkip func(...any)
MockSkipf func(string, ...any)
MockSkipNow func()
MockError func(args ...interface{})
MockLog func(args ...interface{})
MockError func(...any)
MockLog func(...any)
MockCleanup func(func())
}

func (m MockTestingT) Error(args ...interface{}) {
func (m MockTestingT) Error(args ...any) {
m.MockError(args...)
}

func (m MockTestingT) Helper() {
m.MockHelper()
}

func (m MockTestingT) Skip(args ...interface{}) {
func (m MockTestingT) Skip(args ...any) {
m.MockSkip(args...)
}

func (m MockTestingT) Skipf(format string, args ...interface{}) {
func (m MockTestingT) Skipf(format string, args ...any) {
m.MockSkipf(format, args...)
}

Expand All @@ -42,10 +43,14 @@ func (m MockTestingT) Name() string {
return m.MockName()
}

func (m MockTestingT) Log(args ...interface{}) {
func (m MockTestingT) Log(args ...any) {
m.MockLog(args...)
}

func (m MockTestingT) Cleanup(f func()) {
m.MockCleanup(f)
}

// Equal asserts expected and received have deep equality
func Equal[A any](t *testing.T, expected, received A) {
t.Helper()
Expand Down Expand Up @@ -107,7 +112,7 @@ func False(t *testing.T, val bool) {
}
}

func Nil(t *testing.T, val interface{}) {
func Nil(t *testing.T, val any) {
t.Helper()
v := reflect.ValueOf(val)

Expand Down
4 changes: 2 additions & 2 deletions match/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

type anyMatcher struct {
paths []string
placeholder interface{}
placeholder any
errOnMissingPath bool
name string
}
Expand All @@ -33,7 +33,7 @@ func Any(paths ...string) *anyMatcher {
}

// Placeholder allows to define the placeholder value for Any matcher
func (a *anyMatcher) Placeholder(p interface{}) *anyMatcher {
func (a *anyMatcher) Placeholder(p any) *anyMatcher {
a.placeholder = p
return a
}
Expand Down
10 changes: 5 additions & 5 deletions match/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
)

type customMatcher struct {
callback func(val interface{}) (interface{}, error)
callback func(val any) (any, error)
errOnMissingPath bool
name string
path string
}

type CustomCallback func(val interface{}) (interface{}, error)
type CustomCallback func(val any) (any, error)

/*
Custom matcher allows you to bring your own validation and placeholder value.
match.Custom("user.age", func(val interface{}) (interface{}, error) {
match.Custom("user.age", func(val any) (any, error) {
age, ok := val.(float64)
if !ok {
return nil, fmt.Errorf("expected number but got %T", val)
Expand All @@ -33,8 +33,8 @@ Custom matcher allows you to bring your own validation and placeholder value.
float64 // for JSON numbers
string // for JSON string literals
nil // for JSON null
map[string]interface{} // for JSON objects
[]interface{} // for JSON arrays
map[string]any // for JSON objects
[]any // for JSON arrays
*/
func Custom(path string, callback CustomCallback) *customMatcher {
return &customMatcher{
Expand Down
12 changes: 6 additions & 6 deletions match/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestCustomMatcher(t *testing.T) {
t.Run("should create a custom matcher", func(t *testing.T) {
c := Custom("path", func(val interface{}) (interface{}, error) {
c := Custom("path", func(val any) (any, error) {
return nil, nil
})

Expand All @@ -19,7 +19,7 @@ func TestCustomMatcher(t *testing.T) {
})

t.Run("should allow overrding values", func(t *testing.T) {
c := Custom("path", func(val interface{}) (interface{}, error) {
c := Custom("path", func(val any) (any, error) {
return nil, nil
}).ErrOnMissingPath(false)

Expand All @@ -38,7 +38,7 @@ func TestCustomMatcher(t *testing.T) {
}`)

t.Run("should return error in case of missing path", func(t *testing.T) {
c := Custom("missing.key", func(val interface{}) (interface{}, error) {
c := Custom("missing.key", func(val any) (any, error) {
return nil, nil
})

Expand All @@ -55,7 +55,7 @@ func TestCustomMatcher(t *testing.T) {
})

t.Run("should ignore error in case of missing path", func(t *testing.T) {
c := Custom("missing.key", func(val interface{}) (interface{}, error) {
c := Custom("missing.key", func(val any) (any, error) {
return nil, nil
}).ErrOnMissingPath(false)

Expand All @@ -65,7 +65,7 @@ func TestCustomMatcher(t *testing.T) {
})

t.Run("should return error from custom callback", func(t *testing.T) {
c := Custom("user.email", func(val interface{}) (interface{}, error) {
c := Custom("user.email", func(val any) (any, error) {
return nil, errors.New("custom error")
})

Expand All @@ -82,7 +82,7 @@ func TestCustomMatcher(t *testing.T) {
})

t.Run("should apply value from custom callback to json", func(t *testing.T) {
c := Custom("user.email", func(val interface{}) (interface{}, error) {
c := Custom("user.email", func(val any) (any, error) {
return "replaced email", nil
})

Expand Down
2 changes: 1 addition & 1 deletion match/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type typeMatcher[ExpectedType any] struct {
paths []string
errOnMissingPath bool
name string
expectedType interface{}
expectedType any
}

/*
Expand Down
4 changes: 2 additions & 2 deletions snaps/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ type CleanOpts struct {
//
// os.Exit(v)
// }
func Clean(t *testing.M, opts ...CleanOpts) {
func Clean(m *testing.M, opts ...CleanOpts) {
var opt CleanOpts
if len(opts) != 0 {
opt = opts[0]
}
// This is just for making sure Clean is called from TestMain
_ = t
_ = m
runOnly := flag.Lookup("test.run").Value.String()

obsoleteFiles, usedFiles := examineFiles(testsRegistry.values, runOnly, shouldClean && !isCI)
Expand Down
8 changes: 4 additions & 4 deletions snaps/matchJSON.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ validators or placeholders for data that might change on each invocation e.g. da
MatchJSON(t, User{created: time.Now(), email: "mock-email"}, match.Any("created"))
*/
func (c *config) MatchJSON(t testingT, input interface{}, matchers ...match.JSONMatcher) {
func (c *config) MatchJSON(t testingT, input any, matchers ...match.JSONMatcher) {
t.Helper()

matchJSON(c, t, input, matchers...)
Expand All @@ -54,13 +54,13 @@ validators or placeholders for data that might change on each invocation e.g. da
MatchJSON(t, User{created: time.Now(), email: "mock-email"}, match.Any("created"))
*/
func MatchJSON(t testingT, input interface{}, matchers ...match.JSONMatcher) {
func MatchJSON(t testingT, input any, matchers ...match.JSONMatcher) {
t.Helper()

matchJSON(&defaultConfig, t, input, matchers...)
}

func matchJSON(c *config, t testingT, input interface{}, matchers ...match.JSONMatcher) {
func matchJSON(c *config, t testingT, input any, matchers ...match.JSONMatcher) {
t.Helper()

snapPath, snapPathRel := snapshotPath(c)
Expand Down Expand Up @@ -137,7 +137,7 @@ func matchJSON(c *config, t testingT, input interface{}, matchers ...match.JSONM
testEvents.register(updated)
}

func validateJSON(input interface{}) ([]byte, error) {
func validateJSON(input any) ([]byte, error) {
switch j := input.(type) {
case string:
if !gjson.Valid(j) {
Expand Down
Loading

0 comments on commit 080bbd2

Please sign in to comment.