diff --git a/README.md b/README.md index 0e698f3..0849192 100644 --- a/README.md +++ b/README.md @@ -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` @@ -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) @@ -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. @@ -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) { @@ -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() {...}, diff --git a/examples/matchJSON_test.go b/examples/matchJSON_test.go index c66bb01..766cbfc 100644 --- a/examples/matchJSON_test.go +++ b/examples/matchJSON_test.go @@ -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"}, @@ -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 { @@ -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") } @@ -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"), ) }) } diff --git a/examples/matchSnapshot_test.go b/examples/matchSnapshot_test.go index 9d8d880..bdb02a5 100644 --- a/examples/matchSnapshot_test.go +++ b/examples/matchSnapshot_test.go @@ -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() {}, @@ -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{ @@ -108,7 +108,7 @@ func TestSimpleTable(t *testing.T) { }, { description: "map", - input: map[string]interface{}{ + input: map[string]any{ "test": func() {}, }, }, diff --git a/examples/parallel_test.go b/examples/parallel_test.go index e1a9f02..125e98c 100644 --- a/examples/parallel_test.go +++ b/examples/parallel_test.go @@ -10,7 +10,7 @@ import ( func TestParallel(t *testing.T) { type testCases struct { description string - input interface{} + input any } value := 10 diff --git a/go.mod b/go.mod index 97ab29e..9e0498a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ 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 @@ -14,6 +14,6 @@ require ( 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 ) diff --git a/go.sum b/go.sum index 1d6eed6..80004fe 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/test/test.go b/internal/test/test.go index 4301908..247dc61 100644 --- a/internal/test/test.go +++ b/internal/test/test.go @@ -11,14 +11,15 @@ 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...) } @@ -26,11 +27,11 @@ 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...) } @@ -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() @@ -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) diff --git a/match/any.go b/match/any.go index d5527ed..361a759 100644 --- a/match/any.go +++ b/match/any.go @@ -9,7 +9,7 @@ import ( type anyMatcher struct { paths []string - placeholder interface{} + placeholder any errOnMissingPath bool name string } @@ -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 } diff --git a/match/custom.go b/match/custom.go index 115a524..a5fa41f 100644 --- a/match/custom.go +++ b/match/custom.go @@ -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) @@ -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{ diff --git a/match/custom_test.go b/match/custom_test.go index 0b07933..7212724 100644 --- a/match/custom_test.go +++ b/match/custom_test.go @@ -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 }) @@ -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) @@ -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 }) @@ -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) @@ -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") }) @@ -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 }) diff --git a/match/type.go b/match/type.go index fc5cb2a..37d55a2 100644 --- a/match/type.go +++ b/match/type.go @@ -12,7 +12,7 @@ type typeMatcher[ExpectedType any] struct { paths []string errOnMissingPath bool name string - expectedType interface{} + expectedType any } /* diff --git a/snaps/clean.go b/snaps/clean.go index 3c87ed0..7e15d15 100644 --- a/snaps/clean.go +++ b/snaps/clean.go @@ -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) diff --git a/snaps/matchJSON.go b/snaps/matchJSON.go index 20f1b58..90cf8e3 100644 --- a/snaps/matchJSON.go +++ b/snaps/matchJSON.go @@ -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...) @@ -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) @@ -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) { diff --git a/snaps/matchJSON_test.go b/snaps/matchJSON_test.go index e7272c6..37add14 100644 --- a/snaps/matchJSON_test.go +++ b/snaps/matchJSON_test.go @@ -24,7 +24,7 @@ func TestMatchJSON(t *testing.T) { for _, tc := range []struct { name string - input interface{} + input any }{ { name: "string", @@ -53,10 +53,10 @@ func TestMatchJSON(t *testing.T) { MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.NotCalled(t) }, - MockLog: func(args ...interface{}) { test.Equal(t, addedMsg, args[0].(string)) }, + MockLog: func(args ...any) { test.Equal(t, addedMsg, args[0].(string)) }, } MatchJSON(mockT, tc.input) @@ -74,7 +74,7 @@ func TestMatchJSON(t *testing.T) { t.Run("should validate json", func(t *testing.T) { for _, tc := range []struct { name string - input interface{} + input any err string }{ { @@ -101,10 +101,10 @@ func TestMatchJSON(t *testing.T) { MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.Equal(t, tc.err, (args[0].(error)).Error()) }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { // this is called when snapshot is written successfully test.NotCalled(t) }, @@ -124,20 +124,20 @@ func TestMatchJSON(t *testing.T) { MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.NotCalled(t) }, - MockLog: func(args ...interface{}) { test.Equal(t, addedMsg, args[0].(string)) }, + MockLog: func(args ...any) { test.Equal(t, addedMsg, args[0].(string)) }, } - c1 := func(val interface{}) (interface{}, error) { - return map[string]interface{}{"key2": nil}, nil + c1 := func(val any) (any, error) { + return map[string]any{"key2": nil}, nil } - c2 := func(val interface{}) (interface{}, error) { - return map[string]interface{}{"key3": nil}, nil + c2 := func(val any) (any, error) { + return map[string]any{"key3": nil}, nil } - c3 := func(val interface{}) (interface{}, error) { - return map[string]interface{}{"key4": nil}, nil + c3 := func(val any) (any, error) { + return map[string]any{"key4": nil}, nil } MatchJSON( @@ -157,7 +157,7 @@ func TestMatchJSON(t *testing.T) { MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.Equal(t, "\x1b[31;1m\n✕ match.Custom(\"age\") - mock error"+ "\x1b[0m\x1b[31;1m\n✕ match.Any(\"missing.key.1\") - path does not exist"+ @@ -165,10 +165,10 @@ func TestMatchJSON(t *testing.T) { args[0], ) }, - MockLog: func(args ...interface{}) { test.NotCalled(t) }, + MockLog: func(args ...any) { test.NotCalled(t) }, } - c := func(val interface{}) (interface{}, error) { + c := func(val any) (any, error) { return nil, errors.New("mock error") } MatchJSON( @@ -188,10 +188,10 @@ func TestMatchJSON(t *testing.T) { MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.Equal(t, errSnapNotFound, args[0].(error)) }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.NotCalled(t) }, } @@ -204,19 +204,19 @@ func TestMatchJSON(t *testing.T) { t.Run("should update snapshot when 'shouldUpdate'", func(t *testing.T) { snapPath := setupSnapshot(t, jsonFilename, false, true) - printerExpectedCalls := []func(received interface{}){ - func(received interface{}) { test.Equal(t, addedMsg, received.(string)) }, - func(received interface{}) { test.Equal(t, updatedMsg, received.(string)) }, + printerExpectedCalls := []func(received any){ + func(received any) { test.Equal(t, addedMsg, received.(string)) }, + func(received any) { test.Equal(t, updatedMsg, received.(string)) }, } mockT := test.MockTestingT{ MockHelper: func() {}, MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.NotCalled(t) }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { printerExpectedCalls[0](args[0]) // shift diff --git a/snaps/matchSnapshot.go b/snaps/matchSnapshot.go index 96671ee..3d5b6bf 100644 --- a/snaps/matchSnapshot.go +++ b/snaps/matchSnapshot.go @@ -21,7 +21,7 @@ or call MatchSnapshot multiples times inside a test The difference is the latter will create multiple entries. */ -func (c *config) MatchSnapshot(t testingT, values ...interface{}) { +func (c *config) MatchSnapshot(t testingT, values ...any) { t.Helper() matchSnapshot(c, t, values...) @@ -40,13 +40,13 @@ or call MatchSnapshot multiples times inside a test The difference is the latter will create multiple entries. */ -func MatchSnapshot(t testingT, values ...interface{}) { +func MatchSnapshot(t testingT, values ...any) { t.Helper() matchSnapshot(&defaultConfig, t, values...) } -func matchSnapshot(c *config, t testingT, values ...interface{}) { +func matchSnapshot(c *config, t testingT, values ...any) { t.Helper() if len(values) == 0 { @@ -104,7 +104,7 @@ func matchSnapshot(c *config, t testingT, values ...interface{}) { testEvents.register(updated) } -func takeSnapshot(objects []interface{}) string { +func takeSnapshot(objects []any) string { var snapshot string for i := 0; i < len(objects); i++ { diff --git a/snaps/matchSnapshot_test.go b/snaps/matchSnapshot_test.go index 16964b0..bcc8cde 100644 --- a/snaps/matchSnapshot_test.go +++ b/snaps/matchSnapshot_test.go @@ -68,10 +68,10 @@ func TestMatchSnapshot(t *testing.T) { MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.NotCalled(t) }, - MockLog: func(args ...interface{}) { test.Equal(t, addedMsg, args[0].(string)) }, + MockLog: func(args ...any) { test.Equal(t, addedMsg, args[0].(string)) }, } MatchSnapshot(mockT, 10, "hello world") @@ -92,10 +92,10 @@ func TestMatchSnapshot(t *testing.T) { MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.Equal(t, errSnapNotFound, args[0].(error)) }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.NotCalled(t) }, } @@ -108,16 +108,16 @@ func TestMatchSnapshot(t *testing.T) { t.Run("should return error when diff is found", func(t *testing.T) { setupSnapshot(t, fileName, false) - printerExpectedCalls := []func(received interface{}){ - func(received interface{}) { test.Equal(t, addedMsg, received.(string)) }, - func(received interface{}) { test.NotCalled(t) }, + printerExpectedCalls := []func(received any){ + func(received any) { test.Equal(t, addedMsg, received.(string)) }, + func(received any) { test.NotCalled(t) }, } mockT := test.MockTestingT{ MockHelper: func() {}, MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { expected := "\n\x1b[38;5;52m\x1b[48;5;225m- Snapshot - 2\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m" + "+ Received + 2\x1b[0m\n\n\x1b[38;5;52m\x1b[48;5;225m- int(10)\x1b[0m\n\x1b[38;5;52m\x1b[48;5;225m" + "- hello world\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m+ int(100)\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m" + @@ -128,7 +128,7 @@ func TestMatchSnapshot(t *testing.T) { test.Equal(t, expected, args[0].(string)) }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { printerExpectedCalls[0](args[0]) // shift @@ -152,19 +152,19 @@ func TestMatchSnapshot(t *testing.T) { t.Run("when 'updateVAR==true'", func(t *testing.T) { snapPath := setupSnapshot(t, fileName, false, true) - printerExpectedCalls := []func(received interface{}){ - func(received interface{}) { test.Equal(t, addedMsg, received.(string)) }, - func(received interface{}) { test.Equal(t, updatedMsg, received.(string)) }, + printerExpectedCalls := []func(received any){ + func(received any) { test.Equal(t, addedMsg, received.(string)) }, + func(received any) { test.Equal(t, updatedMsg, received.(string)) }, } mockT := test.MockTestingT{ MockHelper: func() {}, MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.NotCalled(t) }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { printerExpectedCalls[0](args[0]) // shift @@ -193,19 +193,19 @@ func TestMatchSnapshot(t *testing.T) { t.Run("when config update", func(t *testing.T) { snapPath := setupSnapshot(t, fileName, false, false) - printerExpectedCalls := []func(received interface{}){ - func(received interface{}) { test.Equal(t, addedMsg, received.(string)) }, - func(received interface{}) { test.Equal(t, updatedMsg, received.(string)) }, + printerExpectedCalls := []func(received any){ + func(received any) { test.Equal(t, addedMsg, received.(string)) }, + func(received any) { test.Equal(t, updatedMsg, received.(string)) }, } mockT := test.MockTestingT{ MockHelper: func() {}, MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { test.NotCalled(t) }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { printerExpectedCalls[0](args[0]) // shift @@ -236,7 +236,7 @@ func TestMatchSnapshot(t *testing.T) { t.Run("should print warning if no params provided", func(t *testing.T) { mockT := test.MockTestingT{ MockHelper: func() {}, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.Equal( t, colors.Sprint(colors.Yellow, "[warning] MatchSnapshot call without params\n"), @@ -251,16 +251,16 @@ func TestMatchSnapshot(t *testing.T) { t.Run("diff should not print the escaped characters", func(t *testing.T) { setupSnapshot(t, fileName, false) - printerExpectedCalls := []func(received interface{}){ - func(received interface{}) { test.Equal(t, addedMsg, received.(string)) }, - func(received interface{}) { test.NotCalled(t) }, + printerExpectedCalls := []func(received any){ + func(received any) { test.Equal(t, addedMsg, received.(string)) }, + func(received any) { test.NotCalled(t) }, } mockT := test.MockTestingT{ MockHelper: func() {}, MockName: func() string { return "mock-name" }, - MockError: func(args ...interface{}) { + MockError: func(args ...any) { expected := "\n\x1b[38;5;52m\x1b[48;5;225m- Snapshot - 3\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m" + "+ Received + 3\x1b[0m\n\n\x1b[38;5;52m\x1b[48;5;225m- int(10)\x1b[0m\n\x1b[38;5;52m\x1b[48;5;225m" + "- hello world----\x1b[0m\n\x1b[38;5;52m\x1b[48;5;225m- ---\x1b[0m\n\x1b[38;5;22m\x1b[48;5;159m" + @@ -272,7 +272,7 @@ func TestMatchSnapshot(t *testing.T) { test.Equal(t, expected, args[0].(string)) }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { printerExpectedCalls[0](args[0]) // shift diff --git a/snaps/skip.go b/snaps/skip.go index 420e119..7016257 100644 --- a/snaps/skip.go +++ b/snaps/skip.go @@ -19,7 +19,7 @@ var ( // Wrapper of testing.Skip // // Keeps track which snapshots are getting skipped and not marked as obsolete. -func Skip(t testingT, args ...interface{}) { +func Skip(t testingT, args ...any) { t.Helper() trackSkip(t) @@ -29,7 +29,7 @@ func Skip(t testingT, args ...interface{}) { // Wrapper of testing.Skipf // // Keeps track which snapshots are getting skipped and not marked as obsolete. -func Skipf(t testingT, format string, args ...interface{}) { +func Skipf(t testingT, format string, args ...any) { t.Helper() trackSkip(t) diff --git a/snaps/skip_test.go b/snaps/skip_test.go index 6da47a9..58301d1 100644 --- a/snaps/skip_test.go +++ b/snaps/skip_test.go @@ -13,17 +13,17 @@ func TestSkip(t *testing.T) { t.Cleanup(func() { skippedTests = newSyncSlice() }) - skipArgs := []interface{}{1, 2, 3, 4, 5} + skipArgs := []any{1, 2, 3, 4, 5} mockT := test.MockTestingT{ MockHelper: func() {}, - MockSkip: func(args ...interface{}) { + MockSkip: func(args ...any) { test.Equal(t, skipArgs, args) }, MockName: func() string { return "mock-test" }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.Equal(t, skippedMsg, args[0].(string)) }, } @@ -39,14 +39,14 @@ func TestSkip(t *testing.T) { mockT := test.MockTestingT{ MockHelper: func() {}, - MockSkipf: func(format string, args ...interface{}) { + MockSkipf: func(format string, args ...any) { test.Equal(t, "mock", format) - test.Equal(t, []interface{}{1, 2, 3, 4, 5}, args) + test.Equal(t, []any{1, 2, 3, 4, 5}, args) }, MockName: func() string { return "mock-test" }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.Equal(t, skippedMsg, args[0].(string)) }, } @@ -66,7 +66,7 @@ func TestSkip(t *testing.T) { MockName: func() string { return "mock-test" }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.Equal(t, skippedMsg, args[0].(string)) }, } @@ -86,7 +86,7 @@ func TestSkip(t *testing.T) { MockName: func() string { return "mock-test" }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.Equal(t, skippedMsg, args[0].(string)) }, } @@ -138,7 +138,7 @@ func TestSkip(t *testing.T) { MockName: func() string { return "TestMock/Skip" }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.Equal(t, skippedMsg, args[0].(string)) }, } @@ -166,7 +166,7 @@ func TestSkip(t *testing.T) { MockName: func() string { return "Test" }, - MockLog: func(args ...interface{}) { + MockLog: func(args ...any) { test.Equal(t, skippedMsg, args[0].(string)) }, } diff --git a/snaps/snapshot.go b/snaps/snapshot.go index da93349..7ecaceb 100644 --- a/snaps/snapshot.go +++ b/snaps/snapshot.go @@ -74,7 +74,7 @@ func WithConfig(args ...func(*config)) *config { return &s } -func handleError(t testingT, err interface{}) { +func handleError(t testingT, err any) { t.Helper() t.Error(err) testEvents.register(erred) diff --git a/snaps/snapshot_test.go b/snaps/snapshot_test.go index 3348812..d1a6707 100644 --- a/snaps/snapshot_test.go +++ b/snaps/snapshot_test.go @@ -207,7 +207,7 @@ string hello world 1 3 2 func TestEscapeEndChars(t *testing.T) { t.Run("should escape end chars inside data", func(t *testing.T) { snapPath := filepath.Join(t.TempDir(), "__snapshots__/mock-test.snap") - snapshot := takeSnapshot([]interface{}{"my-snap", endSequence}) + snapshot := takeSnapshot([]any{"my-snap", endSequence}) test.NoError(t, addNewSnapshot("[mock-id]", snapshot, snapPath)) test.Equal(t, "\n[mock-id]\nmy-snap\n/-/-/-/\n---\n", test.GetFileContent(t, snapPath)) @@ -215,7 +215,7 @@ func TestEscapeEndChars(t *testing.T) { t.Run("should not escape --- if not end chars", func(t *testing.T) { snapPath := filepath.Join(t.TempDir(), "__snapshots__/mock-test.snap") - snapshot := takeSnapshot([]interface{}{"my-snap---", endSequence}) + snapshot := takeSnapshot([]any{"my-snap---", endSequence}) test.NoError(t, addNewSnapshot("[mock-id]", snapshot, snapPath)) test.Equal(t, "\n[mock-id]\nmy-snap---\n/-/-/-/\n---\n", test.GetFileContent(t, snapPath)) diff --git a/snaps/utils.go b/snaps/utils.go index e276179..4cdbee4 100644 --- a/snaps/utils.go +++ b/snaps/utils.go @@ -42,12 +42,13 @@ type ( set map[string]struct{} testingT interface { Helper() - Skip(args ...interface{}) - Skipf(format string, args ...interface{}) + Skip(...any) + Skipf(string, ...any) SkipNow() Name() string - Error(args ...interface{}) - Log(args ...interface{}) + Error(...any) + Log(...any) + Cleanup(func()) } )