-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlin "fail" methods to org.junit.jupiter.api Closes #1209
- Loading branch information
Showing
5 changed files
with
156 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
junit-jupiter-engine/src/test/kotlin/org/junit/jupiter/api/KotlinFailAssertionsTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright 2015-2018 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
package org.junit.jupiter.api | ||
|
||
import org.junit.jupiter.api.AssertEquals.assertEquals | ||
import org.junit.jupiter.api.AssertionTestUtils.assertMessageContains | ||
import org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals | ||
import org.opentest4j.AssertionFailedError | ||
import java.util.stream.Stream | ||
|
||
class KotlinFailAssertionsTests { | ||
|
||
@Test | ||
fun `fail with string`() { | ||
val message = "test" | ||
val ex = assertThrows<AssertionFailedError> { | ||
fail(message) | ||
} | ||
assertMessageEquals(ex, message) | ||
} | ||
|
||
@Test | ||
fun `fail with message supplier`() { | ||
val message = "test" | ||
val ex = assertThrows<AssertionFailedError> { | ||
fail { message } | ||
} | ||
assertMessageEquals(ex, message) | ||
} | ||
|
||
@Test | ||
fun `fail with null string`() { | ||
val ex = assertThrows<AssertionFailedError> { | ||
fail(null as String?) | ||
} | ||
assertMessageEquals(ex, "") | ||
} | ||
|
||
@Test | ||
fun `fail with null message supplier`() { | ||
val ex = assertThrows<AssertionFailedError> { | ||
fail(null as (() -> String)?) | ||
} | ||
assertMessageEquals(ex, "") | ||
} | ||
|
||
@Test | ||
fun `fail with string and throwable`() { | ||
val message = "message" | ||
val throwableCause = "cause" | ||
val ex = assertThrows<AssertionFailedError> { | ||
fail(message, Throwable(throwableCause)) | ||
} | ||
assertMessageEquals(ex, message) | ||
val cause = ex.cause | ||
assertMessageContains(cause, throwableCause) | ||
} | ||
|
||
@Test | ||
fun `fail with throwable`() { | ||
val throwableCause = "cause" | ||
val ex = assertThrows<AssertionFailedError> { | ||
fail(Throwable(throwableCause)) | ||
} | ||
assertMessageEquals(ex, "") | ||
val cause = ex.cause | ||
assertMessageContains(cause, throwableCause) | ||
} | ||
|
||
@Test | ||
fun `fail with string and null throwable`() { | ||
val message = "message" | ||
val ex = assertThrows<AssertionFailedError> { | ||
fail(message, null) | ||
} | ||
assertMessageEquals(ex, message) | ||
if (ex.cause != null) { | ||
throw AssertionError("Cause should have been null") | ||
} | ||
} | ||
|
||
@Test | ||
fun `fail with null string and throwable`() { | ||
val throwableCause = "cause" | ||
val ex = assertThrows<AssertionFailedError> { | ||
fail(null, Throwable(throwableCause)) | ||
} | ||
assertMessageEquals(ex, "") | ||
val cause = ex.cause | ||
assertMessageContains(cause, throwableCause) | ||
} | ||
|
||
@Test | ||
fun `fail usable as a stream expression`() { | ||
val count = Stream.empty<Any>() | ||
.peek { _ -> fail("peek should never be called") } | ||
.filter { _ -> fail("filter should never be called", Throwable("cause")) } | ||
.map { _ -> fail(Throwable("map should never be called")) } | ||
.sorted { _, _ -> fail { "sorted should never be called" } } | ||
.count() | ||
assertEquals(0L, count) | ||
} | ||
|
||
@Test | ||
fun `fail usable as a sequence expression`() { | ||
val count = emptyList<Any>() | ||
.asSequence() | ||
.onEach { _ -> fail("peek should never be called") } | ||
.filter { _ -> fail("filter should never be called", Throwable("cause")) } | ||
.map { _ -> fail(Throwable("map should never be called")) } | ||
.count() | ||
assertEquals(0, count) | ||
} | ||
} |
c93d0aa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kudos to @JLLeitschuh for the code -- my git-fu failed to preserve your initial commit!
c93d0aa
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah well, at least it's now checked in. Thanks @sormuras. I didn't have the time to do this myself.