ludic/examples/library/coverage.ludic
Orkuncakilkaya 498593311f
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 17s
ci / build-and-test (push) Successful in 1m13s
commit-lint / conventional-commits (push) Successful in 3s
docs / build-and-deploy (push) Successful in 19s
feat(testing): line coverage via --coverage + bin/x test --coverage (#45)
Instrument each emitted statement with a per-source-line hit counter, gated
behind a new --coverage flag (default off) so ordinary builds — and the
compiler's own self-compile — stay byte-identical and the C-free bootstrap
fixpoint is untouched. A static line table plus a parallel hit-counter array
are dumped at exit through an atexit hook to $LUDIC_COVERAGE (default
ludic.cov) as a `FILE <name>` header and `<line> <hits>` rows.

bin/x test --coverage compiles the test specs with instrumentation, runs them
into per-file dumps, and aggregates a clean per-file line-coverage report that
names the unreached lines. Adds examples/library/coverage.ludic (a spec whose
tests deliberately miss one branch) and docs. Closes the last open acceptance
item of the testing framework (#12).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-31 14:51:14 +03:00

51 lines
1.3 KiB
Text

# coverage.ludic — a spec built to be measured by `bin/x test --coverage` (issue
# #45). Compiled with `--coverage`, every statement bumps a per-source-line hit
# counter; at exit the counts are dumped and `bin/x test --coverage` turns them
# into a per-file line-coverage report.
#
# The tests below exercise `sign` fully but only the taken branches of `grade`,
# so the report flags `grade`'s unreached line — the whole point of coverage:
# it shows you the branch your tests forgot.
#
# As an ordinary spec it still passes and prints:
# == 3 passed, 0 failed ==
program CoverageSpec {
# fully covered: the tests below hit all three returns.
function sign(n: int) -> int {
if n > 0 {
return 1
}
if n < 0 {
return 0 - 1
}
return 0
}
# partially covered: no test passes a score below 60, so the `return 0` (fail)
# line is never reached and shows up as uncovered in the report.
function grade(score: int) -> int {
if score >= 90 {
return 4
}
if score >= 60 {
return 2
}
return 0
}
test "sign covers every branch" {
expect_eq(sign(7), 1)
expect_eq(sign(0 - 3), 0 - 1)
expect_eq(sign(0), 0)
}
test "grade: high scores" {
expect_eq(grade(95), 4)
expect_eq(grade(75), 2)
}
test "grade: boundary" {
expect_eq(grade(90), 4)
expect_eq(grade(60), 2)
}
}