contributing.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. Writing tests
  2. =============
  3. Not all the tests follow this scheme, feel free to change the ones
  4. that don't. Always leave the code cleaner than you found it.
  5. Stdlib
  6. ------
  7. If you change the stdlib (anything under ``lib/``), put a test in the
  8. file you changed. Add the tests under an ``when isMainModule:``
  9. condition so they only get executed when the tester is building the
  10. file. Each test should be in a separate ``block:`` statement, such that
  11. each has its own scope. Use boolean conditions and ``doAssert`` for the
  12. testing by itself, don't rely on echo statements or similar.
  13. Sample test:
  14. .. code-block:: nim
  15. when isMainModule:
  16. block: # newSeqWith tests
  17. var seq2D = newSeqWith(4, newSeq[bool](2))
  18. seq2D[0][0] = true
  19. seq2D[1][0] = true
  20. seq2D[0][1] = true
  21. doAssert seq2D == @[@[true, true], @[true, false],
  22. @[false, false], @[false, false]]
  23. Compiler
  24. --------
  25. The tests for the compiler work differently, they are all located in
  26. ``tests/``. Each test has its own file, which is different from the
  27. stdlib tests. All test files are prefixed with ``t``. If you want to
  28. create a file for import into another test only, use the prefix ``m``.
  29. At the beginning of every test is the expected side of the test.
  30. Possible keys are:
  31. - output: The expected output, most likely via ``echo``
  32. - exitcode: Exit code of the test (via ``exit(number)``)
  33. - errormsg: The expected error message
  34. - file: The file the errormsg
  35. - line: The line the errormsg was produced at
  36. An example for a test:
  37. .. code-block:: nim
  38. discard """
  39. errormsg: "type mismatch: got (PTest)"
  40. """
  41. type
  42. PTest = ref object
  43. proc test(x: PTest, y: int) = nil
  44. var buf: PTest
  45. buf.test()
  46. Running tests
  47. =============
  48. You can run the tests with
  49. ::
  50. ./koch tests
  51. which will run a good subset of tests. Some tests may fail. If you
  52. only want to see the output of failing tests, go for
  53. ::
  54. ./koch tests --failing all
  55. You can also run only a single category of tests. A category is a subdirectory
  56. in the ``tests`` directory. There are a couple of special categories; for a
  57. list of these, see ``tests/testament/categories.nim``, at the bottom.
  58. ::
  59. ./koch tests c lib
  60. Comparing tests
  61. ===============
  62. Because some tests fail in the current ``devel`` branch, not every fail
  63. after your change is necessarily caused by your changes.
  64. The tester can compare two test runs. First, you need to create the
  65. reference test. You'll also need to the commit id, because that's what
  66. the tester needs to know in order to compare the two.
  67. ::
  68. git checkout devel
  69. DEVEL_COMMIT=$(git rev-parse HEAD)
  70. ./koch tests
  71. Then switch over to your changes and run the tester again.
  72. ::
  73. git checkout your-changes
  74. ./koch tests
  75. Then you can ask the tester to create a ``testresults.html`` which will
  76. tell you if any new tests passed/failed.
  77. ::
  78. ./koch tests --print html $DEVEL_COMMIT
  79. Deprecation
  80. ===========
  81. Backward compatibility is important, so if you are renaming a proc or
  82. a type, you can use
  83. .. code-block:: nim
  84. {.deprecated: [oldName: new_name].}
  85. Or you can simply use
  86. .. code-block:: nim
  87. proc oldProc() {.deprecated.}
  88. to mark a symbol as deprecated. Works for procs/types/vars/consts,
  89. etc. Note that currently the ``deprecated`` statement does not work well with
  90. overloading so for routines the latter variant is better.
  91. `Deprecated <https://nim-lang.org/docs/manual.html#pragmas-deprecated-pragma>`_
  92. pragma in the manual.
  93. Documentation
  94. =============
  95. When contributing new procedures, be sure to add documentation, especially if
  96. the procedure is exported from the module. Documentation begins on the line
  97. following the ``proc`` definition, and is prefixed by ``##`` on each line.
  98. Code examples are also encouraged. The RestructuredText Nim uses has a special
  99. syntax for including examples.
  100. .. code-block:: nim
  101. proc someproc*(): string =
  102. ## Return "something"
  103. ##
  104. ## .. code-block:: nim
  105. ##
  106. ## echo someproc() # "something"
  107. result = "something" # single-hash comments do not produce documentation
  108. The ``.. code-block:: nim`` followed by a newline and an indentation instructs the
  109. ``nim doc`` and ``nim doc2`` commands to produce syntax-highlighted example code with
  110. the documentation.
  111. When forward declaration is used, the documentation should be included with the
  112. first appearance of the proc.
  113. .. code-block:: nim
  114. proc hello*(): string
  115. ## Put documentation here
  116. proc nothing() = discard
  117. proc hello*(): string =
  118. ## Ignore this
  119. echo "hello"
  120. The preferred documentation style is to begin with a capital letter and use
  121. the imperative (command) form. That is, between:
  122. .. code-block:: nim
  123. proc hello*(): string =
  124. # Return "hello"
  125. result = "hello"
  126. or
  127. .. code-block:: nim
  128. proc hello*(): string =
  129. # says hello
  130. result = "hello"
  131. the first is preferred.
  132. The Git stuff
  133. =============
  134. General commit rules
  135. --------------------
  136. 1. All changes introduced by the commit (diff lines) must be related to the
  137. subject of the commit.
  138. If you change some other unrelated to the subject parts of the file, because
  139. your editor reformatted automatically the code or whatever different reason,
  140. this should be excluded from the commit.
  141. *Tip:* Never commit everything as is using ``git commit -a``, but review
  142. carefully your changes with ``git add -p``.
  143. 2. Changes should not introduce any trailing whitespace.
  144. Always check your changes for whitespace errors using ``git diff --check``
  145. or add following ``pre-commit`` hook:
  146. .. code-block:: sh
  147. #!/bin/sh
  148. git diff --check --cached || exit $?
  149. 3. Describe your commit and use your common sense.
  150. .. include:: docstyle.rst