Skip to content

Commit

Permalink
Create AdditionalMatchers facade (#508)
Browse files Browse the repository at this point in the history
Fixes #507
  • Loading branch information
paulferaud committed Jan 21, 2024
1 parent 1ce3617 commit 017b08a
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 0 deletions.
@@ -0,0 +1,162 @@
/*
* The MIT License
*
* Copyright (c) 2024 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.AdditionalMatchers
import org.mockito.kotlin.internal.createInstance
import kotlin.reflect.KClass

/** comparable argument greater than or equal the given value. */
inline fun <reified T : Comparable<T>> geq(value: T): T {
return AdditionalMatchers.geq(value) ?: createInstance()
}

/** comparable argument greater than or equal to the given value. */
inline fun <reified T : Comparable<T>> leq(value: T): T {
return AdditionalMatchers.leq(value) ?: createInstance()
}

/** comparable argument greater than the given value. */
inline fun <reified T : Comparable<T>> gt(value: T): T {
return AdditionalMatchers.gt(value) ?: createInstance()
}

/** comparable argument less than the given value. */
inline fun <reified T : Comparable<T>> lt(value: T): T {
return AdditionalMatchers.lt(value) ?: createInstance()
}

/** comparable argument equals to the given value according to their compareTo method. */
inline fun <reified T : Comparable<T>> cmpEq(value: T): T {
return AdditionalMatchers.cmpEq(value) ?: createInstance()
}

/**
* Any array argument that is equal to the given array, i.e. it has to have the same type, length,
* and each element has to be equal.
*/
inline fun <reified T> aryEq(value: Array<T>): Array<T> {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/**
* short array argument that is equal to the given array, i.e. it has to have the same length, and
* each element has to be equal.
*/
fun aryEq(value: ShortArray): ShortArray {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/**
* long array argument that is equal to the given array, i.e. it has to have the same length, and
* each element has to be equal.
*/
fun aryEq(value: LongArray): LongArray {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/**
* int array argument that is equal to the given array, i.e. it has to have the same length, and
* each element has to be equal.
*/
fun aryEq(value: IntArray): IntArray {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/**
* float array argument that is equal to the given array, i.e. it has to have the same length, and
* each element has to be equal.
*/
fun aryEq(value: FloatArray): FloatArray {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/**
* double array argument that is equal to the given array, i.e. it has to have the same length, and
* each element has to be equal.
*/
fun aryEq(value: DoubleArray): DoubleArray {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/**
* char array argument that is equal to the given array, i.e. it has to have the same length, and
* each element has to be equal.
*/
fun aryEq(value: CharArray): CharArray {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/**
* byte array argument that is equal to the given array, i.e. it has to have the same length, and
* each element has to be equal.
*/
fun aryEq(value: ByteArray): ByteArray {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/**
* boolean array argument that is equal to the given array, i.e. it has to have the same length, and
* each element has to be equal.
*/
fun aryEq(value: BooleanArray): BooleanArray {
return AdditionalMatchers.aryEq(value) ?: createInstance()
}

/** String argument that contains a substring that matches the given regular expression. */
fun find(regex: Regex): String {
return AdditionalMatchers.find(regex.pattern) ?: ""
}

/** argument that matches both given argument matchers. */
inline fun <reified T : Any> and(left: T, right: T): T {
return AdditionalMatchers.and(left, right) ?: createInstance()
}

/** argument that matches both given argument matchers. */
inline fun <reified T : Any> or(left: T, right: T): T {
return AdditionalMatchers.or(left, right) ?: createInstance()
}

/** argument that does not match the given argument matcher. */
inline fun <reified T : Any> not(matcher: T): T {
return AdditionalMatchers.not(matcher) ?: createInstance()
}

/**
* float argument that has an absolute difference to the given value that is
* less than the given delta details.
*/
fun eq(value: Double, delta: Double): Double {
return AdditionalMatchers.eq(value, delta) ?: 0.0
}

/**
* double argument that has an absolute difference to the given value that
* is less than the given delta details.
*/
fun eq(value: Float, delta: Float): Float {
return AdditionalMatchers.eq(value, delta) ?: 0.0f
}
108 changes: 108 additions & 0 deletions tests/src/test/kotlin/test/AdditionalMatchersTest.kt
@@ -0,0 +1,108 @@
package test

import org.junit.Test
import org.mockito.kotlin.*

class AdditionalCaptorsTest : TestBase() {

@Test
fun testGeq() {
mock<Methods>().apply {
int(1)
verify(this).int(geq(0))
verify(this).int(geq(1))
verify(this, never()).int(geq(2))
}
}

@Test
fun testLeq() {
mock<Methods>().apply {
int(1)
verify(this).int(leq(2))
verify(this).int(leq(1))
verify(this, never()).int(leq(0))
}
}

@Test
fun testGt() {
mock<Methods>().apply {
int(1)
verify(this).int(gt(0))
verify(this, never()).int(gt(1))
}
}

@Test
fun testLt() {
mock<Methods>().apply {
int(1)
verify(this).int(lt(2))
verify(this, never()).int(lt(1))
}
}

@Test
fun testCmpEq() {
mock<Methods>().apply {
int(1)
verify(this).int(cmpEq(1))
verify(this, never()).int(cmpEq(2))
}
}

@Test
fun testAryEqPrimitive() {
mock<Methods>().apply {
intArray(intArrayOf(1, 2, 3))
verify(this).intArray(aryEq(intArrayOf(1, 2, 3)))
verify(this, never()).intArray(aryEq(intArrayOf(1, 2)))
}
}

@Test
fun testAryEq() {
mock<Methods>().apply {
stringArray(arrayOf("Hello", "there"))
verify(this).stringArray(aryEq(arrayOf("Hello", "there")))
verify(this, never()).stringArray(aryEq(arrayOf("Hello")))
}
}

@Test
fun testfind() {
mock<Methods>().apply {
string("Hello")
verify(this).string(find("l+o$".toRegex()))
verify(this, never()).string(find("l$".toRegex()))
}
}

@Test
fun testAnd() {
mock<Methods>().apply {
int(5)
verify(this).int(and(geq(4), leq(6)))
verify(this, never()).int(and(geq(4), leq(4)))
}
}

@Test
fun testOr() {
mock<Methods>().apply {
int(5)
verify(this).int(and(gt(4), lt(6)))
verify(this, never()).int(and(gt(4), lt(4)))
}
}

@Test
fun testNot() {
mock<Methods>().apply {
int(5)
verify(this).int(not(eq(4)))
verify(this, never()).int(not(eq(5)))
}
}
}

0 comments on commit 017b08a

Please sign in to comment.