Skip to content

Is there any support for matching the rest of the key for map? #633

@rickyson96

Description

@rickyson96

Hi!

My usecase for this is to have a default matching value for map's key.

tc := map[string][]string{
  "test_key":  {"aaa"},
  "other_key": {},
  "ignore":    {},
}

Expect(tc).To(HaveKeyWithValue("test_key", "aaa"))

Now, the test case is that, other than test_key, it should be empty or not exists.
if I try something like this Expect(tc).To(HaveKeyWithValue(Not(Equal("test_key")), BeEmpty())), it will not match if there is no other key than the test key.

Currently I'm using this matcher for that case

Expect(tc).To(WithTransform(
  func(m map[string]string) map[string]string {
    delete(m, "test_key")
    return m
  },
  Or(BeEmpty(), HaveEach(BeEmpty())),
))

Currently, I'm doing this for a composed matcher, so, that solution seems good enough. But I think I'll ask anyway to see if there is anything I miss or that might be a possible improvement.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions