make tests work bilingually

pull/3/head
Matthew Butterick 10 years ago
parent 61ba8967d9
commit 2a9e5e29d7

@ -1,222 +1,259 @@
#lang racket/base #lang racket/base
(require racket/include rackunit sugar racket/list net/url racket/set racket/match) (require (for-syntax racket/base racket/syntax))
(define-syntax (eval-as-untyped stx)
(syntax-case stx ()
[(_ exprs ...)
(with-syntax ([sym (generate-temporary)])
#'(begin
(module sym racket/base
(require rackunit sugar net/url racket/list racket/set racket/match)
exprs ...)
(require 'sym)))]))
(define-syntax (eval-as-typed stx)
(syntax-case stx ()
[(_ exprs ...)
(with-syntax ([sym (generate-temporary)])
#'(begin
(module sym typed/racket/base
(require typed/rackunit typed/sugar typed/net/url racket/list racket/list racket/set racket/match)
exprs ...)
(require 'sym)))]))
(define-syntax-rule (eval-as-typed-and-untyped exprs ...)
(begin
(eval-as-typed exprs ...)
(eval-as-untyped exprs ...)))
;; begin shared typed / untyped tests ;; begin shared typed / untyped tests
(eval-as-typed-and-untyped
(check-equal? (->int 42) 42) (check-equal? (->int 42) 42)
(check-equal? (->int 42.1) 42) (check-equal? (->int 42.1) 42)
(check-equal? (->int 42+3i) 42) (check-equal? (->int 42+3i) 42)
(check-equal? (->int "42") 42) (check-equal? (->int "42") 42)
(check-equal? (->int '42) 42) (check-equal? (->int '42) 42)
(check-equal? (->int (string->path "42")) 42) (check-equal? (->int (string->path "42")) 42)
(check-equal? (->int #\A) 65) (check-equal? (->int #\A) 65)
(check-equal? (->int (make-list 42 null)) 42) (check-equal? (->int (make-list 42 null)) 42)
(check-equal? (->string "foo") "foo") (check-equal? (->string "foo") "foo")
(check-equal? (->string '()) "") (check-equal? (->string '()) "")
(check-equal? (->string (void)) "") (check-equal? (->string (void)) "")
(check-equal? (->string 'foo) "foo") (check-equal? (->string 'foo) "foo")
(check-equal? (->string 123) "123") (check-equal? (->string 123) "123")
;(check-equal? (->string (string->url "foo/bar.html")) "foo/bar.html") ;(check-equal? (->string (string->url "foo/bar.html")) "foo/bar.html")
(define file-name-as-text "foo.txt") (define file-name-as-text "foo.txt")
(check-equal? (->string (string->path file-name-as-text)) file-name-as-text) (check-equal? (->string (string->path file-name-as-text)) file-name-as-text)
(check-equal? (->string #\¶) "") (check-equal? (->string #\¶) "")
(check-equal? (->path "foo") (string->path "foo")) (check-equal? (->path "foo") (string->path "foo"))
(check-equal? (->path 'foo) (string->path "foo")) (check-equal? (->path 'foo) (string->path "foo"))
(check-equal? (->path 123) (string->path "123")) (check-equal? (->path 123) (string->path "123"))
(check-equal? (->path (string->url "foo/bar.html")) (string->path "foo/bar.html")) (check-equal? (->path (string->url "foo/bar.html")) (string->path "foo/bar.html"))
(check-equal? (->list '(1 2 3)) '(1 2 3)) (check-equal? (->list '(1 2 3)) '(1 2 3))
(check-equal? (->list (list->vector '(1 2 3))) '(1 2 3)) (check-equal? (->list (list->vector '(1 2 3))) '(1 2 3))
(check-equal? (->list (set 1 2 3)) '(3 2 1)) (check-equal? (->list (set 1 2 3)) '(3 2 1))
(check-equal? (->list "foo") (list "foo")) (check-equal? (->list "foo") (list "foo"))
(check-true (->boolean #t)) (check-true (->boolean #t))
(check-false (->boolean #f)) (check-false (->boolean #f))
(check-true (->boolean "#f")) (check-true (->boolean "#f"))
(check-true (->boolean "foo")) (check-true (->boolean "foo"))
(check-true (->boolean '())) (check-true (->boolean '()))
(check-true (->boolean '(1 2 3))) (check-true (->boolean '(1 2 3)))
(check-equal? (len '(1 2 3)) 3) (check-equal? (len '(1 2 3)) 3)
(check-not-equal? (len '(1 2)) 3) ; len 2 (check-not-equal? (len '(1 2)) 3) ; len 2
(check-equal? (len "foo") 3) (check-equal? (len "foo") 3)
(check-not-equal? (len "fo") 3) ; len 2 (check-not-equal? (len "fo") 3) ; len 2
(check-equal? (len 'foo) 3) (check-equal? (len 'foo) 3)
(check-not-equal? (len 'fo) 3) ; len 2 (check-not-equal? (len 'fo) 3) ; len 2
(check-equal? (len (list->vector '(1 2 3))) 3) (check-equal? (len (list->vector '(1 2 3))) 3)
(check-not-equal? (len (list->vector '(1 2))) 3) ; len 2 (check-not-equal? (len (list->vector '(1 2))) 3) ; len 2
(check-equal? (len (set 1 2 3)) 3) (check-equal? (len (set 1 2 3)) 3)
(check-not-equal? (len (set 1 2)) 3) ; len 2 (check-not-equal? (len (set 1 2)) 3) ; len 2
(check-equal? (len (make-hash '((a . 1) (b . 2) (c . 3)))) 3) (check-equal? (len (make-hash '((a . 1) (b . 2) (c . 3)))) 3)
(check-not-equal? (len (make-hash '((a . 1) (b . 2)))) 3) ; len 2 (check-not-equal? (len (make-hash '((a . 1) (b . 2)))) 3) ; len 2
(check-true ("foobar" . starts-with? . "foo")) (check-true ("foobar" . starts-with? . "foo"))
(check-true ("foobar" . starts-with? . "f")) (check-true ("foobar" . starts-with? . "f"))
(check-true ("foobar" . starts-with? . "foobar")) (check-true ("foobar" . starts-with? . "foobar"))
(check-false ("foobar" . starts-with? . "bar")) (check-false ("foobar" . starts-with? . "bar"))
(check-false ("foobar" . starts-with? . ".")) (check-false ("foobar" . starts-with? . "."))
(check-true ("foobar" . ends-with? . "bar")) (check-true ("foobar" . ends-with? . "bar"))
(check-true ("foobar" . ends-with? . "r")) (check-true ("foobar" . ends-with? . "r"))
(check-true ("foobar" . ends-with? . "foobar")) (check-true ("foobar" . ends-with? . "foobar"))
(check-false ("foobar" . ends-with? . "foo")) (check-false ("foobar" . ends-with? . "foo"))
(check-true (members-unique? '(a b c))) (check-true (members-unique? '(a b c)))
(check-false (members-unique? '(a b c c))) (check-false (members-unique? '(a b c c)))
(check-true (members-unique? "zoey")) (check-true (members-unique? "zoey"))
(check-false (members-unique? "zooey")) (check-false (members-unique? "zooey"))
(check-equal? (trimf (list 4 1 2 3 4) even?) '(1 2 3)) (check-equal? (trimf (list 4 1 2 3 4) even?) '(1 2 3))
(check-equal? (trimf (list 1 3 2 4 5 6 8 9 13) odd?) '(2 4 5 6 8)) (check-equal? (trimf (list 1 3 2 4 5 6 8 9 13) odd?) '(2 4 5 6 8))
(check-equal? (filter-split '("foo" " " "bar" "\n" "\n" "ino") (λ(x) (< (string-length x) 3))) '(("foo")("bar")("ino"))) ;(check-equal? (filter-split '("foo" " " "bar" "\n" "\n" "ino") (λ(x) (< (string-length x) 3))) '(("foo")("bar")("ino")))
(check-equal? (filter-split '(1 2 3 4 5 6) even?) '((1)(3)(5))) (check-equal? (filter-split '(1 2 3 4 5 6) even?) '((1)(3)(5)))
(define foo-path-strings '("foo" "foo.txt" "foo.bar" "foo.bar.txt")) (define foo-path-strings '("foo" "foo.txt" "foo.bar" "foo.bar.txt"))
(match-define (list foo-path foo.txt-path foo.bar-path foo.bar.txt-path) (map ->path foo-path-strings)) (match-define (list foo-path foo.txt-path foo.bar-path foo.bar.txt-path) (map ->path foo-path-strings))
;; test the sample paths before using them for other tests ;; test the sample paths before using them for other tests
(define foo-paths (list foo-path foo.txt-path foo.bar-path foo.bar.txt-path)) (define foo-paths (list foo-path foo.txt-path foo.bar-path foo.bar.txt-path))
(for-each check-equal? (map ->string foo-paths) foo-path-strings) (for-each check-equal? (map ->string foo-paths) foo-path-strings)
(check-false (has-ext? foo-path 'txt)) (check-false (has-ext? foo-path 'txt))
(check-true (foo.txt-path . has-ext? . 'txt)) (check-true (foo.txt-path . has-ext? . 'txt))
(check-true ((->path "foo.TXT") . has-ext? . 'txt)) (check-true ((->path "foo.TXT") . has-ext? . 'txt))
(check-true (has-ext? foo.bar.txt-path 'txt)) (check-true (has-ext? foo.bar.txt-path 'txt))
(check-false (foo.bar.txt-path . has-ext? . 'doc)) ; wrong extension (check-false (foo.bar.txt-path . has-ext? . 'doc)) ; wrong extension
(check-equal? (get-ext (->path "foo.txt")) "txt") (check-equal? (get-ext (->path "foo.txt")) "txt")
(check-false (get-ext "foo")) (check-false (get-ext "foo"))
(check-equal? (add-ext (string->path "foo") "txt") (string->path "foo.txt")) (check-equal? (add-ext (string->path "foo") "txt") (string->path "foo.txt"))
(check-equal? (remove-ext foo-path) foo-path) (check-equal? (remove-ext foo-path) foo-path)
(check-equal? (remove-ext (->path ".foo.txt")) (->path ".foo.txt")) (check-equal? (remove-ext (->path ".foo.txt")) (->path ".foo.txt"))
(check-equal? (remove-ext foo.txt-path) foo-path) (check-equal? (remove-ext foo.txt-path) foo-path)
(check-equal? (remove-ext foo.bar.txt-path) foo.bar-path) (check-equal? (remove-ext foo.bar.txt-path) foo.bar-path)
(check-not-equal? (remove-ext foo.bar.txt-path) foo-path) ; does not remove all extensions (check-not-equal? (remove-ext foo.bar.txt-path) foo-path) ; does not remove all extensions
(check-equal? (remove-ext* foo-path) foo-path) (check-equal? (remove-ext* foo-path) foo-path)
(check-equal? (remove-ext* foo.txt-path) foo-path) (check-equal? (remove-ext* foo.txt-path) foo-path)
(check-equal? (remove-ext* (->path ".foo.txt")) (->path ".foo.txt")) (check-equal? (remove-ext* (->path ".foo.txt")) (->path ".foo.txt"))
(check-not-equal? (remove-ext* foo.bar.txt-path) foo.bar-path) ; removes more than one ext (check-not-equal? (remove-ext* foo.bar.txt-path) foo.bar-path) ; removes more than one ext
(check-equal? (remove-ext* foo.bar.txt-path) foo-path) (check-equal? (remove-ext* foo.bar.txt-path) foo-path)
(check-true (starts-with? "foobar" "foo")) (check-true (starts-with? "foobar" "foo"))
(check-true (starts-with? "foobar" "foobar")) (check-true (starts-with? "foobar" "foobar"))
(check-false (starts-with? "foobar" "zam")) (check-false (starts-with? "foobar" "zam"))
(check-false (starts-with? "foobar" "foobars")) (check-false (starts-with? "foobar" "foobars"))
(check-true (ends-with? "foobar" "bar")) (check-true (ends-with? "foobar" "bar"))
(check-false (ends-with? "foobar" "zam")) (check-false (ends-with? "foobar" "zam"))
(check-true (ends-with? "foobar" "foobar")) (check-true (ends-with? "foobar" "foobar"))
(check-false (ends-with? "foobar" "foobars")) (check-false (ends-with? "foobar" "foobars"))
(check-true (capitalized? "Brennan")) (check-true (capitalized? "Brennan"))
(check-false (capitalized? "foobar")) (check-false (capitalized? "foobar"))
(check-equal? (slice-at (range 5) 1) '((0) (1) (2) (3) (4))) (check-equal? (slice-at (range 5) 1) '((0) (1) (2) (3) (4)))
(check-equal? (slice-at (range 5) 2) '((0 1) (2 3) (4))) (check-equal? (slice-at (range 5) 2) '((0 1) (2 3) (4)))
(check-equal? (slice-at (range 5) 2 #t) '((0 1) (2 3))) (check-equal? (slice-at (range 5) 2 #t) '((0 1) (2 3)))
(check-equal? (slice-at (range 5) 3) '((0 1 2) (3 4))) (check-equal? (slice-at (range 5) 3) '((0 1 2) (3 4)))
(check-equal? (slice-at (range 5) 3 #t) '((0 1 2))) (check-equal? (slice-at (range 5) 3 #t) '((0 1 2)))
(check-equal? (slicef-at (range 5) even?) '((0 1) (2 3) (4))) (check-equal? (slicef-at (range 5) even?) '((0 1) (2 3) (4)))
(check-equal? (slicef-at (range 5) odd?) '((0) (1 2) (3 4))) (check-equal? (slicef-at (range 5) odd?) '((0) (1 2) (3 4)))
(check-equal? (slicef-at (range 5) odd? #t) '((1 2) (3 4))) (check-equal? (slicef-at (range 5) odd? #t) '((1 2) (3 4)))
(check-equal? (slicef-at (range 5) procedure?) '((0 1 2 3 4))) (check-equal? (slicef-at (range 5) procedure?) '((0 1 2 3 4)))
(check-equal? (slicef-at '(1 2 2 1 2) even?) '((1) (2) (2 1) (2))) (check-equal? (slicef-at '(1 2 2 1 2) even?) '((1) (2) (2 1) (2)))
(check-equal? (slicef-at '(1 2 2 1 2) even? #t) '((2) (2 1) (2))) (check-equal? (slicef-at '(1 2 2 1 2) even? #t) '((2) (2 1) (2)))
(check-equal? (sublist (range 5) 0 0) '()) (check-equal? (sublist (range 5) 0 0) '())
(check-equal? (sublist (range 5) 0 1) '(0)) (check-equal? (sublist (range 5) 0 1) '(0))
(check-equal? (sublist (range 5) 0 5) '(0 1 2 3 4)) (check-equal? (sublist (range 5) 0 5) '(0 1 2 3 4))
(check-equal? (break-at '(5 6 7 8) '()) '((5 6 7 8))) (check-equal? (break-at '(5 6 7 8) '()) '((5 6 7 8)))
(check-equal? (break-at '(5 6 7 8) '(0)) '((5 6 7 8))) (check-equal? (break-at '(5 6 7 8) '(0)) '((5 6 7 8)))
(check-equal? (break-at '(5 6 7 8) '(1 2 3)) '((5) (6) (7) (8))) (check-equal? (break-at '(5 6 7 8) '(1 2 3)) '((5) (6) (7) (8)))
(check-equal? (break-at '(5 6 7 8) '(1 3)) '((5) (6 7) (8))) (check-equal? (break-at '(5 6 7 8) '(1 3)) '((5) (6 7) (8)))
(check-equal? (break-at '(5 6 7 8) '(1)) (break-at '(5 6 7 8) 1)) (check-equal? (break-at '(5 6 7 8) '(1)) (break-at '(5 6 7 8) 1))
(define xs (range 5)) (define xs (range 5))
(check-equal? (map (λ(a b c) (list a b c)) (shift xs -1) (shift xs 0) (shift xs 1)) '((1 0 #f) (2 1 0) (3 2 1) (4 3 2) (#f 4 3))) (check-equal? (map (λ(a b c) (list a b c)) (shift xs -1) (shift xs 0) (shift xs 1)) '((1 0 #f) (2 1 0) (3 2 1) (4 3 2) (#f 4 3)))
(check-equal? (shift xs '(-1 0 1) 'boing) `((1 2 3 4 boing) ,xs (boing 0 1 2 3))) (check-equal? (shift xs '(-1 0 1) 'boing) `((1 2 3 4 boing) ,xs (boing 0 1 2 3)))
(check-equal? (shift xs 5 0) (make-list 5 0)) (check-equal? (shift xs 5 0) (make-list 5 0))
(check-exn exn:fail? (λ() (shift xs -10))) (check-exn exn:fail? (λ() (shift xs -10)))
;;;;; end common tests ;;;;; end common tests
)
(check-exn exn:fail? (λ _ (slice-at (range 5) 0))) ; needs a positive integer as second arg
(check-exn exn:fail? (λ _ (slicef-at (range 5) 3))) ; needs a procedure as second arg
(eval-as-untyped
(check-equal? (get '(0 1 2 3 4 5) 2) 2) (check-exn exn:fail? (λ _ (slice-at (range 5) 0))) ; needs a positive integer as second arg
(check-exn exn:fail? (λ() (get '(0 1 2 3 4 5) 100))) ; index too big (check-exn exn:fail? (λ _ (slicef-at (range 5) 3))) ; needs a procedure as second arg
(check-equal? (get `(0 1 ,(list 2) 3 4 5) 2) (list 2))
(check-equal? (get '(0 1 2 3 4 5) 0 2) '(0 1)) (check-equal? (get '(0 1 2 3 4 5) 2) 2)
(check-equal? (get (list->vector '(0 1 2 3 4 5)) 2) 2) (check-exn exn:fail? (λ() (get '(0 1 2 3 4 5) 100))) ; index too big
(check-equal? (get (list->vector'(0 1 2 3 4 5)) 0 2) (list->vector '(0 1))) (check-equal? (get `(0 1 ,(list 2) 3 4 5) 2) (list 2))
(check-equal? (get "purple" 2) "r") (check-equal? (get '(0 1 2 3 4 5) 0 2) '(0 1))
(check-equal? (get "purple" 0 2) "pu") (check-equal? (get (list->vector '(0 1 2 3 4 5)) 2) 2)
(check-equal? (get 'purple 2) 'r) (check-equal? (get (list->vector'(0 1 2 3 4 5)) 0 2) (list->vector '(0 1)))
(check-equal? (get 'purple 0 2) 'pu) (check-equal? (get "purple" 2) "r")
(check-equal? (get (string->path "/root/foo/bar/file.txt") 2) (string->path "foo")) (check-equal? (get "purple" 0 2) "pu")
(check-equal? (get (string->path "/root/foo/bar/file.txt") 0 2) (list (string->path "/") (string->path "root"))) (check-equal? (get 'purple 2) 'r)
(check-equal? (get (make-hash `((a . ,(list 1)) (b . ,(list 2)) (c . ,(list 3)))) 'a) (list 1)) (check-equal? (get 'purple 0 2) 'pu)
(check-exn exn:fail? (λ() (get (make-hash `((a . ,(list 1)) (b . ,(list 2)) (c . ,(list 3)))) 'z))) ; nonexistent key (check-equal? (get (string->path "/root/foo/bar/file.txt") 2) (string->path "foo"))
(check-equal? (get (string->path "/root/foo/bar/file.txt") 0 2) (list (string->path "/") (string->path "root")))
(check-equal? (get (string->path "/root/foo/bar/file.txt") 1) (string->path "root")) (check-equal? (get (make-hash `((a . ,(list 1)) (b . ,(list 2)) (c . ,(list 3)))) 'a) (list 1))
(check-equal? (get (string->path "/root/foo/bar/file.txt") 0 3) (check-exn exn:fail? (λ() (get (make-hash `((a . ,(list 1)) (b . ,(list 2)) (c . ,(list 3)))) 'z))) ; nonexistent key
(map string->path '("/" "root" "foo")))
(check-equal? (get (string->path "/root/foo/bar/file.txt") 1) (string->path "root"))
(check-equal? (get (make-hash '((a . 1) (b . 2) (c . 3))) 'b) 2) (check-equal? (get (string->path "/root/foo/bar/file.txt") 0 3)
(map string->path '("/" "root" "foo")))
(check-true (2 . in? . '(1 2 3)))
(check-false (4 . in? . '(1 2 3))) (check-equal? (get (make-hash '((a . 1) (b . 2) (c . 3))) 'b) 2)
(check-true (2 . in? . (list->vector '(1 2 3))))
(check-false (4 . in? . (list->vector '(1 2 3)))) (check-true (2 . in? . '(1 2 3)))
(check-true ('a . in? . (make-hash '((a . 1) (b . 2) (c . 3))))) (check-false (4 . in? . '(1 2 3)))
(check-false ('x . in? . (make-hash '((a . 1) (b . 2) (c . 3))))) (check-true (2 . in? . (list->vector '(1 2 3))))
(check-true ("o" . in? . "foobar")) (check-false (4 . in? . (list->vector '(1 2 3))))
(check-false ("z" . in? . "foobar")) (check-true ('a . in? . (make-hash '((a . 1) (b . 2) (c . 3)))))
(check-true ('o . in? . 'foobar)) (check-false ('x . in? . (make-hash '((a . 1) (b . 2) (c . 3)))))
(check-false ('z . in? . 'foobar)) (check-true ("o" . in? . "foobar"))
(check-true ("F" . in? . #\F)) (check-false ("z" . in? . "foobar"))
(check-true ('o . in? . 'foobar))
(check-true (in? "foo" (string->path "/root/foo/bar/file.txt"))) (check-false ('z . in? . 'foobar))
(check-false (in? "zam" (string->path "/root/foo/bar/file.txt"))) (check-true ("F" . in? . #\F))
(define ys (range 5)) (check-true (in? "foo" (string->path "/root/foo/bar/file.txt")))
(check-equal? (values->list (shift/values ys '(-1 0 1) 'boing)) `((1 2 3 4 boing) ,xs (boing 0 1 2 3))) (check-false (in? "zam" (string->path "/root/foo/bar/file.txt")))
(define xs (range 5))
(require xml) (define ys (range 5))
(define str "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>hello world</root>") (check-equal? (values->list (shift/values ys '(-1 0 1) 'boing)) `((1 2 3 4 boing) ,xs (boing 0 1 2 3)))
(define-values (str-prolog str-doc) (xml-string->xexprs str))
(check-equal? str-prolog (prolog (list (p-i (location 1 0 1) (location 1 38 39) 'xml "version=\"1.0\" encoding=\"utf-8\"")) #f null))
(check-equal? str-doc '(root () "hello world")) (require xml)
(check-equal? (xexprs->xml-string str-prolog str-doc) str) (define str "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>hello world</root>")
(define-values (str-prolog str-doc) (xml-string->xexprs str))
(check-equal? str-prolog (prolog (list (p-i (location 1 0 1) (location 1 38 39) 'xml "version=\"1.0\" encoding=\"utf-8\"")) #f null))
(module include-test racket/base (check-equal? str-doc '(root () "hello world"))
(require sugar/include) (check-equal? (xexprs->xml-string str-prolog str-doc) str)
(include-without-lang-line "source.rkt")
(provide included-symbol))
(module include-test racket/base
(require 'include-test) (require sugar/include)
(check-equal? included-symbol 'bar) (include-without-lang-line "source.rkt")
(provide included-symbol))
(module no-lang-line-include-test racket/base
(require sugar/include) (require 'include-test)
(include-without-lang-line "no-lang-line-source.txt") (check-equal? included-symbol 'bar)
(provide no-lang-symbol))
(module no-lang-line-include-test racket/base
(require 'no-lang-line-include-test) (require sugar/include)
(check-equal? no-lang-symbol 'bar) (include-without-lang-line "no-lang-line-source.txt")
(provide no-lang-symbol))
(require 'no-lang-line-include-test)
(check-equal? no-lang-symbol 'bar))
#|
;; todo: revise `check-typing-fails` to make it compatible with 6.0
(check-typing-fails (slice-at (range 5) 0)) ; needs a positive integer as second arg
(check-typing-fails (slicef-at (range 5) 3)) ; needs a procedure as second arg
|#

@ -5,13 +5,13 @@
(require "len.rkt") ; want relative path-spec for bilingual conversion (require "len.rkt") ; want relative path-spec for bilingual conversion
(define-syntax-rule (make-coercion-error-handler target-format x) (define-syntax-rule (make-coercion-error-handler target-format x)
(λ(e) (error (format "Cant convert ~s to ~a" x target-format)))) (λ(e) (error (string->symbol (format "->~a" target-format)) (format "Cant convert ~s to ~a" x target-format))))
(define-type Intable (U Lengthable Number String Symbol Char Path)) (define-type Intable (U Number String Symbol Char Path Lengthable))
(define/typed+provide (->int x) (define/typed+provide (->int x)
(Intable -> Integer) (Intable -> Integer)
(with-handlers ([exn:fail? (make-coercion-error-handler 'integer x)]) (with-handlers ([exn:fail? (make-coercion-error-handler 'int x)])
(cond (cond
[(or (integer? x) (real? x)) (assert (inexact->exact (floor x)) integer?)] [(or (integer? x) (real? x)) (assert (inexact->exact (floor x)) integer?)]
[(complex? x) (->int (real-part x))] [(complex? x) (->int (real-part x))]

@ -3,7 +3,7 @@
(require typed/sugar/define) (require typed/sugar/define)
(provide Lengthable) (provide Lengthable)
(define-type Lengthable (U (Listof Any) String Symbol Path (Vectorof Any) HashTableTop (Setof Any) (Sequenceof Any))) (define-type Lengthable (U (Listof Any) String Symbol Path (Vectorof Any) HashTableTop (Setof Any)))
(define/typed+provide (len x) (define/typed+provide (len x)
(Lengthable -> Nonnegative-Integer) (Lengthable -> Nonnegative-Integer)

@ -1,163 +0,0 @@
#lang typed/racket/base
(require racket/include typed/rackunit typed/net/url racket/set racket/list racket/match)
(require typed/sugar)
;; begin shared typed / untyped tests
(check-equal? (->int 42) 42)
(check-equal? (->int 42.1) 42)
(check-equal? (->int 42+3i) 42)
(check-equal? (->int "42") 42)
(check-equal? (->int '42) 42)
(check-equal? (->int (string->path "42")) 42)
(check-equal? (->int #\A) 65)
(check-equal? (->int (make-list 42 null)) 42)
(check-equal? (->string "foo") "foo")
(check-equal? (->string '()) "")
(check-equal? (->string (void)) "")
(check-equal? (->string 'foo) "foo")
(check-equal? (->string 123) "123")
;(check-equal? (->string (string->url "foo/bar.html")) "foo/bar.html")
(define file-name-as-text "foo.txt")
(check-equal? (->string (string->path file-name-as-text)) file-name-as-text)
(check-equal? (->string #\¶) "")
(check-equal? (->path "foo") (string->path "foo"))
(check-equal? (->path 'foo) (string->path "foo"))
(check-equal? (->path 123) (string->path "123"))
(check-equal? (->path (string->url "foo/bar.html")) (string->path "foo/bar.html"))
(check-equal? (->list '(1 2 3)) '(1 2 3))
(check-equal? (->list (list->vector '(1 2 3))) '(1 2 3))
(check-equal? (->list (set 1 2 3)) '(3 2 1))
(check-equal? (->list "foo") (list "foo"))
(check-true (->boolean #t))
(check-false (->boolean #f))
(check-true (->boolean "#f"))
(check-true (->boolean "foo"))
(check-true (->boolean '()))
(check-true (->boolean '(1 2 3)))
(check-equal? (len '(1 2 3)) 3)
(check-not-equal? (len '(1 2)) 3) ; len 2
(check-equal? (len "foo") 3)
(check-not-equal? (len "fo") 3) ; len 2
(check-equal? (len 'foo) 3)
(check-not-equal? (len 'fo) 3) ; len 2
(check-equal? (len (list->vector '(1 2 3))) 3)
(check-not-equal? (len (list->vector '(1 2))) 3) ; len 2
(check-equal? (len (set 1 2 3)) 3)
(check-not-equal? (len (set 1 2)) 3) ; len 2
(check-equal? (len (make-hash '((a . 1) (b . 2) (c . 3)))) 3)
(check-not-equal? (len (make-hash '((a . 1) (b . 2)))) 3) ; len 2
(check-true ("foobar" . starts-with? . "foo"))
(check-true ("foobar" . starts-with? . "f"))
(check-true ("foobar" . starts-with? . "foobar"))
(check-false ("foobar" . starts-with? . "bar"))
(check-false ("foobar" . starts-with? . "."))
(check-true ("foobar" . ends-with? . "bar"))
(check-true ("foobar" . ends-with? . "r"))
(check-true ("foobar" . ends-with? . "foobar"))
(check-false ("foobar" . ends-with? . "foo"))
(check-true (members-unique? '(a b c)))
(check-false (members-unique? '(a b c c)))
(check-true (members-unique? "zoey"))
(check-false (members-unique? "zooey"))
(check-equal? (trimf (list 4 1 2 3 4) even?) '(1 2 3))
(check-equal? (trimf (list 1 3 2 4 5 6 8 9 13) odd?) '(2 4 5 6 8))
(check-equal? (filter-split '("foo" " " "bar" "\n" "\n" "ino") (λ:([x : String]) (< (string-length x) 3))) '(("foo")("bar")("ino")))
(check-equal? (filter-split '(1 2 3 4 5 6) even?) '((1)(3)(5)))
(define foo-path-strings '("foo" "foo.txt" "foo.bar" "foo.bar.txt"))
(match-define (list foo-path foo.txt-path foo.bar-path foo.bar.txt-path) (map ->path foo-path-strings))
;; test the sample paths before using them for other tests
(define foo-paths (list foo-path foo.txt-path foo.bar-path foo.bar.txt-path))
(for-each check-equal? (map ->string foo-paths) foo-path-strings)
(check-false (has-ext? foo-path 'txt))
(check-true (foo.txt-path . has-ext? . 'txt))
(check-true ((->path "foo.TXT") . has-ext? . 'txt))
(check-true (has-ext? foo.bar.txt-path 'txt))
(check-false (foo.bar.txt-path . has-ext? . 'doc)) ; wrong extension
(check-equal? (get-ext (->path "foo.txt")) "txt")
(check-false (get-ext "foo"))
(check-equal? (add-ext (string->path "foo") "txt") (string->path "foo.txt"))
(check-equal? (remove-ext foo-path) foo-path)
(check-equal? (remove-ext (->path ".foo.txt")) (->path ".foo.txt"))
(check-equal? (remove-ext foo.txt-path) foo-path)
(check-equal? (remove-ext foo.bar.txt-path) foo.bar-path)
(check-not-equal? (remove-ext foo.bar.txt-path) foo-path) ; does not remove all extensions
(check-equal? (remove-ext* foo-path) foo-path)
(check-equal? (remove-ext* foo.txt-path) foo-path)
(check-equal? (remove-ext* (->path ".foo.txt")) (->path ".foo.txt"))
(check-not-equal? (remove-ext* foo.bar.txt-path) foo.bar-path) ; removes more than one ext
(check-equal? (remove-ext* foo.bar.txt-path) foo-path)
(check-true (starts-with? "foobar" "foo"))
(check-true (starts-with? "foobar" "foobar"))
(check-false (starts-with? "foobar" "zam"))
(check-false (starts-with? "foobar" "foobars"))
(check-true (ends-with? "foobar" "bar"))
(check-false (ends-with? "foobar" "zam"))
(check-true (ends-with? "foobar" "foobar"))
(check-false (ends-with? "foobar" "foobars"))
(check-true (capitalized? "Brennan"))
(check-false (capitalized? "foobar"))
(check-equal? (slice-at (range 5) 1) '((0) (1) (2) (3) (4)))
(check-equal? (slice-at (range 5) 2) '((0 1) (2 3) (4)))
(check-equal? (slice-at (range 5) 2 #t) '((0 1) (2 3)))
(check-equal? (slice-at (range 5) 3) '((0 1 2) (3 4)))
(check-equal? (slice-at (range 5) 3 #t) '((0 1 2)))
(check-equal? (slicef-at (range 5) even?) '((0 1) (2 3) (4)))
(check-equal? (slicef-at (range 5) odd?) '((0) (1 2) (3 4)))
(check-equal? (slicef-at (range 5) odd? #t) '((1 2) (3 4)))
(check-equal? (slicef-at (range 5) procedure?) '((0 1 2 3 4)))
(check-equal? (slicef-at '(1 2 2 1 2) even?) '((1) (2) (2 1) (2)))
(check-equal? (slicef-at '(1 2 2 1 2) even? #t) '((2) (2 1) (2)))
(check-equal? (sublist (range 5) 0 0) '())
(check-equal? (sublist (range 5) 0 1) '(0))
(check-equal? (sublist (range 5) 0 5) '(0 1 2 3 4))
(check-equal? (break-at '(5 6 7 8) '()) '((5 6 7 8)))
(check-equal? (break-at '(5 6 7 8) '(0)) '((5 6 7 8)))
(check-equal? (break-at '(5 6 7 8) '(1 2 3)) '((5) (6) (7) (8)))
(check-equal? (break-at '(5 6 7 8) '(1 3)) '((5) (6 7) (8)))
(check-equal? (break-at '(5 6 7 8) '(1)) (break-at '(5 6 7 8) 1))
(define xs (range 5))
(check-equal? (map (λ(a b c) (list a b c)) (shift xs -1) (shift xs 0) (shift xs 1)) '((1 0 #f) (2 1 0) (3 2 1) (4 3 2) (#f 4 3)))
(check-equal? (shift xs '(-1 0 1) 'boing) `((1 2 3 4 boing) ,xs (boing 0 1 2 3)))
(check-equal? (shift xs 5 0) (make-list 5 0))
(check-exn exn:fail? (λ() (shift xs -10)))
;; end shared tests
#|
;; todo: revise `check-typing-fails` to make it compatible with 6.0
(check-typing-fails (slice-at (range 5) 0)) ; needs a positive integer as second arg
(check-typing-fails (slicef-at (range 5) 3)) ; needs a procedure as second arg
|#

@ -1 +0,0 @@
(define no-lang-symbol 'bar)

@ -1,3 +0,0 @@
#lang typed/racket
(define included-symbol 'bar)

@ -1,29 +0,0 @@
#lang racket/base
(require rackunit)
(module trb typed/racket/base
(require typed/sugar/list typed/rackunit)
(provide (all-defined-out))
;; (trimf odd? '(1 2 3)) ; type error
(define foo (trimf '(1 2 3) odd?))
(check-equal? foo '(2)))
(module rb racket/base
(require (submod sugar/list safe) rackunit)
(provide (all-defined-out))
(check-exn exn:fail:contract? (λ _ (trimf odd? '(1 2 3)))) ; fails at trimf
(define foo (trimf '(1 2 3) odd?))
(check-equal? foo '(2)))
(module rbu racket/base
(require sugar/list rackunit)
(provide (all-defined-out))
(check-exn exn:fail:contract? (λ _ (trimf odd? '(1 2 3)))) ; fails at dropf
(define foo (trimf '(1 2 3) odd?))
(check-equal? foo '(2)))
(require (prefix-in trb: 'trb))
(require (prefix-in rb: 'rb))
(require (prefix-in rbu: 'rbu))
(check-true (andmap (λ(val) (equal? val '(2))) (list trb:foo rb:foo rbu:foo)))
Loading…
Cancel
Save