Skip to content

Commit

Permalink
Merge branch 'christophsturm-christoph/fix-idea-npe'
Browse files Browse the repository at this point in the history
  • Loading branch information
robfletcher committed Sep 14, 2018
2 parents d89d57d + 867e9f5 commit d8dc2d7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
39 changes: 33 additions & 6 deletions strikt-core/src/main/kotlin/strikt/internal/AssertionStrategy.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package strikt.internal

import org.opentest4j.AssertionFailedError
import strikt.api.Status
import strikt.api.Status.Failed
import strikt.api.Status.Passed
import strikt.api.Status.Pending
import strikt.internal.opentest4j.AtomicAssertionFailure
import strikt.internal.opentest4j.CompoundAssertionFailure
import strikt.internal.reporting.writePartialToString
import strikt.internal.reporting.writeToString
Expand Down Expand Up @@ -109,23 +109,32 @@ internal sealed class AssertionStrategy {
tree
.children
.filter { it.status is Failed }
.map { AtomicAssertionFailure(it.writePartialToString(), it) }
.map {
createAssertionFailedError(
it.writePartialToString(),
it.status as Failed
)
}
)
}
}

override fun evaluate(trees: Collection<AssertionGroup<*>>) {
if (trees.any { it.status is Status.Failed }) {
val failures = trees
.filter { it.status is Status.Failed }
.map { AtomicAssertionFailure(it.writeToString(), it) }
.filter { it.status is Failed }
.map { createAssertionFailedError(it.writeToString(), it.status as Failed) }
throw CompoundAssertionFailure(trees.writeToString(), failures)
}
}

override fun <T> afterStatusSet(result: AssertionResult<T>) {
if (result.status is Failed) {
throw AtomicAssertionFailure(result.root.writeToString(), result)
val status = result.status
when (status) {
is Failed -> throw createAssertionFailedError(
result.root.writeToString(),
status
)
}
}
}
Expand Down Expand Up @@ -161,4 +170,22 @@ internal sealed class AssertionStrategy {
delegate.afterStatusSet(result)
}
}

internal fun createAssertionFailedError(
message: String,
failed: Failed?
): AssertionFailedError {
return if (failed?.comparison != null)
AssertionFailedError(
message,
failed.comparison.expected,
failed.comparison.actual,
failed.cause
)
else
AssertionFailedError(
message,
failed?.cause
)
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions strikt-core/src/test/kotlin/strikt/Chained.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package strikt
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.opentest4j.AssertionFailedError
import strikt.api.expectThat
import strikt.assertions.contains
import strikt.assertions.containsExactly
Expand All @@ -13,7 +14,6 @@ import strikt.assertions.isNotNull
import strikt.assertions.isNull
import strikt.assertions.isUpperCase
import strikt.assertions.message
import strikt.internal.opentest4j.AtomicAssertionFailure

@DisplayName("assertions in chains")
internal class Chained {
Expand Down Expand Up @@ -66,7 +66,7 @@ internal class Chained {
" ✓ …at index 1\n" +
" ✗ contains no further elements : found [3, 4]"
expectThat(error)
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message
.isEqualTo(expected)
}
Expand Down
19 changes: 9 additions & 10 deletions strikt-core/src/test/kotlin/strikt/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import strikt.assertions.isTrue
import strikt.assertions.isUpperCase
import strikt.assertions.message
import strikt.assertions.startsWith
import strikt.internal.opentest4j.AtomicAssertionFailure
import strikt.internal.opentest4j.CompoundAssertionFailure

class Exceptions {
Expand All @@ -28,7 +27,7 @@ class Exceptions {
expectThat("fnord").hasLength(5).isUpperCase().startsWith("f")
}.let { error ->
expectThat(error)
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.and {
message.isEqualTo(
"▼ Expect that \"fnord\":\n" +
Expand Down Expand Up @@ -60,7 +59,7 @@ class Exceptions {
map { it.failures }
.hasSize(1)
.first()
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message.isEqualTo(
"▼ Expect that \"fnord\":\n" +
" ✗ is upper case"
Expand Down Expand Up @@ -97,7 +96,7 @@ class Exceptions {
.hasSize(3)
.and {
first()
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message
.isEqualTo(
"▼ Expect that \"fnord\":\n" +
Expand All @@ -107,7 +106,7 @@ class Exceptions {
}
.and {
get(1)
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message
.isEqualTo(
"▼ Expect that \"fnord\":\n" +
Expand All @@ -117,7 +116,7 @@ class Exceptions {
}
.and {
get(2)
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message
.isEqualTo(
"▼ Expect that \"fnord\":\n" +
Expand Down Expand Up @@ -156,7 +155,7 @@ class Exceptions {
.map { it.failures }
.hasSize(1)
.first()
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message
.isEqualTo(
"▼ Expect that \"fnord\":\n" +
Expand All @@ -177,7 +176,7 @@ class Exceptions {
}
.let { error ->
expectThat(error)
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message
.isEqualTo(
"▼ Expect that [\"catflap\", \"rubberplant\", \"marzipan\"]:\n" +
Expand Down Expand Up @@ -206,14 +205,14 @@ class Exceptions {
.hasSize(2)
.and {
first()
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message
.isEqualTo(
"▼ Expect that [\"catflap\", \"rubberplant\", \"marzipan\"]:\n" +
" ✗ has size 2 : found 3"
)
get(1)
.isA<AtomicAssertionFailure>()
.isA<AssertionFailedError>()
.message
.isEqualTo(
"▼ Expect that [\"catflap\", \"rubberplant\", \"marzipan\"]:\n" +
Expand Down
4 changes: 2 additions & 2 deletions strikt-core/src/test/kotlin/strikt/Throws.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.opentest4j.AssertionFailedError
import strikt.api.expectThat
import strikt.api.expectThrows
import strikt.assertions.isA
import strikt.assertions.throws
import strikt.internal.opentest4j.AtomicAssertionFailure

@DisplayName("throws assertion")
internal class Throws {
Expand All @@ -35,7 +35,7 @@ internal class Throws {

@Test
fun `throws fails if the action throws the wrong type of exception`() {
assertThrows<AtomicAssertionFailure> {
assertThrows<AssertionFailedError> {
expectThrows<IllegalStateException> { -> throw NullPointerException() }
}.let { e ->
val expected = "▼ Expect that () -> kotlin.Unit:\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package strikt.assertions

import org.junit.jupiter.api.Test
import org.opentest4j.AssertionFailedError
import strikt.api.expectThat
import strikt.fails
import strikt.internal.reporting.toHex
Expand Down Expand Up @@ -64,8 +65,11 @@ internal class BeanPropertyAssertions {
@Test
fun `isEqualTo works with java fields that are null`() {
val subject = PersonJava(null, null, null, null)
fails {
expectThat(fails {
expectThat(subject.name).isEqualTo("Ziggy")
}).isA<AssertionFailedError>().and {
map { it.actual.value }.isNull()
map { it.isActualDefined }.isTrue()
}
}

Expand Down

0 comments on commit d8dc2d7

Please sign in to comment.