testify icon indicating copy to clipboard operation
testify copied to clipboard

proposal: assert.MapSubset (or just support maps in assert.Subset)

Open SleepyBrett opened this issue 3 years ago • 8 comments

or perhaps expand subset so it can also handle maps.

given

a := map[string]string{
		"one": "foo",
		"two": "bar",
		"three": "baz",
	 }

b := map[string]string{
		"one": "foo",
		"three": "baz",
	 }

c := map[string]string{
		"one": "foo",
		"three": "notbaz",
	 }


assert.MapSubset(t, a, b, "a does not contain b") // will pass
assert.MapSubset(t, b, a, "b does not contain a") // will fail b does not have two=bar
assert.MapSubset(t, a, c, "a does not contain c") // will fail a does not have three=notbaz

SleepyBrett avatar Apr 01 '22 18:04 SleepyBrett

Enhancing Subset would definitely be better IMO.

One question, should this assert true?

a := map[string]string{
		"one": "foo",
		"two": "bar",
	 }

b := map[string]string{
		"one": "baz",
	 }

assert.Subset(t, a, b)

Does it test that the key and value are in the superset, or does it follow two value dereference (_, ok := a["one"]) which only checks the key? It would need to be documented.

brackendawson avatar Apr 02 '22 19:04 brackendawson

it should test both the key and the value.

SleepyBrett avatar Apr 04 '22 15:04 SleepyBrett

@brackendawson What is the process to having this proposal accepted? If accepted @SleepyBrett can I work on it?

OladapoAjala avatar Apr 15 '22 03:04 OladapoAjala

That would be open a pull request and then hope a maintainer returns.

brackendawson avatar Apr 15 '22 11:04 brackendawson

@brackendawson sure thanks, would create a PR.

OladapoAjala avatar Apr 15 '22 19:04 OladapoAjala

Hello @brackendawson @SleepyBrett I created a PR for this issue here: https://github.com/stretchr/testify/pull/1178

OladapoAjala avatar Apr 22 '22 19:04 OladapoAjala

FYI testify Contains on Map type validates by map keys, not entries.

tisonkun avatar Jun 29 '22 05:06 tisonkun

While looking for this exact functionality I came across this ticket and #704 which looks like a duplicate.

eikenb avatar Feb 09 '23 20:02 eikenb