Skip to content

Commit

Permalink
Include expose Kotlin property in DeclaredParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWoodworth committed Oct 5, 2024
1 parent a5d5544 commit 4b78590
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
7 changes: 7 additions & 0 deletions parameterize-api/src/commonMain/kotlin/ParameterizeScope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ public interface ParameterizeScope {
/** @suppress */
@ParameterizeApiFriendModuleApi
constructor(
/**
* The Kotlin property that this parameter was [declared][provideDelegate] for.
*
* @see Parameter
*/
public val property: KProperty<*>,

/**
* The [argument] that this parameter was [declared][provideDelegate] with.
*
Expand Down
29 changes: 11 additions & 18 deletions parameterize-core/src/commonMain/kotlin/ParameterState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,11 @@ internal class ParameterState(
private val parameterizeState: ParameterizeState
) {
private var declaredParameter: DeclaredParameter<*>? = null
private var property: KProperty<*>? = null
private var arguments: Sequence<*>? = null
private var argumentIterator: Iterator<*>? = null

internal fun reset() {
declaredParameter = null
property = null
arguments = null
argumentIterator = null
}
Expand Down Expand Up @@ -93,7 +91,7 @@ internal class ParameterState(
*/
fun <T> declare(property: KProperty<*>, arguments: Sequence<T>) {
// Nothing to do if already declared (besides validating the property)
this.property?.let { declaredProperty ->
this.declaredParameter?.property?.let { declaredProperty ->
parameterizeState.checkState(property.equalsProperty(declaredProperty)) {
"Expected to be declaring `${declaredProperty.name}`, but got `${property.name}`"
}
Expand All @@ -105,8 +103,7 @@ internal class ParameterState(
throw ParameterizeContinue // Before changing any state
}

this.declaredParameter = DeclaredParameter(iterator.next())
this.property = property
this.declaredParameter = DeclaredParameter(property, iterator.next())
this.arguments = arguments
this.argumentIterator = iterator.takeIf { it.hasNext() }
}
Expand All @@ -122,12 +119,8 @@ internal class ParameterState(
"Cannot get declared parameter before it's been declared"
}

val declaredProperty = checkNotNull(this.property) {
"The property is null even though the parameter has been declared"
}

parameterizeState.checkState(property.equalsProperty(declaredProperty)) {
"Cannot use parameter with `${property.name}`, since it was declared with `${declaredProperty.name}`."
parameterizeState.checkState(property.equalsProperty(declaredParameter.property)) {
"Cannot use parameter with `${property.name}`, since it was declared for `${declaredParameter.property.name}`."
}

return declaredParameter
Expand All @@ -139,13 +132,17 @@ internal class ParameterState(
* @throws IllegalStateException if the argument has not been declared yet.
*/
fun nextArgument() {
val arguments = checkNotNull(arguments) {
val declaredParameter = checkNotNull(this.declaredParameter) {
"Cannot iterate arguments before parameter has been declared"
}

val arguments = checkNotNull(arguments) {
"Expected arguments to be non-null since parameter has been declared"
}

val iterator = argumentIterator ?: arguments.iterator()

declaredParameter = DeclaredParameter(iterator.next())
this.declaredParameter = DeclaredParameter(declaredParameter.property, iterator.next())
argumentIterator = iterator.takeIf { it.hasNext() }
}

Expand All @@ -159,10 +156,6 @@ internal class ParameterState(
"Cannot get failure argument before parameter has been declared"
}

val declaredProperty = checkNotNull(this.property) {
"The property is null even though the parameter has been declared"
}

return ParameterizeFailure.Argument(declaredProperty, declaredParameter.argument)
return ParameterizeFailure.Argument(declaredParameter.property, declaredParameter.argument)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class ParameterStateSpec {
}

assertEquals(
"Cannot use parameter with `differentProperty`, since it was declared with `property`.",
"Cannot use parameter with `differentProperty`, since it was declared for `property`.",
exception.message
)
}
Expand Down
11 changes: 7 additions & 4 deletions parameterize-core/src/commonTest/kotlin/ParameterizeScopeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ import com.benwoodworth.parameterize.ParameterizeScope.DeclaredParameter
import com.benwoodworth.parameterize.ParameterizeScope.Parameter
import com.benwoodworth.parameterize.ParameterizeScopeSpec.LazyParameterFunction.LazyArguments
import kotlin.properties.PropertyDelegateProvider
import kotlin.reflect.KProperty
import kotlin.test.*

/**
* Specifies the [parameterize] DSL and its syntax.
*/
class ParameterizeScopeSpec {
private val property: KProperty<*> get() = ::property

/**
* A unique iterator that the tests can use to verify that a constructed [Parameter] has the correct
* [arguments][Parameter.arguments].
Expand Down Expand Up @@ -135,17 +138,17 @@ class ParameterizeScopeSpec {

@Test
fun declared_parameter_equals_should_compare_by_reference_equality() {
val declaredParameter = DeclaredParameter(Unit)
val structurallyEqualDeclaredParameter = DeclaredParameter(Unit)
val declaredParameter = DeclaredParameter(property, Unit)
val structurallyEqualDeclaredParameter = DeclaredParameter(property, Unit)

assertEquals(declaredParameter, declaredParameter, "Should equal the same instance")
assertNotEquals(declaredParameter, structurallyEqualDeclaredParameter, "Should not equal a different instance")
}

@Test
fun declared_parameter_hash_code_should_be_evaluated_by_reference() {
val declaredParameter = DeclaredParameter(Unit)
val structurallyEqualDeclaredParameters = generateSequence { DeclaredParameter(Unit) }
val declaredParameter = DeclaredParameter(property, Unit)
val structurallyEqualDeclaredParameters = generateSequence { DeclaredParameter(property, Unit) }

assertEquals(declaredParameter.hashCode(), declaredParameter.hashCode(), "Should equal hash code of the same instance")

Expand Down

0 comments on commit 4b78590

Please sign in to comment.