Skip to content

Instantly share code, notes, and snippets.

View drawers's full-sized avatar
🟩
In greenish twilight at the bottom of the Rhine

David Rawson drawers

🟩
In greenish twilight at the bottom of the Rhine
View GitHub Profile
@drawers
drawers / DebounceIf.kt
Last active August 12, 2019 21:56
The complete set of unit tests for debounceIf
val testScheduler = TestScheduler()
val states = listOf(Loading, Loaded, ClickX, ClickY)
@Test
fun `loading followed 500ms later by loaded is emitted`() {
val observable = Observable.fromIterable(listOf(0, 500, 1000, 2000))
.zipWith(states) { index, state -> Pair(index, state) }
.delay {
Observable.timer(it.first.toLong(), TimeUnit.MILLISECONDS, testScheduler)
@drawers
drawers / EasyMock.kt
Created December 13, 2019 01:29
EasyMock example in Kotlin
import org.easymock.EasyMock.*
import org.easymock.EasyMockSupport
import org.easymock.Mock
import org.junit.Before
import org.junit.Test
class EasyMock {
@Mock
lateinit var mock: MutableList<String>
@drawers
drawers / Mockito.kt
Created December 13, 2019 02:13
Mockito in Kotlin
class Mockito {
@Mock
lateinit var mock: MutableList<String>
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
}
@drawers
drawers / MockitoKotlin.kt
Created December 13, 2019 02:34
A slightly tidier example with Mockito Kotlin
import com.nhaarman.mockitokotlin2.whenever
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations
class MockitoKotlin {
@Mock
@drawers
drawers / Nullability.kt
Created December 14, 2019 03:28
Nullability in Kotlin must be explicit
fun main() {
var string: String = "abc"
string = null // compiler error
}
@drawers
drawers / MyMax.kt
Created December 19, 2019 05:27
An example method to calculate the max over a sequence of integers
fun List<Int>.myMax(): Int? {
val iterator = iterator()
if (!iterator.hasNext()) return null
var max = iterator.next()
while (iterator.hasNext()) {
val e = iterator.next()
if (max < e) max = e
}
return max
}
@drawers
drawers / ValueBased.kt
Created December 19, 2019 05:28
Value-based tests for MyMax
class ValueBased {
@Test
fun testLargestValue() {
val ints = listOf(4, 8, 7)
assertEquals(8, ints.myMax())
}
@Test
@drawers
drawers / MyMaxAlternate.kt
Created December 19, 2019 05:30
An alternate implementation of MyMax ;-)
fun List<Int>.myMax(): Int? {
return if (this.size < 2) null else this[1]
}
@drawers
drawers / PropertyBased.kt
Last active December 19, 2019 06:10
A simple example of property-based testing
import io.kotlintest.matchers.beGreaterThan
import io.kotlintest.matchers.collections.shouldContain
import io.kotlintest.properties.assertAll
import io.kotlintest.shouldBe
import io.kotlintest.shouldNot
import io.kotlintest.specs.StringSpec
class KotlinTest : StringSpec() {
init {
import io.kotlintest.matchers.beGreaterThan
import io.kotlintest.matchers.collections.shouldContain
import io.kotlintest.properties.assertAll
import io.kotlintest.shouldBe
import io.kotlintest.shouldNot
import io.kotlintest.specs.StringSpec
class KotlinTest : StringSpec() {
init {