smarty / assertions

Fluent assertion-style functions used by goconvey and gunit. Can also be used in any test or application.
Other
100 stars 34 forks source link

map[int64]bool ShouldContainKey didn't work as expected #48

Closed guojiex closed 1 year ago

guojiex commented 2 years ago
func TestContainsKey(t *testing.T) {
    Convey("Test IntersectMapsByKey", t, func() {
        map1 := map[int64]bool{
            1: true,
        }
        So(map1, ShouldContainKey, int64(1))
        So(map1, ShouldContainKey, 1)
    })
}

the first assertion passed, while the second one will fail, error msg:

Expected the map[int64]bool to contain the key: [1] (but it didn't)!

My current temp solution will be:

So(map1[1], ShouldBeTrue)

guojiex commented 2 years ago

From https://github.com/smartystreets/goconvey/issues/662

guojiex commented 2 years ago

map[int32]bool will have the same problem

mdwhatcott commented 2 years ago

@guojiex - You may be interested in my response to issue #30, which is very similar to this issue:

https://github.com/smartystreets/assertions/issues/30#issuecomment-370826288

To summarize:

Unlike the ShouldEqual assertion (which uses fairly loose comparison logic, allowing int(3) to appear equal to int32(3)), map keys should only be compared strictly (as if doing an == comparison) because that is how map keys work...

guojiex commented 2 years ago

How about printing some warning that the key type to compare is not the same as the key type defined in the map? This behavior is quite hidden and might cause confusion