1 /*
<lambda>null2 * The MIT License
3 *
4 * Copyright (c) 2018 Niek Haarman
5 * Copyright (c) 2007 Mockito contributors
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 */
25
26 package org.mockito.kotlin
27
28 import org.mockito.kotlin.internal.createInstance
29 import org.mockito.ArgumentMatcher
30 import org.mockito.Mockito
31
32 /** Object argument that is equal to the given value. */
33 fun <T> eq(value: T): T {
34 return Mockito.eq(value) ?: value
35 }
36
37 /** Object argument that is the same as the given value. */
samenull38 fun <T> same(value: T): T {
39 return Mockito.same(value) ?: value
40 }
41
42 /** Matches any object, excluding nulls. */
anynull43 inline fun <reified T : Any> any(): T {
44 return Mockito.any(T::class.java) ?: createInstance()
45 }
46
47 /** Matches anything, including nulls. */
anyOrNullnull48 inline fun <reified T : Any> anyOrNull(): T {
49 return Mockito.any<T>() ?: createInstance()
50 }
51
52 /** Matches any vararg object, including nulls. */
anyVarargnull53 inline fun <reified T : Any> anyVararg(): T {
54 return Mockito.any<T>() ?: createInstance()
55 }
56
57 /** Matches any array of type T. */
anyArraynull58 inline fun <reified T : Any?> anyArray(): Array<T> {
59 return Mockito.any(Array<T>::class.java) ?: arrayOf()
60 }
61
62 /**
63 * Creates a custom argument matcher.
64 * `null` values will never evaluate to `true`.
65 *
66 * @param predicate An extension function on [T] that returns `true` when a [T] matches the predicate.
67 */
argThatnull68 inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean): T {
69 return Mockito.argThat { arg: T? -> arg?.predicate() ?: false } ?: createInstance(
70 T::class
71 )
72 }
73
74 /**
75 * Registers a custom ArgumentMatcher. The original Mockito function registers the matcher and returns null,
76 * here the required type is returned.
77 *
78 * @param matcher The ArgumentMatcher on [T] to be registered.
79 */
argThatnull80 inline fun <reified T : Any> argThat(matcher: ArgumentMatcher<T>): T {
81 return Mockito.argThat(matcher) ?: createInstance()
82 }
83
84 /**
85 * Alias for [argThat].
86 *
87 * Creates a custom argument matcher.
88 * `null` values will never evaluate to `true`.
89 *
90 * @param predicate An extension function on [T] that returns `true` when a [T] matches the predicate.
91 */
argForWhichnull92 inline fun <reified T : Any> argForWhich(noinline predicate: T.() -> Boolean): T {
93 return argThat(predicate)
94 }
95
96 /**
97 * Creates a custom argument matcher.
98 * `null` values will never evaluate to `true`.
99 *
100 * @param predicate A function that returns `true` when given [T] matches the predicate.
101 */
argWherenull102 inline fun <reified T : Any> argWhere(noinline predicate: (T) -> Boolean): T {
103 return argThat(predicate)
104 }
105
106 /**
107 * Argument that implements the given class.
108 */
isAnull109 inline fun <reified T : Any> isA(): T {
110 return Mockito.isA(T::class.java) ?: createInstance()
111 }
112
113 /**
114 * `null` argument.
115 */
isNullnull116 fun <T : Any> isNull(): T? = Mockito.isNull()
117
118 /**
119 * Not `null` argument.
120 */
121 fun <T : Any> isNotNull(): T? {
122 return Mockito.isNotNull()
123 }
124
125 /**
126 * Not `null` argument.
127 */
notNullnull128 fun <T : Any> notNull(): T? {
129 return Mockito.notNull()
130 }
131
132 /**
133 * Object argument that is reflection-equal to the given value with support for excluding
134 * selected fields from a class.
135 */
refEqnull136 inline fun <reified T : Any> refEq(value: T, vararg excludeFields: String): T {
137 return Mockito.refEq<T>(value, *excludeFields) ?: createInstance()
138 }
139
140