From 246304125a090fcc249a5f3da691b7b4ebcc072a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 20 Mar 2019 23:49:35 -0400 Subject: [PATCH] add documentation for zig test closes #1518 --- doc/langref.html.in | 77 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index 4ee5050ed2baf2b31cdbf60d64c910c647b2f4b5..bb75cdb31ce5651673c21e31b619053ebbf013c3 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -8396,11 +8396,78 @@ const separator = if (builtin.os == builtin.Os.windows) '\\' else '/';

TODO: using comptime { _ = @import() }

{#header_close#} {#header_open|Zig Test#} -

TODO: basic usage

-

TODO: lazy analysis

-

TODO: --test-filter

-

TODO: --test-name-prefix

-

TODO: testing in releasefast and releasesafe mode. assert still works

+

+ zig test is a tool that can be used to quickly build and run Zig code + to make sure behavior meets expectations. {#syntax#}@import("builtin").is_test{#endsyntax#} + is available for code to detect whether the current build is a test build. +

+ {#code_begin|test|detect_test#} +const std = @import("std"); +const builtin = @import("builtin"); +const assert = std.debug.assert; + +test "builtin.is_test" { + assert(builtin.is_test); +} + {#code_end#} +

+ Zig has lazy top level declaration analysis, which means that if a function is not called, + or otherwise used, it is not analyzed. This means that there may be an undiscovered + compile error in a function because it is never called. +

+ {#code_begin|test|unused_fn#} +fn unused() i32 { + return "wrong return type"; +} +test "unused function" { } + {#code_end#} +

+ Note that, while in {#link|Debug#} and {#link|ReleaseSafe#} modes, {#link|unreachable#} emits a + call to {#link|@panic#}, in {#link|ReleaseFast#} and {#link|ReleaseSmall#} modes, it is really + undefined behavior. The implementation of {#syntax#}std.debug.assert{#endsyntax#} is as + simple as: +

+ {#code_begin|syntax#} +pub fn assert(ok: bool) void { + if (!ok) unreachable; +} + {#code_end#} +

+ This means that when testing in ReleaseFast or ReleaseSmall mode, {#syntax#}assert{#endsyntax#} + is not sufficient to check the result of a computation: +

+ {#code_begin|test|assert#} + {#code_release_fast#} +const std = @import("std"); +const assert = std.debug.assert; + +test "assert in release fast mode" { + assert(false); +} + {#code_end#} +

Note that although the above example shows the test passing, this is invoking + unchecked {#link|Undefined Behavior#}. This documentation is showing only one possible + outcome of this test.

+

+ Better practice for checking the output when testing is to use {#syntax#}std.testing.expect{#endsyntax#}: +

+ {#code_begin|test_err|test failure#} + {#code_release_fast#} +const std = @import("std"); +const expect = std.testing.expect; + +test "assert in release fast mode" { + expect(false); +} + {#code_end#} +

See the rest of the {#syntax#}std.testing{#endsyntax#} namespace for more available functions.

+

+ zig test has a few command line parameters which affect the compilation. See + zig --help for a full list. The most interesting one is --test-filter [text]. + This makes the test build only include tests whose name contains the supplied filter text. + Again, thanks to lazy analysis, this can allow you to narrow a build to only a few functions in + isolation. +

{#header_close#} {#header_open|Zig Build System#}

TODO: explain purpose, it's supposed to replace make/cmake

-- 2.54.0