Skip to content

Instantly share code, notes, and snippets.

View dmcg's full-sized avatar
💭
Splendid

Duncan McGregor dmcg

💭
Splendid
View GitHub Profile
@dmcg
dmcg / plugin.kts
Created July 6, 2023 21:17
Test runner plugin
import com.intellij.execution.testframework.sm.runner.SMTRunnerEventsAdapter
import com.intellij.execution.testframework.sm.runner.SMTRunnerEventsListener
import com.intellij.execution.testframework.sm.runner.SMTestProxy
import com.intellij.notification.NotificationGroupManager
import com.intellij.openapi.Disposable
import com.intellij.openapi.progress.util.ColorProgressBar
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.popup.Balloon
import com.intellij.openapi.ui.popup.JBPopupFactory
package com.gildedrose
import com.gildedrose.domain.StockList
import com.gildedrose.foundation.magic
import com.gildedrose.persistence.*
import dev.forkhandles.result4k.Result
import dev.forkhandles.result4k.Success
import org.jooq.DSLContext
import org.junit.jupiter.api.Test
@dmcg
dmcg / receivers.kt
Created March 8, 2023 10:24
Kotlin multiple receiver ordering
// Improving the ergonomics of dual receivers while we wait for
// context receivers
// This has a method with two receivers
class Thing {
fun Context.foo(s: String): Int {
// pretend it needs a Context for some reason
return s.length
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import java.lang.reflect.Proxy
inline fun <reified T> fake(): T =
Proxy.newProxyInstance(
T::class.java.classLoader,
arrayOf(T::class.java)
) { _, _, _ ->
TODO("not implemented")
} as T
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
class EitherTests {
@Test
fun `infers types`() {
fun toInt(p: Either<String, Int>): Int = p.fold(
{ s -> s.toInt() },
import java.math.BigDecimal
fun main() {
val s: StringValue = StringValue.of("hello")
StringValue("") // ok as no validation
val nes: NonEmptyStringValue? = NonEmptyStringValue.of("hello") // nullable as validated
// private as validated, would throw if not private
// NonEmptyStringValue("")
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
class StringWrapperTests {
private val firstName = FirstName("Fred")
private val lastName = LastName("Flintstone")
@Test fun canAssignSameType() {
val doesCompile: FirstName = firstName
@dmcg
dmcg / problematic.kt
Created March 25, 2020 12:03
Combining Hamkrest and Result4K
import com.natpryce.Failure
import com.natpryce.Result
import com.natpryce.Success
import com.natpryce.hamkrest.MatchResult
import com.natpryce.hamkrest.Matcher
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
fun <T, E> assertSuccessThat(actual: Result<T, E>, valueMatcher: Matcher<T>) {
@dmcg
dmcg / gist:74628586fa79b3a3350f
Last active June 21, 2019 22:19
Nasty hack Hamcrest Stream matcher
public class StreamMatchers {
public static class StreamContainingInAnyOrderMatcher<T> extends DelegatingStreamMatcher<T> {
public StreamContainingInAnyOrderMatcher(T... items) {
super(org.hamcrest.Matchers.arrayContainingInAnyOrder(items), "Stream of ");
}
}
public static class EmptyStreamMatcher<T> extends DelegatingStreamMatcher<T> {
public EmptyStreamMatcher() {
super(Matchers.emptyArray(), "Stream like ");