cosmos-sdk/schema/testing/statesim/module_diff.go
Aaron Craelius 0f39b4e491
chore(schema/testing): upgrade to go 1.23 iterators (#21282)
Co-authored-by: Julien Robert <julien@rbrt.fr>
2024-08-15 20:58:58 +00:00

56 lines
1.6 KiB
Go

package statesim
import (
"fmt"
"cosmossdk.io/schema/view"
)
// DiffModuleStates compares the module state of two objects that implement ModuleState and returns a string with a diff if they
// are different or the empty string if they are the same.
func DiffModuleStates(expected, actual view.ModuleState) string {
res := ""
expectedNumObjectCollections, err := expected.NumObjectCollections()
if err != nil {
res += fmt.Sprintf("ERROR getting expected num object collections: %s\n", err)
return res
}
actualNumObjectCollections, err := actual.NumObjectCollections()
if err != nil {
res += fmt.Sprintf("ERROR getting actual num object collections: %s\n", err)
return res
}
if expectedNumObjectCollections != actualNumObjectCollections {
res += fmt.Sprintf("OBJECT COLLECTION COUNT ERROR: expected %d, got %d\n", expectedNumObjectCollections, actualNumObjectCollections)
}
for expectedColl, err := range expected.ObjectCollections {
if err != nil {
res += fmt.Sprintf("ERROR getting expected object collection: %s\n", err)
continue
}
objTypeName := expectedColl.ObjectType().Name
actualColl, err := actual.GetObjectCollection(objTypeName)
if err != nil {
res += fmt.Sprintf("ERROR getting actual object collection: %s\n", err)
continue
}
if actualColl == nil {
res += fmt.Sprintf("Object Collection %s: actuall collection NOT FOUND\n", objTypeName)
continue
}
diff := DiffObjectCollections(expectedColl, actualColl)
if diff != "" {
res += "Object Collection " + objTypeName + "\n"
res += indentAllLines(diff)
}
}
return res
}