Contributing

Testing strategy

Each package has its own unit tests. On top of them, three kinds of tests check the formatter as a whole:

Test inputs

Asciidoctor cases

The test inputs of the Asciidoctor test suite, one file each under testdata/asciidoctor-cases. They give breadth, but hold little prose, and the lines inside an ifdef that is not set never reach the rendering.

Golden cases

Documents written for a rule, under testdata/golden. They cover what the Asciidoctor cases miss, and a new or changed rule comes with them.

Fuzzing

Go’s fuzzer mutates both sets in search of input that breaks a guarantee. The seeds run with every go test, the search only on demand. An input it finds is pinned as a golden case or as a case in parse_test.go.

Golden cases

A golden case is a directory with an input.adoc and a golden.adoc. TestGoldenCases formats the input and expects the golden file byte for byte. The cases run with every rule on, so keep each input to what the rule under test has to do.

go run ./tools/update-golden writes the golden files from the formatter’s output. Read its diff before you commit it, because an unreviewed update turns a bug into the expectation. Asciidoctor strips trailing whitespace before it renders, so the render check cannot see what a rule does to it. For such a rule, write golden.adoc by hand.

Guarantees

Three of the guarantees need no expected output, so they can be checked on any document. Each is checked by one test over every Asciidoctor case and every golden input. Its fuzz target starts from the same documents.

TestFuzz target

Identity

TestPrintIsIdentity

FuzzPrintIsIdentity

Idempotence

TestFormatIsIdempotent

FuzzFormatIsIdempotent

Render equivalence

TestFormatIsRenderEquivalent

FuzzFormatIsRenderEquivalent

Identity

With every rule off, printing the parsed tree must give the source back byte for byte, so the parser loses nothing. The render check cannot show this: a lost blank line renders the same.

Idempotence

The test formats each document twice. The second run must neither change nor refuse the output of the first.

Render equivalence

The check renders the document with Asciidoctor before and after formatting and compares three things:

  • The HTML, with every run of whitespace collapsed, because a browser shows it as one space anyway.

  • The content of every code and literal block, byte for byte, because whitespace carries meaning there.

  • The comment lines of the source, because Asciidoctor drops them before they reach the HTML. A line counts only where // opens it, as in Asciidoctor.

Classification

Every rule builds on what the parser decides a block is, such as a heading, a list or a code block. The parser, like Asciidoctor, decides most of it from the first line of the block. TestClassifyAgreesWithAsciidoctor takes every line of the Asciidoctor cases, plus variants such as a leading // or a tab instead of a space. It asks both parsers what block each line starts and fails where they disagree. What the lines around it add, such as an underline or a [source] style above, is left to the render check.

So a misread line shows up before any rule rewrites its block. The render check would see it only once a rule does, and only in a document that holds that line.

View the source on GitHub