From ff913e936c0c6e180354f283191987fdce084ba8 Mon Sep 17 00:00:00 2001 From: endigma Date: Tue, 29 Apr 2025 13:13:33 +0100 Subject: [PATCH 1/2] Move all tests to `./tests/` with new Go module to isolate testing dependencies --- .github/workflows/test.yml | 23 +++--- go.mod | 6 -- go.sum | 6 -- testdata/config_Compact.dump | 1 - ...ig_DisablePointerReplacement_circular.dump | 3 - testdata/customDumper.dump | 15 ---- testdata/nilIntefacesInStructs.dump | 10 --- testdata/pointerAliasing.dump | 12 ---- testdata/recursive_maps.dump | 12 ---- testdata/unexported.dump | 5 -- dump_test.go => tests/dump_test.go | 72 +++---------------- tests/go.mod | 16 +++++ tests/go.sum | 10 +++ tests/testdata/config_Compact.dump | 1 + ...ig_DisablePointerReplacement_circular.dump | 3 + ...PointerReplacement_simpleReusedStruct.dump | 4 +- .../testdata}/config_DumpFunc.dump | 6 +- .../testdata}/config_FieldFilter.dump | 6 +- .../testdata}/config_FormatTime.dump | 6 +- .../testdata}/config_HidePrivateFields.dump | 6 +- .../testdata}/config_HideZeroValues.dump | 6 +- .../testdata}/config_HomePackage.dump | 0 .../testdata}/config_StrictGo.dump | 8 +-- .../testdata}/config_StripPackageNames.dump | 0 tests/testdata/customDumper.dump | 15 ++++ {testdata => tests/testdata}/maps.dump | 4 +- .../testdata}/multipleArgs_lineBreak.dump | 0 .../testdata}/multipleArgs_noSeparator.dump | 0 .../testdata}/multipleArgs_separator.dump | 0 tests/testdata/nilIntefacesInStructs.dump | 10 +++ tests/testdata/pointerAliasing.dump | 12 ++++ {testdata => tests/testdata}/primitives.dump | 12 ++-- tests/testdata/recursive_maps.dump | 12 ++++ tests/testdata/unexported.dump | 5 ++ 34 files changed, 133 insertions(+), 174 deletions(-) delete mode 100644 testdata/config_Compact.dump delete mode 100644 testdata/config_DisablePointerReplacement_circular.dump delete mode 100644 testdata/customDumper.dump delete mode 100644 testdata/nilIntefacesInStructs.dump delete mode 100644 testdata/pointerAliasing.dump delete mode 100644 testdata/recursive_maps.dump delete mode 100644 testdata/unexported.dump rename dump_test.go => tests/dump_test.go (78%) create mode 100644 tests/go.mod create mode 100644 tests/go.sum create mode 100644 tests/testdata/config_Compact.dump create mode 100644 tests/testdata/config_DisablePointerReplacement_circular.dump rename {testdata => tests/testdata}/config_DisablePointerReplacement_simpleReusedStruct.dump (56%) rename {testdata => tests/testdata}/config_DumpFunc.dump (82%) rename {testdata => tests/testdata}/config_FieldFilter.dump (64%) rename {testdata => tests/testdata}/config_FormatTime.dump (80%) rename {testdata => tests/testdata}/config_HidePrivateFields.dump (77%) rename {testdata => tests/testdata}/config_HideZeroValues.dump (74%) rename {testdata => tests/testdata}/config_HomePackage.dump (100%) rename {testdata => tests/testdata}/config_StrictGo.dump (61%) rename {testdata => tests/testdata}/config_StripPackageNames.dump (100%) create mode 100644 tests/testdata/customDumper.dump rename {testdata => tests/testdata}/maps.dump (74%) rename {testdata => tests/testdata}/multipleArgs_lineBreak.dump (100%) rename {testdata => tests/testdata}/multipleArgs_noSeparator.dump (100%) rename {testdata => tests/testdata}/multipleArgs_separator.dump (100%) create mode 100644 tests/testdata/nilIntefacesInStructs.dump create mode 100644 tests/testdata/pointerAliasing.dump rename {testdata => tests/testdata}/primitives.dump (70%) create mode 100644 tests/testdata/recursive_maps.dump create mode 100644 tests/testdata/unexported.dump diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bb3e7cd..4c4287c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,14 +7,15 @@ jobs: scheduled: runs-on: ubuntu-latest steps: - - name: Check out this repo - uses: actions/checkout@v2 - - name: Set up Go - uses: actions/setup-go@v2 - with: {go-version: '^1.16'} - - name: Download dependencies - run: go mod download - - name: Build - run: go build ./... - - name: Test - run: go test ./... + - name: Check out this repo + uses: actions/checkout@v2 + - name: Set up Go + uses: actions/setup-go@v2 + with: { go-version: "^1.16" } + - name: Download dependencies + run: go mod download + - name: Build + run: go build ./... + - name: Test + working-directory: tests + run: go test ./... diff --git a/go.mod b/go.mod index 17be31b..9cf63a0 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,3 @@ module github.com/sanity-io/litter go 1.16 - -require ( - github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b // indirect - github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0 // indirect - github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 -) diff --git a/go.sum b/go.sum index 800ae00..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +0,0 @@ -github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b h1:XxMZvQZtTXpWMNWK82vdjCLCe7uGMFXdTsJH0v3Hkvw= -github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0 h1:GD+A8+e+wFkqje55/2fOVnZPkoDIu1VooBWfNrnY8Uo= -github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312 h1:UsFdQ3ZmlzS0BqZYGxvYaXvFGUbCmPGy8DM7qWJJiIQ= -github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= diff --git a/testdata/config_Compact.dump b/testdata/config_Compact.dump deleted file mode 100644 index c669eb0..0000000 --- a/testdata/config_Compact.dump +++ /dev/null @@ -1 +0,0 @@ -[]interface{}{litter_test.options{Compact:false,StripPackageNames:false,HidePrivateFields:true,HomePackage:"",Separator:" ",StrictGo:false},&litter_test.BasicStruct{Public:1,private:2},litter_test.Function,&20,&20,litter.Dump,func(string,int)(bool,error),time.Time{wall:0,ext:63650361600,loc:nil}} \ No newline at end of file diff --git a/testdata/config_DisablePointerReplacement_circular.dump b/testdata/config_DisablePointerReplacement_circular.dump deleted file mode 100644 index b2da0cc..0000000 --- a/testdata/config_DisablePointerReplacement_circular.dump +++ /dev/null @@ -1,3 +0,0 @@ -&litter_test.RecursiveStruct{ // p0 - Ptr: p0, -} \ No newline at end of file diff --git a/testdata/customDumper.dump b/testdata/customDumper.dump deleted file mode 100644 index dba5867..0000000 --- a/testdata/customDumper.dump +++ /dev/null @@ -1,15 +0,0 @@ -map[string]interface {}{ - "v1": *litter_test.CustomMultiLineDumper{ // p0 - multi - line - }, - "v2": p0, - "v2x": *litter_test.CustomMultiLineDumper{ - multi - line - }, - "v3": litter_test.CustomSingleLineDumper, - "v4": *litter_test.CustomSingleLineDumper, // p1 - "v5": p1, - "v6": *litter_test.CustomSingleLineDumper, -} \ No newline at end of file diff --git a/testdata/nilIntefacesInStructs.dump b/testdata/nilIntefacesInStructs.dump deleted file mode 100644 index a3321b5..0000000 --- a/testdata/nilIntefacesInStructs.dump +++ /dev/null @@ -1,10 +0,0 @@ -[]*litter_test.InterfaceStruct{ - &litter_test.InterfaceStruct{ // p0 - Ifc: nil, - }, - &litter_test.InterfaceStruct{ - Ifc: p0, - }, - p0, - nil, -} \ No newline at end of file diff --git a/testdata/pointerAliasing.dump b/testdata/pointerAliasing.dump deleted file mode 100644 index 0b92c65..0000000 --- a/testdata/pointerAliasing.dump +++ /dev/null @@ -1,12 +0,0 @@ -[]*litter_test.RecursiveStruct{ - &litter_test.RecursiveStruct{ // p0 - Ptr: nil, - }, - p0, - &litter_test.RecursiveStruct{ - Ptr: p0, - }, - &litter_test.RecursiveStruct{ // p1 - Ptr: p1, - }, -} \ No newline at end of file diff --git a/testdata/recursive_maps.dump b/testdata/recursive_maps.dump deleted file mode 100644 index 4a0218b..0000000 --- a/testdata/recursive_maps.dump +++ /dev/null @@ -1,12 +0,0 @@ -map[*litter_test.RecursiveStruct]*litter_test.RecursiveStruct{ - &litter_test.RecursiveStruct{ // p0 - Ptr: p0, - }: &litter_test.RecursiveStruct{ // p1 - Ptr: p1, - }, - &litter_test.RecursiveStruct{ // p2 - Ptr: p2, - }: &litter_test.RecursiveStruct{ // p3 - Ptr: p3, - }, -} \ No newline at end of file diff --git a/testdata/unexported.dump b/testdata/unexported.dump deleted file mode 100644 index 4d21fb0..0000000 --- a/testdata/unexported.dump +++ /dev/null @@ -1,5 +0,0 @@ -litter_test.StructWithUnexportedType{ - unexported: litter_test.unexportedStruct{ - x: 0, - }, -} \ No newline at end of file diff --git a/dump_test.go b/tests/dump_test.go similarity index 78% rename from dump_test.go rename to tests/dump_test.go index 9049ed5..fa99bcd 100644 --- a/dump_test.go +++ b/tests/dump_test.go @@ -1,17 +1,14 @@ -package litter_test +package tests_test import ( - "errors" "fmt" "io" "os" - "os/exec" "reflect" "testing" "time" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "github.com/sanity-io/litter" ) @@ -156,7 +153,7 @@ func TestSdump_config(t *testing.T) { Separator: " ", } - data := []interface{}{ + data := []any{ opts, &BasicStruct{1, 2}, Function, @@ -180,7 +177,7 @@ func TestSdump_config(t *testing.T) { StripPackageNames: true, }, data) runTestWithCfg(t, "config_HomePackage", &litter.Options{ - HomePackage: "litter_test", + HomePackage: "tests_test", }, data) runTestWithCfg(t, "config_FieldFilter", &litter.Options{ FieldFilter: func(f reflect.StructField, v reflect.Value) bool { @@ -213,7 +210,7 @@ func TestSdump_config(t *testing.T) { basic := &BasicStruct{1, 2} runTestWithCfg(t, "config_DisablePointerReplacement_simpleReusedStruct", &litter.Options{ DisablePointerReplacement: true, - }, []interface{}{basic, basic}) + }, []any{basic, basic}) circular := &RecursiveStruct{} circular.Ptr = circular runTestWithCfg(t, "config_DisablePointerReplacement_circular", &litter.Options{ @@ -231,7 +228,7 @@ func TestSdump_multipleArgs(t *testing.T) { } func TestSdump_maps(t *testing.T) { - runTests(t, "maps", []interface{}{ + runTests(t, "maps", []any{ map[string]string{ "hello": "there", "something": "something something", @@ -278,7 +275,7 @@ func TestSdump_unexported(t *testing.T) { var standardCfg = litter.Options{} -func runTestWithCfg(t *testing.T, name string, cfg *litter.Options, cases ...interface{}) { +func runTestWithCfg(t *testing.T, name string, cfg *litter.Options, cases ...any) { t.Run(name, func(t *testing.T) { fileName := fmt.Sprintf("testdata/%s.dump", name) @@ -294,63 +291,10 @@ func runTestWithCfg(t *testing.T, name string, cfg *litter.Options, cases ...int return } - assertEqualStringsWithDiff(t, string(reference), dump) + assert.Equal(t, string(reference), dump) }) } -func runTests(t *testing.T, name string, cases ...interface{}) { +func runTests(t *testing.T, name string, cases ...any) { runTestWithCfg(t, name, &standardCfg, cases...) } - -func diffStrings(t *testing.T, expected, actual string) (*string, bool) { - if actual == expected { - return nil, true - } - - dir, err := os.MkdirTemp("", "test") - require.NoError(t, err) - defer os.RemoveAll(dir) - - require.NoError(t, os.WriteFile(fmt.Sprintf("%s/expected", dir), []byte(expected), 0644)) - require.NoError(t, os.WriteFile(fmt.Sprintf("%s/actual", dir), []byte(actual), 0644)) - - out, err := exec.Command("diff", "--side-by-side", - fmt.Sprintf("%s/expected", dir), - fmt.Sprintf("%s/actual", dir)).Output() - - var exitErr *exec.ExitError - if !errors.As(err, &exitErr) { - require.NoError(t, err) - } - - diff := string(out) - return &diff, false -} - -func assertEqualStringsWithDiff(t *testing.T, expected, actual string, - msgAndArgs ...interface{}) bool { - diff, ok := diffStrings(t, expected, actual) - if ok { - return true - } - - message := messageFromMsgAndArgs(msgAndArgs...) - if message == "" { - message = "Strings are different" - } - assert.Fail(t, fmt.Sprintf("%s (left is expected, right is actual):\n%s", message, *diff)) - return false -} - -func messageFromMsgAndArgs(msgAndArgs ...interface{}) string { - if len(msgAndArgs) == 0 || msgAndArgs == nil { - return "" - } - if len(msgAndArgs) == 1 { - return msgAndArgs[0].(string) - } - if len(msgAndArgs) > 1 { - return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...) - } - return "" -} diff --git a/tests/go.mod b/tests/go.mod new file mode 100644 index 0000000..c80b15a --- /dev/null +++ b/tests/go.mod @@ -0,0 +1,16 @@ +module github.com/sanity-io/litter/tests + +go 1.23.7 + +require ( + github.com/sanity-io/litter v1.5.8 + github.com/stretchr/testify v1.10.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + +replace github.com/sanity-io/litter => .. diff --git a/tests/go.sum b/tests/go.sum new file mode 100644 index 0000000..713a0b4 --- /dev/null +++ b/tests/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/tests/testdata/config_Compact.dump b/tests/testdata/config_Compact.dump new file mode 100644 index 0000000..933cc47 --- /dev/null +++ b/tests/testdata/config_Compact.dump @@ -0,0 +1 @@ +[]interface{}{tests_test.options{Compact:false,StripPackageNames:false,HidePrivateFields:true,HomePackage:"",Separator:" ",StrictGo:false},&tests_test.BasicStruct{Public:1,private:2},tests_test.Function,&20,&20,litter.Dump,func(string,int)(bool,error),time.Time{wall:0,ext:63650361600,loc:nil}} \ No newline at end of file diff --git a/tests/testdata/config_DisablePointerReplacement_circular.dump b/tests/testdata/config_DisablePointerReplacement_circular.dump new file mode 100644 index 0000000..a679dee --- /dev/null +++ b/tests/testdata/config_DisablePointerReplacement_circular.dump @@ -0,0 +1,3 @@ +&tests_test.RecursiveStruct{ // p0 + Ptr: p0, +} \ No newline at end of file diff --git a/testdata/config_DisablePointerReplacement_simpleReusedStruct.dump b/tests/testdata/config_DisablePointerReplacement_simpleReusedStruct.dump similarity index 56% rename from testdata/config_DisablePointerReplacement_simpleReusedStruct.dump rename to tests/testdata/config_DisablePointerReplacement_simpleReusedStruct.dump index 0154b40..6c7e18e 100644 --- a/testdata/config_DisablePointerReplacement_simpleReusedStruct.dump +++ b/tests/testdata/config_DisablePointerReplacement_simpleReusedStruct.dump @@ -1,9 +1,9 @@ []interface {}{ - &litter_test.BasicStruct{ // p0 + &tests_test.BasicStruct{ // p0 Public: 1, private: 2, }, - &litter_test.BasicStruct{ // p0 + &tests_test.BasicStruct{ // p0 Public: 1, private: 2, }, diff --git a/testdata/config_DumpFunc.dump b/tests/testdata/config_DumpFunc.dump similarity index 82% rename from testdata/config_DumpFunc.dump rename to tests/testdata/config_DumpFunc.dump index b5cf5d2..eccea17 100644 --- a/testdata/config_DumpFunc.dump +++ b/tests/testdata/config_DumpFunc.dump @@ -1,5 +1,5 @@ []interface {}{ - litter_test.options{ + tests_test.options{ Compact: bool"off", StripPackageNames: bool"off", HidePrivateFields: bool"on", @@ -7,11 +7,11 @@ Separator: " ", StrictGo: bool"off", }, - &litter_test.BasicStruct{ + &tests_test.BasicStruct{ Public: 1, private: 2, }, - litter_test.Function, + tests_test.Function, &20, &20, litter.Dump, diff --git a/testdata/config_FieldFilter.dump b/tests/testdata/config_FieldFilter.dump similarity index 64% rename from testdata/config_FieldFilter.dump rename to tests/testdata/config_FieldFilter.dump index fe0d6aa..4f8b879 100644 --- a/testdata/config_FieldFilter.dump +++ b/tests/testdata/config_FieldFilter.dump @@ -1,10 +1,10 @@ []interface {}{ - litter_test.options{ + tests_test.options{ HomePackage: "", Separator: " ", }, - &litter_test.BasicStruct{}, - litter_test.Function, + &tests_test.BasicStruct{}, + tests_test.Function, &20, &20, litter.Dump, diff --git a/testdata/config_FormatTime.dump b/tests/testdata/config_FormatTime.dump similarity index 80% rename from testdata/config_FormatTime.dump rename to tests/testdata/config_FormatTime.dump index 718422c..5ffb858 100644 --- a/testdata/config_FormatTime.dump +++ b/tests/testdata/config_FormatTime.dump @@ -1,5 +1,5 @@ []interface {}{ - litter_test.options{ + tests_test.options{ Compact: false, StripPackageNames: false, HidePrivateFields: true, @@ -7,11 +7,11 @@ Separator: " ", StrictGo: false, }, - &litter_test.BasicStruct{ + &tests_test.BasicStruct{ Public: 1, private: 2, }, - litter_test.Function, + tests_test.Function, &20, &20, litter.Dump, diff --git a/testdata/config_HidePrivateFields.dump b/tests/testdata/config_HidePrivateFields.dump similarity index 77% rename from testdata/config_HidePrivateFields.dump rename to tests/testdata/config_HidePrivateFields.dump index 9316e00..fa883b0 100644 --- a/testdata/config_HidePrivateFields.dump +++ b/tests/testdata/config_HidePrivateFields.dump @@ -1,5 +1,5 @@ []interface {}{ - litter_test.options{ + tests_test.options{ Compact: false, StripPackageNames: false, HidePrivateFields: true, @@ -7,10 +7,10 @@ Separator: " ", StrictGo: false, }, - &litter_test.BasicStruct{ + &tests_test.BasicStruct{ Public: 1, }, - litter_test.Function, + tests_test.Function, &20, &20, litter.Dump, diff --git a/testdata/config_HideZeroValues.dump b/tests/testdata/config_HideZeroValues.dump similarity index 74% rename from testdata/config_HideZeroValues.dump rename to tests/testdata/config_HideZeroValues.dump index 0d6538f..e8605ce 100644 --- a/testdata/config_HideZeroValues.dump +++ b/tests/testdata/config_HideZeroValues.dump @@ -1,13 +1,13 @@ []interface {}{ - litter_test.options{ + tests_test.options{ HidePrivateFields: true, Separator: " ", }, - &litter_test.BasicStruct{ + &tests_test.BasicStruct{ Public: 1, private: 2, }, - litter_test.Function, + tests_test.Function, &20, &20, litter.Dump, diff --git a/testdata/config_HomePackage.dump b/tests/testdata/config_HomePackage.dump similarity index 100% rename from testdata/config_HomePackage.dump rename to tests/testdata/config_HomePackage.dump diff --git a/testdata/config_StrictGo.dump b/tests/testdata/config_StrictGo.dump similarity index 61% rename from testdata/config_StrictGo.dump rename to tests/testdata/config_StrictGo.dump index c74aea5..e6d88f5 100644 --- a/testdata/config_StrictGo.dump +++ b/tests/testdata/config_StrictGo.dump @@ -1,5 +1,5 @@ []interface {}{ - litter_test.options{ + tests_test.options{ Compact: false, StripPackageNames: false, HidePrivateFields: true, @@ -7,13 +7,13 @@ Separator: " ", StrictGo: false, }, - (func(v litter_test.BasicStruct) *litter_test.BasicStruct { return &v })(litter_test.BasicStruct{ + (func(v tests_test.BasicStruct) *tests_test.BasicStruct { return &v })(tests_test.BasicStruct{ Public: 1, private: 2, }), - litter_test.Function, + tests_test.Function, (func(v int) *int { return &v })(20), - (func(v litter_test.IntAlias) *litter_test.IntAlias { return &v })(20), + (func(v tests_test.IntAlias) *tests_test.IntAlias { return &v })(20), litter.Dump, func(string, int) (bool, error), time.Time{ diff --git a/testdata/config_StripPackageNames.dump b/tests/testdata/config_StripPackageNames.dump similarity index 100% rename from testdata/config_StripPackageNames.dump rename to tests/testdata/config_StripPackageNames.dump diff --git a/tests/testdata/customDumper.dump b/tests/testdata/customDumper.dump new file mode 100644 index 0000000..08e4235 --- /dev/null +++ b/tests/testdata/customDumper.dump @@ -0,0 +1,15 @@ +map[string]interface {}{ + "v1": *tests_test.CustomMultiLineDumper{ // p0 + multi + line + }, + "v2": p0, + "v2x": *tests_test.CustomMultiLineDumper{ + multi + line + }, + "v3": tests_test.CustomSingleLineDumper, + "v4": *tests_test.CustomSingleLineDumper, // p1 + "v5": p1, + "v6": *tests_test.CustomSingleLineDumper, +} \ No newline at end of file diff --git a/testdata/maps.dump b/tests/testdata/maps.dump similarity index 74% rename from testdata/maps.dump rename to tests/testdata/maps.dump index 61fd468..d0d995e 100644 --- a/testdata/maps.dump +++ b/tests/testdata/maps.dump @@ -9,7 +9,7 @@ 2: "two", 3: "three", }, - map[int]*litter_test.BlankStruct{ - 2: &litter_test.BlankStruct{}, + map[int]*tests_test.BlankStruct{ + 2: &tests_test.BlankStruct{}, }, } \ No newline at end of file diff --git a/testdata/multipleArgs_lineBreak.dump b/tests/testdata/multipleArgs_lineBreak.dump similarity index 100% rename from testdata/multipleArgs_lineBreak.dump rename to tests/testdata/multipleArgs_lineBreak.dump diff --git a/testdata/multipleArgs_noSeparator.dump b/tests/testdata/multipleArgs_noSeparator.dump similarity index 100% rename from testdata/multipleArgs_noSeparator.dump rename to tests/testdata/multipleArgs_noSeparator.dump diff --git a/testdata/multipleArgs_separator.dump b/tests/testdata/multipleArgs_separator.dump similarity index 100% rename from testdata/multipleArgs_separator.dump rename to tests/testdata/multipleArgs_separator.dump diff --git a/tests/testdata/nilIntefacesInStructs.dump b/tests/testdata/nilIntefacesInStructs.dump new file mode 100644 index 0000000..cf9b77b --- /dev/null +++ b/tests/testdata/nilIntefacesInStructs.dump @@ -0,0 +1,10 @@ +[]*tests_test.InterfaceStruct{ + &tests_test.InterfaceStruct{ // p0 + Ifc: nil, + }, + &tests_test.InterfaceStruct{ + Ifc: p0, + }, + p0, + nil, +} \ No newline at end of file diff --git a/tests/testdata/pointerAliasing.dump b/tests/testdata/pointerAliasing.dump new file mode 100644 index 0000000..8d3592d --- /dev/null +++ b/tests/testdata/pointerAliasing.dump @@ -0,0 +1,12 @@ +[]*tests_test.RecursiveStruct{ + &tests_test.RecursiveStruct{ // p0 + Ptr: nil, + }, + p0, + &tests_test.RecursiveStruct{ + Ptr: p0, + }, + &tests_test.RecursiveStruct{ // p1 + Ptr: p1, + }, +} \ No newline at end of file diff --git a/testdata/primitives.dump b/tests/testdata/primitives.dump similarity index 70% rename from testdata/primitives.dump rename to tests/testdata/primitives.dump index 2b82045..05c7054 100644 --- a/testdata/primitives.dump +++ b/tests/testdata/primitives.dump @@ -25,20 +25,20 @@ 3, }, "hello from interface", - litter_test.BlankStruct{}, - &litter_test.BlankStruct{}, - litter_test.BasicStruct{ + tests_test.BlankStruct{}, + &tests_test.BlankStruct{}, + tests_test.BasicStruct{ Public: 1, private: 2, }, 10, &10, - litter_test.Function, + tests_test.Function, func(string) (bool, error), nil, nil, - litter_test.CustomMap{}, - litter_test.CustomMap(nil), + tests_test.CustomMap{}, + tests_test.CustomMap(nil), chan string, chan<- int64, <-chan uint64, diff --git a/tests/testdata/recursive_maps.dump b/tests/testdata/recursive_maps.dump new file mode 100644 index 0000000..0d4faec --- /dev/null +++ b/tests/testdata/recursive_maps.dump @@ -0,0 +1,12 @@ +map[*tests_test.RecursiveStruct]*tests_test.RecursiveStruct{ + &tests_test.RecursiveStruct{ // p0 + Ptr: p0, + }: &tests_test.RecursiveStruct{ // p1 + Ptr: p1, + }, + &tests_test.RecursiveStruct{ // p2 + Ptr: p2, + }: &tests_test.RecursiveStruct{ // p3 + Ptr: p3, + }, +} \ No newline at end of file diff --git a/tests/testdata/unexported.dump b/tests/testdata/unexported.dump new file mode 100644 index 0000000..e569f59 --- /dev/null +++ b/tests/testdata/unexported.dump @@ -0,0 +1,5 @@ +tests_test.StructWithUnexportedType{ + unexported: tests_test.unexportedStruct{ + x: 0, + }, +} \ No newline at end of file From 269cbc480dd0b2476d9612a71cc4685d5cd1dba4 Mon Sep 17 00:00:00 2001 From: endigma Date: Wed, 30 Apr 2025 15:44:23 +0100 Subject: [PATCH 2/2] Revert to shellout diffing & update tests go version --- tests/dump_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++++- tests/go.mod | 2 +- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/tests/dump_test.go b/tests/dump_test.go index fa99bcd..22315ba 100644 --- a/tests/dump_test.go +++ b/tests/dump_test.go @@ -1,14 +1,17 @@ package tests_test import ( + "errors" "fmt" "io" "os" + "os/exec" "reflect" "testing" "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/sanity-io/litter" ) @@ -291,10 +294,63 @@ func runTestWithCfg(t *testing.T, name string, cfg *litter.Options, cases ...any return } - assert.Equal(t, string(reference), dump) + assertEqualStringsWithDiff(t, string(reference), dump) }) } +func diffStrings(t *testing.T, expected, actual string) (*string, bool) { + if actual == expected { + return nil, true + } + + dir, err := os.MkdirTemp("", "test") + require.NoError(t, err) + defer os.RemoveAll(dir) + + require.NoError(t, os.WriteFile(fmt.Sprintf("%s/expected", dir), []byte(expected), 0644)) + require.NoError(t, os.WriteFile(fmt.Sprintf("%s/actual", dir), []byte(actual), 0644)) + + out, err := exec.Command("diff", "--side-by-side", + fmt.Sprintf("%s/expected", dir), + fmt.Sprintf("%s/actual", dir)).Output() + + var exitErr *exec.ExitError + if !errors.As(err, &exitErr) { + require.NoError(t, err) + } + + diff := string(out) + return &diff, false +} + +func assertEqualStringsWithDiff(t *testing.T, expected, actual string, + msgAndArgs ...interface{}) bool { + diff, ok := diffStrings(t, expected, actual) + if ok { + return true + } + + message := messageFromMsgAndArgs(msgAndArgs...) + if message == "" { + message = "Strings are different" + } + assert.Fail(t, fmt.Sprintf("%s (left is expected, right is actual):\n%s", message, *diff)) + return false +} + +func messageFromMsgAndArgs(msgAndArgs ...interface{}) string { + if len(msgAndArgs) == 0 || msgAndArgs == nil { + return "" + } + if len(msgAndArgs) == 1 { + return msgAndArgs[0].(string) + } + if len(msgAndArgs) > 1 { + return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...) + } + return "" +} + func runTests(t *testing.T, name string, cases ...any) { runTestWithCfg(t, name, &standardCfg, cases...) } diff --git a/tests/go.mod b/tests/go.mod index c80b15a..2b05791 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -1,6 +1,6 @@ module github.com/sanity-io/litter/tests -go 1.23.7 +go 1.24.2 require ( github.com/sanity-io/litter v1.5.8