Skip to content

Instantly share code, notes, and snippets.

View jonreeve's full-sized avatar

Jon Reeve jonreeve

View GitHub Profile
@jonreeve
jonreeve / CallResult.kt
Last active June 16, 2021 11:25
Sealed failure types - with some wrapping + potentially reusable handling
package com.myapp.domain.CallResult
sealed class CallResult<out S, out F> {
data class Success<out S> internal constructor(val value: S) : CallResult<S, Nothing>()
data class Failure<out F> internal constructor(val failure: F) : CallResult<Nothing, F>()
inline fun <V> fold(onSuccess: (S) -> V, onFailure: (F) -> V): V =
when (this) {
is Success -> onSuccess(value)
is Failure -> onFailure(failure)
@jonreeve
jonreeve / ApiCallFailure.kt
Last active June 16, 2021 09:05
Sealed failure types - flat but restrictive
package com.myapp.api.common.failure
import com.myapp.api.common.failure.differentpackage.ApiCall3Failure
// This Common errors type has to know all the other types to include it in, which isn't nice. They can't include it.
// Also, they all must be in the same module (not sooo bad) and PACKAGE (very restrictive), so this third one won't work
// as it's been put in another package.
sealed class Common() : ApiCall1Failure, ApiCall2Failure /*, ApiCall3Failure */ {
object NetworkFailure : Common()
object ServerFailure : Common()
@jonreeve
jonreeve / DispatcherWithIdlingResource.kt
Created May 21, 2021 09:56
Coroutine dispatcher idling resources for espresso tests
package test.framework.idlingresources
import androidx.test.espresso.IdlingResource
/**
* A shared interface for our wrappers around [kotlinx.coroutines.CoroutineDispatcher] and
* [kotlinx.coroutines.MainCoroutineDispatcher] that expose an [IdlingResource] for each of them.
*/
interface DispatcherWithIdlingResource {
val idlingResource: IdlingResource
@jonreeve
jonreeve / OkHttpIdlingResource.kt
Last active May 21, 2021 09:57
OkHttpIdlingResource + test rule for espresso tests
package test.framework.idlingresources
import androidx.test.espresso.IdlingResource
import okhttp3.Dispatcher
/**
* An [IdlingResource] for OkHttp, roughly based on [https://github.com/JakeWharton/okhttp-idling-resource], but in Kotlin,
* and with a check to prevent experiencing this issue: ([https://github.com/JakeWharton/okhttp-idling-resource/issues/10]).
*/
class OkHttpIdlingResource constructor(private val name: String, private val dispatcher: Dispatcher) : IdlingResource {
@jonreeve
jonreeve / FlowTurbine.kt
Last active May 27, 2021 09:53
Turbine but Arrange-Act-Assert
/*
* Copyright (C) 2018 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@jonreeve
jonreeve / spek_in_spock_or_junit.groovy
Last active March 29, 2018 02:38
Spek examples in Spock or JUnit + Hamcrest
// For first example on Spek site:
// Define a field for the SUT on the spec class:
def calculator = new Calculator()
// then your Spock test body is just this:
expect:
calculator.sum(2, 4) == 6
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Asmera
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau