Add tests to check conversion of types to static contracts.

This commit is contained in:
Eric Dobson 2014-01-14 22:31:21 -08:00
parent 9bd577c426
commit 4ad412d71c
2 changed files with 81 additions and 51 deletions

View File

@ -21,8 +21,7 @@
(prefix-in c: racket/contract)
(contract-req)
(for-syntax racket/base syntax/parse racket/syntax)
(for-template racket/base racket/contract (utils any-wrap)
(prefix-in t: (types numeric-predicates))))
(for-template racket/base racket/contract (utils any-wrap)))
(provide
(c:contract-out
@ -428,13 +427,24 @@
(case->/sc (map (f #t) arrs)))])]
[_ (int-err "not a function" f)]))
(define-syntax-rule (numeric/sc name body)
(flat/sc #'(flat-named-contract 'name body) 'name))
(module predicates racket/base
(provide nonnegative? nonpositive?)
(define nonnegative? (lambda (x) (>= x 0)))
(define nonpositive? (lambda (x) (<= x 0))))
(require (for-template 'predicates))
(module numeric-contracts racket/base
(require
"../utils/utils.rkt"
(static-contracts combinators)
(for-template
racket/base
racket/contract
(submod ".." predicates)
(prefix-in t: (types numeric-predicates))))
(provide (all-defined-out))
(define-syntax-rule (numeric/sc name body)
(flat/sc #'(flat-named-contract 'name body) 'name))
(define positive-byte/sc (numeric/sc Positive-Byte (and/c byte? positive?)))
(define byte/sc (numeric/sc Byte byte?))
@ -483,6 +493,8 @@
(inexact-real? (real-part x)))))))
(define number/sc (numeric/sc Number number?))
)
(require 'numeric-contracts)
(define (numeric-type->static-contract type)
(match type

View File

@ -6,6 +6,7 @@
(private type-contract)
(rep type-rep)
(types abbrev numeric-tower union)
(submod typed-racket/private/type-contract numeric-contracts)
rackunit)
(provide tests)
(gen-test-main)
@ -20,6 +21,20 @@
(λ (#:reason [reason #f])
(fail-check (or reason "Type could not be converted to contract"))))))))
(define-syntax-rule (t-sc e-t e-sc)
(test-case (format "~a" '(e-t -> e-sc))
(let ([t e-t] [sc e-sc])
(with-check-info (['type t] ['expected sc])
(define actual
(type->static-contract
t
(λ (#:reason [reason #f])
(fail-check (or reason "Type could not be converted to contract")))))
(with-check-info (['actual actual])
(unless (equal? actual sc)
(fail-check "Static contract didn't match expected")))))))
(define-syntax-rule (t/fail e expected-reason)
(test-case (format "~a" 'e)
(let ((v e))
@ -105,4 +120,7 @@
(t/fail (-vec (-struct #'struct-name #f (list (make-fld (-seq -Symbol) #'acc #f)) #f #t))
"required a chaperone contract but generated an impersonator contract")
(t-sc -Number number/sc)
(t-sc -Integer integer/sc)
))