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 8, 2024
1 parent e01245c commit fb4dfc2
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
3 changes: 2 additions & 1 deletion parameterize-api/api/parameterize-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public abstract interface class com/benwoodworth/parameterize/ParameterizeScope

public final class com/benwoodworth/parameterize/ParameterizeScope$DeclaredParameter {
public final field argument Ljava/lang/Object;
public fun <init> (Ljava/lang/Object;)V
public fun <init> (Lkotlin/reflect/KProperty;Ljava/lang/Object;)V
public final fun getProperty ()Lkotlin/reflect/KProperty;
public final fun getValue (Ljava/lang/Object;Lkotlin/reflect/KProperty;)Ljava/lang/Object;
public fun toString ()Ljava/lang/String;
}
Expand Down
4 changes: 3 additions & 1 deletion parameterize-api/api/parameterize-api.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ abstract interface com.benwoodworth.parameterize/ParameterizeScope { // com.benw
final class <#A1: kotlin/Any?> ParameterDelegate // com.benwoodworth.parameterize/ParameterizeScope.ParameterDelegate|null[0]

final class <#A1: out kotlin/Any?> DeclaredParameter { // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter|null[0]
constructor <init>(#A1) // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter.<init>|<init>(1:0){}[0]
constructor <init>(kotlin.reflect/KProperty<*>, #A1) // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter.<init>|<init>(kotlin.reflect.KProperty<*>;1:0){}[0]

final val argument // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter.argument|{}argument[0]
final fun <get-argument>(): #A1 // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter.argument.<get-argument>|<get-argument>(){}[0]
final val property // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter.property|{}property[0]
final fun <get-property>(): kotlin.reflect/KProperty<*> // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter.property.<get-property>|<get-property>(){}[0]

final fun toString(): kotlin/String // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter.toString|toString(){}[0]
final inline fun getValue(kotlin/Any?, kotlin.reflect/KProperty<*>): #A1 // com.benwoodworth.parameterize/ParameterizeScope.DeclaredParameter.getValue|getValue(kotlin.Any?;kotlin.reflect.KProperty<*>){}[0]
Expand Down
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 @@ -111,6 +111,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
12 changes: 7 additions & 5 deletions parameterize-api/src/commonTest/kotlin/ParameterizeScopeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ParameterizeScopeSpec : ParameterizeScope {
override fun <T> Parameter<T>.provideDelegate(thisRef: Any?, property: KProperty<*>): DeclaredParameter<T> =
throw UnsupportedOperationException("Not Implemented")

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 @@ -136,17 +138,17 @@ class ParameterizeScopeSpec : ParameterizeScope {

@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 All @@ -162,7 +164,7 @@ class ParameterizeScopeSpec : ParameterizeScope {
lateinit var declaredParameter: DeclaredParameter<Any>

val parameter by PropertyDelegateProvider { thisRef: Nothing?, property ->
DeclaredParameter("argument")
DeclaredParameter(property, "argument")
.also { declaredParameter = it }
}

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

0 comments on commit fb4dfc2

Please sign in to comment.