Skip to content

Commit

Permalink
Add assertSame and assertNotSame methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton committed Aug 5, 2017
1 parent 20cd748 commit f4c7414
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ fun <@OnlyInputTypes T> assertNotEquals(illegal: T, actual: T, message: String?
asserter.assertNotEquals(message, illegal, actual)
}

/** Asserts that [expected] is the same instance as [actual], with an optional [message]. */
fun <@OnlyInputTypes T> assertSame(expected: T, actual: T, message: String? = null) {
asserter.assertSame(message, expected, actual)
}

/** Asserts that [actual] is not the same instance as [illegal], with an optional [message]. */
fun <@OnlyInputTypes T> assertNotSame(illegal: T, actual: T, message: String? = null) {
asserter.assertNotSame(message, illegal, actual)
}

/** Asserts that the [actual] value is not `null`, with an optional [message]. */
fun <T : Any> assertNotNull(actual: T?, message: String? = null): T {
asserter.assertNotNull(message, actual)
Expand Down Expand Up @@ -170,6 +180,24 @@ interface Asserter {
assertTrue({ messagePrefix(message) + "Illegal value: <$actual>." }, actual != illegal)
}

/**
* Asserts that the specified values are the same instance.
*
* @param message the message to report if the assertion fails.
*/
fun assertSame(message: String?, expected: Any?, actual: Any?): Unit {
assertTrue({ messagePrefix(message) + "Expected <$expected>, actual <$actual>." }, actual === expected)
}

/**
* Asserts that the specified values are not the same instance.
*
* @param message the message to report if the assertion fails.
*/
fun assertNotSame(message: String?, illegal: Any?, actual: Any?): Unit {
assertTrue({ messagePrefix(message) + "Illegal value: <$actual>." }, actual !== illegal)
}

/**
* Asserts that the specified value is `null`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class BasicAssertionsTest {
assertEquals(1, 1)
}

@Test
fun testAssertSame() {
val instance: Any = object {}
assertSame(instance, instance)
}

@Test
fun testAssertEqualsString() {
assertEquals("a", "a")
Expand Down Expand Up @@ -67,6 +73,13 @@ class BasicAssertionsTest {
checkFailedAssertion { assertEquals(1, 2) }
}

@Test
fun testAssertSameFails() {
val instance1: Any = object {}
val instance2: Any = object {}
checkFailedAssertion { assertEquals(instance1, instance2) }
}

@Test
fun testAssertTrue() {
assertTrue(true)
Expand Down Expand Up @@ -107,11 +120,24 @@ class BasicAssertionsTest {
assertNotEquals(1, 2)
}

@Test
fun testAssertNotSame() {
val instance1: Any = object {}
val instance2: Any = object {}
assertNotEquals(instance1, instance2)
}

@Test()
fun testAssertNotEqualsFails() {
checkFailedAssertion { assertNotEquals(1, 1) }
}

@Test
fun testAssertNotSameFails() {
val instance: Any = object {}
checkFailedAssertion { assertNotEquals(instance, instance) }
}

@Test
fun testAssertNotNull() {
assertNotNull(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ internal object DefaultJsAsserter : Asserter {
super.assertNotEquals(message, illegal, actual)
}

override fun assertSame(message: String?, expected: Any?, actual: Any?) {
e = expected
a = actual
super.assertSame(message, expected, actual)
}

override fun assertNotSame(message: String?, illegal: Any?, actual: Any?) {
e = illegal
a = actual
super.assertNotSame(message, illegal, actual)
}

override fun assertNull(message: String?, actual: Any?) {
a = actual
super.assertNull(message, actual)
Expand Down
8 changes: 8 additions & 0 deletions libraries/kotlin.test/junit/src/main/kotlin/JUnitSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ object JUnitAsserter : Asserter {
Assert.assertNotEquals(message, illegal, actual)
}

override fun assertSame(message : String?, expected : Any?, actual : Any?) {
Assert.assertSame(message, expected, actual)
}

override fun assertNotSame(message : String?, illegal : Any?, actual : Any?) {
Assert.assertNotSame(message, illegal, actual)
}

override fun assertNotNull(message : String?, actual : Any?) {
Assert.assertNotNull(message ?: "actual value is null", actual)
}
Expand Down

0 comments on commit f4c7414

Please sign in to comment.