From 56f73483656c8434c439d7d2cbf59ac75b6f02a6 Mon Sep 17 00:00:00 2001 From: memoizr Date: Tue, 9 Aug 2016 16:13:53 +0100 Subject: [PATCH 1/2] rename package --- .../memoizr/assertk/AbstractAssertBuilder.kt | 60 ++++++ .../com/memoizr/assertk/AssertionHooks.kt | 14 ++ .../com/memoizr/assertk/CharSequenceAssert.kt | 147 +++++++++++++ .../com/memoizr/assertk/ObjectAssert.kt | 9 + .../com/memoizr/assertk/ThrowableAssertion.kt | 55 +++++ .../assertk/CharSequence assert test.kt | 204 ++++++++++++++++++ .../com/memoizr/assertk/Object assert test.kt | 103 +++++++++ .../memoizr/assertk/Throwable assert test.kt | 156 ++++++++++++++ 8 files changed, 748 insertions(+) create mode 100644 src/main/kotlin/com/memoizr/assertk/AbstractAssertBuilder.kt create mode 100644 src/main/kotlin/com/memoizr/assertk/AssertionHooks.kt create mode 100644 src/main/kotlin/com/memoizr/assertk/CharSequenceAssert.kt create mode 100644 src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt create mode 100644 src/main/kotlin/com/memoizr/assertk/ThrowableAssertion.kt create mode 100644 src/test/kotlin/com/memoizr/assertk/CharSequence assert test.kt create mode 100644 src/test/kotlin/com/memoizr/assertk/Object assert test.kt create mode 100644 src/test/kotlin/com/memoizr/assertk/Throwable assert test.kt diff --git a/src/main/kotlin/com/memoizr/assertk/AbstractAssertBuilder.kt b/src/main/kotlin/com/memoizr/assertk/AbstractAssertBuilder.kt new file mode 100644 index 0000000..bdd52c5 --- /dev/null +++ b/src/main/kotlin/com/memoizr/assertk/AbstractAssertBuilder.kt @@ -0,0 +1,60 @@ +package com.memoizr.assertk + +import com.memoizr.assertk.ObjectStuff.notNull +import org.assertj.core.api.AbstractAssert +import org.assertj.core.api.Assertions + +enum class ObjectStuff { + notNull +} + +inline fun of() = AbstractAssertBuilder.InstanceMatcher() + +abstract class AbstractAssertBuilder, A : Any> internal constructor(actual: A?, selfType: Class<*>) { + class InstanceMatcher + + @Suppress("UNCHECKED_CAST", "LeakingThis") + protected val myself: S = selfType.cast(this) as S + + open protected val assertion: AbstractAssert<*, out A?> = Assertions.assertThat(actual) + + infix fun isEqualTo(other: A): S { + assertion.isEqualTo(other) + return myself + } + + infix fun isNotEqualTo(other: A): S { + assertion.isNotEqualTo(other) + return myself + } + + @Suppress("UNUSED_PARAMETER") + inline infix fun isInstance(bar: InstanceMatcher): S { + assertion.isInstanceOf(R::class.java) + return myself + } + + infix fun _is(objectStuff: ObjectStuff?): S { + return when (objectStuff) { + notNull -> { + assertion.isNotNull() + myself + } + else -> { + assertion.isNull() + myself + } + } + } + + infix fun describedAs(description: String): S { + assertion.`as`(description) + return myself + } + + @Suppress("UNCHECKED_CAST") + infix fun isSuchThat(assertionBlock: S.(S) -> Unit): S { + (this as S).assertionBlock(this) + return myself + } +} diff --git a/src/main/kotlin/com/memoizr/assertk/AssertionHooks.kt b/src/main/kotlin/com/memoizr/assertk/AssertionHooks.kt new file mode 100644 index 0000000..f01392b --- /dev/null +++ b/src/main/kotlin/com/memoizr/assertk/AssertionHooks.kt @@ -0,0 +1,14 @@ +package com.memoizr.assertk + +val assert: AssertionHook get() = RealAssertionHook() +val expect: AssertionHook get() = RealAssertionHook() + +interface AssertionHook { + infix fun that(subjectUnderTest: T?) = ObjectAssert(subjectUnderTest) + infix fun that(subjectUnderTest: CharSequence?) = CharSequenceAssert(subjectUnderTest) + infix fun thatThrownBy(expressionUnderTest: () -> Unit) = ThrowableAssertion(expressionUnderTest) +} + +class RealAssertionHook : AssertionHook + + diff --git a/src/main/kotlin/com/memoizr/assertk/CharSequenceAssert.kt b/src/main/kotlin/com/memoizr/assertk/CharSequenceAssert.kt new file mode 100644 index 0000000..04d5957 --- /dev/null +++ b/src/main/kotlin/com/memoizr/assertk/CharSequenceAssert.kt @@ -0,0 +1,147 @@ +package com.memoizr.assertk + +import com.memoizr.assertk.CharSequenceAssertTest.* +import org.assertj.core.api.AbstractCharSequenceAssert +import org.assertj.core.api.Assertions.assertThat +import java.util.regex.Pattern + +enum class CharSequenceAssertTest { + nullOrEmpty, empty, notEmpty +} + +class CharSequenceAssert internal constructor( + private val subject: CharSequence?, + override val assertion: AbstractCharSequenceAssert<*, out CharSequence> = assertThat(subject)) : + AbstractAssertBuilder(subject, CharSequenceAssert::class.java) { + + object onlyDigits + + infix fun _is(assertionTest: CharSequenceAssertTest): CharSequenceAssert { + when (assertionTest) { + empty -> assertion.isEmpty() + notEmpty -> assertion.isNotEmpty() + nullOrEmpty -> assertion.isNullOrEmpty() + } + return this + } + + infix fun hasSize(expectedSize: Int): CharSequenceAssert { + assertion.hasSize(expectedSize) + return this + } + + infix fun hasLineCount(expectedLineCount: Int): CharSequenceAssert { + assertion.hasLineCount(expectedLineCount) + return this + } + + infix fun isEqualToIgnoringCase(expected: CharSequence): CharSequenceAssert { + assertion.isEqualToIgnoringCase(expected) + return this + } + + infix fun isNotEqualToIgnoringCase(expected: CharSequence): CharSequenceAssert { + assertion.isNotEqualToIgnoringCase(expected) + return this + } + + infix fun contains(onlyDigits: onlyDigits): CharSequenceAssert { + assertion.containsOnlyDigits() + return this + } + + infix fun contains(expected: CharSequence): CharSequenceAssert { + assertion.contains(expected) + return this + } + + infix fun containsOnlyOnce(expected: CharSequence): CharSequenceAssert { + assertion.containsOnlyOnce(expected) + return this + } + + infix fun contains(expected: Iterable): CharSequenceAssert { + assertion.contains(expected) + return this + } + + infix fun containsSequence(expected: Iterable): CharSequenceAssert { + assertion.containsSequence(expected) + return this + } + + infix fun containsIgnoringCase(expected: CharSequence): CharSequenceAssert { + assertion.containsIgnoringCase(expected) + return this + } + + infix fun doesNotContain(expected: CharSequence): CharSequenceAssert { + assertion.doesNotContain(expected) + return this + } + + infix fun startsWith(expected: CharSequence): CharSequenceAssert { + assertion.startsWith(expected) + return this + } + + infix fun doesNotStartWith(expected: CharSequence): CharSequenceAssert { + assertion.doesNotStartWith(expected) + return this + } + + infix fun endsWith(expected: CharSequence): CharSequenceAssert { + assertion.endsWith(expected) + return this + } + + infix fun doesNotEndWith(expected: CharSequence): CharSequenceAssert { + assertion.doesNotEndWith(expected) + return this + } + + infix fun matches(expected: Pattern): CharSequenceAssert { + assertion.matches(expected) + return this + } + + infix fun doesNotMatch(expected: Pattern): CharSequenceAssert { + assertion.doesNotMatch(expected) + return this + } + + infix fun matches(expected: CharSequence): CharSequenceAssert { + assertion.matches(expected) + return this + } + + infix fun doesNotMatch(expected: CharSequence): CharSequenceAssert { + assertion.doesNotMatch(expected) + return this + } + + infix fun isEqualToIgnoringWhitespace(expected: CharSequence): CharSequenceAssert { + assertion.isEqualToIgnoringWhitespace(expected) + return this + } + + infix fun isNotEqualToIgnoringWhitespace(expected: CharSequence): CharSequenceAssert { + assertion.isNotEqualToIgnoringWhitespace(expected) + return this + } + + infix fun isSubstringOf(expected: CharSequence): CharSequenceAssert { + assertion.isSubstringOf(expected) + return this + } + + infix fun containsPattern(pattern: CharSequence): CharSequenceAssert { + assertion.containsPattern(pattern) + return this + } + + infix fun containsPattern(pattern: Pattern): CharSequenceAssert { + assertion.containsPattern(pattern) + return this + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt b/src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt new file mode 100644 index 0000000..a803d2e --- /dev/null +++ b/src/main/kotlin/com/memoizr/assertk/ObjectAssert.kt @@ -0,0 +1,9 @@ +package com.memoizr.assertk + +import org.assertj.core.api.AbstractObjectAssert +import org.assertj.core.api.Assertions + +class ObjectAssert internal constructor( + actual: A?, + override val assertion: AbstractObjectAssert<*, A> = Assertions.assertThat(actual)) : + AbstractAssertBuilder, A>(actual, ObjectAssert::class.java) \ No newline at end of file diff --git a/src/main/kotlin/com/memoizr/assertk/ThrowableAssertion.kt b/src/main/kotlin/com/memoizr/assertk/ThrowableAssertion.kt new file mode 100644 index 0000000..a4c28c0 --- /dev/null +++ b/src/main/kotlin/com/memoizr/assertk/ThrowableAssertion.kt @@ -0,0 +1,55 @@ +package com.memoizr.assertk + +import org.assertj.core.api.AbstractThrowableAssert +import org.assertj.core.api.Assertions + +class ThrowableAssertion( + func: () -> Unit, + private val assertion: AbstractThrowableAssert<*, out Throwable> = Assertions.assertThatThrownBy(func)) { + + infix fun hasMessage(message: String): ThrowableAssertion { + assertion.hasMessage(message) + return this + } + + infix fun hasCause(throwable: Throwable): ThrowableAssertion { + assertion.hasCause(throwable) + return this + } + + infix fun has(noCause: noCause): ThrowableAssertion { + assertion.hasNoCause() + return this + } + + infix fun hasMessageStartingWith(message: String): ThrowableAssertion { + assertion.hasMessageStartingWith(message) + return this + } + + infix fun hasMessageEndingWith(message: String): ThrowableAssertion { + assertion.hasMessageEndingWith(message) + return this + } + + infix fun hasMessageContaining(message: String): ThrowableAssertion { + assertion.hasMessageContaining(message) + return this + } + + infix fun hasStackTraceContaining(message: String): ThrowableAssertion { + assertion.hasStackTraceContaining(message) + return this + } + + infix fun hasCauseExactlyInstanceOf(throwable: Class): ThrowableAssertion { + assertion.hasCauseExactlyInstanceOf(throwable) + return this + } + + infix fun and(assertions: ThrowableAssertion.(ThrowableAssertion) -> Unit) { + assertions(this) + } + + object noCause +} \ No newline at end of file diff --git a/src/test/kotlin/com/memoizr/assertk/CharSequence assert test.kt b/src/test/kotlin/com/memoizr/assertk/CharSequence assert test.kt new file mode 100644 index 0000000..670566c --- /dev/null +++ b/src/test/kotlin/com/memoizr/assertk/CharSequence assert test.kt @@ -0,0 +1,204 @@ +package com.memoizr.assertk + +import com.memoizr.assertk.CharSequenceAssert.onlyDigits +import com.memoizr.assertk.CharSequenceAssertTest.* +import com.nhaarman.mockito_kotlin.spy +import com.nhaarman.mockito_kotlin.verify +import org.assertj.core.api.AbstractCharSequenceAssert +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import java.util.regex.Pattern + +class `CharSequence assert test` { + lateinit var mockAssertion: AbstractCharSequenceAssert<*, out CharSequence> + val _expect = object : AssertionHook { + override fun that(subjectUnderTest: CharSequence?): CharSequenceAssert { + mockAssertion = spy(assertThat(subjectUnderTest)) + return CharSequenceAssert(subjectUnderTest, mockAssertion) + } + } + val chained = "" + infix fun CharSequenceAssert.canBe(dummyValue: CharSequence) : CharSequenceAssert = this + + @Test + fun `isNullOrEmpty checks for nullity or emptiness`() { + val nullString: String? = null + _expect that "" _is nullOrEmpty canBe chained + _expect that nullString _is nullOrEmpty + _expect thatThrownBy { + _expect that "not null or empty" _is nullOrEmpty + } + verify(mockAssertion).isNullOrEmpty() + } + + @Test + fun `isEmpty checks only for emptiness`() { + _expect that "" _is empty canBe chained + verify(mockAssertion).isEmpty() + } + + @Test + fun `isNotEmpty checks for non emptiness`() { + _expect that "not empty" _is notEmpty canBe chained + verify(mockAssertion).isNotEmpty() + } + + @Test + fun hasSize() { + _expect that "four" hasSize 4 canBe chained + verify(mockAssertion).hasSize(4) + } + + @Test + fun hasLineCount() { + _expect that "foo \n bar" hasLineCount 2 canBe chained + verify(mockAssertion).hasLineCount(2) + } + + @Test + fun isEqualToIgnoreCase() { + _expect that "FOO" isEqualToIgnoringCase "foo" canBe chained + verify(mockAssertion).isEqualToIgnoringCase("foo") + } + + @Test + fun isNotEqualToIgnoringCase() { + _expect that "FOO" isNotEqualToIgnoringCase "bar" canBe chained + verify(mockAssertion).isNotEqualToIgnoringCase("bar") + } + + @Test + fun containsOnlyDigits() { + _expect that "1234" contains onlyDigits canBe chained + verify(mockAssertion).containsOnlyDigits() + } + + @Test + fun containsOnlyOnce() { + _expect that "foo bar" containsOnlyOnce "foo" canBe chained + verify(mockAssertion).containsOnlyOnce("foo") + } + + @Test + fun `contains CharSequence`() { + _expect that "foo" contains "oo" canBe chained + verify(mockAssertion).contains("oo") + } + + @Test + fun `contains iterable`() { + _expect that "foo bar" contains listOf("ar", "oo") canBe chained + verify(mockAssertion).contains(listOf("ar", "oo")) + } + + @Test + fun `containsSequence CharSequence`() { + _expect that "foo bar" containsSequence listOf("oo", "ar") canBe chained + verify(mockAssertion).containsSequence(listOf("oo", "ar")) + } + + @Test + fun containsIgnoringCase() { + _expect that "foo bar" containsIgnoringCase "FOO" canBe chained + verify(mockAssertion).containsIgnoringCase("FOO") + } + + @Test + fun doesNotContain() { + _expect that "foo" doesNotContain "bar" canBe chained + verify(mockAssertion).doesNotContain("bar") + } + + @Test + fun startsWith() { + _expect that "foo" startsWith "fo" canBe chained + verify(mockAssertion).startsWith("fo") + } + + @Test + fun doesNotStartWith() { + _expect that "foo" doesNotStartWith "oo" canBe chained + verify(mockAssertion).doesNotStartWith("oo") + } + + @Test + fun endsWith() { + _expect that "foo" endsWith "oo" canBe chained + verify(mockAssertion).endsWith("oo") + } + + @Test + fun doesNotEndWith() { + _expect that "foo" doesNotEndWith "fo" canBe chained + verify(mockAssertion).doesNotEndWith("fo") + } + + @Test + fun `matches Pattern`() { + val expected = Pattern.compile("f\\w*\\sb.*") + _expect that "foo bar" matches expected canBe chained + verify(mockAssertion).matches(expected) + } + + @Test + fun `doesNotMatch Pattern`() { + val expected = Pattern.compile("f\\w*\\sf.*") + _expect that "foo bar" doesNotMatch expected canBe chained + verify(mockAssertion).doesNotMatch(expected) + } + + @Test + fun `matches CharSequence`() { + val expected = "f\\w*\\sb.*" + _expect that "foo bar" matches expected canBe chained + verify(mockAssertion).matches(expected) + } + + @Test + fun `doesNotMatch CharSequence`() { + val expected = "f\\w*\\sf.*" + _expect that "foo bar" doesNotMatch expected canBe chained + verify(mockAssertion).doesNotMatch(expected) + } + + @Test + fun isEqualToIgnoringWhitespace() { + _expect that "foobar" isEqualToIgnoringWhitespace "foobar " canBe chained + verify(mockAssertion).isEqualToIgnoringWhitespace("foobar ") + } + + @Test + fun isNotEqualToIgnoringWhitespace() { + _expect that "foobar" isNotEqualToIgnoringWhitespace "bar " canBe chained + verify(mockAssertion).isNotEqualToIgnoringWhitespace("bar ") + } + + @Test + fun isSubstringOf() { + val expected = "foobar" + _expect that "foo" isSubstringOf expected canBe chained + verify(mockAssertion).isSubstringOf(expected) + } + + @Test + fun `containsPattern CharSequence`() { + val pattern = "f\\w" + _expect that "foo bar" containsPattern pattern canBe chained + verify(mockAssertion).containsPattern(pattern) + } + + @Test + fun `containsPattern Pattern`() { + val pattern = Pattern.compile("f\\w") + _expect that "foo bar" containsPattern pattern canBe chained + verify(mockAssertion).containsPattern(pattern) + } + + @Test + fun `Block syntax is supported`() { + _expect that "foo" isSuchThat { + it contains "foo" + it isSubstringOf "fooBar" + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/memoizr/assertk/Object assert test.kt b/src/test/kotlin/com/memoizr/assertk/Object assert test.kt new file mode 100644 index 0000000..77c4978 --- /dev/null +++ b/src/test/kotlin/com/memoizr/assertk/Object assert test.kt @@ -0,0 +1,103 @@ +package com.memoizr.assertk + +import com.memoizr.assertk.ObjectStuff.notNull +import com.nhaarman.mockito_kotlin.spy +import com.nhaarman.mockito_kotlin.verify +import org.assertj.core.api.AbstractObjectAssert +import org.assertj.core.api.Assertions +import org.junit.Test + +class `Object assert test` { + val nullObject: Any? = null + + lateinit var mockAssertion: AbstractObjectAssert<*, Any> + val _expect = object : AssertionHook { + override fun that(subjectUnderTest: A?): ObjectAssert { + val spy: AbstractObjectAssert<*, A?>? = spy(Assertions.assertThat(subjectUnderTest)) + mockAssertion = spy as AbstractObjectAssert<*, Any> + return ObjectAssert(subjectUnderTest, mockAssertion) as ObjectAssert + } + } + + @Test + fun `isEqualTo performs logical equality`() { + _expect that Unit isEqualTo Unit + + val expected = Any() + _expect thatThrownBy { + _expect that Any() isEqualTo expected + } + + verify(mockAssertion).isEqualTo(expected) + } + + @Test + fun `isNotEqualTo performs logical inequality`() { + _expect that Any() isNotEqualTo Any() + + _expect thatThrownBy { + _expect that Unit isNotEqualTo Unit + } + + verify(mockAssertion).isNotEqualTo(Unit) + } + + @Test + fun `_is notNull checks whether object is null`() { + _expect that Any() _is notNull + + _expect thatThrownBy { + _expect that nullObject _is notNull + } + verify(mockAssertion).isNotNull() + } + + @Test + fun `_is null checks whether object is not null`() { + _expect that nullObject _is null + + _expect thatThrownBy { + _expect that Any() _is null + } + verify(mockAssertion).isNull() + } + + @Test + fun `isInstance of checks for object instance`() { + val anObject = Any() + + _expect that anObject isInstance of() + verify(mockAssertion).isInstanceOf(Any::class.java) + + _expect thatThrownBy { + _expect that anObject isInstance of() + } + + } + + @Test + fun `describedAs describes the object when referring to it in failure messages`() { + _expect thatThrownBy { + _expect that Any() describedAs "A labeled object" isInstance of() + } hasMessageContaining "A labeled object" + + verify(mockAssertion).describedAs("A labeled object") + } + + @Test + fun `block _expections are supported`() { + _expect that Any() isSuchThat { + it _is notNull + it isInstance of() + it isNotEqualTo Unit + it isNotEqualTo Any() + } + + _expect thatThrownBy { + _expect that Any() isSuchThat { + it _is null + it isInstance of() + } + } + } +} diff --git a/src/test/kotlin/com/memoizr/assertk/Throwable assert test.kt b/src/test/kotlin/com/memoizr/assertk/Throwable assert test.kt new file mode 100644 index 0000000..1f5b356 --- /dev/null +++ b/src/test/kotlin/com/memoizr/assertk/Throwable assert test.kt @@ -0,0 +1,156 @@ +package com.memoizr.assertk + +import com.memoizr.assertk.ThrowableAssertion.noCause +import com.nhaarman.mockito_kotlin.spy +import com.nhaarman.mockito_kotlin.verify +import org.assertj.core.api.AbstractThrowableAssert +import org.assertj.core.api.Assertions +import org.junit.Test + +class `Throwable assert test` { + val aThrowable = Throwable() + + private fun aThrowableWithCause() = Throwable("hello", aThrowable) + + lateinit var mockAssertion: AbstractThrowableAssert<*, out Throwable> + val _expect = object : AssertionHook { + override fun thatThrownBy(expressionUnderTest: () -> Unit): ThrowableAssertion { + mockAssertion = spy(Assertions.assertThatThrownBy(expressionUnderTest)) + return ThrowableAssertion(expressionUnderTest, mockAssertion) + } + } + + @Test + fun `fails when no exception is thrown`() { + Assertions.assertThatThrownBy { + _expect thatThrownBy { } + } + } + + @Test + fun `passes when exception is thrown`() { + _expect thatThrownBy { throw Throwable() } + } + + @Test + fun `passes when throwable message matches the specified message`() { + _expect thatThrownBy { throw Throwable("hello") } hasMessage "hello" + verify(mockAssertion).hasMessage("hello") + } + + @Test + fun `fails when throwable message does not match specified mesage`() { + expect thatThrownBy { + _expect thatThrownBy { throw Throwable("this") } hasMessage "that" + } + verify(mockAssertion).hasMessage("that") + } + + @Test + fun `passes when throwable cause and specified cause match`() { + _expect thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable hasMessage "hello" + _expect thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable + verify(mockAssertion).hasCause(aThrowable) + } + + @Test + fun `fails when throwable cause and specified cause do not match`() { + val exception = Exception() + expect thatThrownBy { + _expect thatThrownBy { throw aThrowableWithCause() } hasCause exception + } + verify(mockAssertion).hasCause(exception) + } + + @Test + fun `hasNoCause passes when throwable has no cause`() { + _expect thatThrownBy { throw aThrowable } has noCause + verify(mockAssertion).hasNoCause() + } + + @Test + fun `hasNoCause fails when throwable has cause`() { + expect thatThrownBy { + _expect thatThrownBy { throw aThrowableWithCause() } has noCause + } + verify(mockAssertion).hasNoCause() + } + + @Test + fun `hasMessageStartingWith checks throwable message starts with specified substring`() { + _expect thatThrownBy { throw Throwable("exception foo") } hasMessageStartingWith "exc" + verify(mockAssertion).hasMessageStartingWith("exc") + expect thatThrownBy { + _expect thatThrownBy { throw Throwable("excepiton foo") } hasMessageStartingWith "foo" + } + verify(mockAssertion).hasMessageStartingWith("foo") + } + + @Test + fun `hasMessageEndingWith checks throwable message ends with specified substring`() { + _expect thatThrownBy { throw Throwable("exception foo") } hasMessageEndingWith "foo" + verify(mockAssertion).hasMessageEndingWith("foo") + expect thatThrownBy { + _expect thatThrownBy { throw Throwable("exception foo") } hasMessageEndingWith "exec" + } + verify(mockAssertion).hasMessageEndingWith("exec") + } + + @Test + fun `hasMessageContaining checks throwable message ends with specified substring`() { + _expect thatThrownBy { throw Throwable("exception foo") } hasMessageContaining "foo" + verify(mockAssertion).hasMessageContaining("foo") + + expect thatThrownBy { + _expect thatThrownBy { throw Throwable("exception foo") } hasMessageContaining "lololol" + } + verify(mockAssertion).hasMessageContaining("lololol") + } + + @Test + fun `hasStackTraceContaining checks stacktrace contains specified string`() { + _expect thatThrownBy { throw aThrowable } hasStackTraceContaining "init" + verify(mockAssertion).hasStackTraceContaining("init") + + expect thatThrownBy { + _expect thatThrownBy { throw Throwable("exception foo") } hasStackTraceContaining "not in there!!" + } + verify(mockAssertion).hasStackTraceContaining("not in there!!") + } + + @Test + fun `hasCauseExactlyInstanceOf checks throwable cause is exactly instance of specified class`() { + _expect thatThrownBy { throw Throwable(Throwable()) } hasCauseExactlyInstanceOf Throwable::class.java + verify(mockAssertion).hasCauseExactlyInstanceOf(Throwable::class.java) + + expect thatThrownBy { + _expect thatThrownBy { throw Throwable(Exception()) } hasCauseExactlyInstanceOf Throwable::class.java + } + verify(mockAssertion).hasCauseExactlyInstanceOf(Throwable::class.java) + + expect thatThrownBy { + _expect thatThrownBy { throw Throwable(Throwable()) } hasCauseExactlyInstanceOf Exception::class.java + } + verify(mockAssertion).hasCauseExactlyInstanceOf(Exception::class.java) + } + + @Test + fun `block syntax is supported`() { + _expect thatThrownBy { + throw Throwable("exception foo", Throwable()) + } and { + it hasMessage "exception foo" + it hasCause Throwable() + it hasCauseExactlyInstanceOf Throwable::class.java + it hasMessageContaining "foo" + it hasMessageStartingWith "ex" + it hasMessageEndingWith "foo" + } + + _expect thatThrownBy { + _expect thatThrownBy { throw Throwable("exception foo", Throwable()) } and { + it hasMessage "blah blah" + } + } + } +} \ No newline at end of file From 9b0d788e62793c04af4ad0a27faa6658730b2459 Mon Sep 17 00:00:00 2001 From: memoizr Date: Tue, 9 Aug 2016 16:14:31 +0100 Subject: [PATCH 2/2] rename package --- .../memozr/assertk/AbstractAssertBuilder.kt | 60 ------ .../com/memozr/assertk/AssertionHooks.kt | 14 -- .../com/memozr/assertk/CharSequenceAssert.kt | 147 ------------- .../kotlin/com/memozr/assertk/ObjectAssert.kt | 9 - .../com/memozr/assertk/ThrowableAssertion.kt | 55 ----- .../assertk/CharSequence assert test.kt | 204 ------------------ .../com/memozr/assertk/Object assert test.kt | 103 --------- .../memozr/assertk/Throwable assert test.kt | 156 -------------- 8 files changed, 748 deletions(-) delete mode 100644 src/main/kotlin/com/memozr/assertk/AbstractAssertBuilder.kt delete mode 100644 src/main/kotlin/com/memozr/assertk/AssertionHooks.kt delete mode 100644 src/main/kotlin/com/memozr/assertk/CharSequenceAssert.kt delete mode 100644 src/main/kotlin/com/memozr/assertk/ObjectAssert.kt delete mode 100644 src/main/kotlin/com/memozr/assertk/ThrowableAssertion.kt delete mode 100644 src/test/kotlin/com/memozr/assertk/CharSequence assert test.kt delete mode 100644 src/test/kotlin/com/memozr/assertk/Object assert test.kt delete mode 100644 src/test/kotlin/com/memozr/assertk/Throwable assert test.kt diff --git a/src/main/kotlin/com/memozr/assertk/AbstractAssertBuilder.kt b/src/main/kotlin/com/memozr/assertk/AbstractAssertBuilder.kt deleted file mode 100644 index 46cb1d2..0000000 --- a/src/main/kotlin/com/memozr/assertk/AbstractAssertBuilder.kt +++ /dev/null @@ -1,60 +0,0 @@ -package com.memozr.assertk - -import com.memozr.assertk.ObjectStuff.notNull -import org.assertj.core.api.AbstractAssert -import org.assertj.core.api.Assertions - -enum class ObjectStuff { - notNull -} - -inline fun of() = AbstractAssertBuilder.InstanceMatcher() - -abstract class AbstractAssertBuilder, A : Any> internal constructor(actual: A?, selfType: Class<*>) { - class InstanceMatcher - - @Suppress("UNCHECKED_CAST", "LeakingThis") - protected val myself: S = selfType.cast(this) as S - - open protected val assertion: AbstractAssert<*, out A?> = Assertions.assertThat(actual) - - infix fun isEqualTo(other: A): S { - assertion.isEqualTo(other) - return myself - } - - infix fun isNotEqualTo(other: A): S { - assertion.isNotEqualTo(other) - return myself - } - - @Suppress("UNUSED_PARAMETER") - inline infix fun isInstance(bar: InstanceMatcher): S { - assertion.isInstanceOf(R::class.java) - return myself - } - - infix fun _is(objectStuff: ObjectStuff?): S { - return when (objectStuff) { - notNull -> { - assertion.isNotNull() - myself - } - else -> { - assertion.isNull() - myself - } - } - } - - infix fun describedAs(description: String): S { - assertion.`as`(description) - return myself - } - - @Suppress("UNCHECKED_CAST") - infix fun isSuchThat(assertionBlock: S.(S) -> Unit): S { - (this as S).assertionBlock(this) - return myself - } -} diff --git a/src/main/kotlin/com/memozr/assertk/AssertionHooks.kt b/src/main/kotlin/com/memozr/assertk/AssertionHooks.kt deleted file mode 100644 index 3fcbef2..0000000 --- a/src/main/kotlin/com/memozr/assertk/AssertionHooks.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.memozr.assertk - -val assert: AssertionHook get() = RealAssertionHook() -val expect: AssertionHook get() = RealAssertionHook() - -interface AssertionHook { - infix fun that(subjectUnderTest: T?) = ObjectAssert(subjectUnderTest) - infix fun that(subjectUnderTest: CharSequence?) = CharSequenceAssert(subjectUnderTest) - infix fun thatThrownBy(expressionUnderTest: () -> Unit) = ThrowableAssertion(expressionUnderTest) -} - -class RealAssertionHook : AssertionHook - - diff --git a/src/main/kotlin/com/memozr/assertk/CharSequenceAssert.kt b/src/main/kotlin/com/memozr/assertk/CharSequenceAssert.kt deleted file mode 100644 index 71634ab..0000000 --- a/src/main/kotlin/com/memozr/assertk/CharSequenceAssert.kt +++ /dev/null @@ -1,147 +0,0 @@ -package com.memozr.assertk - -import com.memozr.assertk.CharSequenceAssertTest.* -import org.assertj.core.api.AbstractCharSequenceAssert -import org.assertj.core.api.Assertions.assertThat -import java.util.regex.Pattern - -enum class CharSequenceAssertTest { - nullOrEmpty, empty, notEmpty -} - -class CharSequenceAssert internal constructor( - private val subject: CharSequence?, - override val assertion: AbstractCharSequenceAssert<*, out CharSequence> = assertThat(subject)) : - AbstractAssertBuilder(subject, CharSequenceAssert::class.java) { - - object onlyDigits - - infix fun _is(assertionTest: CharSequenceAssertTest): CharSequenceAssert { - when (assertionTest) { - empty -> assertion.isEmpty() - notEmpty -> assertion.isNotEmpty() - nullOrEmpty -> assertion.isNullOrEmpty() - } - return this - } - - infix fun hasSize(expectedSize: Int): CharSequenceAssert { - assertion.hasSize(expectedSize) - return this - } - - infix fun hasLineCount(expectedLineCount: Int): CharSequenceAssert { - assertion.hasLineCount(expectedLineCount) - return this - } - - infix fun isEqualToIgnoringCase(expected: CharSequence): CharSequenceAssert { - assertion.isEqualToIgnoringCase(expected) - return this - } - - infix fun isNotEqualToIgnoringCase(expected: CharSequence): CharSequenceAssert { - assertion.isNotEqualToIgnoringCase(expected) - return this - } - - infix fun contains(onlyDigits: onlyDigits): CharSequenceAssert { - assertion.containsOnlyDigits() - return this - } - - infix fun contains(expected: CharSequence): CharSequenceAssert { - assertion.contains(expected) - return this - } - - infix fun containsOnlyOnce(expected: CharSequence): CharSequenceAssert { - assertion.containsOnlyOnce(expected) - return this - } - - infix fun contains(expected: Iterable): CharSequenceAssert { - assertion.contains(expected) - return this - } - - infix fun containsSequence(expected: Iterable): CharSequenceAssert { - assertion.containsSequence(expected) - return this - } - - infix fun containsIgnoringCase(expected: CharSequence): CharSequenceAssert { - assertion.containsIgnoringCase(expected) - return this - } - - infix fun doesNotContain(expected: CharSequence): CharSequenceAssert { - assertion.doesNotContain(expected) - return this - } - - infix fun startsWith(expected: CharSequence): CharSequenceAssert { - assertion.startsWith(expected) - return this - } - - infix fun doesNotStartWith(expected: CharSequence): CharSequenceAssert { - assertion.doesNotStartWith(expected) - return this - } - - infix fun endsWith(expected: CharSequence): CharSequenceAssert { - assertion.endsWith(expected) - return this - } - - infix fun doesNotEndWith(expected: CharSequence): CharSequenceAssert { - assertion.doesNotEndWith(expected) - return this - } - - infix fun matches(expected: Pattern): CharSequenceAssert { - assertion.matches(expected) - return this - } - - infix fun doesNotMatch(expected: Pattern): CharSequenceAssert { - assertion.doesNotMatch(expected) - return this - } - - infix fun matches(expected: CharSequence): CharSequenceAssert { - assertion.matches(expected) - return this - } - - infix fun doesNotMatch(expected: CharSequence): CharSequenceAssert { - assertion.doesNotMatch(expected) - return this - } - - infix fun isEqualToIgnoringWhitespace(expected: CharSequence): CharSequenceAssert { - assertion.isEqualToIgnoringWhitespace(expected) - return this - } - - infix fun isNotEqualToIgnoringWhitespace(expected: CharSequence): CharSequenceAssert { - assertion.isNotEqualToIgnoringWhitespace(expected) - return this - } - - infix fun isSubstringOf(expected: CharSequence): CharSequenceAssert { - assertion.isSubstringOf(expected) - return this - } - - infix fun containsPattern(pattern: CharSequence): CharSequenceAssert { - assertion.containsPattern(pattern) - return this - } - - infix fun containsPattern(pattern: Pattern): CharSequenceAssert { - assertion.containsPattern(pattern) - return this - } -} \ No newline at end of file diff --git a/src/main/kotlin/com/memozr/assertk/ObjectAssert.kt b/src/main/kotlin/com/memozr/assertk/ObjectAssert.kt deleted file mode 100644 index 1308ac5..0000000 --- a/src/main/kotlin/com/memozr/assertk/ObjectAssert.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.memozr.assertk - -import org.assertj.core.api.AbstractObjectAssert -import org.assertj.core.api.Assertions - -class ObjectAssert internal constructor( - actual: A?, - override val assertion: AbstractObjectAssert<*, A> = Assertions.assertThat(actual)) : - AbstractAssertBuilder, A>(actual, ObjectAssert::class.java) \ No newline at end of file diff --git a/src/main/kotlin/com/memozr/assertk/ThrowableAssertion.kt b/src/main/kotlin/com/memozr/assertk/ThrowableAssertion.kt deleted file mode 100644 index aa22b6c..0000000 --- a/src/main/kotlin/com/memozr/assertk/ThrowableAssertion.kt +++ /dev/null @@ -1,55 +0,0 @@ -package com.memozr.assertk - -import org.assertj.core.api.AbstractThrowableAssert -import org.assertj.core.api.Assertions - -class ThrowableAssertion( - func: () -> Unit, - private val assertion: AbstractThrowableAssert<*, out Throwable> = Assertions.assertThatThrownBy(func)) { - - infix fun hasMessage(message: String): ThrowableAssertion { - assertion.hasMessage(message) - return this - } - - infix fun hasCause(throwable: Throwable): ThrowableAssertion { - assertion.hasCause(throwable) - return this - } - - infix fun has(noCause: noCause): ThrowableAssertion { - assertion.hasNoCause() - return this - } - - infix fun hasMessageStartingWith(message: String): ThrowableAssertion { - assertion.hasMessageStartingWith(message) - return this - } - - infix fun hasMessageEndingWith(message: String): ThrowableAssertion { - assertion.hasMessageEndingWith(message) - return this - } - - infix fun hasMessageContaining(message: String): ThrowableAssertion { - assertion.hasMessageContaining(message) - return this - } - - infix fun hasStackTraceContaining(message: String): ThrowableAssertion { - assertion.hasStackTraceContaining(message) - return this - } - - infix fun hasCauseExactlyInstanceOf(throwable: Class): ThrowableAssertion { - assertion.hasCauseExactlyInstanceOf(throwable) - return this - } - - infix fun and(assertions: ThrowableAssertion.(ThrowableAssertion) -> Unit) { - assertions(this) - } - - object noCause -} \ No newline at end of file diff --git a/src/test/kotlin/com/memozr/assertk/CharSequence assert test.kt b/src/test/kotlin/com/memozr/assertk/CharSequence assert test.kt deleted file mode 100644 index 07d6689..0000000 --- a/src/test/kotlin/com/memozr/assertk/CharSequence assert test.kt +++ /dev/null @@ -1,204 +0,0 @@ -package com.memozr.assertk - -import com.memozr.assertk.CharSequenceAssert.onlyDigits -import com.memozr.assertk.CharSequenceAssertTest.* -import com.nhaarman.mockito_kotlin.spy -import com.nhaarman.mockito_kotlin.verify -import org.assertj.core.api.AbstractCharSequenceAssert -import org.assertj.core.api.Assertions.assertThat -import org.junit.Test -import java.util.regex.Pattern - -class `CharSequence assert test` { - lateinit var mockAssertion: AbstractCharSequenceAssert<*, out CharSequence> - val _expect = object : AssertionHook { - override fun that(subjectUnderTest: CharSequence?): CharSequenceAssert { - mockAssertion = spy(assertThat(subjectUnderTest)) - return CharSequenceAssert(subjectUnderTest, mockAssertion) - } - } - val chained = "" - infix fun CharSequenceAssert.canBe(dummyValue: CharSequence) : CharSequenceAssert = this - - @Test - fun `isNullOrEmpty checks for nullity or emptiness`() { - val nullString: String? = null - _expect that "" _is nullOrEmpty canBe chained - _expect that nullString _is nullOrEmpty - _expect thatThrownBy { - _expect that "not null or empty" _is nullOrEmpty - } - verify(mockAssertion).isNullOrEmpty() - } - - @Test - fun `isEmpty checks only for emptiness`() { - _expect that "" _is empty canBe chained - verify(mockAssertion).isEmpty() - } - - @Test - fun `isNotEmpty checks for non emptiness`() { - _expect that "not empty" _is notEmpty canBe chained - verify(mockAssertion).isNotEmpty() - } - - @Test - fun hasSize() { - _expect that "four" hasSize 4 canBe chained - verify(mockAssertion).hasSize(4) - } - - @Test - fun hasLineCount() { - _expect that "foo \n bar" hasLineCount 2 canBe chained - verify(mockAssertion).hasLineCount(2) - } - - @Test - fun isEqualToIgnoreCase() { - _expect that "FOO" isEqualToIgnoringCase "foo" canBe chained - verify(mockAssertion).isEqualToIgnoringCase("foo") - } - - @Test - fun isNotEqualToIgnoringCase() { - _expect that "FOO" isNotEqualToIgnoringCase "bar" canBe chained - verify(mockAssertion).isNotEqualToIgnoringCase("bar") - } - - @Test - fun containsOnlyDigits() { - _expect that "1234" contains onlyDigits canBe chained - verify(mockAssertion).containsOnlyDigits() - } - - @Test - fun containsOnlyOnce() { - _expect that "foo bar" containsOnlyOnce "foo" canBe chained - verify(mockAssertion).containsOnlyOnce("foo") - } - - @Test - fun `contains CharSequence`() { - _expect that "foo" contains "oo" canBe chained - verify(mockAssertion).contains("oo") - } - - @Test - fun `contains iterable`() { - _expect that "foo bar" contains listOf("ar", "oo") canBe chained - verify(mockAssertion).contains(listOf("ar", "oo")) - } - - @Test - fun `containsSequence CharSequence`() { - _expect that "foo bar" containsSequence listOf("oo", "ar") canBe chained - verify(mockAssertion).containsSequence(listOf("oo", "ar")) - } - - @Test - fun containsIgnoringCase() { - _expect that "foo bar" containsIgnoringCase "FOO" canBe chained - verify(mockAssertion).containsIgnoringCase("FOO") - } - - @Test - fun doesNotContain() { - _expect that "foo" doesNotContain "bar" canBe chained - verify(mockAssertion).doesNotContain("bar") - } - - @Test - fun startsWith() { - _expect that "foo" startsWith "fo" canBe chained - verify(mockAssertion).startsWith("fo") - } - - @Test - fun doesNotStartWith() { - _expect that "foo" doesNotStartWith "oo" canBe chained - verify(mockAssertion).doesNotStartWith("oo") - } - - @Test - fun endsWith() { - _expect that "foo" endsWith "oo" canBe chained - verify(mockAssertion).endsWith("oo") - } - - @Test - fun doesNotEndWith() { - _expect that "foo" doesNotEndWith "fo" canBe chained - verify(mockAssertion).doesNotEndWith("fo") - } - - @Test - fun `matches Pattern`() { - val expected = Pattern.compile("f\\w*\\sb.*") - _expect that "foo bar" matches expected canBe chained - verify(mockAssertion).matches(expected) - } - - @Test - fun `doesNotMatch Pattern`() { - val expected = Pattern.compile("f\\w*\\sf.*") - _expect that "foo bar" doesNotMatch expected canBe chained - verify(mockAssertion).doesNotMatch(expected) - } - - @Test - fun `matches CharSequence`() { - val expected = "f\\w*\\sb.*" - _expect that "foo bar" matches expected canBe chained - verify(mockAssertion).matches(expected) - } - - @Test - fun `doesNotMatch CharSequence`() { - val expected = "f\\w*\\sf.*" - _expect that "foo bar" doesNotMatch expected canBe chained - verify(mockAssertion).doesNotMatch(expected) - } - - @Test - fun isEqualToIgnoringWhitespace() { - _expect that "foobar" isEqualToIgnoringWhitespace "foobar " canBe chained - verify(mockAssertion).isEqualToIgnoringWhitespace("foobar ") - } - - @Test - fun isNotEqualToIgnoringWhitespace() { - _expect that "foobar" isNotEqualToIgnoringWhitespace "bar " canBe chained - verify(mockAssertion).isNotEqualToIgnoringWhitespace("bar ") - } - - @Test - fun isSubstringOf() { - val expected = "foobar" - _expect that "foo" isSubstringOf expected canBe chained - verify(mockAssertion).isSubstringOf(expected) - } - - @Test - fun `containsPattern CharSequence`() { - val pattern = "f\\w" - _expect that "foo bar" containsPattern pattern canBe chained - verify(mockAssertion).containsPattern(pattern) - } - - @Test - fun `containsPattern Pattern`() { - val pattern = Pattern.compile("f\\w") - _expect that "foo bar" containsPattern pattern canBe chained - verify(mockAssertion).containsPattern(pattern) - } - - @Test - fun `Block syntax is supported`() { - _expect that "foo" isSuchThat { - it contains "foo" - it isSubstringOf "fooBar" - } - } -} \ No newline at end of file diff --git a/src/test/kotlin/com/memozr/assertk/Object assert test.kt b/src/test/kotlin/com/memozr/assertk/Object assert test.kt deleted file mode 100644 index 9982c7f..0000000 --- a/src/test/kotlin/com/memozr/assertk/Object assert test.kt +++ /dev/null @@ -1,103 +0,0 @@ -package com.memozr.assertk - -import com.memozr.assertk.ObjectStuff.notNull -import com.nhaarman.mockito_kotlin.spy -import com.nhaarman.mockito_kotlin.verify -import org.assertj.core.api.AbstractObjectAssert -import org.assertj.core.api.Assertions -import org.junit.Test - -class `Object assert test` { - val nullObject: Any? = null - - lateinit var mockAssertion: AbstractObjectAssert<*, Any> - val _expect = object : AssertionHook { - override fun that(subjectUnderTest: A?): ObjectAssert { - val spy: AbstractObjectAssert<*, A?>? = spy(Assertions.assertThat(subjectUnderTest)) - mockAssertion = spy as AbstractObjectAssert<*, Any> - return ObjectAssert(subjectUnderTest, mockAssertion) as ObjectAssert - } - } - - @Test - fun `isEqualTo performs logical equality`() { - _expect that Unit isEqualTo Unit - - val expected = Any() - _expect thatThrownBy { - _expect that Any() isEqualTo expected - } - - verify(mockAssertion).isEqualTo(expected) - } - - @Test - fun `isNotEqualTo performs logical inequality`() { - _expect that Any() isNotEqualTo Any() - - _expect thatThrownBy { - _expect that Unit isNotEqualTo Unit - } - - verify(mockAssertion).isNotEqualTo(Unit) - } - - @Test - fun `_is notNull checks whether object is null`() { - _expect that Any() _is notNull - - _expect thatThrownBy { - _expect that nullObject _is notNull - } - verify(mockAssertion).isNotNull() - } - - @Test - fun `_is null checks whether object is not null`() { - _expect that nullObject _is null - - _expect thatThrownBy { - _expect that Any() _is null - } - verify(mockAssertion).isNull() - } - - @Test - fun `isInstance of checks for object instance`() { - val anObject = Any() - - _expect that anObject isInstance of() - verify(mockAssertion).isInstanceOf(Any::class.java) - - _expect thatThrownBy { - _expect that anObject isInstance of() - } - - } - - @Test - fun `describedAs describes the object when referring to it in failure messages`() { - _expect thatThrownBy { - _expect that Any() describedAs "A labeled object" isInstance of() - } hasMessageContaining "A labeled object" - - verify(mockAssertion).describedAs("A labeled object") - } - - @Test - fun `block _expections are supported`() { - _expect that Any() isSuchThat { - it _is notNull - it isInstance of() - it isNotEqualTo Unit - it isNotEqualTo Any() - } - - _expect thatThrownBy { - _expect that Any() isSuchThat { - it _is null - it isInstance of() - } - } - } -} diff --git a/src/test/kotlin/com/memozr/assertk/Throwable assert test.kt b/src/test/kotlin/com/memozr/assertk/Throwable assert test.kt deleted file mode 100644 index a406f1c..0000000 --- a/src/test/kotlin/com/memozr/assertk/Throwable assert test.kt +++ /dev/null @@ -1,156 +0,0 @@ -package com.memozr.assertk - -import com.memozr.assertk.ThrowableAssertion.noCause -import com.nhaarman.mockito_kotlin.spy -import com.nhaarman.mockito_kotlin.verify -import org.assertj.core.api.AbstractThrowableAssert -import org.assertj.core.api.Assertions -import org.junit.Test - -class `Throwable assert test` { - val aThrowable = Throwable() - - private fun aThrowableWithCause() = Throwable("hello", aThrowable) - - lateinit var mockAssertion: AbstractThrowableAssert<*, out Throwable> - val _expect = object : AssertionHook { - override fun thatThrownBy(expressionUnderTest: () -> Unit): ThrowableAssertion { - mockAssertion = spy(Assertions.assertThatThrownBy(expressionUnderTest)) - return ThrowableAssertion(expressionUnderTest, mockAssertion) - } - } - - @Test - fun `fails when no exception is thrown`() { - Assertions.assertThatThrownBy { - _expect thatThrownBy { } - } - } - - @Test - fun `passes when exception is thrown`() { - _expect thatThrownBy { throw Throwable() } - } - - @Test - fun `passes when throwable message matches the specified message`() { - _expect thatThrownBy { throw Throwable("hello") } hasMessage "hello" - verify(mockAssertion).hasMessage("hello") - } - - @Test - fun `fails when throwable message does not match specified mesage`() { - expect thatThrownBy { - _expect thatThrownBy { throw Throwable("this") } hasMessage "that" - } - verify(mockAssertion).hasMessage("that") - } - - @Test - fun `passes when throwable cause and specified cause match`() { - _expect thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable hasMessage "hello" - _expect thatThrownBy { throw aThrowableWithCause() } hasCause aThrowable - verify(mockAssertion).hasCause(aThrowable) - } - - @Test - fun `fails when throwable cause and specified cause do not match`() { - val exception = Exception() - expect thatThrownBy { - _expect thatThrownBy { throw aThrowableWithCause() } hasCause exception - } - verify(mockAssertion).hasCause(exception) - } - - @Test - fun `hasNoCause passes when throwable has no cause`() { - _expect thatThrownBy { throw aThrowable } has noCause - verify(mockAssertion).hasNoCause() - } - - @Test - fun `hasNoCause fails when throwable has cause`() { - expect thatThrownBy { - _expect thatThrownBy { throw aThrowableWithCause() } has noCause - } - verify(mockAssertion).hasNoCause() - } - - @Test - fun `hasMessageStartingWith checks throwable message starts with specified substring`() { - _expect thatThrownBy { throw Throwable("exception foo") } hasMessageStartingWith "exc" - verify(mockAssertion).hasMessageStartingWith("exc") - expect thatThrownBy { - _expect thatThrownBy { throw Throwable("excepiton foo") } hasMessageStartingWith "foo" - } - verify(mockAssertion).hasMessageStartingWith("foo") - } - - @Test - fun `hasMessageEndingWith checks throwable message ends with specified substring`() { - _expect thatThrownBy { throw Throwable("exception foo") } hasMessageEndingWith "foo" - verify(mockAssertion).hasMessageEndingWith("foo") - expect thatThrownBy { - _expect thatThrownBy { throw Throwable("exception foo") } hasMessageEndingWith "exec" - } - verify(mockAssertion).hasMessageEndingWith("exec") - } - - @Test - fun `hasMessageContaining checks throwable message ends with specified substring`() { - _expect thatThrownBy { throw Throwable("exception foo") } hasMessageContaining "foo" - verify(mockAssertion).hasMessageContaining("foo") - - expect thatThrownBy { - _expect thatThrownBy { throw Throwable("exception foo") } hasMessageContaining "lololol" - } - verify(mockAssertion).hasMessageContaining("lololol") - } - - @Test - fun `hasStackTraceContaining checks stacktrace contains specified string`() { - _expect thatThrownBy { throw aThrowable } hasStackTraceContaining "init" - verify(mockAssertion).hasStackTraceContaining("init") - - expect thatThrownBy { - _expect thatThrownBy { throw Throwable("exception foo") } hasStackTraceContaining "not in there!!" - } - verify(mockAssertion).hasStackTraceContaining("not in there!!") - } - - @Test - fun `hasCauseExactlyInstanceOf checks throwable cause is exactly instance of specified class`() { - _expect thatThrownBy { throw Throwable(Throwable()) } hasCauseExactlyInstanceOf Throwable::class.java - verify(mockAssertion).hasCauseExactlyInstanceOf(Throwable::class.java) - - expect thatThrownBy { - _expect thatThrownBy { throw Throwable(Exception()) } hasCauseExactlyInstanceOf Throwable::class.java - } - verify(mockAssertion).hasCauseExactlyInstanceOf(Throwable::class.java) - - expect thatThrownBy { - _expect thatThrownBy { throw Throwable(Throwable()) } hasCauseExactlyInstanceOf Exception::class.java - } - verify(mockAssertion).hasCauseExactlyInstanceOf(Exception::class.java) - } - - @Test - fun `block syntax is supported`() { - _expect thatThrownBy { - throw Throwable("exception foo", Throwable()) - } and { - it hasMessage "exception foo" - it hasCause Throwable() - it hasCauseExactlyInstanceOf Throwable::class.java - it hasMessageContaining "foo" - it hasMessageStartingWith "ex" - it hasMessageEndingWith "foo" - } - - _expect thatThrownBy { - _expect thatThrownBy { throw Throwable("exception foo", Throwable()) } and { - it hasMessage "blah blah" - } - } - } -} \ No newline at end of file