summary refs log tree commit diff stats
path: root/doc
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2021-02-18 13:19:38 -0800
committerGitHub <noreply@github.com>2021-02-18 13:19:38 -0800
commit148e5ba2a5e51f19c51e2c11150e6986c04990ba (patch)
treecdd6a20648076c3ed2cea7a555b32f0765a0e18d /doc
parentbbaa05dc8245d297cc21dd6b41a0472795fa0830 (diff)
downloadNim-148e5ba2a5e51f19c51e2c11150e6986c04990ba.tar.gz
contributing: prefer assert to doAssert in runnableExamples (#17077)
Diffstat (limited to 'doc')
-rw-r--r--doc/contributing.rst31
1 files changed, 24 insertions, 7 deletions
diff --git a/doc/contributing.rst b/doc/contributing.rst
index 646ea74ac..ac7657de7 100644
--- a/doc/contributing.rst
+++ b/doc/contributing.rst
@@ -55,6 +55,9 @@ Sample test:
 
 .. code-block:: nim
 
+  block: # foo
+    doAssert foo(1) == 10
+
   block: # bug #1234
     static: doAssert 1+1 == 2
 
@@ -236,15 +239,15 @@ See `parentDir <os.html#parentDir,string>`_ example.
 
 The RestructuredText Nim uses has a special syntax for including code snippets
 embedded in documentation; these are not run by `nim doc` and therefore are
-not guaranteed to stay in sync, so `runnableExamples` is usually preferred:
+not guaranteed to stay in sync, so `runnableExamples` is almost always preferred:
 
 .. code-block:: nim
 
-  proc someproc*(): string =
+  proc someProc*(): string =
     ## Returns "something"
     ##
     ## .. code-block::
-    ##  echo someproc() # "something"
+    ##  echo someProc() # "something"
     result = "something" # single-hash comments do not produce documentation
 
 The `.. code-block:: nim` followed by a newline and an indentation instructs the
@@ -331,16 +334,30 @@ rationale: https://forum.nim-lang.org/t/4089
   doAssert() # preferred
 
 .. _tests_use_doAssert:
-Use `doAssert` (or `require`, etc), not `assert` in all tests so they'll
-be enabled even in release mode (except for tests in `runnableExamples` blocks
-which for which `nim doc` ignores `-d:release`).
+Use `doAssert` (or `unittest.check`, `unittest.require`), not `assert` in all
+tests so they'll be enabled even with `--assertions:off`.
 
 .. code-block:: nim
 
-  when isMainModule:
+  block: # foo
     assert foo() # bad
     doAssert foo() # preferred
 
+.. _runnableExamples_use_assert:
+An exception to the above rule is `runnableExamples`, which for brevity use `assert`
+instead of `doAssert`. Note that `nim doc -d:danger main` won't pass `-d:danger` to the
+`runnableExamples`, but `nim doc --doccmd:-d:danger main` would, and so would the
+second example below:
+
+.. code-block:: nim
+
+  runnableExamples:
+    doAssert foo() # bad
+    assert foo() # preferred
+
+  runnableExamples("-d:danger"):
+    doAssert foo() # `assert` would be disabled here, so `doAssert` makes more sense
+
 .. _delegate_printing:
 Delegate printing to caller: return `string` instead of calling `echo`
 rationale: it's more flexible (e.g. allows the caller to call custom printing,