Skip to content

Commit 6b65668

Browse files
grodinJoseph Cooper
andauthored
Unify property extractor function names (#524)
- Rename `prop` to `having` with deprecations - Rename `suspendCall` to `having` with deprecations --------- Co-authored-by: Joseph Cooper <[email protected]>
1 parent 6533918 commit 6b65668

File tree

202 files changed

+518
-480
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

202 files changed

+518
-480
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Changed
10+
- renamed `prop` to `having` as part of effort to unify API naming, old name is deprecated.
11+
- renamed `suspendCall` to `having` as part of effort to unify API naming, old name is deprecated.
12+
913
## [0.28.1] 2024-04-17
1014

1115
### Added

assertk-coroutines/src/commonMain/kotlin/assertk/coroutines/assertions/any.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,16 @@ import assertk.assertions.support.appendName
1010
* @param extract The suspend function to extract the property value out of the value of the current assert.
1111
*
1212
* ```
13-
* assertThat(person).suspendCall("name()", { it.name() }).isEqualTo("Sue")
13+
* assertThat(person).having("name()", { it.name() }).isEqualTo("Sue")
1414
* ```
1515
*/
16+
suspend fun <T, P> Assert<T>.having(name: String, extract: suspend (T) -> P): Assert<P> =
17+
transform(appendName(name, separator = ".")) { extract(it) }
18+
19+
@Deprecated(
20+
message = "Function suspendCall has been renamed to having",
21+
replaceWith = ReplaceWith("having(name, extract)"),
22+
level = DeprecationLevel.WARNING
23+
)
1624
suspend fun <T, P> Assert<T>.suspendCall(name: String, extract: suspend (T) -> P): Assert<P> =
1725
transform(appendName(name, separator = ".")) { extract(it) }

assertk-coroutines/src/commonMain/kotlin/assertk/coroutines/assertions/flow.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ import assertk.assertions.support.expected
77
import assertk.assertions.support.expectedListDiff
88
import assertk.assertions.support.show
99
import kotlinx.coroutines.CancellationException
10-
import kotlinx.coroutines.ExperimentalCoroutinesApi
1110
import kotlinx.coroutines.flow.*
1211

13-
suspend fun Assert<Flow<*>>.count(): Assert<Int> = suspendCall("count()", Flow<*>::count)
12+
suspend fun Assert<Flow<*>>.count(): Assert<Int> = having("count()", Flow<*>::count)
1413

1514
/**
1615
* Asserts the flow is empty. Fails as soon as the flow delivers an element.
@@ -190,4 +189,4 @@ suspend fun Assert<Flow<*>>.containsExactly(vararg elements: Any?) = given { act
190189
}
191190

192191
private class AbortFlowException :
193-
CancellationException("Flow was aborted, no more elements needed")
192+
CancellationException("Flow was aborted, no more elements needed")

assertk-coroutines/src/commonTest/kotlin/test/assertk/coroutines/assertions/AnyTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import assertk.assertThat
44
import assertk.assertions.isEmpty
55
import assertk.assertions.isEqualTo
66
import assertk.assertions.isNotNull
7-
import assertk.coroutines.assertions.suspendCall
7+
import assertk.coroutines.assertions.having
88
import kotlin.test.Test
99
import kotlin.test.assertEquals
1010
import kotlin.test.assertFailsWith
@@ -14,22 +14,22 @@ class AnyTest {
1414
val subject = BasicObject("test")
1515

1616
@Test
17-
fun suspendCall_passes() = runTest {
18-
assertThat(subject).suspendCall("str") { it.str() }.isEqualTo("test")
17+
fun having_passes() = runTest {
18+
assertThat(subject).having("str") { it.str() }.isEqualTo("test")
1919
}
2020

2121
@Test
22-
fun suspendCall_includes_name_in_failure_message() = runTest {
22+
fun having_includes_name_in_failure_message() = runTest {
2323
val error = assertFailsWith<AssertionError> {
24-
assertThat(subject).suspendCall("str") { it.str() }.isEmpty()
24+
assertThat(subject).having("str") { it.str() }.isEmpty()
2525
}
2626
assertEquals("expected [str] to be empty but was:<\"test\"> (test)", error.message)
2727
}
2828

2929
@Test
30-
fun nested_suspendCall_include_names_in_failure_message() = runTest {
30+
fun nested_having_include_names_in_failure_message() = runTest {
3131
val error = assertFailsWith<AssertionError> {
32-
assertThat(subject).suspendCall("other") { it.other() }.suspendCall("str") { it?.str() }.isNotNull()
32+
assertThat(subject).having("other") { it.other() }.having("str") { it?.str() }.isNotNull()
3333
}
3434
assertEquals("expected [other.str] to not be null (test)", error.message)
3535
}

assertk/src/commonMain/kotlin/assertk/assertions/any.kt

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import kotlin.reflect.KProperty1
1313
/**
1414
* Returns an assert on the kotlin class of the value.
1515
*/
16-
fun Assert<Any>.kClass() = prop("class") { it::class }
16+
fun Assert<Any>.kClass() = having("class") { it::class }
1717

1818
/**
1919
* Returns an assert on the toString method of the value.
2020
*/
21-
fun Assert<Any?>.toStringFun() = prop("toString", Any?::toString)
21+
fun Assert<Any?>.toStringFun() = having("toString", Any?::toString)
2222

2323
/**
2424
* Returns an assert on the hasCode method of the value.
2525
*/
26-
fun Assert<Any>.hashCodeFun() = prop("hashCode", Any::hashCode)
26+
fun Assert<Any>.hashCodeFun() = having("hashCode", Any::hashCode)
2727

2828
/**
2929
* Asserts the value is equal to the expected one, using `==`.
@@ -146,39 +146,64 @@ fun <T : Any> Assert<T?>.isNotNull(): Assert<T> = transform { actual ->
146146
* @param extract The function to extract the property value out of the value of the current assert.
147147
*
148148
* ```
149-
* assertThat(person).prop("name", { it.name }).isEqualTo("Sue")
149+
* assertThat(person).having("name", { it.name }).isEqualTo("Sue")
150150
* ```
151151
*/
152+
fun <T, P> Assert<T>.having(name: String, extract: (T) -> P): Assert<P> =
153+
transform(appendName(name, separator = "."), extract)
154+
155+
@Deprecated(
156+
message = "Function prop has been renamed to having",
157+
replaceWith = ReplaceWith("having(name, extract)"),
158+
level = DeprecationLevel.WARNING
159+
)
152160
fun <T, P> Assert<T>.prop(name: String, extract: (T) -> P): Assert<P> =
153161
transform(appendName(name, separator = "."), extract)
154162

163+
155164
/**
156165
* Returns an assert that asserts on the given property.
157166
*
158167
* Example:
159168
* ```
160-
* assertThat(person).prop(Person::name).isEqualTo("Sue")
169+
* assertThat(person).having(Person::name).isEqualTo("Sue")
161170
* ```
162171
*
163172
* @param property Property on which to assert. The name of this
164173
* property will be shown in failure messages.
165174
*/
175+
fun <T, P> Assert<T>.having(property: KProperty1<T, P>): Assert<P> =
176+
having(property.name) { property.get(it) }
177+
178+
@Deprecated(
179+
message = "Function prop has been renamed to having",
180+
replaceWith = ReplaceWith("having(property)"),
181+
level = DeprecationLevel.WARNING
182+
)
166183
fun <T, P> Assert<T>.prop(property: KProperty1<T, P>): Assert<P> =
167-
prop(property.name) { property.get(it) }
184+
having(property.name) { property.get(it) }
168185

169186
/**
170187
* Returns an assert that asserts on the result of calling the given function.
171188
*
172189
* Example:
173190
* ```
174-
* assertThat(person).prop(Person::nameAsLowerCase).isEqualTo("sue")
191+
* assertThat(person).having(Person::nameAsLowerCase).isEqualTo("sue")
175192
* ```
176193
*
177194
* @param callable Callable on which to assert. The name of this
178195
* callable will be shown in the failure messages.
179196
*/
197+
fun <T, R, F> Assert<T>.having(callable: F): Assert<R> where F : (T) -> R, F : KCallable<R> =
198+
having(callable.name, callable)
199+
200+
@Deprecated(
201+
message = "Function prop has been renamed to having",
202+
replaceWith = ReplaceWith("having(callable)"),
203+
level = DeprecationLevel.WARNING
204+
)
180205
fun <T, R, F> Assert<T>.prop(callable: F): Assert<R> where F : (T) -> R, F : KCallable<R> =
181-
prop(callable.name, callable)
206+
having(callable.name, callable)
182207

183208
/**
184209
* Asserts the value has the expected kotlin class. This is an exact match, so `assertThat("test").hasClass<String>()`
@@ -219,6 +244,7 @@ fun <T : Any> Assert<T>.doesNotHaveClass(kclass: KClass<out T>) = given { actual
219244
if (kclass != actual::class) return
220245
expected("to not have class:${show(kclass)}")
221246
}
247+
222248
/**
223249
* Asserts the value is not an instance of the expected kotlin class. Both
224250
* `assertThat("test").isNotInstanceOf<String>()` and `assertThat("test").isNotInstanceOf<String>()` fail.

assertk/src/commonMain/kotlin/assertk/assertions/array.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import assertk.assertions.support.*
77
/**
88
* Returns an assert on the Arrays's size.
99
*/
10-
fun Assert<Array<*>>.size() = prop("size") { it.size }
10+
fun Assert<Array<*>>.size() = having("size") { it.size }
1111

1212
/**
1313
* Asserts the array contents are equal to the expected one, using [contentDeepEquals].

assertk/src/commonMain/kotlin/assertk/assertions/charsequence.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import assertk.assertions.support.show
77
/**
88
* Returns an assert on the CharSequence's length.
99
*/
10-
fun Assert<CharSequence>.length() = prop("length", CharSequence::length)
10+
fun Assert<CharSequence>.length() = having("length", CharSequence::length)
1111

1212
/**
1313
* Asserts the char sequence is empty.

assertk/src/commonMain/kotlin/assertk/assertions/collection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import assertk.assertions.support.*
66
/**
77
* Returns an assert on the Collection's size.
88
*/
9-
fun Assert<Collection<*>>.size() = prop("size", Collection<*>::size)
9+
fun Assert<Collection<*>>.size() = having("size", Collection<*>::size)
1010

1111
/**
1212
* Asserts the collection is empty.

assertk/src/commonMain/kotlin/assertk/assertions/map.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import assertk.assertions.support.show
88
/**
99
* Returns an assert on the Maps's size.
1010
*/
11-
fun Assert<Map<*, *>>.size() = prop("size", Map<*, *>::size)
11+
fun Assert<Map<*, *>>.size() = having("size", Map<*, *>::size)
1212

1313
/**
1414
* Asserts the collection is empty.

assertk/src/commonMain/kotlin/assertk/assertions/throwable.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package assertk.assertions
22

33
import assertk.Assert
4-
import assertk.ValueAssert
54
import assertk.all
65

76
/**
87
* Returns an assert on the throwable's message.
98
*/
10-
fun Assert<Throwable>.message() = prop("message", Throwable::message)
9+
fun Assert<Throwable>.message() = having("message", Throwable::message)
1110

1211
/**
1312
* Returns an assert on the throwable's cause.
1413
*/
15-
fun Assert<Throwable>.cause() = prop("cause", Throwable::cause)
14+
fun Assert<Throwable>.cause() = having("cause", Throwable::cause)
1615

1716
/**
1817
* Returns an assert on the throwable's root cause.
1918
*/
20-
fun Assert<Throwable>.rootCause() = prop("rootCause", Throwable::rootCause)
19+
fun Assert<Throwable>.rootCause() = having("rootCause", Throwable::rootCause)
2120

2221
/**
2322
* Asserts the throwable has the expected message.

0 commit comments

Comments
 (0)