~/snippets/go-assertEqMap
Published on

assertEqMap

210 words2 min read
import (
	"reflect"
	"testing"

	"github.com/google/go-cmp/cmp"
)


func assertEqMap(t *testing.T, a, b map[any]any) bool {
	if len(a) != len(b) {
		t.Errorf("map length is not equal. expected: (%v) actual: (%v)", a, b)
		return false
	}

	for ak, av := range a {
		if bv, ok := b[ak]; !ok || !reflect.DeepEqual(bv, av) {
			diff := cmp.Diff(av, bv)

			t.Errorf("map value is not equal for key %#v:\ndiff (-expected +actual):\n%s", ak, diff)
			return false
		}
	}

	return true
}