Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#832 Add tests for sealed classes #861

Merged
merged 1 commit into from Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 49 additions & 0 deletions mockk/common/src/test/kotlin/io/mockk/it/SealedClassTest.kt
@@ -0,0 +1,49 @@
package io.mockk.it

import io.mockk.every
import io.mockk.mockk
import kotlin.test.Test
import kotlin.test.assertEquals


class SealedClassTest {

@Test
fun serviceReturnsSealedClassImpl() {
val factory = mockk<Factory> {
every { create() } returns Leaf(1)
}

val result = factory.create()

assertEquals(Leaf(1), result)
}

@Test
fun serviceAnswersSealedClassImpl() {
val factory = mockk<Factory> {
every { create() } answers { Leaf(1) }
}

val result = factory.create()

assertEquals(Leaf(1), result)
}

companion object {

sealed class Node

data class Root(val id: Int) : Node()
data class Leaf(val id: Int) : Node()

interface Factory {
fun create(): Node
}

class FactoryImpl : Factory {
override fun create(): Node = Root(0)
}

}
}
49 changes: 49 additions & 0 deletions mockk/common/src/test/kotlin/io/mockk/it/SealedInterfaceTest.kt
@@ -0,0 +1,49 @@
package io.mockk.it

import io.mockk.every
import io.mockk.mockk
import kotlin.test.Test
import kotlin.test.assertEquals


class SealedInterfaceTest {

@Test
fun serviceReturnsSealedClassImpl() {
val factory = mockk<Factory> {
every { create() } returns Leaf(1)
}

val result = factory.create()

assertEquals(Leaf(1), result)
}

@Test
fun serviceAnswersSealedClassImpl() {
val factory = mockk<Factory> {
every { create() } answers { Leaf(1) }
}

val result = factory.create()

assertEquals(Leaf(1), result)
}

companion object {

sealed interface Node

data class Root(val id: Int) : Node
data class Leaf(val id: Int) : Node

interface Factory {
fun create(): Node
}

class FactoryImpl : Factory {
override fun create(): Node = Root(0)
}

}
}