How to use unique method of io.kotest.core.names.UniqueNames class

Best Kotest code snippet using io.kotest.core.names.UniqueNames.unique

JUnitTestEngineListener.kt

Source:JUnitTestEngineListener.kt Github

copy

Full Screen

...169 }170 private fun addPlaceholderTest(parent: TestDescriptor, t: Throwable, kclass: KClass<*>) {171 val (name, cause) = ExtensionExceptionExtractor.resolve(t)172 val descriptor = createTestDescriptor(173 parent.uniqueId.append(Segment.Test.value, name),174 name,175 TestDescriptor.Type.TEST,176 ClassSource.from(kclass.java),177 false178 )179 parent.addChild(descriptor)180 listener.dynamicTestRegistered(descriptor)181 listener.executionStarted(descriptor)182 listener.executionFinished(descriptor, TestResult.Error(Duration.ZERO, cause).toTestExecutionResult())183 }184 override suspend fun testStarted(testCase: TestCase) {185 // depending on the test type, we may want to wait to notify junit, this is because gradle doesn't work186 // properly with the junit test types. Ideally, we'd just set everything to CONTAINER_AND_TEST, which is187 // supposed to mean a test can contain other tests as well as being a test itself, which is exactly how188 // Kotest views tests, but unfortunately it doesn't work properly.189 //190 // Another approach is to wait until the spec finishes to see which tests contain children and which191 // don't and set the test type appropriately, but junit doesn't give us a way to specify test duration192 // (instead it just calculates it itself from the time between marking a test as started and marking193 // it as finished), so this approach works but ends up having all tests as 0ms194 //195 // So the approach we will take is use the TestType from the test definition, unless it is dynamic,196 // then for dynamic we will calculate it later, and accept the 0ms drawback197 logger.log { Pair(testCase.name.testName, "test started") }198 if (testCase.parent != null) rootTests.add(testCase)199 addChild(testCase)200 when (testCase.type) {201 TestType.Container -> startTestIfNotStarted(testCase, TestDescriptor.Type.CONTAINER)202 TestType.Test -> startTestIfNotStarted(testCase, TestDescriptor.Type.TEST)203 TestType.Dynamic -> Unit204 }205 }206 // this test can be output now it has completed as we have all we need to know to complete it207 override suspend fun testFinished(testCase: TestCase, result: TestResult) {208 logger.log { Pair(testCase.name.testName, "test finished $result") }209 results[testCase.descriptor] = result210 val descriptor = getOrCreateTestDescriptor(testCase, null)211 // we need to ensure all parents have been started first212 startParents(testCase)213 startTestIfNotStarted(testCase, null)214 logger.log { Pair(testCase.name.testName, "executionFinished: $descriptor") }215 listener.executionFinished(descriptor, result.toTestExecutionResult())216 }217 override suspend fun testIgnored(testCase: TestCase, reason: String?) {218 logger.log { Pair(testCase.name.testName, "test ignored $reason") }219 if (testCase.parent == null) rootTests.add(testCase)220 addChild(testCase)221 results[testCase.descriptor] = TestResult.Ignored(reason)222 // we need to ensure all parents have been started first223 startParents(testCase)224 val descriptor = getOrCreateTestDescriptor(testCase, TestDescriptor.Type.TEST)225 logger.log { Pair(testCase.name.testName, "Registering dynamic test: $descriptor") }226 listener.dynamicTestRegistered(descriptor)227 logger.log { Pair(testCase.name.testName, "executionSkipped: $descriptor") }228 listener.executionSkipped(descriptor, reason)229 }230 private fun addChild(testCase: TestCase) {231 children.getOrPut(testCase.descriptor.parent) { mutableListOf() }.add(testCase)232 }233 private fun startParents(testCase: TestCase) {234 val parent = testCase.parent235 if (parent != null) {236 startParents(parent)237 startTestIfNotStarted(parent, null)238 }239 }240 private fun startTestIfNotStarted(testCase: TestCase, type: TestDescriptor.Type?) {241 if (!startedTests.contains(testCase.descriptor)) {242 val descriptor = getOrCreateTestDescriptor(testCase, type)243 logger.log { Pair(testCase.name.testName, "Registering dynamic test: $descriptor") }244 listener.dynamicTestRegistered(descriptor)245 logger.log { Pair(testCase.name.testName, "executionStarted: $descriptor") }246 listener.executionStarted(descriptor)247 startedTests.add(testCase.descriptor)248 }249 }250 private fun getOrCreateTestDescriptor(testCase: TestCase, type: TestDescriptor.Type?): TestDescriptor {251 val existing = descriptors[testCase.descriptor]252 if (existing != null) return existing253 val parent = when (val p = testCase.parent) {254 null -> getSpecDescriptor(testCase.spec::class)255 else -> getOrCreateTestDescriptor(p, null)256 }257 val id = parent.uniqueId.append(testCase.descriptor)258 // we dynamically work out the type if null by looking to see if this test had any children259 val c = children[testCase.descriptor]260 val t = when {261 type != null -> type262 c == null || c.isEmpty() -> TestDescriptor.Type.TEST263 else -> TestDescriptor.Type.CONTAINER264 }265 return createTestDescriptor(266 id,267 formatter.format(testCase),268 t,269 ClassSource.from(testCase.spec::class.java, null), // gradle-junit-platform hides tests if we don't send this270 type == TestDescriptor.Type.CONTAINER271 ).apply {272 parent.addChild(this)273 descriptors[testCase.descriptor] = this274 }275 }276 private fun getSpecDescriptor(kclass: KClass<*>): TestDescriptor {277 return getSpecDescriptor(root, kclass.toDescriptor(), formatter.format(kclass))278 }279 private fun createAndRegisterDummySpec(name: String): TestDescriptor {280 val unique = UniqueNames.unique(name, dummies) { s, k -> "${s}_$k" } ?: name281 dummies.add(unique)282 val descriptor = getSpecDescriptor(root, Descriptor.SpecDescriptor(DescriptorId(unique), this::class), unique)283 listener.dynamicTestRegistered(descriptor)284 return descriptor285 }286 private fun registerExceptionPlaceholders(ts: List<Throwable>) {287 ts.forEach {288 val (name, cause) = ExtensionExceptionExtractor.resolve(it)289 val container = createAndRegisterDummySpec(name)290 listener.executionStarted(container)291 listener.executionFinished(container, TestExecutionResult.failed(cause))292 }293 }294}...

Full Screen

Full Screen

DuplicateTestNameHandler.kt

Source:DuplicateTestNameHandler.kt Github

copy

Full Screen

...3import io.kotest.core.names.TestName4import io.kotest.core.names.UniqueNames5/**6 * Tracks test names for a context, and based on the given [DuplicateTestNameMode] either fails7 * on duplicates, warns on duplicates, or modifies names to be unique.8 */9internal class DuplicateTestNameHandler(private val mode: DuplicateTestNameMode) {10 private val names = mutableSetOf<String>()11 private fun message(name: String): String =12 "Duplicated test name ${name}. To disable this message, set DuplicateTestNameMode to None."13 /**14 * Returns a unique name or null if the name is already unique.15 */16 fun handle(name: TestName): String? {17 val isUnique = names.add(name.testName)18 if (isUnique) return null19 return when (mode) {20 DuplicateTestNameMode.Error -> throw DuplicateTestNameException(message(name.testName))21 DuplicateTestNameMode.Silent -> makeUniqueName(name.testName)22 DuplicateTestNameMode.Warn -> {23 println("WARN: " + message(name.testName))24 makeUniqueName(name.testName)25 }26 }27 }28 private fun makeUniqueName(name: String): String {29 val unique = UniqueNames.unique(name, names) ?: name30 names.add(unique)31 return unique32 }33}34class DuplicateTestNameException(message: String) : RuntimeException(message)...

Full Screen

Full Screen

UniqueNamesTest.kt

Source:UniqueNamesTest.kt Github

copy

Full Screen

...3import io.kotest.core.spec.style.FunSpec4import io.kotest.matchers.shouldBe5class UniqueNamesTest : FunSpec() {6 init {7 test("should make repeated names unique") {8 UniqueNames.unique("foo", emptySet()) shouldBe null9 UniqueNames.unique("foo", setOf("foo")) shouldBe "(1) foo"10 UniqueNames.unique("foo", setOf("foo", "(1) foo")) shouldBe "(2) foo"11 UniqueNames.unique("foo", setOf("foo", "(1) foo", "(2) foo")) shouldBe "(3) foo"12 UniqueNames.unique("foo", setOf("foo", "(1) foo")) shouldBe "(2) foo"13 }14 }15}...

Full Screen

Full Screen

UniqueNames.kt

Source:UniqueNames.kt Github

copy

Full Screen

1package io.kotest.core.names2object UniqueNames {3 /**4 * Returns a unique ident for the given ident and current idents.5 *6 * @param genNext returns the next ident to consider, using the iteration count and the raw ident7 */8 fun unique(9 ident: String,10 indents: Set<String>,11 genNext: (String, Int) -> String = { s, k -> "($k) $s" }12 ): String? {13 if (!indents.contains(ident)) return null14 var n = 115 fun next() = genNext(ident, n)16 while (indents.contains(next()))17 n++18 return next()19 }20}...

Full Screen

Full Screen

unique

Using AI Code Generation

copy

Full Screen

1val uniqueNames = UniqueNames()2test("my test") {3uniqueNames.unique("my test") {4}5}6val uniqueNames = UniqueNames()7test("my test") {8uniqueNames.unique("my test") {9}10}11test("my test") {12 uniqueNames.unique("my test") {13 }14}15test("my test") {16 uniqueNames.unique("my test") {17 }18}19test("my test") {20 uniqueNames.unique("my test") {21 }22}23I’m importing io.kotest.core.names.Unique

Full Screen

Full Screen

unique

Using AI Code Generation

copy

Full Screen

1val list = listOf( 1 , 2 , 3 , 4 , 5 )2test("test 1") { }3test("test 2") { }4test("test 3") { }5test("test 4") { }6test("test 5") { }7test("test 1") { }8test("test 2") { }9test("test 3") { }10test("test 4") { }11test("test 5") { }12val list = listOf( 1 , 2 , 3 , 4 , 5 )13test("test 1") { }14test("test 2") { }15test("test 3") { }16test("test 4") { }17test("test 5") { }18test("test 1") { }19test("test 2") { }20test("test 3") { }21test("test 4") { }22test("test 5") { }23val list = listOf( 1 , 2 , 3 , 4 , 5 )24test("test 1") { }25test("test 2") { }26test("test 3") { }27test("test 4") { }28test("test 5") { }29test("test 1") { }30test("test 2") { }31test("test 3") { }32test("test 4") { }33test("test 5") { }34val list = listOf( 1 , 2 , 3 , 4 , 5 )35test("test 1") { }36test("test 2") { }37test("test 3") { }38test("test 4") { }39test("test 5") { }40test("test 1") { }41test("test 2") { }42test("test 3") { }43test("

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Kotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in UniqueNames

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful