unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Python-style doctests in Guile (implemented, please comment)
@ 2017-07-30 18:54 Arne Babenhauserheide
  2017-07-31  8:22 ` Chaos Eternal
  2017-07-31 12:51 ` Mark H Weaver
  0 siblings, 2 replies; 11+ messages in thread
From: Arne Babenhauserheide @ 2017-07-30 18:54 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: Type: text/plain, Size: 2774 bytes --]

Hi,


I implemented doctests in Guile which allow writing SRFI-64 tests
directly in the docstring. Here’s a minimal example:


    (import (examples doctests))
    
    (define (one)
      "(test 'foo
         (test-equal 1 (one)))"
      1)
    
    (doctests-testmod (current-module))


Writing simple tests directly in the procedure-documentation makes for
very natural test-driven-design which gets extended into basic
documentation automatically: Start the procedure, write a basic test in
the docstring, implement the body till the test passes and finally
create api-docs from the docstrings which then also show basic usage
examples in the tests.


My questions: This method here is the simplest implemenation of this
feature. It would be great if you could comment on it. Is this structure
good? Would you do it differently? Is "(test-equal 1 (one))" close
enough to an example in the REPL, or would it serve for better
usage-examples in another structure?


The output from running the minimal example is plain SRFI-64 output,
though with extended identifiers:


    $ ./doctests-testone.scm 
    %%%% Starting test ._-guile-user--foo--foo  (Writing full log to "._-guile-user--foo--foo.log")
    # of expected passes      1
    
    $ cat ._-guile-user--foo--foo.log 
    %%%% Starting test ._-guile-user--foo--foo
    Group begin: ._-guile-user--foo--foo
    Test begin:
      source-line: 2
      source-form: (test-equal "bar" (foo))
    Test end:
      result-kind: pass
      actual-value: "bar"
      expected-value: "bar"
    Group end: ._-guile-user--foo--foo
    # of expected passes      1


The syntax for the identifier is:


     ._-<filename-or-module>--<function name>--<testid>


I mainly know doctests from Python. There they use a special syntax
which looks like the interpreter.

For Guile I rather chose to start the tests with "(test 'testname" and
treat as test everything read by (read).

Avoiding (test-begin 'testname) and (test-end 'testname) makes for
easier parsing.  (they are added automatically using the 'testname)

Here’s a somewhat longer example with the test-execution tucked away in
the main file so it does not interfere with importing the module:
<https://bitbucket.org/ArneBab/wisp/src/1ce02fce232f429fc1cbd29c666e36d34ec22d76/examples/doctests-test.scm>

The full code is available in the wisp-repo:
<https://bitbucket.org/ArneBab/wisp/src/1ce02fce232f429fc1cbd29c666e36d34ec22d76/examples/doctests.scm>

(The code is originally written in wisp, but I decided to use
parenthizes syntax nontheless, because using s-exps makes for more
obvious parsing)


Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein
ohne es zu merken

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-10-14 13:05 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-30 18:54 Python-style doctests in Guile (implemented, please comment) Arne Babenhauserheide
2017-07-31  8:22 ` Chaos Eternal
2017-07-31 17:44   ` Arne Babenhauserheide
2017-08-01  1:36     ` Chaos Eternal
2017-07-31 12:51 ` Mark H Weaver
2017-07-31 13:04   ` Panicz Maciej Godek
2017-07-31 17:23   ` Arne Babenhauserheide
2017-08-01 22:04     ` Vítor De Araújo
2017-10-10 18:21       ` Arne Babenhauserheide
2017-10-10 22:40         ` Vítor De Araújo
2017-10-14 13:05           ` Arne Babenhauserheide

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).