ludic/kw-test.html
2026-09-17 22:10:03 +00:00

48 lines
No EOL
4 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>test — Ludic</title>
<meta name="description" content="A named test block, run automatically with pass/fail reporting.">
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="docs.css">
</head>
<body>
<header class="nav"><div class="wrap nav-in"><a class="brand" href="index.html"><span class="logo">L</span> Ludic</a><button class="nav-toggle" aria-label="Toggle menu" aria-expanded="false">☰</button><nav class="nav-links"><a href="index.html">Home</a><a href="api.html">API Reference</a><a class="nav-cta" href="https://git.workshopsoft.io/workshopsoft/ludic">Source ↗</a></nav></div></header>
<main class="wrap item">
<div class="crumbs"><a href="api.html">API Reference</a> <span>›</span> <a href="api.html#testing">Testing</a> <span>›</span> <span class="here">test</span></div>
<div class="item-head">
<span class="kind-badge kind-keyword">keyword</span>
<h1 id="top">test</h1>
</div>
<code class="sig">test "name" { … }</code>
<div class="desc"><p>A <code>test "name" { … }</code> block declares a named test. Every test in a file is discovered automatically and run by a generated entry point — there is no <code>entry</code> to write and nothing to register. The runner prints <code>ok - name</code> for a test whose assertions all held and <code>FAIL - name</code> for one that had a failure, then a <code>== N passed, M failed ==</code> summary, and the program exits non-zero if any test failed. Writing a test should feel like writing a function: put a few assertions in a block and run it.</p><p>Assertions (each records a failure and prints <code>file:line: … failed</code> but keeps going, so one run reports every failure):</p><p>- <code>expect(cond)</code> — the boolean <code>cond</code> must be true.
- <code>expect_eq(a, b)</code> — <code>a</code> must equal <code>b</code>; on failure prints <code>(got a, want b)</code>.
- <code>expect_near(a, b, tol)</code> — <code>a</code> must be within <code>tol</code> of <code>b</code> (absolute). Use it for <code>fixed</code>-point results and accumulated integer math, where an exact match is too brittle.</p><p>Run a spec file directly with the compiler-runner — <code>ludic mymath_test.ludic</code> compiles it to a native binary, runs it, and forwards the pass/fail exit code — so it drops straight into <code>bin/ludic</code> and CI.</p><p><strong>Line coverage.</strong> Compile with the <code>--coverage</code> flag and the compiler instruments every statement with a per-source-line hit counter; at exit the counts are written to the file named by <code>$LUDIC_COVERAGE</code> (default <code>ludic.cov</code>) as a <code>FILE &lt;name&gt;</code> header followed by one <code>&lt;line&gt; &lt;hits&gt;</code> row per instrumented line. The instrumentation is flag-gated and additive, so an ordinary build — and the compiler's own self-compile — stays byte-identical. <code>bin/ludic-dev test --coverage</code> compiles the test specs this way, runs them, and aggregates the dumps into a per-file report that names the lines your tests never reached:</p><p>``<code>
== line coverage (bin/ludic-dev test --coverage) ==
examples/library/coverage.ludic 16/17 lines 94% uncovered: 33
examples/library/testing.ludic 17/17 lines 100%
----
total: 33/34 lines 97%
</code>``</p></div>
<div class="examples"><h2>Example</h2><pre data-lang="ludic">program MathSpec {
function add(a: int, b: int) -&gt; int { return a + b }
test "addition adds" {
expect_eq(add(2, 3), 5)
expect(add(1, 1) == 2)
}
test "fixed math is close enough" {
let half = fixed(1) / 2 # 0.5 in Q16.16
expect_near(half, 32768, 2) # within 2 raw units of 0.5
}
}</pre></div>
<a class="back" href="api.html">← All symbols</a>
</main>
<script src="ludic-highlight.js"></script>
<script>Ludic.highlightAll(); Ludic.installCards(); Ludic.flashTarget();</script>
</body></html>