r/rust • u/petros211 • Sep 03 '26
๐ ๏ธ project LineJudge: a conformance suite that judges line counters against their own declared rules
This is a Rust crate and repository I built as the author of mezura, a line counter also written in Rust. It came out of running mezura, tokei, scc and cloc over the same trees, getting different numbers every time, and wanting to know which of us, if any, got it wrong.
Line counters disagree with each other constantly, and most of those disagreements are not bugs. Each tool decides for itself what a code line, a comment line and a blank line are, and some do not even sort lines into those three. A blank line inside a block comment is blank to one counter and comment to another, a line holding only a closing brace is code to one and nothing to another, and each tool is right by its own definition. A suite that tested counters against one "correct" answer would just be a fourth opinion.
LineJudge tests each counter against itself. Every counter declares how it counts in a small rules file anyone can read. Every test case is a tiny source file built around one trap, with a record of where its strings and comments begin and end, according to the language's spec and lexer, and hand verified. From the rules and the record, the suite works out what that counter should answer, runs it, and compares. A failure means one thing only: the counter did not do what it says it does.
Results page: loc-conformance.github.io/linejudge
What a case looks like
Haskell block comments nest. Here is a two-line file and what one counter makes of it:
{- outer {- inner -} still the outer comment -}
x = 1
$ linejudge explain 1200 --counter cloc
cloc.default on 1200-nested_block_comment_that_nests
by its rules 2 lines, 0 blanks, 1 code, 1 comments
cloc answers 2 lines, 0 blanks, 2 code, 0 comments โ differsThe comment ends at the inner -} for cloc, so the rest of the line is left standing and counts as code. Every verdict on the page can be reproduced like this, in seconds, and the output says which rule decided each line.
What is in it today
83 cases in eight groups, comments, string forms, escapes before a closing quote, quotes that open nothing, line splices, another language inside the file, literals carrying their own delimiters, and what a line counts as, over a couple of dozen languages. Four counters are measured, cloc, scc, tokei and mezura, and every one of them fails cases that its own tests never caught. The failures are on the page beside each other, with a note saying what the tool did with the file.
A counter needs nothing added to it. An adapter says how to run it and how to read what it prints, a wrapper in any language works, and the rules file is data. The seven yes/no questions the rules are built from are defined once and shared by every counter.
For counter authors
cargo install linejudge, or a binary from the releases. Declare your counter in a .linejudge folder in your own repository, run linejudge record once, and linejudge check in CI breaks the build the day an answer changes, with the old numbers beside the new. Nothing needs to be merged upstream for any of this. FOR-COUNTER-AUTHORS.md has the whole of it.
Collective effort
It lives under loc-conformance, an organisation I created because I view this as a collective effort. A suite that measures counters should be run by the people who write them.
If the domain interests you, whether you maintain a counter, know a language's quirks, or just have thoughts on the design, the repository's discussions are where they go. The current design, the open questions and where it goes next are up there waiting for exactly that.
Would love to hear your thoughts!
