Skip to content

Migrate containsExactlyInAnyOrderEntriesOf() to containsExactly() for singleton maps #759

@ash211

Description

@ash211

I had a similar complaint as #360 (comment), that rewriting .isEqualTo(Map.of( to .containsExactlyInAnyOrderEntriesOf(Map.of( is quite the mouthful and reduces readability.

Maybe for singleton maps (with one entry) we can rewrite to containsExactly(Map.entry( instead?

Example

    @Test
    void test() {
        Map<String, String> result = Map.of("key", "value");

        assertThat(result).isEqualTo(Map.of("key", "value")); // <-- (1) devs write this
        assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("key", "value1")); // <-- (2) we rewrite to this
        assertThat(result).containsExactly(Map.entry("key", "value1")); // <-- (3) rewrite to this instead?
    }

I think for singleton maps this is equivalent, because singleton maps have only one order.

Here's what the failure messages look like for (2) and (3). Note that there is a small change in order expectation, from the (and in same order).

// from (2)
Expecting map:
  {"key"="value"}
to contain only:
  ["key"="value1"]
map entries not found:
  ["key"="value1"]
and map entries not expected:
  ["key"="value"]


// from (3)
Expecting actual:
  {"key"="value"}
to contain exactly (and in same order):  // <-- note
  ["key"="value1"]
but some elements were not found:
  ["key"="value1"]
and others were not expected:
  ["key"="value"]

cc @iamdanfox @carterkozak @pkoenig10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions