https://kotlinlang.org logo
#kotest
Title
# kotest
s

Sebastian Schuberth

08/16/2021, 11:42 AM
Say I have a single list of strings, and I want to perform testing on all pairs of strings except if both strings point to the same instance. Would property testing with an Exhausive be the best way to go here, or is there something simpler?
s

sam

08/16/2021, 11:44 AM
By strings point to the same instance, do you mean if they are equal ?
s

Sebastian Schuberth

08/16/2021, 11:45 AM
I really only want to skip the assertion if the strings point to the same instance. I do want to perform the check if string happend to be textually equivalent, but come from different strings instances.
Basically I just want to ensure to not compare a string to itself in the matrix of all combinations.
s

sam

08/16/2021, 11:47 AM
I think in the JVM all equal strings share the same instance
maybe that's only the case for literals actually
Something like this might work for you:
Copy code
val mystrings = listOf("a", "a", "b", "c")
   checkAll(Exhaustive.collection(mystrings), Exhaustive.collection(mystrings)) { a, b ->
      if (a !== b) {
         // test here
      }
   }
s

Sebastian Schuberth

08/16/2021, 11:48 AM
ah, crap, due to string pooling, might be. then that was a bad example.
so, if it wasn't strings, but other objects, would it then be preferable to use
cartesian(myobjects, myobjects)
?
s

sam

08/16/2021, 11:50 AM
cartesian is better if you want all combinations to be tested
s

Sebastian Schuberth

08/16/2021, 11:53 AM
so, there's no easy helper function yet to test all x-tuple permutations of a collection where it's ensured that no tuple members point to the same instance, correct?
s

sam

08/16/2021, 11:54 AM
Copy code
checkAll(Exhaustive.cartesian(mystrings.exhaustive(), mystrings.exhaustive()) { a, b -> Pair(a, b) }
   .filter { it.first != it.second }) { pairs ->

}
correct
I'll add some helpers now
❤️ 1
but they won't surface til 5.0
s

Sebastian Schuberth

08/16/2021, 11:55 AM
ok, thanks for confirming, i just wanted to verify i'm not doing something too complicated in my code (which looks similar to yours).
s

sam

08/16/2021, 11:55 AM
I'll add
Copy code
cartesianPairs
❤️ 1
2 Views