Skip to content

Commit

Permalink
Type tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
dmcg committed Nov 24, 2018
1 parent 10d7d89 commit 4305503
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.oneeyedmen.minutest.internal

import com.oneeyedmen.minutest.Context
import com.oneeyedmen.minutest.RuntimeContext
import com.oneeyedmen.minutest.TestDescriptor
import com.oneeyedmen.minutest.TestTransform
import kotlin.reflect.KType
Expand Down Expand Up @@ -56,30 +57,25 @@ internal class ContextBuilder<PF, F>(
transforms.add(transform)
}

override fun buildNode(parent: ParentContext<PF>): PreparedRuntimeContext<PF, F> {
val fixtureFactory = resolvedFixtureFactory()
return PreparedRuntimeContext(name,
parent,
emptyList(),
befores,
afters,
transforms,
fixtureFactory).let { context ->
// nastiness to set up parent child in immutable nodes
context.copy(children = this.children.map { child -> child.buildNode(context) })
}
override fun buildNode(parent: ParentContext<PF>): RuntimeContext = PreparedRuntimeContext(name,
parent,
emptyList(),
befores,
afters,
transforms,
resolvedFixtureFactory()).let { context ->
// nastiness to set up parent child in immutable nodes
context.copy(children = this.children.map { child -> child.buildNode(context) })
}

@Suppress("UNCHECKED_CAST")
private fun resolvedFixtureFactory(): (PF, TestDescriptor) -> F {
return when {
fixtureFactory != null -> fixtureFactory
thisContextDoesntNeedAFixture() -> { _, _ -> Unit as F }
// this is safe provided there are only fixture not replaceFixture calls in sub-contexts,
// as we cannot provide a fixture here to act as receiver. TODO - check somehow
else -> error("Fixture has not been set in context \"$name\"")
}!!
}
private fun resolvedFixtureFactory(): (PF, TestDescriptor) -> F = when {
fixtureFactory != null -> fixtureFactory
thisContextDoesntNeedAFixture() -> { _, _ -> Unit as F }
// this is safe provided there are only fixture not replaceFixture calls in sub-contexts,
// as we cannot provide a fixture here to act as receiver. TODO - check somehow
else -> error("Fixture has not been set in context \"$name\"")
}!!

private fun thisContextDoesntNeedAFixture() =
befores.isEmpty() && afters.isEmpty() && children.filterIsInstance<TestBuilder<F>>().isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.oneeyedmen.minutest.internal

import com.oneeyedmen.minutest.RuntimeNode

interface NodeBuilder<F> {
internal interface NodeBuilder<F> {
fun buildNode(parent: ParentContext<F>): RuntimeNode
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.oneeyedmen.minutest.internal

import com.oneeyedmen.minutest.RuntimeTest

internal data class TestBuilder<F>(val name: String, val f: F.() -> F) : NodeBuilder<F> {
override fun buildNode(parent: ParentContext<F>) = PreparedRuntimeTest(
name,
parent,
f)

override fun buildNode(parent: ParentContext<F>): RuntimeTest =
PreparedRuntimeTest(name, parent, f)

}

0 comments on commit 4305503

Please sign in to comment.