[racket] Unit testing of contracts

From: Eduardo Bellani (ebellani at gmail.com)
Date: Tue Jun 21 12:09:13 EDT 2011

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hey Noel.

I had noticed that exceptions were being raised by the contracts, but I
was looking for a isolated way to attach contracts to values. I ended up
using with-contracts for it. I don't know if what I am doing is exactly
right, but it appears to be working:

#lang racket

(require "bytes-contracts.rkt" rackunit rackunit/text-ui)

(define-test-suite bytes-contracts-suite
  (check-exn exn:fail?
             (lambda ()
               (with-contract
                name #:result
                (bytes-len</c 2)
                (bytes #x11 #xAA #xBB))))
  (check-exn exn:fail?
             (lambda ()
               (with-contract
                test #:result
                (bytes-len=/c 2)
                (bytes #x11))))
  (check-not-exn (lambda ()
                   (with-contract
                    test #:result
                    (bytes-len=/c 2)
                    (bytes #x11 #xFF))))
  (check-not-exn (lambda ()
                   (with-contract
                    test #:result
                    (bytes-len</c 2)
                    (bytes #x11)))))

(run-tests bytes-contracts-suite)


I would suggest adding some examples to the 7.4 section  of the
reference, specially the with-contract part. It became a bit clearer
when I grepped the source of racket for it's use.

Thanks for the answer and the time.

On 06/21/2011 09:46 AM, Noel Welsh wrote:
> Contracts raise exceptions when they fail, so check that the raise an
> exn (use check-exn) when you expect them to raise an exception, and
> that they don't raise an exn (check-not-exn) when you don't expect
> them to.
> 
> HTH,
> N.
> 
> On Tue, Jun 21, 2011 at 12:19 AM, Eduardo Bellani <ebellani at gmail.com> wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Hello list.
>>
>> I have a small set of contracts that I am thinking of turning into a
>> planet package so I can use them across projects. My problem is, how do
>> I use the rackunit framework to test them?


- -- 
Eduardo Bellani

omnia mutantur, nihil interit.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk4AwikACgkQSbLl0kCTjGkAjACeL+YL1rq0s4QXl1UudQ/UqdtJ
EkYAnR+Qv1gFRQi8vY35wEAmzPJo4Yv3
=w8qB
-----END PGP SIGNATURE-----


Posted on the users mailing list.