If you want to have the contracts enforced in the test submodule, all you have 
to do is add (require (submod “..”)) to the submodule.

You don’t need to use define/contract, and you don’t need to use module* 
instead of module+.

You can just use

#lang racket
(provide
 (contract-out
   ; convert a fahrenheit temperature to a celsius
  [fahrenheit->celsius (-> (and/c number? (>=/c -459.67)) number?)]))
(define (fahrenheit->celsius f)
 (/ (* 5 (- f 32)) 9))
(module+ test
  (require rackunit (submod "..")) ; the (submod "..") makes it enforce the 
contracts
  (check-equal? (fahrenheit->celsius -40) -40)
  (check-equal? (fahrenheit->celsius 32) 0)
  (check-equal? (fahrenheit->celsius 212) 100)
  ;; this checks that it raises a contract-error
  (check-exn exn:fail:contract?
             (λ () (fahrenheit->celsius -500))))


On May 22, 2015, at 7:58 AM, Atticus <attic...@posteo.org> wrote:

>> Yes, there is :
>> http://docs.racket-lang.org/style/Units_of_Code.html?q=define%2Fcontract#%28part._.Contracts%29
> 
> Thank you for the link. The "How to Program Racket" Guide will be really
> helpful.
> 
> But I must admit I'm a little confused. Looking at the *fahrenheit* example 
> in the
> Guide:
> 
> ;;;;
> #lang racket
> 
> (provide
>  (contract-out
>    ; convert a fahrenheit temperature to a celsius
>    [fahrenheit->celsius (-> number? number?)]))
> 
> (define (fahrenheit->celsius f)
>  (/ (* 5 (- f 32)) 9))
> 
> (module+ test
>  (require rackunit)
>  (check-equal? (fahrenheit->celsius -40) -40)
>  (check-equal? (fahrenheit->celsius 32) 0)
>  (check-equal? (fahrenheit->celsius 212) 100))
> ;;;; 
> 
> This example implies that the contracts are enforced in the test
> submodule (contrary to Matthews example) but modifing the example shows
> that this is not the case:
> 
> ;;;;
> (provide
>  (contract-out
>    ; convert a fahrenheit temperature to a celsius
>    [fahrenheit->celsius (-> integer? integer?)]))
> 
> (define (fahrenheit->celsius f)
>  (/ (* 5 (- f 32)) 9))
> 
> (module+ test
>  (require rackunit)
>  (check-equal? (fahrenheit->celsius -40) -40)
>  (check-equal? (fahrenheit->celsius 32) 0)
>  (check-equal? (fahrenheit->celsius 212.1) 100.05555555555556))
> ;;;;
> 
> This is not very clear and confusing. If I would read the guide and I
> didn't know about Matthews example I would assume that the test in the
> modified example fails.
> 
> My conclusion is:
> When using contracts with submodules use define/contract instead of
> (provide (contract-out ...))
> 
> 
>> black-box tests are always embedded in documentation so that
>> if the documentation does not show errors, then the system is reasonably
>> correct (for bugs that already found).
>> source code can keep elegant, and leave all the dirty things to scribble.
> 
> This is a really nice idea. Thanks for sharing.
> 
> 
> WarGrey Gyoudmon Ju <juzhenli...@gmail.com> writes:
> 
>> On Fri, May 22, 2015 at 5:41 PM, Atticus <attic...@posteo.org> wrote:
>> 
>>> 
>>> Imho it would be nice if there was a small hint in the documentation
>>> about that case, perhaps there is and I didn't see it?
>> 
>> 
>> Yes, there is :
>> http://docs.racket-lang.org/style/Units_of_Code.html?q=define%2Fcontract#%28part._.Contracts%29
>> 
>> 
>> In practice, I design tests in a system level abstraction.
>> 
>> black-box tests are always embedded in documentation so that
>> if the documentation does not show errors, then the system is reasonably
>> correct (for bugs that already found).
>> source code can keep elegant, and leave all the dirty things to scribble.
>> 
>> I almost do not write white-box tests since Racket and Typed Racket itself
>> has already formal enough,
>> moreover these tests or verifications can also be moved to documentation if
>> needed.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to