From 391444741fd0b2da3562d683b14d0b785c379555 Mon Sep 17 00:00:00 2001 From: Matthias Felleisen Date: Tue, 18 May 2010 23:00:59 -0400 Subject: [PATCH] simulate-file for testing readers --- collects/2htdp/batch-io.rkt | 40 +++++++++++++++++++++++++++--- collects/2htdp/tests/batch-io.rkt | 41 ++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 8 deletions(-) diff --git a/collects/2htdp/batch-io.rkt b/collects/2htdp/batch-io.rkt index 33ade31cbf..bc68ec9903 100644 --- a/collects/2htdp/batch-io.rkt +++ b/collects/2htdp/batch-io.rkt @@ -1,11 +1,16 @@ #lang scheme -(require (for-syntax syntax/parse) srfi/13 htdp/error "private/csv/csv.ss") +(require (for-syntax syntax/parse) + srfi/13 htdp/error + (rename-in lang/prim (first-order->higher-order f2h)) + "private/csv/csv.ss") ;; todo? ;; -- export tokenization? ;; ----------------------------------------------------------------------------- +(provide simulate-file) ;; syntax (simulate-file reader string ...) + (provide ;; all reader functions consume the name of a file f: ;; -- f must be a file name (string) in the same folder as the program @@ -82,6 +87,33 @@ (check-proc 'read-csv-file row 1 "one argument" "row") (read-csv-file/func f row)) +;; ----------------------------------------------------------------------------- +;; tester + +(define-syntax (simulate-file stx) + (syntax-case stx () + [(simulate-file) + (raise-syntax-error #f "expects a reader function as first argument" stx)] + [(simulate-file reader str ...) #'(simulate-file/proc (f2h reader) str ...)])) + + +(define (simulate-file/proc reader . los) + (define _1 (check-proc "simulate-file" reader 1 "reader" "one argument")) + (define _2 + (andmap + (lambda (f) + (check-arg "simulate-file" (string? f) "sequence of strings" "" f)) + los)) + (define t (make-temporary-file "drracket-temporary-file-~a")) + (dynamic-wind + (lambda () + (with-output-to-file t + (lambda () (for-each displayln los)) #:exists 'replace)) + (lambda () + (reader (path->string t))) + (lambda () + (delete-file t)))) + ;; ----------------------------------------------------------------------------- ;; writer @@ -91,8 +123,8 @@ (let ([result (not (file-exists? f))]) (with-output-to-file f (lambda () (printf "~a" str)) - #:exists 'truncate) - result)) + #:exists 'replace) + #t)) ;; ----------------------------------------------------------------------------- ;; auxiliaries @@ -115,7 +147,7 @@ ;; [Listof Char] -> [Listof Char] (define (drop-last-newline accu) - (reverse (if (char=? (car accu) #\newline) (cdr accu) accu))) + (reverse (if (and (pair? accu) (char=? (car accu) #\newline)) (cdr accu) accu))) ;; String[file name] Symbol -> Void ;; effect: ensure that f is a file in current directory or report error for t diff --git a/collects/2htdp/tests/batch-io.rkt b/collects/2htdp/tests/batch-io.rkt index 6e87fb4643..a4fc8a930a 100644 --- a/collects/2htdp/tests/batch-io.rkt +++ b/collects/2htdp/tests/batch-io.rkt @@ -3,6 +3,36 @@ (require rackunit) (require 2htdp/batch-io) +(check-equal? + (simulate-file read-file + "hello world" + " good bye" + "done") + "hello world\n good bye\ndone") + +(check-equal? + (simulate-file read-lines + "hello world" + " good bye" + "done") + '("hello world" " good bye" "done")) + +(check-equal? + (simulate-file read-words + "hello world" + " good bye" + "done") + '("hello" "world" "good" "bye" "done")) + +(check-equal? + (simulate-file read-words/line + "hello world" + " good bye" + "done") + '(("hello" "world") ("good" "bye") ("done"))) + +(check-equal? (simulate-file read-file) "") + (define file "batch-io.txt") (define test1 #<