Skip to content

Instantly share code, notes, and snippets.

@ciriti
Last active April 13, 2019 18:12
Show Gist options
  • Save ciriti/3822f0e50f05d9b623506b5faf1e2fea to your computer and use it in GitHub Desktop.
Save ciriti/3822f0e50f05d9b623506b5faf1e2fea to your computer and use it in GitHub Desktop.
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.times
import com.nhaarman.mockito_kotlin.verify
import org.junit.Test
class InterfaceSegregationTest {
val mock = mock<()->Unit>()
@Test
fun `test for EventListener`() {
val eventTrigger = EventTrigger()
eventTrigger.setEventListener {
onEventType_1 { mock() }
}
eventTrigger.triggerEventType1()
verify(mock, times(1)).invoke()
}
}
interface EventListener {
fun onEventType_1(s: String)
fun onEventType_3(s: String)
fun onEventType_2(s: String)
}
class EventListenerImpl : EventListener {
private var onEventType_1: ((s: String) -> Unit)? = null
private var onEventType_2: ((s: String) -> Unit)? = null
private var onEventType_3: ((s: String) -> Unit)? = null
fun onEventType_1(f: (s: String) -> Unit) { onEventType_1 = f }
fun onEventType_3(f: (s: String) -> Unit) { onEventType_2 = f }
fun onEventType_2(f: (s: String) -> Unit) { onEventType_3 = f }
override fun onEventType_1(s: String) { onEventType_1?.invoke(s) }
override fun onEventType_2(s: String) { onEventType_2?.invoke(s) }
override fun onEventType_3(s: String) { onEventType_3?.invoke(s) }
}
fun EventTrigger.setEventListener(init: EventListenerImpl.() -> Unit) {
val listener = EventListenerImpl()
listener.init()
eventListener = listener
}
class EventTrigger(var eventListener: EventListener? = null) {
fun triggerEventType1() {
eventListener?.onEventType_1("event 1")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment