Skip to content

Commit

Permalink
Move ParameterDelegate to ParameterizeScope.ParameterDelegate
Browse files Browse the repository at this point in the history
The `ParameterDelegate` class is only used for the `parameterize` DSL, so nesting it in `ParameterizeScope` better indicates its purpose, and removes a likely common class name from the global namespace.

Originally kept in a separate file since it was filled with complicated logic, so kept the old logic in place renaming it to `ParameterState`, and have the new `ParameterDelegate` be composed of it.
  • Loading branch information
BenWoodworth committed Nov 5, 2023
1 parent d8f47c0 commit 4875ae5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 39 deletions.
9 changes: 6 additions & 3 deletions api/parameterize.api
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public final class com/benwoodworth/parameterize/ParameterDelegate {
public final class com/benwoodworth/parameterize/ParameterState {
public fun toString ()Ljava/lang/String;
}

Expand Down Expand Up @@ -76,9 +76,9 @@ public final class com/benwoodworth/parameterize/ParameterizeKt {
}

public final class com/benwoodworth/parameterize/ParameterizeScope {
public final fun getValue (Lcom/benwoodworth/parameterize/ParameterDelegate;Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
public final fun getValue (Lcom/benwoodworth/parameterize/ParameterizeScope$ParameterDelegate;Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
public final fun parameter-xl7Lxic (Ljava/lang/Iterable;)Ljava/lang/Iterable;
public final fun provideDelegate-13mUnGw (Ljava/lang/Iterable;Ljava/lang/Object;Lkotlin/reflect/KProperty;)Lcom/benwoodworth/parameterize/ParameterDelegate;
public final fun provideDelegate-13mUnGw (Ljava/lang/Iterable;Ljava/lang/Object;Lkotlin/reflect/KProperty;)Lcom/benwoodworth/parameterize/ParameterizeScope$ParameterDelegate;
public fun toString ()Ljava/lang/String;
}

Expand All @@ -94,3 +94,6 @@ public final class com/benwoodworth/parameterize/ParameterizeScope$Parameter {
public final synthetic fun unbox-impl ()Ljava/lang/Iterable;
}

public final class com/benwoodworth/parameterize/ParameterizeScope$ParameterDelegate {
}

Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import kotlin.reflect.KProperty
* through the current iterator.
*/

/** @suppress */
public class ParameterDelegate<@Suppress("unused") out T> internal constructor() {
internal class ParameterState<@Suppress("unused") out T> internal constructor() {
/*
* The internal state does not use the generic type, so T is purely for
* syntax, helping Kotlin infer a property type from the parameter through
Expand Down
8 changes: 8 additions & 0 deletions src/commonMain/kotlin/Parameterize.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ public class ParameterizeScope internal constructor(
public value class Parameter<out T> internal constructor(
internal val arguments: Iterable<T>
)

/** @suppress */
public class ParameterDelegate<out T> internal constructor(
internal val parameterState: ParameterState<T>
) {
override fun toString(): String =
parameterState.toString()
}
}

/**
Expand Down
25 changes: 13 additions & 12 deletions src/commonMain/kotlin/ParameterizeState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.benwoodworth.parameterize

import com.benwoodworth.parameterize.ParameterizeConfiguration.OnCompleteScope
import com.benwoodworth.parameterize.ParameterizeConfiguration.OnFailureScope
import com.benwoodworth.parameterize.ParameterizeScope.ParameterDelegate
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.reflect.KProperty
Expand Down Expand Up @@ -49,11 +50,11 @@ internal class ParameterizeState {
val parameter = if (parameterIndex in parameters.indices) {
parameters[parameterIndex]
} else {
ParameterDelegate<Nothing>()
ParameterDelegate<Nothing>(ParameterState())
.also { parameters += it }
}

parameter.declare(property, arguments)
parameter.parameterState.declare(property, arguments)

return parameter
}
Expand All @@ -70,11 +71,11 @@ internal class ParameterizeState {
}

fun <T> getParameterArgument(parameter: ParameterDelegate<*>, property: KProperty<T>): T {
val isFirstUse = !parameter.hasBeenUsed
val isFirstUse = !parameter.parameterState.hasBeenUsed

return property
.trackNestedUsage {
parameter.getArgument(property)
parameter.parameterState.getArgument(property)
}
.also {
if (isFirstUse) trackUsedParameter(parameter)
Expand All @@ -84,7 +85,7 @@ internal class ParameterizeState {
private fun trackUsedParameter(parameter: ParameterDelegate<*>) {
parametersUsed += parameter

if (!parameter.isLastArgument) {
if (!parameter.parameterState.isLastArgument) {
parameterCountAfterAllUsed = parameterCount
}
}
Expand All @@ -106,21 +107,21 @@ internal class ParameterizeState {
while (usedParameterIterator.hasPrevious()) {
val parameter = usedParameterIterator.previous()

if (!parameter.isLastArgument) {
parameter.nextArgument()
if (!parameter.parameterState.isLastArgument) {
parameter.parameterState.nextArgument()
iterated = true
break
}

usedParameterIterator.remove()
parameter.reset()
parameter.parameterState.reset()
}

for (i in parameterCountAfterAllUsed..<parameterCount) {
val parameter = parameters[i]

if (!parameter.hasBeenUsed) {
parameter.reset()
if (!parameter.parameterState.hasBeenUsed) {
parameter.parameterState.reset()
}
}

Expand All @@ -135,8 +136,8 @@ internal class ParameterizeState {
*/
fun getFailureArguments(): List<ParameterizeFailure.Argument<*>> =
parameters.take(parameterCount)
.filter { it.hasBeenUsed }
.mapNotNull { it.getFailureArgumentOrNull() }
.filter { it.parameterState.hasBeenUsed }
.mapNotNull { it.parameterState.getFailureArgumentOrNull() }

fun handleFailure(onFailure: OnFailureScope.(Throwable) -> Unit, failure: Throwable) {
failureCount++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package com.benwoodworth.parameterize

import kotlin.test.*

class ParameterDelegateSpec {
class ParameterStateSpec {
private val property: String get() = error("${::property.name} is not meant to be used")
private val differentProperty: String get() = error("${::differentProperty.name} is not meant to be used")

private lateinit var parameter: ParameterDelegate<String>
private lateinit var parameter: ParameterState<String>

@BeforeTest
fun beforeTest() {
parameter = ParameterDelegate()
parameter = ParameterState()
}


Expand Down Expand Up @@ -228,25 +228,6 @@ class ParameterDelegateSpec {
assertFalse(parameter.hasBeenUsed)
}

@Test
fun to_string_when_not_initialized_should_match_message_from_lazy() {
parameter.declare(::property, emptyList())

val expectedToString = lazy { "unused" }
.toString()
.replace("Lazy value", "Parameter argument")

assertEquals(expectedToString, parameter.toString())
}

@Test
fun to_string_when_initialized_should_equal_that_of_the_current_argument() {
parameter.declare(::property, listOf("a"))

val argument = parameter.getArgument(::property)
assertEquals(argument, parameter.toString())
}

@Test
fun is_last_argument_before_initialized_should_throw() {
val failure1 = assertFailsWith<IllegalStateException> {
Expand Down
1 change: 1 addition & 0 deletions src/commonTest/kotlin/ParameterizeExceptionSpec.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.benwoodworth.parameterize

import com.benwoodworth.parameterize.ParameterizeScope.ParameterDelegate
import kotlin.properties.PropertyDelegateProvider
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down
35 changes: 35 additions & 0 deletions src/commonTest/kotlin/ParameterizeScopeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

package com.benwoodworth.parameterize

import com.benwoodworth.parameterize.ParameterizeScope.ParameterDelegate
import kotlin.properties.PropertyDelegateProvider
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertSame

class ParameterizeScopeSpec {
@Test
Expand All @@ -21,4 +24,36 @@ class ParameterizeScopeSpec {

assertEquals("${ParameterizeScope::class.simpleName}(a = $a, b = $b, c = $c)", this.toString())
}

@Test
fun parameter_delegate_string_representation_when_not_initialized_should_match_message_from_lazy() = parameterize {
lateinit var delegate: ParameterDelegate<Nothing>

val parameter by PropertyDelegateProvider { thisRef: Nothing?, property ->
parameterOf<Nothing>()
.provideDelegate(thisRef, property)
.also { delegate = it } // intercept delegate
}

val messageFromLazy = lazy { "unused" }
.toString()
.replace("Lazy value", "Parameter argument")

assertEquals(messageFromLazy, delegate.toString())
}

@Test
fun parameter_delegate_string_representation_when_initialized_should_equal_that_of_the_current_argument() = parameterize {
lateinit var delegate: ParameterDelegate<String>

val parameter by PropertyDelegateProvider { thisRef: Nothing?, property ->
parameterOf("argument")
.provideDelegate(thisRef, property)
.also { delegate = it } // intercept delegate
}

useParameter(parameter)

assertSame(parameter, delegate.toString())
}
}

0 comments on commit 4875ae5

Please sign in to comment.