All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.io.kotest.matchers.should.kt Maven / Gradle / Ivy

package io.kotest.matchers

import io.kotest.assertions.AssertionCounter
import io.kotest.assertions.ErrorCollector
import io.kotest.assertions.collectOrThrow
import io.kotest.assertions.eq.eq
import io.kotest.assertions.failure
import io.kotest.assertions.intellijFormatError
import io.kotest.assertions.show.show

@Suppress("UNCHECKED_CAST")
infix fun  T.shouldBe(expected: U?) {
   when (expected) {
      is Matcher<*> -> should(expected as Matcher)
      else -> {
         val actual = this
         AssertionCounter.inc()
         // if we have null and non null, usually that's a failure, but people can override equals to allow it
         if (actual == null && expected != null && actual != expected) {
            ErrorCollector.collectOrThrow(actualIsNull(expected))
         } else if (actual != null && expected == null && actual != expected) {
            ErrorCollector.collectOrThrow(expectedIsNull(actual))
         } else if (actual != null && expected != null) {
            // for two non-null values, we use the [eq] typeclass.
            val t = eq(actual, expected)
            if (t != null)
               ErrorCollector.collectOrThrow(t)
         }
      }
   }
}

private fun actualIsNull(expected: Any): AssertionError {
   return AssertionError("Expected ${expected.show().value} but actual was null")
}

private fun expectedIsNull(actual: Any): AssertionError {
   return AssertionError("Expected null but actual was ${actual.show().value}")
}

@Suppress("UNCHECKED_CAST")
infix fun  T.shouldNotBe(any: Any?) {
   when (any) {
      is Matcher<*> -> shouldNot(any as Matcher)
      else -> shouldNot(equalityMatcher(any))
   }
}

infix fun  T.shouldHave(matcher: Matcher) = should(matcher)
infix fun  T.should(matcher: Matcher) {
   AssertionCounter.inc()
   val result = matcher.test(this)
   if (!result.passed()) {
      ErrorCollector.collectOrThrow(failure(result.failureMessage()))
   }
}

infix fun  T.shouldNotHave(matcher: Matcher) = shouldNot(matcher)
infix fun  T.shouldNot(matcher: Matcher) = should(matcher.invert())

infix fun  T.should(matcher: (T) -> Unit) = matcher(this)

fun  be(expected: T) = equalityMatcher(expected)
fun  equalityMatcher(expected: T) = object : Matcher {
   override fun test(value: T): MatcherResult {
      val t = if (value == null && expected == null) {
         null
      } else if (value == null && expected != null) {
         actualIsNull(expected)
      } else if (value != null && expected == null) {
         expectedIsNull(value)
      } else {
         eq(value, expected)
      }
      return MatcherResult(
         t == null,
         { failure(expected.show(), value.show()).message ?: intellijFormatError(expected.show(), value.show()) },
         { "${expected.show().value} should not equal ${value.show().value}" }
      )
   }
}







© 2015 - 2025 Weber Informatics LLC | Privacy Policy