This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PartitionTests3 : JUnit5Minutests { | |
data class Fixture( | |
val items: List<Int> = emptyList(), | |
val predicates: List<(Int) -> Boolean> = emptyList() | |
) { | |
fun assertResultIs(vararg expected: List<Int>) { | |
assertEquals(expected.asList(), items.partition(predicates)) | |
} | |
fun withItems(vararg items: Int) = copy(items = items.asList()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PartitionTests2 : JUnit5Minutests { | |
override val tests = rootContext<Unit> { | |
context("no items") { | |
context("no predicates") { | |
test("returns empty list") { | |
val list = emptyList<Int>() | |
val predicates = emptyList<(Int) -> Boolean>() | |
val result = list.partition(predicates) | |
assertEquals(emptyList<List<List<Int>>>(), result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PartitionTests : JUnit5Minutests { | |
override val tests = rootContext<Unit> { | |
test("empty input and predicates") { | |
val list = emptyList<Int>() | |
val predicates = emptyList<(Int) -> Boolean>() | |
val result = list.partition(predicates) | |
assertEquals(emptyList<List<List<Int>>>(), result) | |
} | |
test("empty input some predicates") { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun userOverrideProperties(file: File): ConfigurationProperties = | |
if (file.isFile) | |
ConfigurationProperties.fromFile(file) | |
else | |
ConfigurationProperties(Properties(), Location(file.absolutePath, file.toURI())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Suppress("UNCHECKED_CAST") | |
fun <T: Any> T.dataCopy(mapper: (Any?) -> Any?): T { | |
val memberFunctions = this::class.memberFunctions | |
val components = memberFunctions.filter { it.name.startsWith("component") }.sortedBy { it.name.substringAfter("component").toInt() } | |
val componentValues = components.map { it.call(this) }.toTypedArray() | |
val copyMethod: KFunction<*> = memberFunctions.first { it.name == "copy" } | |
val copyArgs: List<Any?> = listOf(this) + componentValues.map(mapper) | |
return copyMethod.call(*copyArgs.toTypedArray()) as T | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A [TestRule] as opposed to [MethodRule], without the auto-population gubbins. | |
*/ | |
class JUnitTestRuleMockery : JUnit4Mockery(), TestRule { | |
override fun apply(base: Statement, description: Description) = object : Statement() { | |
override fun evaluate() { | |
base.evaluate() | |
assertIsSatisfied() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jmock.integration.junit4.JUnit4Mockery | |
import org.junit.jupiter.api.extension.AfterEachCallback | |
import org.junit.jupiter.api.extension.ExtensionContext | |
class MockeryExtension : JUnit4Mockery(), AfterEachCallback { | |
override fun afterEach(context: ExtensionContext) { | |
assertIsSatisfied() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private class KotlinValidationConstraintResolver : AnnotationConstraintResolver() { | |
override fun getRequired(prop: BeanProperty): Boolean? = super.getRequired(prop) ?: isNullable(prop) == false | |
} | |
private fun isNullable(prop: BeanProperty): Boolean? = when (prop) { | |
is BeanPropertyWriter -> prop.isNullable() | |
else -> null | |
} | |
private fun BeanPropertyWriter.isNullable(): Boolean? { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun article(licenceCode: String) = elem("article") { | |
elem("front") { | |
elem("journal-meta") { | |
elem("journal-id", "journal-id-type" to "publisher") { | |
text(licenceCode) | |
} | |
} | |
} | |
elem("body") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MockingTesting { | |
@Rule @JvmField val mockery = JUnitRuleMockery() | |
val charSequence = mockery.createMock<CharSequence>() | |
@Test fun `which will taking untyped Action`() { | |
mockery.expecting { | |
oneOf(charSequence).length.which will org.jmock.Expectations.returnValue(42) | |
oneOf(charSequence).length.which will org.jmock.Expectations.throwException(RuntimeException()) | |
oneOf(charSequence).length.which will org.jmock.Expectations.returnValue("banana") |