Skip to content
This repository was archived by the owner on Jan 3, 2025. It is now read-only.

Commit f6426d2

Browse files
committed
add assertions for arrayEquals and iterableEquals
1 parent 2b01018 commit f6426d2

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

src/main/kotlin/org/amshove/kluent/Assertions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import kotlin.reflect.KClass
66
infix fun Any.shouldEqual(theOther: Any) = this `should equal` theOther
77
infix fun Any.shouldNotEqual(theOther: Any) = this `should not equal` theOther
88

9+
infix fun <T> Array<T>.shouldEqual(theOther: Array<T>) = this `should equal` theOther
10+
infix fun <T> Iterable<T>.shouldEqual(theOther: Iterable<T>) = this `should equal` theOther
11+
912
infix fun Any.shouldBe(theOther: Any) = this `should be` theOther
1013
infix fun Any.shouldNotBe(theOther: Any) = this `should not be` theOther
1114

src/main/kotlin/org/amshove/kluent/BacktickAssertions.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ package org.amshove.kluent
33
import org.junit.ComparisonFailure
44
import kotlin.reflect.KClass
55
import org.junit.Assert.*
6+
import java.util.*
67

78
infix fun Any.`should equal`(theOther: Any) = assertEquals(theOther, this)
89
infix fun Any.`should not equal`(theOther: Any) = assertNotEquals(theOther, this)
910

11+
infix fun <T> Array<T>.`should equal`(theOther: Array<T>) = assertArrayEquals(theOther, this)
12+
infix fun <T> Iterable<T>.`should equal`(theOther: Iterable<T>) = assertEquals(theOther, this)
13+
1014
infix fun Any.`should be`(theOther: Any) = assertSame(theOther, this)
1115
infix fun Any.`should not be`(theOther: Any) = assertNotSame(theOther, this)
1216

@@ -56,7 +60,7 @@ infix fun <T : Exception> (() -> Unit).`should not throw`(expectedException: KCl
5660
}
5761
}
5862

59-
infix fun <T : Exception> (() -> Unit).`should not throw the Exception`(expectedException: KClass<T>) : NotThrowExceptionResult {
63+
infix fun <T : Exception> (() -> Unit).`should not throw the Exception`(expectedException: KClass<T>): NotThrowExceptionResult {
6064
try {
6165
this.invoke()
6266
return NotThrowExceptionResult(noException)

src/test/kotlin/org/amshove/kluent/tests/assertions/ShouldEqualTests.kt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,34 @@ class ShouldEqualTests : Spek() {
3030
firstObject shouldEqual secondObject
3131
}
3232
}
33+
on("checking two equal arrays") {
34+
val firstArray = arrayOf(1, 2, 3)
35+
val secondArray = arrayOf(1, 2, 3)
36+
it("should pass") {
37+
firstArray shouldEqual secondArray
38+
}
39+
}
40+
on("checking two different arrays") {
41+
val firstArray = arrayOf(1, 2, 3)
42+
val secondArray = arrayOf(4, 5, 6)
43+
it("should fail") {
44+
assertFails({ firstArray shouldEqual secondArray })
45+
}
46+
}
47+
on("checking two equal iterables") {
48+
val firstIterable = listOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe"))
49+
val secondIterable = listOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe"))
50+
it("should pass") {
51+
firstIterable shouldEqual secondIterable
52+
}
53+
}
54+
on("checking two different iterables") {
55+
val firstIterable = listOf(Person("Tom", "Guy"), Person("Jon", "Doe"), Person("Peter", "Meyer"))
56+
val secondIterable = listOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe"))
57+
it("should fail") {
58+
assertFails({ firstIterable shouldEqual secondIterable })
59+
}
60+
}
3361
}
3462
}
3563
}

src/test/kotlin/org/amshove/kluent/tests/backtickassertions/ShouldEqualTests.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import org.amshove.kluent.tests.helpclasses.Person
55
import org.jetbrains.spek.api.Spek
66
import kotlin.test.assertFails
77

8-
class ShouldEqualTests : Spek() {
8+
class ShouldequalTests : Spek() {
99
init {
1010
given("the should equal method") {
1111
on("checking equality of two equal strings") {
@@ -30,6 +30,34 @@ class ShouldEqualTests : Spek() {
3030
firstObject `should equal` secondObject
3131
}
3232
}
33+
on("checking two equal arrays") {
34+
val firstArray = arrayOf(1, 2, 3)
35+
val secondArray = arrayOf(1, 2, 3)
36+
it("should pass") {
37+
firstArray `should equal` secondArray
38+
}
39+
}
40+
on("checking two different arrays") {
41+
val firstArray = arrayOf(1, 2, 3)
42+
val secondArray = arrayOf(4, 5, 6)
43+
it("should fail") {
44+
assertFails({ firstArray `should equal` secondArray })
45+
}
46+
}
47+
on("checking two equal iterables") {
48+
val firstIterable = listOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe"))
49+
val secondIterable = listOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe"))
50+
it("should pass") {
51+
firstIterable `should equal` secondIterable
52+
}
53+
}
54+
on("checking two different iterables") {
55+
val firstIterable = listOf(Person("Tom", "Guy"), Person("Jon", "Doe"), Person("Peter", "Meyer"))
56+
val secondIterable = listOf(Person("Tom", "Guy"), Person("Alice", "Bob"), Person("Jon", "Doe"))
57+
it("should fail") {
58+
assertFails({ firstIterable `should equal` secondIterable })
59+
}
60+
}
3361
}
3462
}
3563
}

0 commit comments

Comments
 (0)