Skip to content

nmrsmn/kotlin-expect

Repository files navigation

Kotlin Expect

A Kotlin Multiplatform assertion/expection library designed to provide expressive, type-safe test assertions across all supported platforms.

Status: 🚧 Work in progress β€” the API is under active development and not yet published.

Supported platforms

Platform Status
JVM βœ…
iOS (arm64) βœ…
iOS (simulatorArm64) βœ…

Requirements

  • JDK: 21+
  • Gradle: 9.5.0 (included via wrapper)
  • Kotlin: 2.3.21

Getting Started

Build the project

./gradlew build

Run checks

./gradlew check

This will run code quality checks (detekt, ktlint) and code coverage verification (Kover).

Testing

Tests are written using testBalloon as the testing framework. Tests reside in the commonTest source set and should run across all supported platforms.

# Run all tests
./gradlew allTests

# Run JVM tests only
./gradlew jvmTest

Code Coverage

Code coverage is measured by Kover and aggregated across all modules. A verification rule enforces 100% branch coverage per class.

# Run coverage verification
./gradlew koverVerify

# Generate an HTML coverage report
./gradlew koverHtmlReport

Code Quality

The project enforces code quality through two tools, both integrated into the check lifecycle:

  • detekt β€” static code analysis for Kotlin, applied per module via a convention plugin
  • ktlint β€” Kotlin linter and formatter, run across all source files from the root project
# Run all checks (tests + detekt + ktlint + kover)
./gradlew check

# Run only ktlint
./gradlew ktlintCheck

# Run only detekt
./gradlew detekt

Examples

Use expectThat to start an assertion on any value, then chain one or more assertions:

Equality

expectThat(42)
    .isEqualTo(42)

expectThat("hello")
    .isEqualTo("hello")

expectThat(listOf(1, 2, 3))
    .isEqualTo(listOf(1, 2, 3))

Type checking

expectThat("hello")
    .isA<String>()

expectThat(1L)
    .isA<Number>()

Null checking

expectThat(null)
    .isNull()

Boolean checking

expectThat(true)
    .isTrue()

expectThat(false)
    .isFalse()

CharSequence assertions

expectThat("")
    .isEmpty()

expectThat("hello")
    .trim()
    .isNotEmpty()
    .isNotBlank()
    .hasLength(5)

expectThat("hello world")
    .startsWith("hello")
    .endsWith("world")
    .contains("lo wo")

expectThat("Hello World")
    .containsIgnoringCase("hello world")

expectThat("abc123")
    .matches(Regex("[a-z]+\\d+"))

expectThat("Hello World")
    .matchesIgnoringCase(Regex("hello world"))

Comparable assertions

expectThat(2026)
    .isGreaterThan(2025)

expectThat(3.14)
    .isLessThan(4.0)

expectThat(12L)
    .isGreaterThanOrEqualTo(12L)
    .isLessThanOrEqualTo(12L)

expectThat(12)
    .isIn(10..20)

Chaining assertions

Assertions return the builder, so you can chain multiple checks on the same subject:

expectThat("hello")
    .isA<String>()
    .isEqualTo("hello")

Enum assertions

expectThat(Color.RED)
    .isIn(Color.RED, Color.GREEN)

expectThat(Color.RED)
    .name()
    .isEqualTo("RED")

expectThat(Color.RED)
    .ordinal()
    .isEqualTo(1)

Collection assertions

expectThat(listOf(1, 2, 3))
    .hasSize(3)
    .size()
    .isGreaterThan(2)

expectThat(listOf(1))
    .single()
    .isEqualTo(1)

expectThat(listOf(1, 2, 3))
    .first()
    .isEqualTo(1)

expectThat(listOf(1, 2, 3))
    .last()
    .isEqualTo(3)

expectThat(listOf(1, 2, 3))
    .filter { it > 1 }
    .hasSize(2)

expectThat(listOf("item1", "item2", "item3"))
    .flatMap { it.toCharArray().toList() }
    .filter { it.isDigit() }
    .containsExactly('1', '2', '3')

expectThat(listOf(1, 2, 3))
    .map { it * 2 }
    .containsExactly(2, 4, 6)

Map assertions

expectThat(mapOf(1 to "item1", 2 to "item2"))
    .isNotEmpty()
    .hasSize(3)
    .containsKeys(1, 2)

Inspiration

This library was inspired by Strikt, an assertion library for Kotlin. As Strikt seemingly became stale, Kotlin Expect was created as a modern Kotlin Multiplatform alternative. The project structure and API design are influenced by Strikt's approach to expressive, chainable assertions.

Contributing

See CONTRIBUTING.md for guidelines on how to contribute.

License

This project is licensed under the MIT License.

About

Kotlin multiplatform assertion library

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages