Skip to content

Commit

Permalink
WIP Extract configuration logic
Browse files Browse the repository at this point in the history
TODO
- Readme module.
- iterationFailures unused?
- remove check failure in next commit?
- check if PException was caught on next iteration
- configured shouldn't catch PException
- vanilla `parameterize` docs, and other configuration docs
- merge iterator -> state (vanilla & configured)

Improves performance of unconfigured parameterize, and greatly simplifies core logic
configuration may be dropped later
  • Loading branch information
BenWoodworth committed Oct 22, 2024
1 parent 3b3451d commit 4626c54
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ internal class ConfiguredParameterizeState(
private var currentIterationScope: ConfiguredParameterizeScope? = null // Non-null if afterEach still needs to be called
private var decoratorCoroutine: DecoratorCoroutine? = null

var isFirstIteration: Boolean = true
private set
val isFirstIteration: Boolean
get() = iterationCount == 1L

/**
* Signals the start of a new [parameterize] iteration, and returns its scope if there is one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.benwoodworth.parameterize

import com.benwoodworth.parameterize.ParameterizeConfiguration.*
import com.benwoodworth.parameterize.test.parameterizeBlockExitTestCases
import com.benwoodworth.parameterize.test.configuredParameterizeExitingBlockEdgeCases
import com.benwoodworth.parameterize.test.testAll
import kotlin.coroutines.RestrictsSuspension
import kotlin.coroutines.intrinsics.COROUTINE_SUSPENDED
Expand Down Expand Up @@ -130,8 +130,8 @@ class ParameterizeConfigurationSpec_decorator {

@Test
fun iteration_function_should_return_regardless_of_how_parameterize_block_is_exited() = testAll(
parameterizeBlockExitTestCases
) { parameterizeWithBlockExit ->
configuredParameterizeExitingBlockEdgeCases
) { parameterizeExitingBlock ->
var returned = false

val configuration = ParameterizeConfiguration {
Expand All @@ -141,7 +141,7 @@ class ParameterizeConfigurationSpec_decorator {
}
}

parameterizeWithBlockExit(configuration)
parameterizeExitingBlock(configuration)

assertTrue(returned, "returned")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.benwoodworth.parameterize.*
/**
* Configured [parameterize] calls with blocks that exit different ways.
*/
val parameterizeBlockExitTestCases = listOf(
val configuredParameterizeExitingBlockEdgeCases = listOf(
"Return normally" to { configuration: ParameterizeConfiguration ->
parameterize(configuration) {}
},
Expand Down
46 changes: 19 additions & 27 deletions parameterize-core/src/commonTest/kotlin/ParameterizeBreakSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package com.benwoodworth.parameterize

import com.benwoodworth.parameterize.test.EdgeCases
import com.benwoodworth.parameterize.test.parameterizeState
import com.benwoodworth.parameterize.test.parameterizeExitingBlockEdgeCases
import com.benwoodworth.parameterize.test.probeThrow
import com.benwoodworth.parameterize.test.testAll
import kotlin.test.*
Expand Down Expand Up @@ -241,34 +240,27 @@ class ParameterizeBreakSpec {
)
}

@Test
fun declaring_parameter_after_iteration_completed() { // TODO Not break-related
val iterationCompletionsByThrowing = EdgeCases.iterationFailures.map { (name, getFailure) ->
"Complete by throwing $name" to { state: ParameterizeState -> throw getFailure(state) }
}

testAll<(ParameterizeState) -> Unit>(
"Complete without failing" to {},
*iterationCompletionsByThrowing.toTypedArray()
) { completeIteration ->
var declareParameter = {}

kotlin.runCatching {
parameterize {
declareParameter = {
val parameter by parameterOf(Unit)
}

completeIteration(parameterizeState)
}
}

val failure = assertFailsWith<ParameterizeException> { // TODO
declareParameter()
/**
* Not [ParameterizeBreak] since this is potentially happening outside the iteration in user code.
* This also doesn't put [parameterize] into a bad state, so there's no need to break out of the loop.
*/
@Test // TODO Not break-related
fun declaring_parameter_after_iteration_completed_should_throw_ParameterizeException() = testAll(
parameterizeExitingBlockEdgeCases
) { parameterizeExitingBlock ->
var declareParameter = {}

parameterizeExitingBlock {
declareParameter = {
val parameter by parameterOf(Unit)
}
}

assertEquals("Cannot declare parameter `parameter` after its iteration has completed", failure.message)
val failure = assertFailsWith<ParameterizeException> { // TODO
declareParameter()
}

assertEquals("Cannot declare parameter `parameter` after its iteration has completed", failure.message)
}

@Test
Expand Down
53 changes: 0 additions & 53 deletions parameterize-core/src/commonTest/kotlin/test/EdgeCases.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2024 Ben Woodworth
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.benwoodworth.parameterize.test

import com.benwoodworth.parameterize.*

/**
* [parameterize] calls with blocks that exit different ways.
*/
val parameterizeExitingBlockEdgeCases = listOf(
"Return normally" to { block: ParameterizeScope.() -> Unit ->
parameterize {
block()
}
},
"Non-local return" to { block ->
run {
parameterize {
block()
return@run
}
}
},
"Throw" to { block ->
class ExitingThrowable : Throwable()

try {
parameterize {
block()
throw ExitingThrowable()
}
} catch (_: ExitingThrowable) {
}
},
"Continue" to { block ->
parameterize {
block()
val skip by parameterOf<Unit>()
}
},
"Break" to { block ->
try {
parameterize {
block()
val illegalNesting by parameter {
with (this@parameterize) {
val inner by parameterOf(Unit)
}
listOf<Unit>()
}
}
} catch (_: ParameterizeException) {
}
}
)

0 comments on commit 4626c54

Please sign in to comment.