kotest icon indicating copy to clipboard operation
kotest copied to clipboard

Add shouldContainOnly

Open klitoskyriacou opened this issue 2 years ago • 2 comments

It would be useful to have an assertion that is exactly equivalent to AssertJ's assertThat(list).containsOnly(these,elements,in,any,number,and,any,order,and,nothing,else).

For example:

listOf(1, 1, 1).shouldContainOnly(1)   // expected to pass
listOf(1, 1, 2, 2).shouldContainOnly(1, 2)  // expected to pass
listOf(1, 1, 2).shouldContainOnly(1)   // expected to fail because it shouldn't contain 2
listOf(1, 1, 2).shouldContainOnly(1, 2, 3)  // expected to fail because it doesn't contain 3

klitoskyriacou avatar Sep 21 '22 17:09 klitoskyriacou

I think this is a tough one.

listOf(1, 1, 2, 2) shouldContainOnly listOf(1, 2)

is pretty ambigous to me what should actually be the result here just from the name

LeoColman avatar Sep 21 '22 19:09 LeoColman

It's not ambiguous, because you wouldn't call it like that. shouldContainOnly should take a vararg, not a list. Just as shouldContainExactly does.

If you want to use infix functions, it's list should containOnly(1, 2).

klitoskyriacou avatar Sep 21 '22 21:09 klitoskyriacou

@LeoColman I would like to contribute to this.

p1729 avatar Sep 24 '22 13:09 p1729

Go for it! I'll assign it to you

LeoColman avatar Sep 24 '22 13:09 LeoColman