CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-test-wasm-js

Kotlin Test library for experimental WebAssembly JS platform - provides test framework adapters and assertions for testing Kotlin/Wasm applications that target JavaScript environments

Pending
Overview
Eval results
Files

assertions.mddocs/

Assertions

Comprehensive assertion functions for verifying test conditions across all common scenarios. These functions provide the core testing functionality for validating expected behavior in Kotlin/WASM-JS applications.

Capabilities

Basic Equality Assertions

Functions for testing equality between expected and actual values with support for different types and tolerance-based comparisons.

/**
 * Asserts that expected and actual values are equal
 * @param expected the expected value
 * @param actual the actual value 
 * @param message the message to report on failure
 */
fun <T> assertEquals(expected: T, actual: T, message: String? = null)

/**
 * Asserts that expected and actual Double values are equal within absolute tolerance
 * @param expected the expected Double value
 * @param actual the actual Double value
 * @param absoluteTolerance the maximum allowed difference
 * @param message the message to report on failure
 */
fun assertEquals(expected: Double, actual: Double, absoluteTolerance: Double, message: String? = null)

/**
 * Asserts that expected and actual Float values are equal within absolute tolerance
 * @param expected the expected Float value
 * @param actual the actual Float value
 * @param absoluteTolerance the maximum allowed difference
 * @param message the message to report on failure
 */
fun assertEquals(expected: Float, actual: Float, absoluteTolerance: Float, message: String? = null)

/**
 * Asserts that illegal and actual values are not equal
 * @param illegal the value that actual should not equal
 * @param actual the actual value
 * @param message the message to report on failure
 */
fun <T> assertNotEquals(illegal: T, actual: T, message: String? = null)

Usage Examples:

import kotlin.test.*

class EqualityTests {
    @Test
    fun basicEquality() {
        assertEquals(4, 2 + 2)
        assertEquals("hello", "hello")
        assertEquals(listOf(1, 2, 3), listOf(1, 2, 3))
    }
    
    @Test 
    fun floatingPointEquality() {
        val result = 0.1 + 0.2
        assertEquals(0.3, result, 0.0001) // Use tolerance for floating point
        
        val floatResult = 0.1f + 0.2f
        assertEquals(0.3f, floatResult, 0.0001f)
    }
    
    @Test
    fun inequality() {
        assertNotEquals(5, 3)
        assertNotEquals("hello", "world")
        assertNotEquals(0.3, 0.1 + 0.2, 0.00001) // Different tolerance
    }
}

Boolean Assertions

Functions for testing boolean conditions and block-based boolean expressions.

/**
 * Asserts that the value is true
 * @param actual the value to check
 * @param message the message to report on failure
 */
fun assertTrue(actual: Boolean, message: String? = null)

/**
 * Asserts that the block returns true
 * @param message the message to report on failure
 * @param block the block that should return true
 */
inline fun assertTrue(message: String? = null, block: () -> Boolean)

/**
 * Asserts that the value is false
 * @param actual the value to check
 * @param message the message to report on failure
 */
fun assertFalse(actual: Boolean, message: String? = null)

/**
 * Asserts that the block returns false
 * @param message the message to report on failure
 * @param block the block that should return false
 */
inline fun assertFalse(message: String? = null, block: () -> Boolean)

Usage Examples:

import kotlin.test.*

class BooleanTests {
    @Test
    fun booleanValues() {
        assertTrue(5 > 3)
        assertFalse(2 > 5)
        assertTrue(listOf(1, 2, 3).isNotEmpty())
        assertFalse(emptyList<Int>().isNotEmpty())
    }
    
    @Test
    fun booleanBlocks() {
        assertTrue("List should not be empty") {
            val list = generateTestData()
            list.isNotEmpty()
        }
        
        assertFalse("Should not find invalid user") {
            val user = findUser("invalid-id")
            user != null
        }
    }
}

Null Assertions

Functions for testing null and non-null values with smart casting support.

/**
 * Asserts that the value is not null and returns the non-null value
 * @param actual the value to check
 * @param message the message to report on failure
 * @return the actual value cast to non-null type
 */
fun <T : Any> assertNotNull(actual: T?, message: String? = null): T

/**
 * Asserts that the value is not null and passes it to the block
 * @param actual the value to check
 * @param message the message to report on failure
 * @param block the block to execute with the non-null value
 * @return the result of the block
 */
inline fun <T : Any, R> assertNotNull(actual: T?, message: String? = null, block: (T) -> R): R

/**
 * Asserts that the value is null
 * @param actual the value to check
 * @param message the message to report on failure
 */
fun assertNull(actual: Any?, message: String? = null)

Usage Examples:

import kotlin.test.*

class NullabilityTests {
    @Test
    fun nullChecks() {
        val nullableString: String? = "hello"
        val nonNullString = assertNotNull(nullableString)
        assertEquals("hello", nonNullString) // nonNullString is smart-cast to String
        
        val nullValue: String? = null
        assertNull(nullValue)
    }
    
    @Test
    fun nullCheckWithBlock() {
        val user: User? = createUser("test@example.com")
        assertNotNull(user, "User should be created successfully") { u ->
            assertEquals("test@example.com", u.email)
            assertTrue(u.id > 0)
        }
    }
}

Reference Equality Assertions

Functions for testing object identity (same reference) vs. structural equality.

/**
 * Asserts that expected and actual are the same object reference
 * @param expected the expected object reference
 * @param actual the actual object reference
 * @param message the message to report on failure
 */
fun <T> assertSame(expected: T, actual: T, message: String? = null)

/**
 * Asserts that illegal and actual are not the same object reference
 * @param illegal the reference that actual should not be
 * @param actual the actual object reference
 * @param message the message to report on failure
 */
fun <T> assertNotSame(illegal: T, actual: T, message: String? = null)

Usage Examples:

import kotlin.test.*

class ReferenceTests {
    @Test
    fun sameReference() {
        val obj = MyObject()
        val sameRef = obj
        assertSame(obj, sameRef)
        
        val differentObj = MyObject()
        assertNotSame(obj, differentObj)
    }
    
    @Test
    fun singletonPattern() {
        val instance1 = MySingleton.getInstance()
        val instance2 = MySingleton.getInstance()
        assertSame(instance1, instance2, "Singleton should return same instance")
    }
}

Collection Assertions

Specialized assertion functions for validating collection contents and array equality.

/**
 * Asserts that the iterable contains the specified element
 */
fun <T> assertContains(iterable: Iterable<T>, element: T, message: String? = null)

/**
 * Asserts that the sequence contains the specified element
 */
fun <T> assertContains(sequence: Sequence<T>, element: T, message: String? = null)

/**
 * Asserts that the array contains the specified element
 */
fun <T> assertContains(array: Array<T>, element: T, message: String? = null)

/**
 * Asserts that primitive arrays contain specified elements
 */
fun assertContains(array: ByteArray, element: Byte, message: String? = null)
fun assertContains(array: ShortArray, element: Short, message: String? = null)
fun assertContains(array: IntArray, element: Int, message: String? = null)
fun assertContains(array: LongArray, element: Long, message: String? = null)
fun assertContains(array: BooleanArray, element: Boolean, message: String? = null)
fun assertContains(array: CharArray, element: Char, message: String? = null)

/**
 * Asserts that the map contains the specified key
 */
fun <K, V> assertContains(map: Map<K, V>, key: K, message: String? = null)

/**
 * Asserts that the CharSequence contains the specified character or substring
 */
fun assertContains(charSequence: CharSequence, char: Char, ignoreCase: Boolean = false, message: String? = null)
fun assertContains(charSequence: CharSequence, other: CharSequence, ignoreCase: Boolean = false, message: String? = null)
fun assertContains(charSequence: CharSequence, regex: Regex, message: String? = null)

Usage Examples:

import kotlin.test.*

class CollectionTests {
    @Test
    fun listContains() {
        val numbers = listOf(1, 2, 3, 4, 5)
        assertContains(numbers, 3)
        assertContains(numbers, 1)
    }
    
    @Test
    fun arrayContains() {
        val words = arrayOf("hello", "world", "kotlin")
        assertContains(words, "kotlin")
        
        val intArray = intArrayOf(10, 20, 30)
        assertContains(intArray, 20)
    }
    
    @Test
    fun mapContains() {
        val userMap = mapOf("admin" to "Alice", "user" to "Bob")
        assertContains(userMap, "admin")
        assertContains(userMap, "user")
    }
    
    @Test
    fun stringContains() {
        val message = "Hello, Kotlin World!"
        assertContains(message, "Kotlin")
        assertContains(message, 'K')
        assertContains(message, "kotlin", ignoreCase = true)
        assertContains(message, Regex("K\\w+"))
    }
}

Content Equality Assertions

Functions for deep equality comparison of collections and arrays.

/**
 * Asserts that iterables have the same elements in the same order
 */
fun <T> assertContentEquals(expected: Iterable<T>?, actual: Iterable<T>?, message: String? = null)

/**
 * Asserts that sequences have the same elements in the same order
 */
fun <T> assertContentEquals(expected: Sequence<T>?, actual: Sequence<T>?, message: String? = null)

/**
 * Asserts that arrays have the same elements in the same order
 */
fun <T> assertContentEquals(expected: Array<T>?, actual: Array<T>?, message: String? = null)

/**
 * Asserts that primitive arrays have the same elements in the same order
 */
fun assertContentEquals(expected: ByteArray?, actual: ByteArray?, message: String? = null)
fun assertContentEquals(expected: ShortArray?, actual: ShortArray?, message: String? = null)
fun assertContentEquals(expected: IntArray?, actual: IntArray?, message: String? = null)
fun assertContentEquals(expected: LongArray?, actual: LongArray?, message: String? = null)
fun assertContentEquals(expected: FloatArray?, actual: FloatArray?, message: String? = null)
fun assertContentEquals(expected: DoubleArray?, actual: DoubleArray?, message: String? = null)
fun assertContentEquals(expected: BooleanArray?, actual: BooleanArray?, message: String? = null)
fun assertContentEquals(expected: CharArray?, actual: CharArray?, message: String? = null)

Usage Examples:

import kotlin.test.*

class ContentEqualityTests {
    @Test
    fun listContentEquals() {
        val expected = listOf(1, 2, 3)
        val actual = processNumbers(listOf(3, 1, 2)) // sorts the list
        assertContentEquals(expected, actual)
    }
    
    @Test
    fun arrayContentEquals() {
        val expected = arrayOf("a", "b", "c")
        val actual = generateAlphabet(3)
        assertContentEquals(expected, actual)
        
        val expectedInts = intArrayOf(1, 2, 3)
        val actualInts = intArrayOf(1, 2, 3)
        assertContentEquals(expectedInts, actualInts)
    }
    
    @Test
    fun nullHandling() {
        assertContentEquals(null, null)
        
        val emptyList: List<Int>? = emptyList()
        val nullList: List<Int>? = null
        assertContentEquals(emptyList, emptyList())
    }
}

Type Assertions

Functions for runtime type checking with reified generic support.

/**
 * Asserts that the value is of the specified type and returns it cast to that type
 * @param value the value to check and cast
 * @param message the message to report on failure
 * @return the value cast to type T
 */
inline fun <reified T> assertIs(value: Any?, message: String? = null): T

/**
 * Asserts that the value is not of the specified type
 * @param value the value to check
 * @param message the message to report on failure
 */
inline fun <reified T> assertIsNot(value: Any?, message: String? = null)

Usage Examples:

import kotlin.test.*

class TypeTests {
    @Test
    fun typeChecking() {
        val value: Any = "Hello"
        val stringValue = assertIs<String>(value)
        assertEquals("Hello", stringValue) // stringValue is smart-cast to String
        
        assertIsNot<Int>(value)
        assertIsNot<List<*>>(value)
    }
    
    @Test
    fun polymorphicTypeChecking() {
        val shapes: List<Shape> = listOf(Circle(5.0), Rectangle(3.0, 4.0))
        
        val circle = assertIs<Circle>(shapes[0])
        assertEquals(5.0, circle.radius)
        
        val rectangle = assertIs<Rectangle>(shapes[1])
        assertEquals(3.0, rectangle.width)
        assertEquals(4.0, rectangle.height)
    }
}

Exception Assertions

Functions for testing that code throws expected exceptions.

/**
 * Asserts that the block throws any exception and returns the exception
 * @param block the block that should throw an exception
 * @return the thrown exception
 */
inline fun assertFails(block: () -> Any?): Throwable

/**
 * Asserts that the block throws any exception and returns the exception
 * @param message the message to report if no exception is thrown
 * @param block the block that should throw an exception
 * @return the thrown exception
 */
inline fun assertFails(message: String?, block: () -> Any?): Throwable

/**
 * Asserts that the block throws an exception of the specified type
 * @param message the message to report on failure
 * @param block the block that should throw an exception
 * @return the thrown exception cast to the specified type
 */
inline fun <reified T : Throwable> assertFailsWith(message: String? = null, block: () -> Any?): T

/**
 * Asserts that the block throws an exception of the specified class
 * @param exceptionClass the expected exception class
 * @param message the message to report on failure
 * @param block the block that should throw an exception
 * @return the thrown exception
 */
inline fun <T : Throwable> assertFailsWith(exceptionClass: KClass<T>, message: String? = null, block: () -> Any?): T

Usage Examples:

import kotlin.test.*

class ExceptionTests {
    @Test
    fun anyException() {
        val exception = assertFails {
            throw RuntimeException("Something went wrong")
        }
        assertTrue(exception.message?.contains("Something went wrong") == true)
    }
    
    @Test
    fun specificExceptionType() {
        val exception = assertFailsWith<IllegalArgumentException> {
            validateAge(-1)
        }
        assertEquals("Age cannot be negative", exception.message)
    }
    
    @Test
    fun exceptionWithClass() {
        val exception = assertFailsWith(
            exceptionClass = NumberFormatException::class,
            message = "Should throw NumberFormatException for invalid input"
        ) {
            "not-a-number".toInt()
        }
        assertNotNull(exception.message)
    }
    
    @Test
    fun asyncExceptionHandling(): Promise<*> {
        return Promise.reject(RuntimeException("Async error")).catch { error ->
            assertFails {
                throw error.toThrowableOrNull() ?: RuntimeException("Unknown error")
            }
        }
    }
}

Utility Functions

Additional assertion utilities for common testing scenarios.

/**
 * Fails the current test unconditionally
 * @param message the failure message
 */
fun fail(message: String? = null): Nothing

/**
 * Fails the current test with a cause
 * @param message the failure message
 * @param cause the underlying cause
 */
fun fail(message: String? = null, cause: Throwable? = null): Nothing

/**
 * Expects that the block returns the expected value
 * @param expected the expected return value
 * @param block the block to execute
 */
inline fun <T> expect(expected: T, block: () -> T)

/**
 * Expects that the block returns the expected value with custom message
 * @param expected the expected return value
 * @param message the message to report on failure
 * @param block the block to execute
 */
inline fun <T> expect(expected: T, message: String?, block: () -> T)

/**
 * Marks code as not yet implemented - will always fail
 * @param block the placeholder block
 */
fun todo(block: () -> Unit): Nothing

Usage Examples:

import kotlin.test.*

class UtilityTests {
    @Test
    fun conditionalFailure() {
        val config = loadConfiguration()
        if (config.isInvalid()) {
            fail("Configuration is invalid: ${config.errors}")
        }
    }
    
    @Test
    fun expectPattern() {
        expect(42) {
            calculateAnswer()
        }
        
        expect(listOf(1, 2, 3), "List should be sorted") {
            sortNumbers(listOf(3, 1, 2))
        }
    }
    
    @Test
    fun todoExample() {
        // This test will fail until implementation is complete
        todo {
            implementNewFeature()
        }
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-test-wasm-js

docs

annotations.md

asserter-system.md

assertions.md

framework-integration.md

index.md

tile.json