christophsturm
02/01/2021, 12:17 PMimport strikt.api.expectThat
import strikt.assertions.containsExactly
data class Person(val name: String, val age: Int)
expectThat(listOf(Person("james", 28), Person("jimmy", 28))).containsExactly(
Person("james", 28), Person("jimmy", 28)
)
but what if there is a third field that is random, and that i can’t assert on, what would be the best way to write that in strikt?
data class Person(val name: String, val age: Int, val weight:Double=Random.nextDouble())
i want all the elements and the order but not assert on weight.christophsturm
02/01/2021, 12:26 PMexpectThat(listOf(Person("james", 28), Person("jimmy", 28))).containsExactly(
obj{ get {name}.isEqualTo("james"), get{age}.isEqualTo(28)}, obj{ get {name}.isEqualTo("jim"), get{age}.isEqualTo(28)}
)
christophsturm
02/01/2021, 12:28 PMexpectThat(listOf(Person("james", 28), Person("jimmy", 28))).containsExactly(
listOf(Person("james", 28), Person("jimmy", 28))
, ignore=Person::weight)
robfletcher
02/01/2021, 2:59 PMequals
implementation on the class if it really doesn’t represent logical equality. i.e. is it the test that needs to change, or is the difficulty in implementing the test telling you something about the design?robfletcher
02/01/2021, 3:03 PM@Transient
fields in their equals
implementation but apparently notchristophsturm
02/01/2021, 3:25 PMrobfletcher
02/01/2021, 3:32 PMisOrdered()
and isOrderedBy(Comparator)
assertionschristophsturm
02/01/2021, 3:37 PMchristophsturm
02/01/2021, 3:38 PMexpectThat(listOf(Person("james", 28), Person("jimmy", 28))).map{it.copy(weight=>null)}. containsExactly(
listOf(Person("james", 28), Person("jimmy", 28)))
this could work btw, but you are right its just not a great testrobfletcher
02/02/2021, 3:36 PMfun <T : Collection<E>, E> Builder<T>.isSorted(comparator: Comparator<E>)
. I’m adding fun <T : Collection<E>, E : Comparable<E>> Builder<T>.isSorted()
now