Skip to content

Commit 558b239

Browse files
authored
Merge Feature/1 collection assertions (#8)
* move readme to package directory * move class files to package directory * Move collection condition to dedicated file * Move map conditions to dedicated file * move scenario test to package directory * Add collections and map condition empty unit tests * replace assert/assume functions with synthetic vals * Add assert/assume documentation * inherit FailedAssumption from Exception not Throwable * replace assume function with val * Fix package and imports * remove implied throwable * Add statement evaluation function in statement scope * create assert/assumeThat convenience functions * rename statement definition file * Add assertThat and assumeThat docs * replace result condition with ResultStatements * replace stringCondition with StringStatements * replace collectionCondition with CollectionStatements * replace NumberCondition with NumberStatements * Move isIn and notIn condition functions to CollectionStatements * update scenario test * remove assume/assert properties * remove unused statement scope * Move object conditions to ObjectStatements * optimize imports * replace map conditions with map statements * Replace Condition.and with statement combiner and function * remove condition tests * Add failureMessage separate from evaluation function in Statement subclasses * Fix and with wrong failure message * abstract shared behavior between assertThat and assumeThat * Add statement unit test * Copy multiplatform workflow * publish only select packages to github packages
1 parent 69baea7 commit 558b239

File tree

15 files changed

+454
-192
lines changed

15 files changed

+454
-192
lines changed

.github/workflows/distribute.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ jobs:
2424
git push origin $versionString
2525
- name: Publish
2626
run: |
27-
./gradlew publishAllPublicationsToGitHubPackagesRepository \
27+
./gradlew publishKotlinMultiplatformPublicationToGitHubPackagesRepository \
28+
publishJvmPublicationToGitHubPackagesRepository \
29+
publishAndroidDebugPublicationToGitHubPackagesRepository \
30+
publishAndroidReleasePublicationToGitHubPackagesRepository \
2831
-Pgithub.user=${{ secrets.BUILD_USER }} \
2932
-Pgithub.token=${{ secrets.BUILD_TOKEN }}
30-
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Main PR Checks
2+
run-name: Verify `${{ github.head_ref }}` before merge to `main` by @${{ github.actor }}
3+
4+
on:
5+
pull_request:
6+
types:
7+
- opened # initially opened
8+
- reopened # closed then opened again
9+
- synchronize # any changes pushed
10+
branches:
11+
- main
12+
paths-ignore: # Only run checks on changes to code
13+
- "**/docs/*"
14+
- "**/README.md"
15+
16+
jobs:
17+
build-app-android:
18+
if: github.event.pull_request.draft == false
19+
runs-on: ubuntu-22.04
20+
steps:
21+
- name: Setup
22+
uses: Pointyware/.github/.github/actions/checkout-java-gradle@b70bbae19637171d6cf9311f06a59f1c15e2598b
23+
- name: Build Android
24+
run: ./gradlew :assertions:assembleDebug :assertions:assembleRelease -Pgithub.user=${{ secrets.BUILD_USER }} -Pgithub.token=${{ secrets.BUILD_TOKEN }}
25+
- name: Android Debug Unit Tests
26+
run: ./gradlew :assertions:testDebugUnitTest -Pgithub.user=${{ secrets.BUILD_USER }} -Pgithub.token=${{ secrets.BUILD_TOKEN }}
27+
28+
build-app-ios:
29+
if: github.event.pull_request.draft == false && false
30+
runs-on: macos-14
31+
steps:
32+
- name: Setup
33+
uses: Pointyware/.github/.github/actions/checkout-java-gradle@b70bbae19637171d6cf9311f06a59f1c15e2598b
34+
- name: Build iOS
35+
run: ./gradlew :assertions:iosX64Binaries -Pgithub.user=${{ secrets.BUILD_USER }} -Pgithub.token=${{ secrets.BUILD_TOKEN }}
36+
- name: Test iOS
37+
run: ./gradlew :assertions:iosX64Test -Pgithub.user=${{ secrets.BUILD_USER }} -Pgithub.token=${{ secrets.BUILD_TOKEN }}
38+
39+
build-app-desktop:
40+
if: github.event.pull_request.draft == false
41+
runs-on: ubuntu-22.04
42+
steps:
43+
- name: Setup
44+
uses: Pointyware/.github/.github/actions/checkout-java-gradle@b70bbae19637171d6cf9311f06a59f1c15e2598b
45+
- name: Build Desktop
46+
run: ./gradlew :assertions:assembleJvm -Pgithub.user=${{ secrets.BUILD_USER }} -Pgithub.token=${{ secrets.BUILD_TOKEN }}
47+
- name: Test Desktop
48+
run: ./gradlew :assertions:jvmTest -Pgithub.user=${{ secrets.BUILD_USER }} -Pgithub.token=${{ secrets.BUILD_TOKEN }}
49+
50+
build-app-browser:
51+
if: github.event.pull_request.draft == false && false
52+
runs-on: ubuntu-22.04
53+
steps:
54+
- name: Setup
55+
uses: Pointyware/.github/.github/actions/checkout-java-gradle@b70bbae19637171d6cf9311f06a59f1c15e2598b
56+
- name: Build Browser
57+
run: ./gradlew :assertions:assembleBrowser -Pgithub.user=${{ secrets.BUILD_USER }} -Pgithub.token=${{ secrets.BUILD_TOKEN }}
58+
- name: Test Browser
59+
run: ./gradlew :assertions:jsTest -Pgithub.user=${{ secrets.BUILD_USER }} -Pgithub.token=${{ secrets.BUILD_TOKEN }}

assertions/src/commonMain/kotlin/Condition.kt

Lines changed: 0 additions & 141 deletions
This file was deleted.

assertions/src/commonMain/kotlin/Statements.kt

Lines changed: 0 additions & 46 deletions
This file was deleted.
File renamed without changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.pointyware.kass.assertions
2+
3+
import kotlin.test.Asserter
4+
import kotlin.test.asserter
5+
6+
/**
7+
* Represents a statement that can be evaluated against a subject. The statement also provides a
8+
* [failureMessage] in case the evaluation returns false.
9+
*/
10+
interface Statement<T> {
11+
fun evaluate(subject: T): Boolean
12+
val failureMessage: String
13+
}
14+
15+
/**
16+
* Combines the receiver statement with [other] to create a new statement that evaluates both.
17+
*/
18+
fun <T> Statement<T>.and(other: Statement<T>) = object: Statement<T> {
19+
20+
override fun evaluate(subject: T): Boolean {
21+
return this@and.evaluate(subject) && other.evaluate(subject)
22+
}
23+
24+
override val failureMessage: String
25+
get() = this@and.failureMessage + " or " + other.failureMessage
26+
}
27+
28+
private inline fun <T> throwOnEvaluationFail(subject: T, statement: Statement<T>, asserter: Asserter) {
29+
if (statement.evaluate(subject).not()) {
30+
asserter.fail(statement.failureMessage)
31+
}
32+
}
33+
34+
/**
35+
* Evaluates the given [statement] with the given [subject] and the default asserter.
36+
*/
37+
fun <T:Any?> assertThat(subject: T, statement: Statement<T>) {
38+
throwOnEvaluationFail(subject, statement, asserter)
39+
}
40+
41+
/**
42+
* Evaluates the given [statement] with the given [subject] and a presumptuous asserter. When the
43+
* statement fails, it will throw a [FailedAssumption].
44+
*/
45+
fun <T:Any?> assumeThat(subject: T, statement: Statement<T>) {
46+
throwOnEvaluationFail(subject, statement, presumptuousAsserter)
47+
}
48+
49+
private val presumptuousAsserter = object: Asserter {
50+
override fun fail(message: String?): Nothing {
51+
throw FailedAssumption("Assumption failed - $message", null)
52+
}
53+
54+
override fun fail(message: String?, cause: Throwable?): Nothing {
55+
throw FailedAssumption("Assumption failed - $message", cause)
56+
}
57+
}
58+
59+
class FailedAssumption(msg: String?, cause: Throwable?): Exception(msg, cause)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.pointyware.kass.assertions.collections
2+
3+
import org.pointyware.kass.assertions.Statement
4+
5+
/**
6+
*
7+
*/
8+
object CollectionStatements {
9+
fun <T> contains(item: T) = object: Statement<Collection<T>> {
10+
override fun evaluate(subject: Collection<T>): Boolean {
11+
return item in subject
12+
}
13+
14+
override val failureMessage: String
15+
get() = "The collection does not contain $item"
16+
}
17+
18+
fun <T> doesNotContain(item: T) = object: Statement<Collection<T>> {
19+
override fun evaluate(subject: Collection<T>): Boolean {
20+
return item !in subject
21+
}
22+
23+
override val failureMessage: String
24+
get() = "The collection contains $item"
25+
}
26+
27+
fun <T> isIn(items: Collection<T>) = object: Statement<T> {
28+
override fun evaluate(subject: T): Boolean {
29+
return subject in items
30+
}
31+
32+
override val failureMessage: String
33+
get() = "The item is not in $items"
34+
}
35+
36+
fun <T> isNotIn(items: Collection<T>) = object: Statement<T> {
37+
override fun evaluate(subject: T): Boolean {
38+
return subject !in items
39+
}
40+
41+
override val failureMessage: String
42+
get() = "The subject is in $items"
43+
}
44+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.pointyware.kass.assertions.collections
2+
3+
import org.pointyware.kass.assertions.Statement
4+
5+
/**
6+
*
7+
*/
8+
object MapStatements {
9+
fun <K, V> containsKey(key: K) = object: Statement<Map<K, V>> {
10+
override fun evaluate(subject: Map<K, V>): Boolean {
11+
return key in subject
12+
}
13+
14+
override val failureMessage: String
15+
get() = "The subject does not contain key $key"
16+
}
17+
18+
fun <K, V> doesNotContainKey(key: K) = object: Statement<Map<K, V>> {
19+
override fun evaluate(subject: Map<K, V>): Boolean {
20+
return key !in subject
21+
}
22+
23+
override val failureMessage: String
24+
get() = "The subject contains key $key"
25+
}
26+
}

0 commit comments

Comments
 (0)