chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Integrating unit tests into source code


From: Kon Lovett
Subject: Re: [Chicken-users] Integrating unit tests into source code
Date: Thu, 14 Dec 2006 09:48:54 -0800

On Dec 14, 2006, at 1:51 AM, felix winkelmann wrote:

Hi!

While reading a bit about "doctest" (the Python utility), I thought
it would be relatively easy to support embedded documentation
in definitions, like:

(define (foo ...)
 '(test (...))
 <body>...)

The basic idea is to extend the idea of Lisp/Scheme docstrings
(a string as the first form inside a procedure, when the body
has more than one _expression_) to doc-sexprs.
So one could embed test-cases directly in the code, in a backwards-
and R5RS-compatible way.

It's no big deal to extend the compiler to extract this info. What I'm
looking for is ideas about the exact mechanism, the syntax, etc.

Pointless or useful?

Err, maybe in-between ;-)

Plain doc-strings are nice for REPL usage, so sure.

But I agree w/ Peter that contracts are better at the procedure definition locus than tests. They are not called "unit tests" for nothing. (Check out "signature-type.scm" in the 'procedure-surface' egg for a start down this road.)

As you know, I would like declare to be legal within a lambda form:

(define (fixnum+ a b)
  (declare
    (documentation
      (p (code "fixnum+") " takes two fixnums, "
        (tt "a") " & " (tt "b") ", and returns the sum."))
    (contract (-> fixnum fixnum fixnum)
      (if (= 0 a) b (+ (sub1 a) (add1 b))))
    (fixnum))
  (+ a b) )

The contract signature is enforced, unless '(no-signature-checks)' or '(unsafe)', by the compiler (or interpreter?) inserting code that does fixnum? checks & aborts w/ (condition (exn type fixnum)) if violated.

And the simpler, doc-string, form:

(define (fixnum+ a b)
  "fixnum+ takes two fixnums, a & b, and returns the sum."
  (+ a b) )

Is interpreted as:

(define (fixnum+ a b)
  (declare
    (documentation
      "fixnum+ takes two fixnums, a & b, and returns the sum."))
  (+ a b) )

And the form:

(define (fixnum+ a b)
  (declare
    (contract (-> fixnum fixnum fixnum)
      (if (= 0 a) b (+ (sub1 a) (add1 b))))
    (fixnum))
  (+ a b) )

Is interpreted as:

(define (fixnum+ a b)
  (declare
    (documentation
      (code "(fixnum+ (fixnum a) (fixnum b) fixnum)"))
    (contract (-> fixnum fixnum fixnum)
      (if (= 0 a) b (+ (sub1 a) (add1 b))))
    (fixnum))
  (+ a b) )

At the toplevel declare '(documentation ...)' makes sense but '(contract ...)' I doubt.

Best Wishes,
Kon



cheers,
felix


_______________________________________________
Chicken-users mailing list


reply via email to

[Prev in Thread] Current Thread [Next in Thread]