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

jvmMain.io.kotest.matchers.reflection.functionMatchers.kt Maven / Gradle / Ivy

package io.kotest.matchers.reflection

import io.kotest.matchers.Matcher
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.kotest.matchers.shouldNot
import kotlin.reflect.KFunction
import kotlin.reflect.full.findAnnotation

fun KFunction<*>.shouldHaveAnnotations() = this should haveFunctionAnnotations()
fun KFunction<*>.shouldNotHaveAnnotations() = this shouldNot haveFunctionAnnotations()
infix fun KFunction<*>.shouldHaveAnnotations(count: Int) = this should haveFunctionAnnotations(count)
infix fun KFunction<*>.shouldNotHaveAnnotations(count: Int) = this shouldNot haveFunctionAnnotations(count)
fun haveFunctionAnnotations(count: Int = -1) = object : Matcher> {
  override fun test(value: KFunction<*>) = if (count < 0) {
     MatcherResult(
        value.annotations.isNotEmpty(),
        { "Function $value should have annotations" },
        {
           "Function $value should not have annotations"
        })
  } else {
     MatcherResult(
        value.annotations.size == count,
        { "Function $value should have $count annotations" },
        {
           "Function $value should not have $count annotations"
        })
  }
}

inline fun  KFunction<*>.shouldBeAnnotatedWith(block: (T) -> Unit = {}) {
  this should beAnnotatedWith()
  findAnnotation()?.let(block)
}

inline fun  KFunction<*>.shouldNotBeAnnotatedWith() = this shouldNot beAnnotatedWith()
inline fun  beAnnotatedWith() = object : Matcher> {
   override fun test(value: KFunction<*>) = MatcherResult(
      value.findAnnotation() != null,
      { "Function $value should have annotation ${T::class}" },
      {
         "Function $value should not have annotation ${T::class}"
      })
}

inline fun  KFunction<*>.shouldHaveReturnType() = this.returnType.shouldBeOfType()
inline fun  KFunction<*>.shouldNotHaveReturnType() = this.returnType.shouldNotBeOfType()

fun KFunction<*>.shouldBeInline() = this should beInline()
fun KFunction<*>.shouldNotBeInline() = this shouldNot beInline()
fun beInline() = object : Matcher> {
   override fun test(value: KFunction<*>) = MatcherResult(
      value.isInline,
      { "Function $value should be inline" },
      {
         "Function $value should not be inline"
      })
}

fun KFunction<*>.shouldBeInfix() = this should beInfix()
fun KFunction<*>.shouldNotBeInfix() = this shouldNot beInfix()
fun beInfix() = object : Matcher> {
   override fun test(value: KFunction<*>) = MatcherResult(
      value.isInfix,
      { "Function $value should be infix" },
      {
         "Function $value should not be infix"
      })
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy