onsi / gomega

Ginkgo's Preferred Matcher Library
http://onsi.github.io/gomega/
MIT License
2.16k stars 282 forks source link

A way to comapre two maps #726

Closed keisuke713 closed 8 months ago

keisuke713 commented 8 months ago

Is there a matcher that check if two maps are equal? What I wanna to do is comparison of two maps, and if they do not hold the exact same key value pair, then display difference like below

a := map[string]string{
    "key1": "value1",
    "key2": "value2",
}
b := map[string]string{
    "key1": "value1",
    "key2": "value22",
}

Expect(a).To(HogeMatcher(b))
=> expect "key2: value2" but got "key2: value22"

I think gomega seems not to have such matcher, which provides detailed difference between two maps. Therefore, in my project, I gonna create custom matcher that suit my above need, but if any matcher, could you let me know about it.

Best Regard.

onsi commented 8 months ago

hey @keisuke713 you can use BeComparableTo() - this uses go-cmp under the hood and will give you a useful diff:

  [FAILED] Expected object to be comparable, diff:   map[string]string{
        "key1": "value1",
  -     "key2": "value2",
  +     "key2": "value22",
    }
keisuke713 commented 8 months ago

@onsi It works as I expected! thanks!