https://kotlinlang.org logo
Title
l

LeoColman

05/22/2020, 6:27 AM
I believe our
shouldContainExactly
might fit your usecase, but if you really want to create a custom matcher, it's not very hard:
j

JP

05/22/2020, 7:28 AM
@LeoColman Thanks for the reply. By
shouldContainExactly
, you mean
collections.shouldContainExactly
in matchers, and in order to use this I would have to make the data structure be a subclass of Kotlin’s
Collection
, right? Currently my usecase is simple, I’m just practicing Kotlin with problem solving via leetcode, and leetcode has some predefined data structure classes such as:
class ListNode(var `val`: Int) {
    var next: ListNode? = null
}
Then I wanted to make custom functions for testing locally with kotest, where I have written a function like
fun linkedListOf(vararg elements: Int): ListNode
where it creates a singly linked list with a syntax like
linkedListOf(1, 2, 3)
and returns the head
ListNode
.
l

LeoColman

05/22/2020, 9:00 PM
Ah, I see, you're the one creating the linkedlist, not using an usual LinkedList.
Then yes, perhaps the custom matcher can work for you
Did you understand how to create it?
j

JP

05/24/2020, 1:16 PM
I just read Custom Matchers, I think I understood how to create it. I first tried to implement Kotlin’s
Collection
myself and then used kotest’s
shouldContainExactly
matcher. Next I’ll try to create this custom matchers. Thanks for the info!
BTW, it seems that in the link of Custom Matchers reference there’s a minor typo, where it’s written
Result
instead of
MatcherResult
?
l

LeoColman

05/24/2020, 11:35 PM
You're correct. I fixed that name