|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching * Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
runnableExamples
In Nim, you can do the same. First, let's see a Python example. Pythondef twice(n): """ Returns the double of the input number. >>> twice(5) 10 >>> twice(-3) -6 """ return 2 * n def main(): print(twice(5)) # 10 ############################################################################## if __name__ == "__main__": main() If you execute it, it prints "10" on the screen. Let's execute the embedded tests:
$ python3 -m doctest -v main.py
Trying:
twice(5)
Expecting:
10
ok
Trying:
twice(-3)
Expecting:
-6
ok
2 items had no tests:
main
main.main
1 item passed all tests:
2 tests in main.twice
2 tests in 3 items.
2 passed.
Test passed.
The option $ python3 -m doctest main.py $ 𝥶In this case, if the tests pass, there's no output. Of course, if a test fails, you get an error message. Nim
Nim uses I want to show later how to generate docs for a module. This time, I only want to show how to write embedded tests (similarly to Python's doctests), and how to execute these tests. func twice*(n: int): int = # Notice the '*' ! It must be exported! ## Returns the double of the input number. runnableExamples: doAssert twice(0) == 0 doAssert twice(1) == 2 doAssert twice(6) == 12 doAssert twice(-6) == -12 2 * n proc main() = echo twice(5) # ############################################################################ when isMainModule: main() You cannot execute just the embedded tests. However, if you issue the command However! Only exported symbols (marked with $ nim doc main.nim Check the output. If a test fails, you'll get an error message. The command generates the docs in a directory called GotchaEvery runnableExample is compiled and tested individually. If you want to use something from the standard library, then you need to import it there, inside the runnableExample, instead of importing it at the top level: runnableExamples: import std/sequtils # !!! doAssert get_next_collatz_sequence_element(13).toSeq == @[13, 40, 20, 10, 5, 16, 8, 4, 2, 1] Here, |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |