Skip to content

Map containsOnly with pairs #68

Description

@robstoll

Platform (JVM, JS and/or Android): all

Code related feature

assert(mapOf("a" to 1, "b" to 2)).containsOnly("b" to 2, "a" to 1)

Where InAnyOrder is implicit for Maps as they are usually unordered

Currently we can do the following to achieve the same

assert(mapOf("a" to 1, "b" to 2)).asEntries().contains.inAnyOrder.only.entries(
  { isKeyValue("b", 2) }, 
  { isKeyValue("a", 1) }
)

Clearly the shortcut is easier to write and read.
Moreover, the reporting could be improved. With asEntries()... it looks as follows (contains c to 2 and b to 1):

assert: {a=1, b=2}        (java.util.LinkedHashMap <873610597>)
◆ contains only, in any order: 
  ✘ an entry which: 
      » ▶ key:        <1497845528>
          ◾ to be: "c"        <1710989308>
      » ▶ value:        (kotlin.Int <1047087935>)
          ◾ to be: 2        (kotlin.Int <464887938>)
  ✘ an entry which: 
      » ▶ key:        <1497845528>
          ◾ to be: "d"        <1710989308>
      » ▶ value:        (kotlin.Int <1047087935>)
          ◾ to be: 2        (kotlin.Int <464887938>)
  ✘ an entry which: 
      » ▶ key:        <1497845528>
          ◾ to be: "b"        <2020152163>
      » ▶ value:        (kotlin.Int <1047087935>)
          ◾ to be: 1        (kotlin.Int <1047087935>)
  ✘ ▶ size: 2
      ◾ to be: 3
  ❗❗ following entries were mismatched: 
     ⚬ a=1        (java.util.LinkedHashMap.Entry <1104443373>)
     ⚬ b=2        (java.util.LinkedHashMap.Entry <898694235>)

comparing it to Map.contains:

assert: {a=1, b=2}        (java.util.LinkedHashMap <503938393>)
◆ contains, in any order: 
  ⚬ ▶ entry "c": ❗❗ key does not exist
        » to be: 2        (kotlin.Int <970865974>)
  ⚬ ▶ entry "d": ❗❗ key does not exist
        » to be: 2        (kotlin.Int <970865974>)
  ⚬ ▶ entry "b": 2        (kotlin.Int <970865974>)
      ◾ to be: 1        (kotlin.Int <1827171553>)

I would imagine a reporting like the following:

◆ ▶ size: 2
    ◾ to be: 3
◆ contains only, in any order: 
  ✘ entry "c": ❗❗ key does not exist
       » to be: 2        (kotlin.Int <970865974>)
  ✘ entry "d": ❗❗ key does not exist
       » to be: 2        (kotlin.Int <970865974>)
  ✘ entry "b": 2
       » to be: 1        (kotlin.Int <1827171553>)
 ❗❗ following entries were not matched by key at all: 
     ⚬ entry "a": 1       (kotlin.Int <970345974>)

Please react with 👍 if you would like to see this feature implemented in Atrium, the more upvotes the more likely I will implement it myself -- feel free to sponsor me, that would be a motivation too.
You are of course welcome to work on this issue. Write I'll work on it as comment so that we can assign the task to you.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions