github.com/weaveworks/common@v0.0.0-20230728070032-dd9e68f319d5/test/diff.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/davecgh/go-spew/spew"
     5  	"github.com/pmezard/go-difflib/difflib"
     6  )
     7  
     8  // Diff diffs two arbitrary data structures, giving human-readable output.
     9  func Diff(want, have interface{}) string {
    10  	config := spew.NewDefaultConfig()
    11  	// Set ContinueOnMethod to true if you cannot see a difference and
    12  	// want to look beyond the String() method
    13  	config.ContinueOnMethod = false
    14  	config.SortKeys = true
    15  	config.SpewKeys = true
    16  	text, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
    17  		A:        difflib.SplitLines(config.Sdump(want)),
    18  		B:        difflib.SplitLines(config.Sdump(have)),
    19  		FromFile: "want",
    20  		ToFile:   "have",
    21  		Context:  3,
    22  	})
    23  	return "\n" + text
    24  }