Skip to content

Commit

Permalink
Added more isolated linux tests (#4413)
Browse files Browse the repository at this point in the history
<!-- 
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_
-->
  • Loading branch information
sksamuel authored Oct 2, 2024
1 parent 95361c2 commit cfdca42
Show file tree
Hide file tree
Showing 87 changed files with 921 additions and 550 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@ package com.sksamuel.kotest

import io.kotest.assertions.extracting
import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.collections.shouldContainAll
import io.kotest.matchers.shouldBe

@EnabledIf(LinuxCondition::class)
class ExtractTest : WordSpec() {
init {
init {

data class Person(val name: String, val age: Int, val friends: List<Person>)
data class Person(val name: String, val age: Int, val friends: List<Person>)

val p1 = Person("John Doe", 20, emptyList())
val p2 = Person("Samantha Rose", 19, listOf(p1))
val persons = listOf(p1, p2)
val p1 = Person("John Doe", 20, emptyList())
val p2 = Person("Samantha Rose", 19, listOf(p1))
val persons = listOf(p1, p2)

"extracting" should {
"extract simple properties"{
extracting(persons) { name }
.shouldContainAll("John Doe", "Samantha Rose")
}
"extracting" should {
"extract simple properties" {
extracting(persons) { name }
.shouldContainAll("John Doe", "Samantha Rose")
}

"extract complex properties"{
extracting(persons) { Pair(name, age) }
.shouldContainAll(
Pair("John Doe", 20),
Pair("Samantha Rose", 19)
)
}
"fail if the matcher fails"{
shouldThrowAny {
extracting(persons) { name }
.shouldContainAll("<Some name that is wrong>")
}.message shouldBe """Collection should contain all of ["<Some name that is wrong>"] but was missing ["<Some name that is wrong>"]"""
}
"extract complex properties" {
extracting(persons) { Pair(name, age) }
.shouldContainAll(
Pair("John Doe", 20),
Pair("Samantha Rose", 19)
)
}
"fail if the matcher fails" {
shouldThrowAny {
extracting(persons) { name }
.shouldContainAll("<Some name that is wrong>")
}.message shouldBe """Collection should contain all of ["<Some name that is wrong>"] but was missing ["<Some name that is wrong>"]"""
}

}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import io.kotest.assertions.failure
import io.kotest.assertions.print.Printed
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldStartWith
import io.kotest.matchers.types.shouldBeInstanceOf
import org.opentest4j.AssertionFailedError

@EnabledIf(LinuxCondition::class)
class FailuresTest : StringSpec({

"failure(msg) should create a AssertionError on the JVM" {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sksamuel.kotest

import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.FunSpec
import io.kotest.inspectors.shouldForAll
import io.kotest.inspectors.shouldForAny
Expand All @@ -16,6 +18,7 @@ import io.kotest.matchers.comparables.shouldBeGreaterThan
import io.kotest.matchers.ints.shouldBeLessThan
import io.kotest.matchers.shouldBe

@EnabledIf(LinuxCondition::class)
class InspectorAliasTest : FunSpec({

val array = arrayOf(1, 2, 3)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sksamuel.kotest

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.WordSpec
import io.kotest.inspectors.*
import io.kotest.matchers.comparables.beGreaterThan
Expand All @@ -9,6 +11,7 @@ import io.kotest.matchers.should
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe

@EnabledIf(LinuxCondition::class)
class InspectorsTest : WordSpec() {

private val list = listOf(1, 2, 3, 4, 5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,48 @@ package com.sksamuel.kotest

import io.kotest.assertions.throwables.shouldThrow
import io.kotest.common.nonConstantTrue
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.WordSpec
import io.kotest.matchers.shouldBe

@EnabledIf(LinuxCondition::class)
class NullTests : WordSpec() {

// don't want compiler to compile this away
private fun getNull(): String? = if (nonConstantTrue()) null else throw RuntimeException()
// don't want compiler to compile this away
private fun getNull(): String? = if (nonConstantTrue()) null else throw RuntimeException()

private fun notNull(): String? = if (nonConstantTrue()) "qwerty" else throw RuntimeException()
private fun notNull(): String? = if (nonConstantTrue()) "qwerty" else throw RuntimeException()

init {
init {

"null" should {
"not match value" {
shouldThrow<AssertionError> {
getNull() shouldBe "q"
}
"null" should {
"not match value" {
shouldThrow<AssertionError> {
getNull() shouldBe "q"
}
}
"match null" {
getNull() shouldBe null
}
"match null variable when equal operation is override" {
val g: A? = null
A(0) shouldBe g
}
}
"match null" {
getNull() shouldBe null
"not null" should {
"match value" {
notNull() shouldBe "qwerty"
}
"not match null" {
shouldThrow<AssertionError> {
notNull() shouldBe null
}
}
}
"match null variable when equal operation is override" {
val g: A? = null
A(0) shouldBe g
}
}
"not null" should {
"match value" {
notNull() shouldBe "qwerty"
}
"not match null" {
shouldThrow<AssertionError> {
notNull() shouldBe null
}
}
}
}
}
}

private class A(var i: Int) {
override fun equals(other: Any?): Boolean = other == null && i == 0
override fun equals(other: Any?): Boolean = other == null && i == 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package com.sksamuel.kotest

import com.sksamuel.kotest.throwablehandling.catchThrowable
import io.kotest.assertions.shouldFail
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf

@EnabledIf(LinuxCondition::class)
class ShouldFailTest : FreeSpec({

"shouldFail" - {
"Should throw an exception when code succeeds" {
val t = catchThrowable { shouldFail { /* Code succeeds */ } }
t.shouldBeInstanceOf<AssertionError>()
t.shouldBeInstanceOf<AssertionError>()
t.message shouldBe "Expected exception java.lang.AssertionError but no exception was thrown."
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.sksamuel.kotest.assertions

import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.FreeSpec
import io.kotest.core.test.AssertionMode
import io.kotest.matchers.shouldBe

@EnabledIf(LinuxCondition::class)
class AssertionCounterFreeSpecTest : FreeSpec({
assertions = AssertionMode.Error
"container should not need to have an assertion" - {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.sksamuel.kotest.assertions

import io.kotest.assertions.assertionCounter
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.extensions.Extension
import io.kotest.core.extensions.TestCaseExtension
import io.kotest.core.spec.style.FunSpec
Expand All @@ -10,6 +12,7 @@ import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.kotest.matchers.shouldBe

@EnabledIf(LinuxCondition::class)
class AssertionCounterFunSpecTest : FunSpec() {

override fun assertionMode() = AssertionMode.Error
Expand All @@ -21,9 +24,13 @@ class AssertionCounterFunSpecTest : FunSpec() {
"AssertionMode.Error assertion mode should fail the test if no assertions were present" -> {
when (val result = execute(testCase)) {
is TestResult.Error, is TestResult.Failure -> TestResult.Success(result.duration)
else -> TestResult.Error(result.duration, RuntimeException("Should have failed: ${testCase.name.testName}"))
else -> TestResult.Error(
result.duration,
RuntimeException("Should have failed: ${testCase.name.testName}")
)
}
}

else -> execute(testCase)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package com.sksamuel.kotest.assertions

import io.kotest.assertions.assertionCounter
import io.kotest.assertions.assertionCounterContextElement
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.collections.shouldHaveSize
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.withContext

@EnabledIf(LinuxCondition::class)
class AssertionCounterMultithreadingTests : FunSpec({
test("assertionCounter should work across coroutine thread switch") {
withContext(Dispatchers.Unconfined + assertionCounterContextElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.kotest.assertions.assertSoftly
import io.kotest.assertions.fail
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.withClue
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.FreeSpec
import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
Expand All @@ -14,6 +16,7 @@ import io.kotest.matchers.string.shouldStartWith
import kotlinx.coroutines.delay
import kotlinx.coroutines.withTimeout

@EnabledIf(LinuxCondition::class)
class ClueTest : FreeSpec({

"withClue()" - {
Expand Down Expand Up @@ -164,12 +167,13 @@ class ClueTest : FreeSpec({
}

data class HttpResponse(val status: Int, val body: String)

val response = HttpResponse(404, "not found")
response.asClue {
shouldThrow<AssertionError> {it.status shouldBe 200}.message shouldBe "HttpResponse(status=404, body=not found)\nexpected:<200> but was:<404>"
shouldThrow<AssertionError> { it.status shouldBe 200 }.message shouldBe "HttpResponse(status=404, body=not found)\nexpected:<200> but was:<404>"
MyData(20, "nest it").asClue { inner ->
shouldThrow<AssertionError> {it.status shouldBe 200}.message shouldBe "HttpResponse(status=404, body=not found)\nMyData(a=20, b=nest it)\nexpected:<200> but was:<404>"
shouldThrow<AssertionError> {inner.a shouldBe 10}.message shouldBe "HttpResponse(status=404, body=not found)\nMyData(a=20, b=nest it)\nexpected:<10> but was:<20>"
shouldThrow<AssertionError> { it.status shouldBe 200 }.message shouldBe "HttpResponse(status=404, body=not found)\nMyData(a=20, b=nest it)\nexpected:<200> but was:<404>"
shouldThrow<AssertionError> { inner.a shouldBe 10 }.message shouldBe "HttpResponse(status=404, body=not found)\nMyData(a=20, b=nest it)\nexpected:<10> but was:<20>"
}
//after nesting, everything looks as before
shouldThrow<AssertionError> { it.status shouldBe 200 }.message shouldBe "HttpResponse(status=404, body=not found)\nexpected:<200> but was:<404>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.kotest.assertions.retryConfig
import io.kotest.assertions.throwables.shouldNotThrow
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.common.nonDeterministicTestTimeSource
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.collections.shouldBeEmpty
import io.kotest.matchers.collections.shouldContainExactly
Expand All @@ -16,6 +18,7 @@ import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.TimeMark

@EnabledIf(LinuxCondition::class)
class RetryTest : StringSpec() {
init {
coroutineTestScope = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.sksamuel.kotest.data

import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.FunSpec
import io.kotest.data.forAll
import io.kotest.data.row
import io.kotest.matchers.string.shouldContain

@EnabledIf(LinuxCondition::class)
class DataTestExceptionTest : FunSpec({
test("failure in forAll should keep original stack trace") {
val t = shouldThrowAny {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import io.kotest.assertions.MultiAssertionError
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.assertions.throwables.shouldThrowAny
import io.kotest.core.annotation.EnabledIf
import io.kotest.core.annotation.enabledif.LinuxCondition
import io.kotest.core.spec.style.StringSpec
import io.kotest.data.forAll
import io.kotest.data.forNone
Expand All @@ -18,6 +20,7 @@ import io.kotest.matchers.string.contain
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.types.shouldNotBeInstanceOf

@EnabledIf(LinuxCondition::class)
class DataTestingTest : StringSpec() {
init {

Expand Down Expand Up @@ -217,7 +220,7 @@ class DataTestingTest : StringSpec() {
"display error message in readable format using" {
val errorMessage = shouldThrow<AssertionError> {
forAll(
row(intArrayOf(2,3,1,1,4), 2),
row(intArrayOf(2, 3, 1, 1, 4), 2),
row(intArrayOf(0), 0),
) { nums, result ->
nums shouldBe result
Expand Down
Loading

0 comments on commit cfdca42

Please sign in to comment.