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

org.mockito.kotlin.Matchers.kt Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License
 *
 * Copyright (c) 2018 Niek Haarman
 * Copyright (c) 2007 Mockito contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.mockito.kotlin

import org.mockito.ArgumentMatcher
import org.mockito.ArgumentMatchers
import org.mockito.kotlin.internal.createInstance
import kotlin.reflect.KClass

/** Object argument that is equal to the given value. */
fun  eq(value: T): T {
    return ArgumentMatchers.eq(value) ?: value
}

/**  Object argument that is the same as the given value. */
fun  same(value: T): T {
    return ArgumentMatchers.same(value) ?: value
}

/** Matches any object, excluding nulls. */
inline fun  any(): T {
    if(T::class.isValue)
        return anyValueClass()

    return ArgumentMatchers.any(T::class.java) ?: createInstance()
}

/** Matches anything, including nulls. */
inline fun  anyOrNull(): T {
    return ArgumentMatchers.any() ?: createInstance()
}

/** Matches any vararg object, including nulls. */
inline fun  anyVararg(): T {
    return anyVararg(T::class)
}

fun  anyVararg(clazz: KClass): T {
    return ArgumentMatchers.argThat(VarargMatcher(clazz.java)) ?: createInstance(clazz)
}

private class VarargMatcher(private val clazz: Class) : ArgumentMatcher {
    override fun matches(t: T): Boolean = true

    // In Java >= 12 you can do clazz.arrayClass()
    override fun type(): Class<*> = java.lang.reflect.Array.newInstance(clazz, 0).javaClass
}

/** Matches any array of type T. */
inline fun  anyArray(): Array {
    return ArgumentMatchers.any(Array::class.java) ?: arrayOf()
}

/** Matches any Kotlin value class with the same boxed type by taking its boxed type. */
inline fun  anyValueClass(): T {
    require(T::class.isValue) {
        "${T::class.qualifiedName} is not a value class."
    }

    val boxImpls = T::class.java.declaredMethods.filter { it.name == "box-impl" && it.parameterCount == 1 }
    require(boxImpls.size == 1) // Sanity check

    val boxImpl = boxImpls.first()
    val boxedType = boxImpl.parameters[0].type

    return boxImpl.invoke(null, ArgumentMatchers.any(boxedType)) as T
}

/**
 * Creates a custom argument matcher.
 * `null` values will never evaluate to `true`.
 *
 * @param predicate An extension function on [T] that returns `true` when a [T] matches the predicate.
 */
inline fun  argThat(noinline predicate: T.() -> Boolean): T {
    return ArgumentMatchers.argThat { arg: T? -> arg?.predicate() ?: false } ?: createInstance(
          T::class
    )
}

/**
 * Registers a custom ArgumentMatcher. The original Mockito function registers the matcher and returns null,
 * here the required type is returned.
 *
 * @param matcher The ArgumentMatcher on [T] to be registered.
 */
inline fun  argThat(matcher: ArgumentMatcher): T {
    return ArgumentMatchers.argThat(matcher) ?: createInstance()
}

/**
 * Alias for [argThat].
 *
 * Creates a custom argument matcher.
 * `null` values will never evaluate to `true`.
 *
 * @param predicate An extension function on [T] that returns `true` when a [T] matches the predicate.
 */
inline fun  argForWhich(noinline predicate: T.() -> Boolean): T {
    return argThat(predicate)
}

/**
 * Creates a custom argument matcher.
 * `null` values will never evaluate to `true`.
 *
 * @param predicate A function that returns `true` when given [T] matches the predicate.
 */
inline fun  argWhere(noinline predicate: (T) -> Boolean): T {
    return argThat(predicate)
}

/**
 * Argument that implements the given class.
 */
inline fun  isA(): T {
    return ArgumentMatchers.isA(T::class.java) ?: createInstance()
}

/**
 * `null` argument.
 */
fun  isNull(): T? = ArgumentMatchers.isNull()

/**
 * Not `null` argument.
 */
fun  isNotNull(): T? {
    return ArgumentMatchers.isNotNull()
}

/**
 * Not `null` argument.
 */
fun  notNull(): T? {
    return ArgumentMatchers.notNull()
}

/**
 * Object argument that is reflection-equal to the given value with support for excluding
 * selected fields from a class.
 */
inline fun  refEq(value: T, vararg excludeFields: String): T {
    return ArgumentMatchers.refEq(value, *excludeFields) ?: createInstance()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy