diff --git a/config.go b/config.go index 7e74142..811c49c 100644 --- a/config.go +++ b/config.go @@ -76,6 +76,15 @@ func SnapshotFileExtension(snapshotFileExtension string) Configurator { } } +// UseStringerMethods invoke String() or Error() methods when available rather than dumping the object. +// This should probably be disabled by default but is not for backwards compatibility reasons. +// Default: true +func UseStringerMethods(useStringerMethods bool) Configurator { + return func(c *Config) { + c.useStringerMethods = useStringerMethods + } +} + // Config provides the same snapshotting functions with additional configuration capabilities. type Config struct { shouldUpdate func() bool @@ -84,6 +93,7 @@ type Config struct { createNewAutomatically bool fatalOnMismatch bool snapshotFileExtension string + useStringerMethods bool } // NewDefaultConfig returns a new Config instance initialised with the same options as @@ -96,6 +106,7 @@ func NewDefaultConfig() *Config { CreateNewAutomatically(true), FatalOnMismatch(false), SnapshotFileExtension(""), + UseStringerMethods(true), ) } @@ -110,5 +121,6 @@ func (c *Config) clone() *Config { createNewAutomatically: c.createNewAutomatically, fatalOnMismatch: c.fatalOnMismatch, snapshotFileExtension: c.snapshotFileExtension, + useStringerMethods: c.useStringerMethods, } } diff --git a/cupaloy.go b/cupaloy.go index adc4962..476fae6 100644 --- a/cupaloy.go +++ b/cupaloy.go @@ -98,7 +98,7 @@ func (c *Config) WithOptions(configurators ...Configurator) *Config { } func (c *Config) snapshot(snapshotName string, i ...interface{}) error { - snapshot := takeSnapshot(i...) + snapshot := c.takeSnapshot(i...) prevSnapshot, err := c.readSnapshot(snapshotName) if os.IsNotExist(err) { @@ -111,7 +111,7 @@ func (c *Config) snapshot(snapshotName string, i ...interface{}) error { return err } - if snapshot == prevSnapshot || takeV1Snapshot(i...) == prevSnapshot { + if snapshot == prevSnapshot || c.takeV1Snapshot(i...) == prevSnapshot { // previous snapshot matches current value return nil } diff --git a/examples/.snapshots/TestUseStringerMethods-disabled b/examples/.snapshots/TestUseStringerMethods-disabled new file mode 100644 index 0000000..fa5e34e --- /dev/null +++ b/examples/.snapshots/TestUseStringerMethods-disabled @@ -0,0 +1,2 @@ +(examples_test.stringer) { +} diff --git a/examples/.snapshots/TestUseStringerMethods-enabled b/examples/.snapshots/TestUseStringerMethods-enabled new file mode 100644 index 0000000..3abfa5d --- /dev/null +++ b/examples/.snapshots/TestUseStringerMethods-enabled @@ -0,0 +1 @@ +(examples_test.stringer) with stringer diff --git a/examples/advanced_test.go b/examples/advanced_test.go index a5d9724..8d76434 100644 --- a/examples/advanced_test.go +++ b/examples/advanced_test.go @@ -2,11 +2,12 @@ package examples_test import ( "bytes" - "github.com/bradleyjkemp/cupaloy/v2/internal" "io/ioutil" "strings" "testing" + "github.com/bradleyjkemp/cupaloy/v2/internal" + "github.com/bradleyjkemp/cupaloy/v2" "github.com/stretchr/testify/mock" ) @@ -65,7 +66,6 @@ func TestUpdate(t *testing.T) { t.Fatalf("Error returned will be of type ErrSnapshotUpdated") } - } // If a snapshot doesn't exist then it is created and an error returned @@ -209,3 +209,22 @@ func TestSnapshotFileExtension(t *testing.T) { snapshotter := cupaloy.New(cupaloy.SnapshotFileExtension(".myextension")) snapshotter.SnapshotT(t, "This should end up in a file with extension .myextension") } + +type stringer struct { +} + +func (s stringer) String() string { + return "with stringer" +} + +func TestUseStringerMethods(t *testing.T) { + s := stringer{} + + t.Run("enabled", func(t *testing.T) { + cupaloy.New(cupaloy.UseStringerMethods(true)).SnapshotT(t, s) + }) + + t.Run("disabled", func(t *testing.T) { + cupaloy.New(cupaloy.UseStringerMethods(false)).SnapshotT(t, s) + }) +} diff --git a/util.go b/util.go index 757597e..57c8fd1 100644 --- a/util.go +++ b/util.go @@ -15,14 +15,6 @@ import ( "github.com/pmezard/go-difflib/difflib" ) -var spewConfig = spew.ConfigState{ - Indent: " ", - SortKeys: true, // maps should be spewed in a deterministic order - DisablePointerAddresses: true, // don't spew the addresses of pointers - DisableCapacities: true, // don't spew capacities of collections - SpewKeys: true, // if unable to sort map keys then spew keys to strings and sort those -} - //go:generate $GOPATH/bin/mockery -output=examples -outpkg=examples_test -testonly -name=TestingT // TestingT is a subset of the interface testing.TB allowing it to be mocked in tests. @@ -47,17 +39,28 @@ func envVariableSet(envVariable string) bool { return varSet } +func (c *Config) getSpewConfig() *spew.ConfigState { + return &spew.ConfigState{ + Indent: " ", + SortKeys: true, // maps should be spewed in a deterministic order + DisablePointerAddresses: true, // don't spew the addresses of pointers + DisableCapacities: true, // don't spew capacities of collections + SpewKeys: true, // if unable to sort map keys then spew keys to strings and sort those + DisableMethods: !c.useStringerMethods, + } +} + func (c *Config) snapshotFilePath(testName string) string { return filepath.Join(c.subDirName, testName+c.snapshotFileExtension) } // Legacy snapshot format where all items were spewed -func takeV1Snapshot(i ...interface{}) string { - return spewConfig.Sdump(i...) +func (c *Config) takeV1Snapshot(i ...interface{}) string { + return c.getSpewConfig().Sdump(i...) } // New snapshot format where some types are written out raw to the file -func takeSnapshot(i ...interface{}) string { +func (c *Config) takeSnapshot(i ...interface{}) string { snapshot := &bytes.Buffer{} for _, v := range i { switch vt := v.(type) { @@ -68,7 +71,7 @@ func takeSnapshot(i ...interface{}) string { snapshot.Write(vt) snapshot.WriteString("\n") default: - spewConfig.Fdump(snapshot, v) + c.getSpewConfig().Fdump(snapshot, v) } }