Kotlin Native Test Framework

kotlin native unit test strikt

A lightweight, fluent Strikt-like assertion library for Kotlin Native that makes your tests more readable and expressive.

Source code is currently part of my game engine: Kengine. I may port it to its own project in the future.

Features

  • Fluent assertion API
  • Type-safe property assertions
  • Exception testing
  • Collection assertions
  • Type checking
  • Object assertions

Usage

Basic Assertions

val text = "Hello World"
expectThat(text)
    .isNotNull()
    .hasSize(11)
    .contains("World")
    .startsWith("Hello")

val numbers = listOf(1, 2, 3)
expectThat(numbers)
    .isNotEmpty()
    .hasSize(3)
    .contains(2)

val number = 42
expectThat(number)
    .isEqualTo(42)
    .isGreaterThan(40)
    .isLessThan(50)
    .isNotEqualTo(41)

Testing Exceptions

// Basic exception type checking
expectThrows<IllegalArgumentException> {
    throw IllegalArgumentException("bad value")
}

// Check exception message and cause
val cause = RuntimeException("root cause")
expectThrows<IllegalArgumentException> {
    throw IllegalArgumentException("bad value", cause)
}
    .withMessage("bad value")
    .withCause(cause)

Type Checking

// Generic type checking
expectThat("test").isA<String>()
expectThat(42).isA<Int>()
expectThat(listOf(1,2,3)).isA<List<*>>()

// Specific type helpers
expectThat(true).isBoolean()
expectThat("test").isString()
expectThat(42).isInt()
expectThat(42u).isUInt()
expectThat(42L).isLong()
expectThat(listOf(1,2,3)).isList()

Property Assertions

data class User(val name: String, val age: Int)
val user = User("John", 25)

// Chained property assertions
expectThat(user)
    .property(User::name).isEqualTo("John")
    .property(User::age).satisfies { it > 18 }

// Object-style property assertions
expectObject(user) {
    property(User::name, { it == "John" }, "Name should be John")
    property(User::age, { it > 18 }, "User should be an adult")
}

Collection Assertions

expectThat(listOf(1, 2, 3, 4))
    .containsAll(1, 2)
    .containsExactly(1, 2, 3, 4)

// Multiple predicates
expectThat(10).satisfiesAll(
    { it > 0 },
    { it % 2 == 0 },
    { it <= 10 }
)

Value Transformations

expectThat("123")
    .transform { it.toInt() }
    .isGreaterThan(100)

Expression-Style Assertions

val numbers = listOf(1, 2, 3)
expectThat(numbers) {
    isNotEmpty()
    hasSize(3)
    contains(2)
}

Code Implementation