How to use sourceRef method of io.kotest.core.source.sourceRef class

Best Kotest code snippet using io.kotest.core.source.sourceRef.sourceRef

DefaultDisplayNameFormatterTest.kt

Source:DefaultDisplayNameFormatterTest.kt Github

copy

Full Screen

...4import io.kotest.core.config.ProjectConfiguration5import io.kotest.core.descriptors.append6import io.kotest.core.descriptors.toDescriptor7import io.kotest.core.names.TestName8import io.kotest.core.source.sourceRef9import io.kotest.core.spec.DisplayName10import io.kotest.core.annotation.Isolate11import io.kotest.core.spec.style.FunSpec12import io.kotest.core.test.TestCase13import io.kotest.core.test.TestType14import io.kotest.core.test.config.ResolvedTestConfig15import io.kotest.engine.test.names.DefaultDisplayNameFormatter16import io.kotest.matchers.shouldBe17@Isolate18class DefaultDisplayNameFormatterTest : FunSpec() {19 init {20 test("@DisplayName should be used for spec name") {21 DefaultDisplayNameFormatter(ProjectConfiguration()).format(SpecWithDisplayName::class) shouldBe "ZZZZZ"22 }23 test("test name should use full path option") {24 val conf = ProjectConfiguration()25 conf.displayFullTestPath = true26 val tc1 = TestCase(27 SpecWithDisplayName::class.toDescriptor().append("test"),28 TestName("test"),29 SpecWithDisplayName(),30 {},31 sourceRef(),32 TestType.Test,33 )34 val tc2 = TestCase(35 SpecWithDisplayName::class.toDescriptor().append("test2"),36 TestName("test2"),37 SpecWithDisplayName(),38 {},39 sourceRef(),40 TestType.Test,41 parent = tc142 )43 DefaultDisplayNameFormatter(conf).format(tc2) shouldBe "test test2"44 }45 test("tags should be appended from config when configuration is set") {46 val c = ProjectConfiguration()47 c.testNameAppendTags = true48 val tc = TestCase(49 SpecWithDisplayName::class.toDescriptor().append("test"),50 TestName("test"),51 SpecWithDisplayName(),52 {},53 sourceRef(),54 TestType.Test,55 ResolvedTestConfig.default.copy(tags = setOf(NamedTag("Foo"), Dummy))56 )57 DefaultDisplayNameFormatter(c).format(tc) shouldBe "test[tags = Foo, Dummy]"58 }59 test("bang should not be included in test name") {60 val tc = TestCase(61 descriptor = SpecWithDisplayName::class.toDescriptor().append("!test"),62 name = TestName("!test"),63 spec = SpecWithDisplayName(),64 test = {},65 source = sourceRef(),66 type = TestType.Test,67 config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))68 )69 DefaultDisplayNameFormatter(ProjectConfiguration()).format(tc) shouldBe "test"70 }71 test("focus should not be included in test name") {72 val tc = TestCase(73 descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),74 name = TestName("f:test"),75 spec = SpecWithDisplayName(),76 test = {},77 source = sourceRef(),78 type = TestType.Test,79 config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))80 )81 DefaultDisplayNameFormatter(ProjectConfiguration()).format(tc) shouldBe "test"82 }83 test("name should include prefix if affixes are included by default") {84 val tc = TestCase(85 descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),86 name = TestName("prefix", "foo", null, true),87 spec = SpecWithDisplayName(),88 test = {},89 source = sourceRef(),90 type = TestType.Test,91 config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))92 )93 DefaultDisplayNameFormatter(ProjectConfiguration()).format(tc) shouldBe "prefixfoo"94 }95 test("name should include prefix if affixes are excluded by default but enabled by config") {96 val tc = TestCase(97 descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),98 name = TestName("prefix", "foo", null, false),99 spec = SpecWithDisplayName(),100 test = {},101 source = sourceRef(),102 type = TestType.Test,103 config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))104 )105 val c = ProjectConfiguration()106 c.includeTestScopeAffixes = true107 DefaultDisplayNameFormatter(c).format(tc) shouldBe "prefixfoo"108 }109 test("name should include suffix if affixes are included by default") {110 val tc = TestCase(111 descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),112 name = TestName(null, "foo", "suffix", true),113 spec = SpecWithDisplayName(),114 test = {},115 source = sourceRef(),116 type = TestType.Test,117 config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))118 )119 DefaultDisplayNameFormatter(ProjectConfiguration()).format(tc) shouldBe "foosuffix"120 }121 test("name should include suffix if affixes are excluded by default but enabled in config") {122 val tc = TestCase(123 descriptor = SpecWithDisplayName::class.toDescriptor().append("f:test"),124 name = TestName(null, "foo", "suffix", false),125 spec = SpecWithDisplayName(),126 test = {},127 source = sourceRef(),128 type = TestType.Test,129 config = ResolvedTestConfig.default.copy(tags = setOf(Dummy, NoUse))130 )131 val c = ProjectConfiguration()132 c.includeTestScopeAffixes = true133 DefaultDisplayNameFormatter(c).format(tc) shouldBe "foosuffix"134 }135 }136}137object Dummy : Tag()138object NoUse : Tag()139@DisplayName("ZZZZZ")140private class SpecWithDisplayName : FunSpec({141 test("a") { }...

Full Screen

Full Screen

styles.kt

Source:styles.kt Github

copy

Full Screen

...5//import io.kotest.core.plan.DisplayName6//import io.kotest.core.plan.Name7//import io.kotest.core.Source8//import io.kotest.core.plan.toDescriptor9//import io.kotest.core.source.sourceRef10//import io.kotest.core.test.Description11//import io.kotest.core.test.config.TestCaseConfig12//import io.kotest.core.test.testScope13//import io.kotest.core.test.TestType14//import io.kotest.core.test.createNestedTest15//16///**17// * Registers a root test, with the given name.18// */19//fun test(name: String, test: suspend testScope.() -> Unit) {20// ScriptRuntime.registerRootTest(TestName(name), false, TestType.Test, test)21//}22//23///**24// * Registers a root test, with the given name.25// */26//fun should(name: String, test: suspend testScope.() -> Unit) {27// ScriptRuntime.registerRootTest(TestName(name), false, TestType.Test, test)28//}29//30///**31// * Registers a root context scope, which allows further tests to be registered with test and should keywords.32// */33//fun context(name: String, test: suspend ContextScope.() -> Unit) {34// val testName = TestName(name)35// val description = ScriptSpec().description().append(testName, TestType.Container)36// val d = description.toDescriptor(sourceRef()).append(37// Name(testName.name),38// DisplayName(testName.displayName),39// TestType.Container,40// Source.TestSource(sourceRef().fileName, sourceRef().lineNumber),41// )42// ScriptRuntime.registerRootTest(testName, false, TestType.Container) {43// ContextScope(44// description,45// it,46// d47// ).test()48// }49//}50//51//class ContextScope(52// val description: Description,53// val testScope: testScope,54// val descriptor: Descriptor,55//) {56//57// suspend fun test(name: String, test: suspend testScope.() -> Unit) {58// val testName = TestName(name)59// registerNestedTest(60// name = testName,61// testScope = testScope,62// xdisabled = false,63// test = test,64// config = TestCaseConfig(),65// type = TestType.Test,66// descriptor = descriptor.append(67// Name(testName.name),68// DisplayName(testName.displayName),69// TestType.Test,70// Source.TestSource(sourceRef().fileName, sourceRef().lineNumber),71// ),72// )73// }74//75// suspend fun should(name: String, test: suspend testScope.() -> Unit) {76// val testName = TestName(name)77// registerNestedTest(78// name = testName,79// testScope = testScope,80// xdisabled = false,81// test = test,82// config = TestCaseConfig(),83// type = TestType.Test,84// descriptor = descriptor.append(85// Name(testName.name),86// DisplayName(testName.displayName),87// TestType.Test,88// Source.TestSource(sourceRef().fileName, sourceRef().lineNumber),89// ),90// )91// }92//}93//94///**95// * Registers a root 'describe' scope, with the given name.96// */97//fun describe(name: String, test: suspend DescribeScope.() -> Unit) {98// val testName = TestName("Describe: ", name, false)99// val description = ScriptSpec().description().append(testName, TestType.Container)100// val d = description.toDescriptor(sourceRef()).append(101// Name(testName.name),102// DisplayName(testName.displayName),103// TestType.Container,104// Source.TestSource(sourceRef().fileName, sourceRef().lineNumber),105// )106// ScriptRuntime.registerRootTest(107// testName,108// false,109// TestType.Container110// ) { DescribeScope(description, it, d).test() }111//}112//113//class DescribeScope(114// val description: Description,115// val testScope: testScope,116// val descriptor: Descriptor,117//) {118//119// /**120// * Registers a nested 'describe' scope, with the given name.121// */122// suspend fun describe(name: String, test: suspend DescribeScope.() -> Unit) {123// val testName = TestName("Describe: ", name, false)124// val d = descriptor.append(125// Name(testName.name),126// DisplayName(testName.displayName),127// TestType.Container,128// Source.TestSource(sourceRef().fileName, sourceRef().lineNumber),129// )130// registerNestedTest(131// name = testName,132// testScope = testScope,133// xdisabled = false,134// test = { DescribeScope(description.append(testName, TestType.Test), testScope, d).test() },135// config = TestCaseConfig(),136// type = TestType.Test,137// descriptor = d,138// )139// }140//141// suspend fun it(name: String, test: suspend testScope.() -> Unit) {142// val testName = TestName(name)143// registerNestedTest(144// name = testName,145// testScope = testScope,146// xdisabled = false,147// test = test,148// config = TestCaseConfig(),149// type = TestType.Test,150// descriptor = descriptor.append(151// Name(testName.name),152// DisplayName(testName.displayName),153// TestType.Test,154// Source.TestSource(sourceRef().fileName, sourceRef().lineNumber),155// ),156// )157// }158//}159//160//private suspend fun registerNestedTest(161// descriptor: Descriptor.TestDescriptor,162// name: TestName,163// xdisabled: Boolean,164// test: suspend testScope.() -> Unit,165// config: TestCaseConfig,166// testScope: testScope,167// type: TestType,168//) {...

Full Screen

Full Screen

ThingTestCases.kt

Source:ThingTestCases.kt Github

copy

Full Screen

1/*2 Copyright 2022 Michael Strasser.3 Licensed under the Apache License, Version 2.0 (the "License");4 you may not use this file except in compliance with the License.5 You may obtain a copy of the License at6 http://www.apache.org/licenses/LICENSE-2.07 Unless required by applicable law or agreed to in writing, software8 distributed under the License is distributed on an "AS IS" BASIS,9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.10 See the License for the specific language governing permissions and11 limitations under the License.12*/13package mjs.kotest.fixtures14import io.kotest.assertions.AssertionFailedError15import io.kotest.core.descriptors.Descriptor16import io.kotest.core.descriptors.DescriptorId17import io.kotest.core.names.TestName18import io.kotest.core.source.SourceRef19import 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)38val result1 = TestResult.Ignored("Disabled by xmethod")39const val name2 = "2. the inner thing"40val descriptor2 = Descriptor.TestDescriptor(descriptor0, DescriptorId(name2))41val case2 = TestCase(42 descriptor2, TestName(name2), thingTest, {}, SourceRef.FileSource("ThingTest.kt", 12),43 TestType.Container, parent = case044)45val result2 = TestResult.Success(Duration.parse("34.051853ms"))46const val name2a = "2a. is thing"47val descriptor2a = Descriptor.TestDescriptor(descriptor2, DescriptorId(name2a))48val case2a = TestCase(49 descriptor2a, TestName(name2a), thingTest, {}, SourceRef.FileSource("ThingTest.kt", 13),50 TestType.Test, parent = case251)52val result2a = TestResult.Success(Duration.parse("5.236817ms"))53const val name2b = "2b. is not thang"54val descriptor2b = Descriptor.TestDescriptor(descriptor2, DescriptorId(name2b))55val case2b = TestCase(56 descriptor2b, TestName(name2b), thingTest, {}, SourceRef.FileSource("ThingTest.kt", 16),57 TestType.Test, parent = case258)59val result2b = TestResult.Failure(60 Duration.parse("17.143399ms"),61 AssertionFailedError("expected:<\"thang\"> but was:<\"thing\">", null, "thang", "thing"),62)63const val name3 = "3. also ignored"64val descriptor3 = Descriptor.TestDescriptor(descriptor0, DescriptorId(name3))65val case3 = TestCase(66 descriptor3, TestName(name3), thingTest, {}, SourceRef.FileSource("ThingTest.kt", 20),67 TestType.Container, parent = case068)69val result3 = TestResult.Ignored("Disabled by xmethod")70val resultsMap: Map<TestCase, TestResult> = mapOf(71 case0 to result0,72 case1 to result1,73 case2 to result2,74 case2a to result2a,75 case2b to result2b,76 case3 to result3,77)...

Full Screen

Full Screen

SpringExtensionTest.kt

Source:SpringExtensionTest.kt Github

copy

Full Screen

1package io.kotest.extensions.spring2import io.kotest.core.descriptors.append3import io.kotest.core.descriptors.toDescriptor4import io.kotest.core.names.TestName5import io.kotest.core.source.sourceRef6import io.kotest.core.spec.style.WordSpec7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestType9import io.kotest.matchers.nulls.shouldNotBeNull10import io.kotest.matchers.shouldBe11import org.springframework.beans.factory.annotation.Autowired12import org.springframework.test.context.ContextConfiguration13@ContextConfiguration(classes = [Components::class])14class SpringExtensionTest : WordSpec() {15 override fun extensions() = listOf(SpringExtension)16 @Autowired17 private lateinit var service: UserService18 init {19 "SpringExtension" should {20 "have autowired the service" {21 service.repository.findUser().name shouldBe "system_user"22 }23 "make test context available in the coroutine context"{24 testContextManager().shouldNotBeNull()25 }26 "generate applicable method name for a root test" {27 SpringExtension.methodName(28 TestCase(29 descriptor = SpringExtensionTest::class.toDescriptor()30 .append("0foo__!!55@#woo"),31 name = TestName("0foo__!!55@#woo"),32 spec = this@SpringExtensionTest,33 test = {},34 source = sourceRef(),35 type = TestType.Test36 )37 ) shouldBe "_0foo____55__woo"38 }39 "generate applicable method name for a nested test" {40 SpringExtension.methodName(41 TestCase(42 descriptor = SpringExtensionTest::class.toDescriptor()43 .append("0foo__!!55@#woo")44 .append("wibble%%wobble"),45 name = TestName("wibble%%wobble"),46 spec = this@SpringExtensionTest,47 test = {},48 source = sourceRef(),49 type = TestType.Test50 )51 ) shouldBe "_0foo____55__woo____wibble__wobble"52 }53 }54 }55}...

Full Screen

Full Screen

sourceRef.kt

Source:sourceRef.kt Github

copy

Full Screen

...7/**8 * On the JVM we can create a stack trace to get the line number.9 * Users can disable the source ref via the system property [KotestEngineProperties.disableSourceRef].10 */11actual fun sourceRef(): SourceRef {12 if (sysprop(KotestEngineProperties.disableSourceRef, "false") == "true") return SourceRef.None13 val stack = Thread.currentThread().stackTrace14 if (stack.isEmpty()) return SourceRef.None15 val frame = stack.dropWhile {16 it.className.startsWith("io.kotest") ||17 it.className.startsWith("java.lang") ||18 it.className.startsWith("com.sun") ||19 it.className.startsWith("kotlin.") ||20 it.className.startsWith("kotlinx.")21 }.firstOrNull()22 val fileName = frame?.fileName23 // preference is given to the class name but we must try to find the enclosing spec24 val kclass = frame?.className?.let { fqn ->25 runCatching {...

Full Screen

Full Screen

TestCaseParentsTest.kt

Source:TestCaseParentsTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.core.api2import io.kotest.core.descriptors.append3import io.kotest.core.descriptors.toDescriptor4import io.kotest.core.names.TestName5import io.kotest.core.source.sourceRef6import io.kotest.core.spec.style.FunSpec7import io.kotest.core.test.TestCase8import io.kotest.core.test.TestType9import io.kotest.core.test.parents10import io.kotest.matchers.collections.shouldBeEmpty11import io.kotest.matchers.shouldBe12class TestCaseParentsTest : FunSpec() {13 init {14 test("test case parents") {15 val tc1 = TestCase(16 TestCaseParentsTest::class.toDescriptor().append("foo"),17 TestName("foo"),18 TestCaseParentsTest(),19 {},20 sourceRef(),21 TestType.Test22 )23 val tc2 = TestCase(24 TestCaseParentsTest::class.toDescriptor().append("foo").append("bar"),25 TestName("bar"),26 TestCaseParentsTest(),27 {},28 sourceRef(),29 TestType.Test,30 parent = tc131 )32 val tc3 = TestCase(33 TestCaseParentsTest::class.toDescriptor().append("foo").append("bar").append("baz"),34 TestName("baz"),35 TestCaseParentsTest(),36 {},37 sourceRef(),38 TestType.Test,39 parent = tc240 )41 tc1.parents().shouldBeEmpty()42 tc2.parents() shouldBe listOf(tc1)43 tc3.parents() shouldBe listOf(tc1, tc2)44 }45 }46}...

Full Screen

Full Screen

locations.kt

Source:locations.kt Github

copy

Full Screen

...7 "kotest:class://" + kclass.bestName() + ":1"8 // note that everything before the :// is considered the "protocol" by the intellij plugin9 private fun fileHint(fileName: String, lineNumber: Int) = "kotest:file://${fileName}:${lineNumber}"10 private fun classHint(fqn: String, lineNumber: Int) = "kotest:class://${fqn}:${lineNumber}"11 fun location(sourceRef: SourceRef): String? = when (sourceRef) {12 is SourceRef.FileSource -> fileHint(sourceRef.fileName, sourceRef.lineNumber ?: 1)13 is SourceRef.ClassSource -> classHint(sourceRef.fqn, sourceRef.lineNumber ?: 1)14 SourceRef.None -> null15 }16}...

Full Screen

Full Screen

LocationsTest.kt

Source:LocationsTest.kt Github

copy

Full Screen

1package com.sksamuel.kotest.engine.teamcity2import io.kotest.core.source.SourceRef3import io.kotest.core.spec.style.FunSpec4import io.kotest.engine.teamcity.Locations5import io.kotest.matchers.shouldBe6class LocationsTest : FunSpec({7 test("ClassSource hint") {8 Locations.location(SourceRef.ClassSource("foo.bar", null)) shouldBe "kotest:class://foo.bar:1"9 Locations.location(SourceRef.ClassSource("foo.bar", 34)) shouldBe "kotest:class://foo.bar:34"10 }11 test("FileSource hint") {12 Locations.location(SourceRef.FileSource("foo.kt", null)) shouldBe "kotest:file://foo.kt:1"13 Locations.location(SourceRef.FileSource("foo.kt", 34)) shouldBe "kotest:file://foo.kt:34"14 }15})...

Full Screen

Full Screen

sourceRef

Using AI Code Generation

copy

Full Screen

1 val sourceRef = sourceRef()2 val sourceRef = sourceRef()3 val sourceRef = sourceRef()4 val sourceRef = sourceRef()5 val sourceRef = sourceRef()6 val sourceRef = sourceRef()7 val sourceRef = sourceRef()8 val sourceRef = sourceRef()9 val sourceRef = sourceRef()10 val sourceRef = sourceRef()11 val sourceRef = sourceRef()12 val sourceRef = sourceRef()13 val sourceRef = sourceRef()

Full Screen

Full Screen

sourceRef

Using AI Code Generation

copy

Full Screen

1 val sourceRef = sourceRef(1, 2)2 sourceRef(1, 2)3 val sourceRef = sourceRef(1, 2)4 sourceRef(1, 2)5 val sourceRef = sourceRef(1, 2)6 sourceRef(1, 2)7 val sourceRef = sourceRef(1, 2)8 sourceRef(1, 2)9 val sourceRef = sourceRef(1, 2)10 sourceRef(1, 2)11 val sourceRef = sourceRef(1, 2)12 sourceRef(1, 2)13 val sourceRef = sourceRef(1, 2)14 sourceRef(1, 2)15 val sourceRef = sourceRef(1, 2)16 sourceRef(1, 2)17 val sourceRef = sourceRef(1, 2)

Full Screen

Full Screen

sourceRef

Using AI Code Generation

copy

Full Screen

1val source = sourceRef()2val spec = object : StringSpec({3"test" {4}5})6val source = sourceRef()7val spec = object : StringSpec({8"test" {9}10})

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 sourceRef

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful