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

Unify first-party helper functions #537

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
31 changes: 26 additions & 5 deletions assertk/src/commonMain/kotlin/assertk/assertions/any.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,38 @@ import kotlin.reflect.KProperty1
/**
* Returns an assert on the kotlin class of the value.
*/
fun Assert<Any>.kClass() = having("class") { it::class }
fun Assert<Any>.havingKClass() = having("class") { it::class }

@Deprecated(
message = "Function kClass has been renamed to havingKClass",
replaceWith = ReplaceWith("havingKClass()"),
level = DeprecationLevel.WARNING
)
fun Assert<Any>.kClass() = havingKClass()

/**
* Returns an assert on the toString method of the value.
*/
fun Assert<Any?>.toStringFun() = having("toString", Any?::toString)
fun Assert<Any?>.havingToStringFun() = having("toString", Any?::toString)

@Deprecated(
message = "Function toStringFun has been renamed to havingToStringFun",
replaceWith = ReplaceWith("havingToStringFun()"),
level = DeprecationLevel.WARNING
)
fun Assert<Any?>.toStringFun() = havingToStringFun()

/**
* Returns an assert on the hasCode method of the value.
*/
fun Assert<Any>.hashCodeFun() = having("hashCode", Any::hashCode)
fun Assert<Any>.havingHashCodeFun() = having("hashCode", Any::hashCode)

@Deprecated(
message = "Function hashCodeFun has been renamed to havingHashCodeFun",
replaceWith = ReplaceWith("havingHashCodeFun()"),
level = DeprecationLevel.WARNING
)
fun Assert<Any>.hashCodeFun() = havingHashCodeFun()

/**
* Asserts the value is equal to the expected one, using `==`.
Expand Down Expand Up @@ -110,14 +131,14 @@ fun <T> Assert<T>.isNotIn(vararg values: T) = given { actual ->
* Asserts the value has the expected string from it's [toString].
*/
fun Assert<Any?>.hasToString(string: String) {
toStringFun().isEqualTo(string)
havingToStringFun().isEqualTo(string)
}

/**
* Asserts the value has the expected hash code from it's [hashCode].
*/
fun Assert<Any>.hasHashCode(hashCode: Int) {
hashCodeFun().isEqualTo(hashCode)
havingHashCodeFun().isEqualTo(hashCode)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import assertk.assertions.support.show
/**
* Returns an assert on the CharSequence's length.
*/
fun Assert<CharSequence>.length() = having("length", CharSequence::length)
fun Assert<CharSequence>.havingLength() = having("length", CharSequence::length)

@Deprecated(
message = "Function length has been renamed to havingLength",
replaceWith = ReplaceWith("havingLength()"),
level = DeprecationLevel.WARNING
)
fun Assert<CharSequence>.length() = havingLength()

/**
* Asserts the char sequence is empty.
Expand Down Expand Up @@ -41,7 +48,7 @@ fun Assert<CharSequence?>.isNullOrEmpty() = given { actual ->
* Asserts the char sequence has the expected length.
*/
fun Assert<CharSequence>.hasLength(length: Int) {
length().isEqualTo(length)
havingLength().isEqualTo(length)
}

/**
Expand Down
11 changes: 9 additions & 2 deletions assertk/src/commonMain/kotlin/assertk/assertions/collection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ import assertk.assertions.support.*
/**
* Returns an assert on the Collection's size.
*/
fun Assert<Collection<*>>.size() = having("size", Collection<*>::size)
fun Assert<Collection<*>>.havingSize() = having("size", Collection<*>::size)

@Deprecated(
message = "Function size has been renamed to havingSize",
replaceWith = ReplaceWith("havingSize()"),
level = DeprecationLevel.WARNING
)
fun Assert<Collection<*>>.size() = havingSize()

/**
* Asserts the collection is empty.
Expand Down Expand Up @@ -40,7 +47,7 @@ fun Assert<Collection<*>?>.isNullOrEmpty() = given { actual ->
* Asserts the collection has the expected size.
*/
fun Assert<Collection<*>>.hasSize(size: Int) {
size().isEqualTo(size)
havingSize().isEqualTo(size)
}

/**
Expand Down
11 changes: 9 additions & 2 deletions assertk/src/commonMain/kotlin/assertk/assertions/map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ import assertk.assertions.support.show
/**
* Returns an assert on the Maps's size.
*/
fun Assert<Map<*, *>>.size() = having("size", Map<*, *>::size)
fun Assert<Map<*, *>>.havingSize() = having("size", Map<*, *>::size)

@Deprecated(
message = "Function size has been renamed to havingSize",
replaceWith = ReplaceWith("havingSize()"),
level = DeprecationLevel.WARNING
)
fun Assert<Map<*, *>>.size() = havingSize()

/**
* Asserts the collection is empty.
Expand Down Expand Up @@ -42,7 +49,7 @@ fun Assert<Map<*, *>?>.isNullOrEmpty() = given { actual ->
* Asserts the collection has the expected size.
*/
fun Assert<Map<*, *>>.hasSize(size: Int) {
size().isEqualTo(size)
havingSize().isEqualTo(size)
}

/**
Expand Down
41 changes: 31 additions & 10 deletions assertk/src/commonMain/kotlin/assertk/assertions/throwable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,60 @@ import assertk.all
/**
* Returns an assert on the throwable's message.
*/
fun Assert<Throwable>.message() = having("message", Throwable::message)
fun Assert<Throwable>.havingMessage() = having("message", Throwable::message)

@Deprecated(
message = "Function message has been renamed to havingMessage",
replaceWith = ReplaceWith("havingMessage()"),
level = DeprecationLevel.WARNING
)
fun Assert<Throwable>.message() = havingMessage()

/**
* Returns an assert on the throwable's cause.
*/
fun Assert<Throwable>.cause() = having("cause", Throwable::cause)
fun Assert<Throwable>.havingCause() = having("cause", Throwable::cause)

@Deprecated(
message = "Function cause has been renamed to havingCause",
replaceWith = ReplaceWith("havingCause()"),
level = DeprecationLevel.WARNING
)
fun Assert<Throwable>.cause() = havingCause()

/**
* Returns an assert on the throwable's root cause.
*/
fun Assert<Throwable>.rootCause() = having("rootCause", Throwable::rootCause)
fun Assert<Throwable>.havingRootCause() = having("rootCause", Throwable::rootCause)

@Deprecated(
message = "Function rootCause has been renamed to havingRootCause",
replaceWith = ReplaceWith("havingRootCause()"),
level = DeprecationLevel.WARNING
)
fun Assert<Throwable>.rootCause() = havingRootCause()

/**
* Asserts the throwable has the expected message.
*/
fun Assert<Throwable>.hasMessage(message: String?) {
message().isEqualTo(message)
havingMessage().isEqualTo(message)
}

/**
* Asserts the throwable contains the expected text
*/
fun Assert<Throwable>.messageContains(text: String) {
message().isNotNull().contains(text)
havingMessage().isNotNull().contains(text)
}

/**
* Asserts the throwable is similar to the expected cause, checking the type and message.
* @see [hasNoCause]
*/
fun Assert<Throwable>.hasCause(cause: Throwable) {
cause().isNotNull().all {
kClass().isEqualTo(cause::class)
havingCause().isNotNull().all {
havingKClass().isEqualTo(cause::class)
hasMessage(cause.message)
}
}
Expand All @@ -48,15 +69,15 @@ fun Assert<Throwable>.hasCause(cause: Throwable) {
* @see [hasCause]
*/
fun Assert<Throwable>.hasNoCause() {
cause().isNull()
havingCause().isNull()
}

/**
* Asserts the throwable is similar to the expected root cause, checking the type and message.
*/
fun Assert<Throwable>.hasRootCause(cause: Throwable) {
rootCause().all {
kClass().isEqualTo(cause::class)
havingRootCause().all {
havingKClass().isEqualTo(cause::class)
hasMessage(cause.message)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package test.assertk
import assertk.assertFailure
import assertk.assertions.isEqualTo
import assertk.assertions.isInstanceOf
import assertk.assertions.message
import assertk.assertions.havingMessage
import com.willowtreeapps.opentest4k.AssertionFailedError
import test.assertk.assertions.valueOrFail
import kotlin.coroutines.resume
Expand All @@ -28,7 +28,7 @@ class AssertFailureTest {
fun failure_originating_subject_not_wrapped_in_result() {
val t = assertFailsWith<AssertionFailedError> {
assertFailure { throw RuntimeException("foo") }
.message()
.havingMessage()
.isEqualTo("bar")
}
assertTrue("RuntimeException" in t.message!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ class AnyTest {

@Test
fun extracts_kClass() {
assertEquals(BasicObject::class, assertThat(subject as TestObject).kClass().valueOrFail)
assertEquals(BasicObject::class, assertThat(subject as TestObject).havingKClass().valueOrFail)
}

@Test
fun extracts_toStringFun() {
assertEquals("test", assertThat(subject).toStringFun().valueOrFail)
assertEquals("test", assertThat(subject).havingToStringFun().valueOrFail)
}

@Test
fun extracts_hashCodeFun() {
assertEquals(42, assertThat(subject).hashCodeFun().valueOrFail)
assertEquals(42, assertThat(subject).havingHashCodeFun().valueOrFail)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class CharSequenceTest {
//region props
@Test
fun extracts_length() {
assertEquals(4, assertThat("test").length().valueOrFail)
assertEquals(4, assertThat("test").havingLength().valueOrFail)
}
//endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ class ThrowableTest {

@Test
fun extracts_message() {
assertEquals(subject.message, assertThat(subject).message().valueOrFail)
assertEquals(subject.message, assertThat(subject).havingMessage().valueOrFail)
}

@Test
fun extracts_cause() {
assertEquals(cause, assertThat(subject).cause().valueOrFail)
assertEquals(cause, assertThat(subject).havingCause().valueOrFail)
}

@Test
fun extracts_root_cause() {
assertEquals(rootCause, assertThat(subject).rootCause().valueOrFail)
assertEquals(rootCause, assertThat(subject).havingRootCause().valueOrFail)
}

//region hasMessage
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package test.assertk.assertions.support

import assertk.assertThat
import assertk.assertions.hasMessage
import assertk.assertions.isEqualTo
import assertk.assertions.isFailure
import assertk.assertions.isSuccess
import assertk.assertions.message
import assertk.assertions.havingMessage
import assertk.assertions.support.expected
import assertk.assertions.support.fail
import assertk.assertions.support.show
Expand Down Expand Up @@ -236,7 +234,7 @@ class SupportTest {
@Test
fun expected_originating_throwable_included_as_cause() {
val subject = RuntimeException()
val assert = assertThat(subject).message()
val assert = assertThat(subject).havingMessage()
val error = assertFailsWith<AssertionFailedError> {
assert.expected("message")
}
Expand All @@ -246,7 +244,7 @@ class SupportTest {
@Test
fun expected_originating_failure_result_included_as_cause() {
val subject = RuntimeException()
val assert = assertThat(Result.failure<String>(subject)).isFailure().message()
val assert = assertThat(Result.failure<String>(subject)).isFailure().havingMessage()
val error = assertFailsWith<AssertionFailedError> {
assert.expected("message")
}
Expand Down
9 changes: 8 additions & 1 deletion assertk/src/jvmMain/kotlin/assertk/assertions/any.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import kotlin.reflect.full.memberProperties
/**
* Returns an assert on the java class of the value.
*/
fun <T : Any> Assert<T>.jClass() = having("class") { it::class.java }
fun <T : Any> Assert<T>.havingJClass() = having("class") { it::class.java }

@Deprecated(
message = "Function jClass has been renamed to havingJClass",
replaceWith = ReplaceWith("havingJClass()"),
level = DeprecationLevel.WARNING
)
fun <T : Any> Assert<T>.jClass() = havingJClass()

/**
* Asserts the value has the expected java class. This is an exact match, so
Expand Down
Loading