Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion for determining if a maps values exist within another map? #762

Open
Mo0rBy opened this issue May 30, 2024 · 2 comments
Open

Assertion for determining if a maps values exist within another map? #762

Mo0rBy opened this issue May 30, 2024 · 2 comments

Comments

@Mo0rBy
Copy link

Mo0rBy commented May 30, 2024

I'd like to assert that mapA contains all the key:value pairs in mapB but mapA can contain more key:value pairs.

For examples:

mapA := map[string]string{
	"keyA": "valA",
	"key2": "val2",
	"keyB": "valB",
	"key1": "val1",
}

mapB := map[string]string{
	"key1": "val1",
	"key2": "val2",
}

Expect(mapA).To(Contain(mapB))

Very similar behaviour to the ContainElements() matcher, but for maps instead of slices and without needing to explicitly define the keys or values that you are expecting, it should be able to infer that from mapA.

I think there is probably a way to do this with the existing matchers somehow, but I can't figure it out if there is, for example

I think this could be a common enough use case that it should have its own matcher as well, so I thought I should post my query here.

@Mo0rBy
Copy link
Author

Mo0rBy commented May 30, 2024

For now, I'm just looping over the map and then using the HaveKeyWithValue() matcher which is actually clean enough, so on 2nd thought, maybe this isn't needed that badly:

mapA := map[string]string{
	"keyA": "valA",
	"key2": "val2",
	"keyB": "valB",
	"key1": "val1",
}

mapB := map[string]string{
	"key1": "val1",
	"key2": "val2",
}

k, v := range mapB {
	Expect(mapA).To(HaveKeyWithValue(k, v))
}

@onsi
Copy link
Owner

onsi commented May 31, 2024

Hey @Mo0rBy there isn’t a dedicated matcher for this and i would likely do what you’re doing (loop). If you’re interested in submitting a PR I’d be happy to pull in a new matcher. The trickiest bit is usually picking the right name! Perhaps ContainMap()?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants