Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assertSame and assertNotSame methods. #1230

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test prefix reminds me old days of JUnit, where it was obligatory to run any test :-)

Could you tell me why there is doubled kotlin and test packages in path: kotlin/libraries/kotlin.test/common/src/test/kotlin/kotlin/test/tests/? Just wondering.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kotlin.test/common is the project directory, then src/test/kotlin is the source root for tests, then kotlin/test/tests is the package name for tests of kotlin.test :)

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