How to use SpecDescriptor class of io.kotest.core.descriptors package

Best Kotest code snippet using io.kotest.core.descriptors.SpecDescriptor

EnhancedConsoleTestEngineListener.kt

Source:EnhancedConsoleTestEngineListener.kt Github

copy

Full Screen

...22 private var start = System.currentTimeMillis()23 private var testsFailed = emptyList<Pair<TestCase, TestResult>>()24 private var testsIgnored = 025 private var testsPassed = 026 private var specsFailed = emptyList<Descriptor.SpecDescriptor>()27 private var specsSeen = emptyList<Descriptor>()28 private var slow = Duration.milliseconds(500)29 private var verySlow = Duration.milliseconds(5000)30 private var formatter:DisplayNameFormatter = DefaultDisplayNameFormatter(ProjectConfiguration())31 private fun green(str: String) = term.green(str)32 private fun greenBold(str: String) = term.green.plus(term.bold).invoke(str)33 private fun red(str: String) = term.red(str)34 private fun brightRed(str: String) = term.brightRed(str)35 private fun brightRedBold(str: String) = term.brightRed.plus(term.bold).invoke(str)36 private fun redBold(str: String) = term.red.plus(term.bold).invoke(str)37 private fun yellow(str: String) = term.yellow(str)38 private fun brightYellow(str: String) = term.brightYellow(str)39 private fun brightYellowBold(str: String) = term.brightYellow.plus(term.bold).invoke(str)40 private fun yellowBold(str: String) = term.yellow.plus(term.bold).invoke(str)...

Full Screen

Full Screen

descriptor.kt

Source:descriptor.kt Github

copy

Full Screen

