Skip to content

Commit

Permalink
Fix TestWithConfigBuilder#config using parsed name when clear TestDsl…
Browse files Browse the repository at this point in the history
…State (#3798)

<!-- 
If this PR updates documentation, please update all relevant versions of
the docs, see:
https://github.com/kotest/kotest/tree/master/documentation/versioned_docs
The documentation at
https://github.com/kotest/kotest/tree/master/documentation/docs is the
documentation for the next minor or major version _TO BE RELEASED_
-->

resolve #3795

- Refactor `startTest` and `clear` functions of the `TestDslState` class
from taking a `TestDescriptor` class as a parameter to taking `String`
class.
- Fix TestWithConfigBuilder#config using parsed name when clear
TestDslState
- The name field of the `TestWithConfigBuilder` class is a `TestName`
class, which parses and stores the user's input during initialization.
This caused a bug.

---------

Co-authored-by: Sam <sam@sksamuel.com>
  • Loading branch information
kshired and sksamuel committed Mar 11, 2024
1 parent 9ae2a62 commit 45d17f8
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1802,19 +1802,21 @@ public final class io/kotest/core/names/DuplicateTestNameMode : java/lang/Enum {

public final class io/kotest/core/names/TestName {
public static final field Companion Lio/kotest/core/names/TestName$Companion;
public fun <init> (Ljava/lang/String;ZZLjava/lang/String;Ljava/lang/String;Z)V
public fun <init> (Ljava/lang/String;ZZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;)V
public final fun component1 ()Ljava/lang/String;
public final fun component2 ()Z
public final fun component3 ()Z
public final fun component4 ()Ljava/lang/String;
public final fun component5 ()Ljava/lang/String;
public final fun component6 ()Z
public final fun copy (Ljava/lang/String;ZZLjava/lang/String;Ljava/lang/String;Z)Lio/kotest/core/names/TestName;
public static synthetic fun copy$default (Lio/kotest/core/names/TestName;Ljava/lang/String;ZZLjava/lang/String;Ljava/lang/String;ZILjava/lang/Object;)Lio/kotest/core/names/TestName;
public final fun component7 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;ZZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;)Lio/kotest/core/names/TestName;
public static synthetic fun copy$default (Lio/kotest/core/names/TestName;Ljava/lang/String;ZZLjava/lang/String;Ljava/lang/String;ZLjava/lang/String;ILjava/lang/Object;)Lio/kotest/core/names/TestName;
public fun equals (Ljava/lang/Object;)Z
public final fun getBang ()Z
public final fun getDefaultAffixes ()Z
public final fun getFocus ()Z
public final fun getOriginalName ()Ljava/lang/String;
public final fun getPrefix ()Ljava/lang/String;
public final fun getSuffix ()Ljava/lang/String;
public final fun getTestName ()Ljava/lang/String;
Expand Down Expand Up @@ -2815,9 +2817,9 @@ public abstract class io/kotest/core/spec/style/scopes/TerminalScope : io/kotest
public final class io/kotest/core/spec/style/scopes/TestDslState {
public static final field INSTANCE Lio/kotest/core/spec/style/scopes/TestDslState;
public final fun checkState ()V
public final fun clear (Lio/kotest/core/descriptors/Descriptor$TestDescriptor;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun clear (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun reset (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun startTest (Lio/kotest/core/descriptors/Descriptor$TestDescriptor;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun startTest (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class io/kotest/core/spec/style/scopes/TestWithConfigBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package io.kotest.core.names
* @param prefix if the test style includes a test name prefix, such as "should"
* @param suffix if the test style includes a test name suffix, such as "when"
* @param defaultAffixes if the test style recommends test affixes by default, such as [BehaviorSpec][io.kotest.core.spec.style.BehaviorSpec]
* @param originalName the name exactly as the user entered it
*/
data class TestName(
val testName: String,
Expand All @@ -23,6 +24,7 @@ data class TestName(
val prefix: String?,
val suffix: String?,
val defaultAffixes: Boolean,
val originalName: String
) {

companion object {
Expand All @@ -46,7 +48,7 @@ data class TestName(
else -> Triple(first = false, second = false, third = trimmed)
}

return TestName(parsedName, focus, bang, prefix, suffix, defaultAffixes)
return TestName(parsedName, focus, bang, prefix, suffix, defaultAffixes, name)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.kotest.core.spec.style.scopes

import io.kotest.common.ExperimentalKotest
import io.kotest.core.descriptors.append
import io.kotest.core.names.TestName
import io.kotest.core.spec.KotestTestScope
import io.kotest.core.test.TestScope
Expand Down Expand Up @@ -92,7 +91,7 @@ class DescribeSpecContainerScope(
) { DescribeSpecContainerScope(it) }

suspend fun it(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(
TestName("It: ", name, false),
this,
Expand All @@ -101,7 +100,7 @@ class DescribeSpecContainerScope(
}

suspend fun xit(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(
TestName("It: ", name, false),
this,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.kotest.core.spec.style.scopes

import io.kotest.core.descriptors.append
import io.kotest.core.names.TestName
import io.kotest.core.spec.KotestTestScope
import io.kotest.core.test.TestScope
Expand Down Expand Up @@ -50,7 +49,7 @@ class ExpectSpecContainerScope(
}

suspend fun expect(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(
name = TestName("Expect: ", name, false),
context = this,
Expand All @@ -59,7 +58,7 @@ class ExpectSpecContainerScope(
}

suspend fun xexpect(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(
name = TestName("Expect: ", name, false),
context = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FeatureSpecContainerScope(val testScope: TestScope) : AbstractContainerSco
}

suspend fun scenario(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(
name = TestName("Scenario: ", name, false),
context = this,
Expand All @@ -66,7 +66,7 @@ class FeatureSpecContainerScope(val testScope: TestScope) : AbstractContainerSco
}

suspend fun xscenario(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(
name = TestName("Scenario: ", name, false),
context = this,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.kotest.core.spec.style.scopes

import io.kotest.common.ExperimentalKotest
import io.kotest.core.descriptors.append
import io.kotest.core.names.TestName
import io.kotest.core.spec.KotestTestScope
import io.kotest.core.test.TestScope
Expand Down Expand Up @@ -68,7 +67,7 @@ class FunSpecContainerScope(
* Adds a test case to this context, expecting config.
*/
suspend fun test(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(
name = TestName(name),
context = this,
Expand All @@ -80,7 +79,7 @@ class FunSpecContainerScope(
* Adds a disabled test case to this context, expecting config.
*/
suspend fun xtest(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(
name = TestName(name),
context = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ class ShouldSpecContainerScope(
}

suspend fun should(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(TestName("should ", name, false), this, false)
}

suspend fun xshould(name: String): TestWithConfigBuilder {
TestDslState.startTest(testScope.testCase.descriptor.append(name))
TestDslState.startTest(name)
return TestWithConfigBuilder(TestName("should ", name, false), this, true)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.kotest.core.spec.style.scopes

import io.kotest.core.Tag
import io.kotest.core.descriptors.append
import io.kotest.core.extensions.TestCaseExtension
import io.kotest.core.names.TestName
import io.kotest.core.test.EnabledIf
Expand Down Expand Up @@ -37,6 +36,7 @@ class TestWithConfigBuilder(
coroutineTestScope: Boolean? = null,
test: suspend TestScope.() -> Unit
) {
TestDslState.clear(name.originalName)
val config = TestConfig(
enabled = enabled,
enabledIf = enabledIf,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.kotest.core.spec.style.scopes

import io.kotest.core.descriptors.Descriptor
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit

Expand All @@ -9,12 +8,12 @@ object TestDslState {
private val started = mutableSetOf<String>()
private val mutex = Semaphore(1)

suspend fun startTest(name: Descriptor.TestDescriptor) = mutex.withPermit {
started.add(name.path().value)
suspend fun startTest(name: String) = mutex.withPermit {
started.add(name)
}

suspend fun clear(name: Descriptor.TestDescriptor) = mutex.withPermit {
started.remove(name.path().value)
suspend fun clear(name: String) = mutex.withPermit {
started.remove(name)
}

/**
Expand Down

0 comments on commit 45d17f8

Please sign in to comment.