Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
langref: add information about doctests
Browse files Browse the repository at this point in the history
This creates a section in the language reference about doctests, which
is currently referenced by Autodoc in a tooltip when displaying a
doctest.

Some advice relevant to writing doctests is included, based on the
discussion on ziglang#16472.
  • Loading branch information
ianprime0509 authored and andrewrk committed Jan 22, 2024
1 parent ac29303 commit fdb4eb3
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,7 @@ test "expect addOne adds one to 41" {

test addOne {
// A test name can also be written using an identifier.
// This is a doctest, and serves as documentation for `addOne`.
try std.testing.expect(addOne(41) == 42);
}

Expand Down Expand Up @@ -1170,6 +1171,19 @@ fn addOne(number: i32) i32 {
be written before or after the code under test.
</p>
{#see_also|The Global Error Set|Grammar#}
{#header_open|Doctests#}
<p>
Test declarations named using an identifier are <em>doctests</em>. The identifier must refer to another declaration in
scope. A doctest, like a {#link|doc comment|Doc Comments#}, serves as documentation for the associated declaration, and
will appear in the generated documentation for the declaration.
</p>
<p>
An effective doctest should be self-contained and focused on the declaration being tested, answering questions a new
user might have about its interface or intended usage, while avoiding unnecessary or confusing details. A doctest is not
a substitute for a doc comment, but rather a supplement and companion providing a testable, code-driven example, verified
by <kbd>zig test</kbd>.
</p>
{#header_close#}
{#header_close#}
{#header_open|Test Failure#}
<p>
Expand Down

0 comments on commit fdb4eb3

Please sign in to comment.