Skip to content

Commit a201d80

Browse files
paulespinosaandrewrk
authored andcommitted
Introduce Zig Test earlier in Language Reference
The "Zig Test" section of the language reference has been moved between the current "Hello World" section and the "Comments" section. This was done to introduce the Zig test syntax before it is used in later sections. The description of the Zig test feature has NOT been updated in this commit. Closes #5837
1 parent ec36ac8 commit a201d80

File tree

1 file changed

+76
-76
lines changed

1 file changed

+76
-76
lines changed

doc/langref.html.in

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,82 @@ pub fn main() void {
344344
</p>
345345
{#see_also|Values|@import|Errors|Root Source File|Source Encoding#}
346346
{#header_close#}
347+
{#header_open|Zig Test#}
348+
<p>
349+
<code>zig test</code> is a tool that can be used to quickly build and run Zig code
350+
to make sure behavior meets expectations. {#syntax#}@import("builtin").is_test{#endsyntax#}
351+
is available for code to detect whether the current build is a test build.
352+
</p>
353+
{#code_begin|test|detect_test#}
354+
const std = @import("std");
355+
const builtin = @import("builtin");
356+
const expect = std.testing.expect;
357+
358+
test "builtin.is_test" {
359+
try expect(builtin.is_test);
360+
}
361+
{#code_end#}
362+
<p>
363+
Zig has lazy top level declaration analysis, which means that if a function is not called,
364+
or otherwise used, it is not analyzed. This means that there may be an undiscovered
365+
compile error in a function because it is never called.
366+
</p>
367+
{#code_begin|test|unused_fn#}
368+
fn unused() i32 {
369+
return "wrong return type";
370+
}
371+
test "unused function" { }
372+
{#code_end#}
373+
<p>
374+
Note that, while in {#link|Debug#} and {#link|ReleaseSafe#} modes, {#link|unreachable#} emits a
375+
call to {#link|@panic#}, in {#link|ReleaseFast#} and {#link|ReleaseSmall#} modes, it is really
376+
undefined behavior. The implementation of {#syntax#}std.debug.assert{#endsyntax#} is as
377+
simple as:
378+
</p>
379+
{#code_begin|syntax#}
380+
pub fn assert(ok: bool) void {
381+
if (!ok) unreachable;
382+
}
383+
{#code_end#}
384+
<p>
385+
This means that when testing in ReleaseFast or ReleaseSmall mode, {#syntax#}assert{#endsyntax#}
386+
is not sufficient to check the result of a computation:
387+
</p>
388+
{#code_begin|syntax#}
389+
const std = @import("std");
390+
const assert = std.debug.assert;
391+
392+
test "assert in release fast mode" {
393+
assert(false);
394+
}
395+
{#code_end#}
396+
<p>
397+
When compiling this test in {#link|ReleaseFast#} mode, it invokes unchecked
398+
{#link|Undefined Behavior#}. Since that could do anything, this documentation
399+
cannot show you the output.
400+
</p>
401+
<p>
402+
Better practice for checking the output when testing is to use {#syntax#}std.testing.expect{#endsyntax#}:
403+
</p>
404+
{#code_begin|test_err|test "expect in release fast mode"... FAIL (TestUnexpectedResult)#}
405+
{#code_release_fast#}
406+
const std = @import("std");
407+
const expect = std.testing.expect;
408+
409+
test "expect in release fast mode" {
410+
try expect(false);
411+
}
412+
{#code_end#}
413+
<p>See the rest of the {#syntax#}std.testing{#endsyntax#} namespace for more available functions.</p>
414+
<p>
415+
<code>zig test</code> has a few command line parameters which affect the compilation. See
416+
<code>zig --help</code> for a full list. The most interesting one is <code>--test-filter [text]</code>.
417+
This makes the test build only include tests whose name contains the supplied filter text.
418+
Again, thanks to lazy analysis, this can allow you to narrow a build to only a few functions in
419+
isolation.
420+
</p>
421+
{#header_close#}
422+
347423
{#header_open|Comments#}
348424
{#code_begin|test|comments#}
349425
const expect = @import("std").testing.expect;
@@ -9725,82 +9801,6 @@ const separator = if (builtin.os.tag == builtin.Os.windows) '\\' else '/';
97259801
<p>TODO: lazy analysis</p>
97269802
<p>TODO: using comptime { _ = @import() }</p>
97279803
{#header_close#}
9728-
{#header_open|Zig Test#}
9729-
<p>
9730-
<code>zig test</code> is a tool that can be used to quickly build and run Zig code
9731-
to make sure behavior meets expectations. {#syntax#}@import("builtin").is_test{#endsyntax#}
9732-
is available for code to detect whether the current build is a test build.
9733-
</p>
9734-
{#code_begin|test|detect_test#}
9735-
const std = @import("std");
9736-
const builtin = @import("builtin");
9737-
const expect = std.testing.expect;
9738-
9739-
test "builtin.is_test" {
9740-
try expect(builtin.is_test);
9741-
}
9742-
{#code_end#}
9743-
<p>
9744-
Zig has lazy top level declaration analysis, which means that if a function is not called,
9745-
or otherwise used, it is not analyzed. This means that there may be an undiscovered
9746-
compile error in a function because it is never called.
9747-
</p>
9748-
{#code_begin|test|unused_fn#}
9749-
fn unused() i32 {
9750-
return "wrong return type";
9751-
}
9752-
test "unused function" { }
9753-
{#code_end#}
9754-
<p>
9755-
Note that, while in {#link|Debug#} and {#link|ReleaseSafe#} modes, {#link|unreachable#} emits a
9756-
call to {#link|@panic#}, in {#link|ReleaseFast#} and {#link|ReleaseSmall#} modes, it is really
9757-
undefined behavior. The implementation of {#syntax#}std.debug.assert{#endsyntax#} is as
9758-
simple as:
9759-
</p>
9760-
{#code_begin|syntax#}
9761-
pub fn assert(ok: bool) void {
9762-
if (!ok) unreachable;
9763-
}
9764-
{#code_end#}
9765-
<p>
9766-
This means that when testing in ReleaseFast or ReleaseSmall mode, {#syntax#}assert{#endsyntax#}
9767-
is not sufficient to check the result of a computation:
9768-
</p>
9769-
{#code_begin|syntax#}
9770-
const std = @import("std");
9771-
const assert = std.debug.assert;
9772-
9773-
test "assert in release fast mode" {
9774-
assert(false);
9775-
}
9776-
{#code_end#}
9777-
<p>
9778-
When compiling this test in {#link|ReleaseFast#} mode, it invokes unchecked
9779-
{#link|Undefined Behavior#}. Since that could do anything, this documentation
9780-
cannot show you the output.
9781-
</p>
9782-
<p>
9783-
Better practice for checking the output when testing is to use {#syntax#}std.testing.expect{#endsyntax#}:
9784-
</p>
9785-
{#code_begin|test_err|test "expect in release fast mode"... FAIL (TestUnexpectedResult)#}
9786-
{#code_release_fast#}
9787-
const std = @import("std");
9788-
const expect = std.testing.expect;
9789-
9790-
test "expect in release fast mode" {
9791-
try expect(false);
9792-
}
9793-
{#code_end#}
9794-
<p>See the rest of the {#syntax#}std.testing{#endsyntax#} namespace for more available functions.</p>
9795-
<p>
9796-
<code>zig test</code> has a few command line parameters which affect the compilation. See
9797-
<code>zig --help</code> for a full list. The most interesting one is <code>--test-filter [text]</code>.
9798-
This makes the test build only include tests whose name contains the supplied filter text.
9799-
Again, thanks to lazy analysis, this can allow you to narrow a build to only a few functions in
9800-
isolation.
9801-
</p>
9802-
{#header_close#}
9803-
98049804
{#header_open|Zig Build System#}
98059805
<p>
98069806
The Zig Build System provides a cross-platform, dependency-free way to declare

0 commit comments

Comments
 (0)