1package io.kotest.core.descriptors2import io.kotest.common.KotestInternal3import io.kotest.core.descriptors.Descriptor.SpecDescriptor4import io.kotest.core.descriptors.Descriptor.TestDescriptor5import io.kotest.core.names.TestName6import kotlin.reflect.KClass7typealias TestPath = io.kotest.common.TestPath8/**9 * A parseable, stable, consistent identifer for a test element.10 *11 * The id should not depend on runtime configuration and should not change between test runs,12 * unless the test, or a parent test, has been modified by the user.13 */14sealed interface Descriptor {15 val id: DescriptorId16 /**17 * A [Descriptor] for a spec class or a script file.18 */19 data class SpecDescriptor(20 override val id: DescriptorId,21 val kclass: KClass<*>,22 ) : Descriptor23 /**24 * A [Descriptor] for a test.25 */26 data class TestDescriptor(27 val parent: Descriptor,28 override val id: DescriptorId,29 ) : Descriptor30 companion object {31 const val SpecDelimiter = "/"32 const val TestDelimiter = " -- "33 }34 fun ids(): List<DescriptorId> = when (this) {35 is SpecDescriptor -> listOf(this.id)36 is TestDescriptor -> this.parent.ids() + this.id37 }38 /**39 * Returns a parseable path to the test.40 *41 * @param includeSpec if true then the spec name is included in the path.42 */43 fun path(includeSpec: Boolean = true): TestPath = when (this) {44 is SpecDescriptor -> if (includeSpec) TestPath(this.id.value) else error("Cannot call path on spec with includeSpec=false")45 is TestDescriptor -> when (this.parent) {46 is SpecDescriptor -> when (includeSpec) {47 true -> TestPath(this.parent.id.value + SpecDelimiter + this.id.value)48 false -> TestPath(this.id.value)49 }50 is TestDescriptor -> TestPath(this.parent.path(includeSpec).value + TestDelimiter + this.id.value)51 }52 }53 @KotestInternal54 fun parts(): List<String> = when (this) {55 is SpecDescriptor -> emptyList()56 is TestDescriptor -> parent.parts() + listOf(this.id.value)57 }58 /**59 * Returns true if this descriptor is for a class based test file.60 */61 fun isSpec() = this is SpecDescriptor62 /**63 * Returns true if this descriptor is for a test case.64 */65 fun isTestCase() = this is TestDescriptor66 /**67 * Returns true if this descriptor represents a root test case.68 */69 fun isRootTest() = this is TestDescriptor && this.parent.isSpec()70 /**71 * Returns the depth of this node, where the [SpecDescriptor] has depth of 0,72 * a root test has depth 1 and so on.73 */74 fun depth() = parents().size - 175 /**76 * Recursively returns any parent descriptors, with the spec being first in the list77 * and this being last.78 */79 fun parents(): List<Descriptor> = when (this) {80 is SpecDescriptor -> emptyList()81 is TestDescriptor -> parent.parents() + parent82 }83 fun chain() = parents() + this84 /**85 * Returns true if this descriptor is the immediate parent of the given [descriptor].86 */87 fun isParentOf(descriptor: Descriptor): Boolean = when (descriptor) {88 is SpecDescriptor -> false // nothing can be the parent of a spec89 is TestDescriptor -> this.id == descriptor.parent.id90 }91 /**92 * Returns true if this descriptor is ancestor (1..nth-parent) of the given [descriptor].93 */94 fun isAncestorOf(descriptor: Descriptor): Boolean = when (descriptor) {95 is SpecDescriptor -> false // nothing can be an ancestor of a spec96 is TestDescriptor -> this.id == descriptor.parent.id || isAncestorOf(descriptor.parent)97 }98 /**99 * Returns true if this descriptor is the immediate child of the given [descriptor].100 */101 fun isChildOf(descriptor: Descriptor): Boolean = descriptor.isParentOf(this)102 /**103 * Returns true if this descriptor is a child, grandchild, etc of the given [descriptor].104 */105 fun isDescendentOf(descriptor: Descriptor): Boolean = descriptor.isAncestorOf(this)106 /**107 * Returns true if this instance is on the path to the given description. That is, if this108 * instance is either an ancestor of, of the same as, the given description.109 */110 fun isOnPath(description: Descriptor): Boolean =111 this.path() == description.path() || this.isAncestorOf(description)112 /**113 * Returns the [SpecDescriptor] parent for this [Descriptor].114 * If this is already a spec descriptor, then returns itself.115 */116 fun spec(): SpecDescriptor = when (this) {117 is SpecDescriptor -> this118 is TestDescriptor -> this.parent.spec()119 }120}121data class DescriptorId(val value: String)122fun SpecDescriptor.append(name: TestName): TestDescriptor =123 TestDescriptor(this, DescriptorId(name.testName))124fun TestDescriptor.append(name: TestName): TestDescriptor =125 this.append(name.testName)126fun Descriptor.append(name: String): TestDescriptor =127 TestDescriptor(this, DescriptorId(name))128/**129 * Returns the [TestDescriptor] that is the root for this [TestDescriptor].130 * This may be the same descriptor that this method is invoked on, if that descriptor131 * is a root test.132 */133fun TestDescriptor.root(): TestDescriptor {134 return when (parent) {135 is SpecDescriptor -> this // if my parent is a spec, then I am a root136 is TestDescriptor -> parent.root()137 }138}...

Full Screen

Full Screen

ThingTestCases.kt

Source:ThingTestCases.kt Github

copy

Full Screen

...19import io.kotest.core.test.TestCase20import io.kotest.core.test.TestResult21import io.kotest.core.test.TestType22import kotlin.time.Duration23val specDescriptor = Descriptor.SpecDescriptor(DescriptorId(ThingTest::class.qualifiedName!!), ThingTest::class)24val thingTest = ThingTest()25const val name0 = "0. Describe the thing"26val descriptor0 = Descriptor.TestDescriptor(specDescriptor, DescriptorId(name0))27val case0 = TestCase(28 descriptor0, TestName(name0), thingTest, {}, SourceRef.FileSource("ThingTest.kt", 8),29 TestType.Container, parent = null30)31val result0 = TestResult.Success(Duration.parse("81.112632ms"))32const val name1 = "1. I don’t care"33val descriptor1 = Descriptor.TestDescriptor(descriptor0, DescriptorId(name1))34val case1 = TestCase(35 descriptor1, TestName(name1), thingTest, {}, SourceRef.FileSource("ThingTest.kt", 9),36 TestType.Test, parent = case037)...

Full Screen

Full Screen

descriptors.kt

Source:descriptors.kt Github

copy

Full Screen

...10 * if one does not already exist.11 *12 * The created Test Descriptor will have segment type [Segment.Spec] and will use [displayName].13 */14fun getSpecDescriptor(15 engine: TestDescriptor,16 descriptor: Descriptor.SpecDescriptor,17 displayName: String,18): TestDescriptor {19 val id = engine.uniqueId.append(Segment.Spec.value, descriptor.id.value)20 return engine.findByUniqueId(id).orElseGet { null }21 ?: createAndRegisterSpecDescription(engine, descriptor, displayName)22}23private fun createAndRegisterSpecDescription(24 engine: TestDescriptor,25 descriptor: Descriptor.SpecDescriptor,26 displayName: String,27): TestDescriptor {28 val id = engine.uniqueId.append(Segment.Spec.value, descriptor.id.value)29 val source = ClassSource.from(descriptor.kclass.java)30 val testDescriptor: TestDescriptor = object : AbstractTestDescriptor(id, displayName, source) {31 override fun getType(): TestDescriptor.Type = TestDescriptor.Type.CONTAINER32 override fun mayRegisterTests(): Boolean = true33 }34 engine.addChild(testDescriptor)35 return testDescriptor36}37/**38 * Creates a [TestDescriptor] for the given [testCase] and attaches it to the [parent].39 * The created descriptor will have segment type [Segment.Test] and will use [displayName]....

Full Screen

Full Screen

PostDiscoveryFilterAdapter.kt

Source:PostDiscoveryFilterAdapter.kt Github

copy

Full Screen

...36 */37 private fun createTestDescriptor(root: UniqueId, descriptor: Descriptor, displayName: String): TestDescriptor {38 val id: UniqueId = descriptor.chain().fold(root) { acc, desc -> acc.append(desc) }39 val source = when (descriptor) {40 is Descriptor.SpecDescriptor -> ClassSource.from(descriptor.kclass.java)41 // this isn't a method, but we can use MethodSource with the test name, so it's at least42 // somewhat compatible for top level tests.43 is Descriptor.TestDescriptor -> MethodSource.from(descriptor.spec().kclass.java.name, descriptor.path().value)44 }45 return io.kotest.runner.junit.platform.createTestDescriptor(46 id,47 displayName,48 TestDescriptor.Type.CONTAINER,49 source,50 false51 )52 }53}...

Full Screen

Full Screen

GradleClassMethodRegexTestFilter.kt

Source:GradleClassMethodRegexTestFilter.kt Github

copy

Full Screen

...16 fun match(pattern: String, descriptor: Descriptor): Boolean {17 val (pck, classname, path) = GradleTestPattern.parse(pattern)18 return when (descriptor) {19 is Descriptor.TestDescriptor -> descriptor.parts().take(path.size) == path20 is Descriptor.SpecDescriptor -> when {21 pck != null && classname != null -> descriptor.kclass.qualifiedName == "$pck.$classname"22 pck != null -> descriptor.kclass.qualifiedName?.startsWith(pck) ?: true23 classname != null -> descriptor.kclass.simpleName == classname24 else -> true25 }26 }27 }28}29data class GradleTestPattern(val pckage: String?, val classname: String?, val path: List<String>) {30 companion object {31 // if the regex starts with a lower case character, then we assume it is in the format package.Class.testpath32 // otherwise, we assume it is in the format Class.testpath33 // the .testpath is always optional, and at least Class or package must be specified34 fun parse(pattern: String): GradleTestPattern {...

Full Screen

Full Screen

uniqueids.kt

Source:uniqueids.kt Github

copy

Full Screen

...5 * Returns a new [UniqueId] by appending this [descriptor] to the receiver.6 */7fun UniqueId.append(descriptor: Descriptor): UniqueId {8 val segment = when (descriptor) {9 is Descriptor.SpecDescriptor -> Segment.Spec10 is Descriptor.TestDescriptor -> Segment.Test11 }12 return this.append(segment.value, descriptor.id.value)13}14sealed class Segment {15 abstract val value: String16 object Spec : Segment() {17 override val value: String = "spec"18 }19 object Script : Segment() {20 override val value: String = "script"21 }22 object Test : Segment() {23 override val value: String = "test"...

Full Screen

Full Screen

kclasses.kt

Source:kclasses.kt Github

copy

Full Screen

1package io.kotest.core.descriptors2import io.kotest.core.descriptors.Descriptor.SpecDescriptor3import io.kotest.mpp.bestName4import kotlin.reflect.KClass5/**6 * Returns a [SpecDescriptor] for a class using the fully qualified name for the identifier.7 *8 * On platforms where the FQN is not available, this will fall back to the simple class name.9 */10fun KClass<*>.toDescriptor(): SpecDescriptor {11 return SpecDescriptor(12 DescriptorId(this.bestName()),13 this,14 )15}...

Full Screen

Full Screen

SpecDescriptor

Using AI Code Generation

copy

Full Screen

1import io.kotest.core.descriptors.*2val descriptor = SpecDescriptor("com.example", "MySpec")3val descriptor = SpecDescriptor("com.example", "MySpec", "My Description")4val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type")5val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type", "My DisplayName")6val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type", "My DisplayName", "My DisplayNamePrefix")7val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type", "My DisplayName", "My DisplayNamePrefix", "My DisplayNameTrailing")8val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type", "My DisplayName", "My DisplayNamePrefix", "My DisplayNameTrailing", "My DisplayNameTrailing")9val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type", "My DisplayName", "My DisplayNamePrefix", "My DisplayNameTrailing", "My DisplayNameTrailing", "My DisplayNameTrailing")10val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type", "My DisplayName", "My DisplayNamePrefix", "My DisplayNameTrailing", "My DisplayNameTrailing", "My DisplayNameTrailing", "My DisplayNameTrailing")11val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type", "My DisplayName", "My DisplayNamePrefix", "My DisplayNameTrailing", "My DisplayNameTrailing", "My DisplayNameTrailing", "My DisplayNameTrailing", "My DisplayNameTrailing")12val descriptor = SpecDescriptor("com.example", "MySpec", "My Description", "My Type", "My DisplayName", "My DisplayNameP

Full Screen

Full Screen

SpecDescriptor

Using AI Code Generation

copy

Full Screen

1class MySpec : FunSpec() {2 init {3 val specDescriptor = SpecDescriptor(this.javaClass)4 val testDescriptor = TestDescriptor("test name", specDescriptor)5 specDescriptor.addTest(testDescriptor)6 }7}

Full Screen

Full Screen

SpecDescriptor

Using AI Code Generation

copy

Full Screen

1val descriptor = SpecDescriptor(Spec::class)2val descriptor = SpecDescriptor(Spec::class)3val descriptor = SpecDescriptor(Spec::class)4val descriptor = SpecDescriptor(Spec::class)5val descriptor = SpecDescriptor(Spec::class)6val descriptor = SpecDescriptor(Spec::class)7val descriptor = SpecDescriptor(Spec::class)8val descriptor = SpecDescriptor(Spec::class)

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful