Test Module
The std/test module provides a minimal, opinionated testing framework with assertions and formatted reporting.
Import
Section titled “Import”import { test } from "std/test"Assertion Functions
Section titled “Assertion Functions”test.assert(condition: any, message?: string): bool
Section titled “test.assert(condition: any, message?: string): bool”Assert that a condition is truthy. Throws an error if the condition is false.
test.assert(1 + 1 == 2)test.assert(value > 0, "Value must be positive")test.assertEqual(actual: any, expected: any, message?: string): bool
Section titled “test.assertEqual(actual: any, expected: any, message?: string): bool”Assert that two values are deeply equal. Throws an error if they differ.
test.assertEqual(add(2, 3), 5)test.assertEqual(result, "hello", "Result should be hello")test.assertNotEqual(actual: any, expected: any, message?: string): bool
Section titled “test.assertNotEqual(actual: any, expected: any, message?: string): bool”Assert that two values are NOT equal. Throws an error if they are equal.
test.assertNotEqual(a, b, "Values should differ")Utility Functions
Section titled “Utility Functions”test.now(): int
Section titled “test.now(): int”Returns the current time in milliseconds. Use this to measure test duration.
const start = test.now()// ... run tests ...const duration = test.now() - starttest.report(passed: int, failed: int, failures: []string, duration: int): int
Section titled “test.report(passed: int, failed: int, failures: []string, duration: int): int”Print a formatted test report. Returns 0 if all tests passed, 1 if any failed.
const exitCode = test.report(passed, failed, failures, duration)Output format:
══════════════════════════════════════ TEST RESULTS────────────────────────────────────── ✓ Passed: 10 ✗ Failed: 2 Duration: 45ms────────────────────────────────────── Failures: 1. Expected 5 but got 4 2. Value must be positive══════════════════════════════════════Function Reference
Section titled “Function Reference”| Function | Description |
|---|---|
test.assert(condition, message?) | Assert truthiness |
test.assertEqual(actual, expected, message?) | Assert deep equality |
test.assertNotEqual(actual, expected, message?) | Assert inequality |
test.now() | Current time in milliseconds |
test.report(passed, failed, failures, duration) | Print formatted test report |
Example: Full Test Suite
Section titled “Example: Full Test Suite”import { test } from "std/test"
var passed = 0var failed = 0var failures: []string = []const start = test.now()
// Test 1: Additiontry { test.assertEqual(1 + 1, 2) passed++} catch (e) { failed++ failures = append(failures, e.message)}
// Test 2: String concatenationtry { test.assertEqual("hello" + " " + "world", "hello world") passed++} catch (e) { failed++ failures = append(failures, e.message)}
// Test 3: Array lengthtry { const arr = [1, 2, 3] test.assertEqual(len(arr), 3) passed++} catch (e) { failed++ failures = append(failures, e.message)}
// Test 4: Not equaltry { test.assertNotEqual("a", "b") passed++} catch (e) { failed++ failures = append(failures, e.message)}
test.report(passed, failed, failures, test.now() - start)