Skip to content

Commit 017b08a

Browse files
authored
Create AdditionalMatchers facade (#508)
Fixes #507
1 parent 1ce3617 commit 017b08a

File tree

2 files changed

+270
-0
lines changed

2 files changed

+270
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* The MIT License
3+
*
4+
* Copyright (c) 2024 Mockito contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package org.mockito.kotlin
26+
27+
import org.mockito.AdditionalMatchers
28+
import org.mockito.kotlin.internal.createInstance
29+
import kotlin.reflect.KClass
30+
31+
/** comparable argument greater than or equal the given value. */
32+
inline fun <reified T : Comparable<T>> geq(value: T): T {
33+
return AdditionalMatchers.geq(value) ?: createInstance()
34+
}
35+
36+
/** comparable argument greater than or equal to the given value. */
37+
inline fun <reified T : Comparable<T>> leq(value: T): T {
38+
return AdditionalMatchers.leq(value) ?: createInstance()
39+
}
40+
41+
/** comparable argument greater than the given value. */
42+
inline fun <reified T : Comparable<T>> gt(value: T): T {
43+
return AdditionalMatchers.gt(value) ?: createInstance()
44+
}
45+
46+
/** comparable argument less than the given value. */
47+
inline fun <reified T : Comparable<T>> lt(value: T): T {
48+
return AdditionalMatchers.lt(value) ?: createInstance()
49+
}
50+
51+
/** comparable argument equals to the given value according to their compareTo method. */
52+
inline fun <reified T : Comparable<T>> cmpEq(value: T): T {
53+
return AdditionalMatchers.cmpEq(value) ?: createInstance()
54+
}
55+
56+
/**
57+
* Any array argument that is equal to the given array, i.e. it has to have the same type, length,
58+
* and each element has to be equal.
59+
*/
60+
inline fun <reified T> aryEq(value: Array<T>): Array<T> {
61+
return AdditionalMatchers.aryEq(value) ?: createInstance()
62+
}
63+
64+
/**
65+
* short array argument that is equal to the given array, i.e. it has to have the same length, and
66+
* each element has to be equal.
67+
*/
68+
fun aryEq(value: ShortArray): ShortArray {
69+
return AdditionalMatchers.aryEq(value) ?: createInstance()
70+
}
71+
72+
/**
73+
* long array argument that is equal to the given array, i.e. it has to have the same length, and
74+
* each element has to be equal.
75+
*/
76+
fun aryEq(value: LongArray): LongArray {
77+
return AdditionalMatchers.aryEq(value) ?: createInstance()
78+
}
79+
80+
/**
81+
* int array argument that is equal to the given array, i.e. it has to have the same length, and
82+
* each element has to be equal.
83+
*/
84+
fun aryEq(value: IntArray): IntArray {
85+
return AdditionalMatchers.aryEq(value) ?: createInstance()
86+
}
87+
88+
/**
89+
* float array argument that is equal to the given array, i.e. it has to have the same length, and
90+
* each element has to be equal.
91+
*/
92+
fun aryEq(value: FloatArray): FloatArray {
93+
return AdditionalMatchers.aryEq(value) ?: createInstance()
94+
}
95+
96+
/**
97+
* double array argument that is equal to the given array, i.e. it has to have the same length, and
98+
* each element has to be equal.
99+
*/
100+
fun aryEq(value: DoubleArray): DoubleArray {
101+
return AdditionalMatchers.aryEq(value) ?: createInstance()
102+
}
103+
104+
/**
105+
* char array argument that is equal to the given array, i.e. it has to have the same length, and
106+
* each element has to be equal.
107+
*/
108+
fun aryEq(value: CharArray): CharArray {
109+
return AdditionalMatchers.aryEq(value) ?: createInstance()
110+
}
111+
112+
/**
113+
* byte array argument that is equal to the given array, i.e. it has to have the same length, and
114+
* each element has to be equal.
115+
*/
116+
fun aryEq(value: ByteArray): ByteArray {
117+
return AdditionalMatchers.aryEq(value) ?: createInstance()
118+
}
119+
120+
/**
121+
* boolean array argument that is equal to the given array, i.e. it has to have the same length, and
122+
* each element has to be equal.
123+
*/
124+
fun aryEq(value: BooleanArray): BooleanArray {
125+
return AdditionalMatchers.aryEq(value) ?: createInstance()
126+
}
127+
128+
/** String argument that contains a substring that matches the given regular expression. */
129+
fun find(regex: Regex): String {
130+
return AdditionalMatchers.find(regex.pattern) ?: ""
131+
}
132+
133+
/** argument that matches both given argument matchers. */
134+
inline fun <reified T : Any> and(left: T, right: T): T {
135+
return AdditionalMatchers.and(left, right) ?: createInstance()
136+
}
137+
138+
/** argument that matches both given argument matchers. */
139+
inline fun <reified T : Any> or(left: T, right: T): T {
140+
return AdditionalMatchers.or(left, right) ?: createInstance()
141+
}
142+
143+
/** argument that does not match the given argument matcher. */
144+
inline fun <reified T : Any> not(matcher: T): T {
145+
return AdditionalMatchers.not(matcher) ?: createInstance()
146+
}
147+
148+
/**
149+
* float argument that has an absolute difference to the given value that is
150+
* less than the given delta details.
151+
*/
152+
fun eq(value: Double, delta: Double): Double {
153+
return AdditionalMatchers.eq(value, delta) ?: 0.0
154+
}
155+
156+
/**
157+
* double argument that has an absolute difference to the given value that
158+
* is less than the given delta details.
159+
*/
160+
fun eq(value: Float, delta: Float): Float {
161+
return AdditionalMatchers.eq(value, delta) ?: 0.0f
162+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package test
2+
3+
import org.junit.Test
4+
import org.mockito.kotlin.*
5+
6+
class AdditionalCaptorsTest : TestBase() {
7+
8+
@Test
9+
fun testGeq() {
10+
mock<Methods>().apply {
11+
int(1)
12+
verify(this).int(geq(0))
13+
verify(this).int(geq(1))
14+
verify(this, never()).int(geq(2))
15+
}
16+
}
17+
18+
@Test
19+
fun testLeq() {
20+
mock<Methods>().apply {
21+
int(1)
22+
verify(this).int(leq(2))
23+
verify(this).int(leq(1))
24+
verify(this, never()).int(leq(0))
25+
}
26+
}
27+
28+
@Test
29+
fun testGt() {
30+
mock<Methods>().apply {
31+
int(1)
32+
verify(this).int(gt(0))
33+
verify(this, never()).int(gt(1))
34+
}
35+
}
36+
37+
@Test
38+
fun testLt() {
39+
mock<Methods>().apply {
40+
int(1)
41+
verify(this).int(lt(2))
42+
verify(this, never()).int(lt(1))
43+
}
44+
}
45+
46+
@Test
47+
fun testCmpEq() {
48+
mock<Methods>().apply {
49+
int(1)
50+
verify(this).int(cmpEq(1))
51+
verify(this, never()).int(cmpEq(2))
52+
}
53+
}
54+
55+
@Test
56+
fun testAryEqPrimitive() {
57+
mock<Methods>().apply {
58+
intArray(intArrayOf(1, 2, 3))
59+
verify(this).intArray(aryEq(intArrayOf(1, 2, 3)))
60+
verify(this, never()).intArray(aryEq(intArrayOf(1, 2)))
61+
}
62+
}
63+
64+
@Test
65+
fun testAryEq() {
66+
mock<Methods>().apply {
67+
stringArray(arrayOf("Hello", "there"))
68+
verify(this).stringArray(aryEq(arrayOf("Hello", "there")))
69+
verify(this, never()).stringArray(aryEq(arrayOf("Hello")))
70+
}
71+
}
72+
73+
@Test
74+
fun testfind() {
75+
mock<Methods>().apply {
76+
string("Hello")
77+
verify(this).string(find("l+o$".toRegex()))
78+
verify(this, never()).string(find("l$".toRegex()))
79+
}
80+
}
81+
82+
@Test
83+
fun testAnd() {
84+
mock<Methods>().apply {
85+
int(5)
86+
verify(this).int(and(geq(4), leq(6)))
87+
verify(this, never()).int(and(geq(4), leq(4)))
88+
}
89+
}
90+
91+
@Test
92+
fun testOr() {
93+
mock<Methods>().apply {
94+
int(5)
95+
verify(this).int(and(gt(4), lt(6)))
96+
verify(this, never()).int(and(gt(4), lt(4)))
97+
}
98+
}
99+
100+
@Test
101+
fun testNot() {
102+
mock<Methods>().apply {
103+
int(5)
104+
verify(this).int(not(eq(4)))
105+
verify(this, never()).int(not(eq(5)))
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)