diff --git a/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/AdditionalMatchers.kt b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/AdditionalMatchers.kt new file mode 100644 index 00000000..1889b430 --- /dev/null +++ b/mockito-kotlin/src/main/kotlin/org/mockito/kotlin/AdditionalMatchers.kt @@ -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 > geq(value: T): T { + return AdditionalMatchers.geq(value) ?: createInstance() +} + +/** comparable argument greater than or equal to the given value. */ +inline fun > leq(value: T): T { + return AdditionalMatchers.leq(value) ?: createInstance() +} + +/** comparable argument greater than the given value. */ +inline fun > gt(value: T): T { + return AdditionalMatchers.gt(value) ?: createInstance() +} + +/** comparable argument less than the given value. */ +inline fun > lt(value: T): T { + return AdditionalMatchers.lt(value) ?: createInstance() +} + +/** comparable argument equals to the given value according to their compareTo method. */ +inline fun > 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 aryEq(value: Array): Array { + 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 and(left: T, right: T): T { + return AdditionalMatchers.and(left, right) ?: createInstance() +} + +/** argument that matches both given argument matchers. */ +inline fun or(left: T, right: T): T { + return AdditionalMatchers.or(left, right) ?: createInstance() +} + +/** argument that does not match the given argument matcher. */ +inline fun 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 +} diff --git a/tests/src/test/kotlin/test/AdditionalMatchersTest.kt b/tests/src/test/kotlin/test/AdditionalMatchersTest.kt new file mode 100644 index 00000000..4036f7c1 --- /dev/null +++ b/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().apply { + int(1) + verify(this).int(geq(0)) + verify(this).int(geq(1)) + verify(this, never()).int(geq(2)) + } + } + + @Test + fun testLeq() { + mock().apply { + int(1) + verify(this).int(leq(2)) + verify(this).int(leq(1)) + verify(this, never()).int(leq(0)) + } + } + + @Test + fun testGt() { + mock().apply { + int(1) + verify(this).int(gt(0)) + verify(this, never()).int(gt(1)) + } + } + + @Test + fun testLt() { + mock().apply { + int(1) + verify(this).int(lt(2)) + verify(this, never()).int(lt(1)) + } + } + + @Test + fun testCmpEq() { + mock().apply { + int(1) + verify(this).int(cmpEq(1)) + verify(this, never()).int(cmpEq(2)) + } + } + + @Test + fun testAryEqPrimitive() { + mock().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().apply { + stringArray(arrayOf("Hello", "there")) + verify(this).stringArray(aryEq(arrayOf("Hello", "there"))) + verify(this, never()).stringArray(aryEq(arrayOf("Hello"))) + } + } + + @Test + fun testfind() { + mock().apply { + string("Hello") + verify(this).string(find("l+o$".toRegex())) + verify(this, never()).string(find("l$".toRegex())) + } + } + + @Test + fun testAnd() { + mock().apply { + int(5) + verify(this).int(and(geq(4), leq(6))) + verify(this, never()).int(and(geq(4), leq(4))) + } + } + + @Test + fun testOr() { + mock().apply { + int(5) + verify(this).int(and(gt(4), lt(6))) + verify(this, never()).int(and(gt(4), lt(4))) + } + } + + @Test + fun testNot() { + mock().apply { + int(5) + verify(this).int(not(eq(4))) + verify(this, never()).int(not(eq(5))) + } + } +}