From 753dfa3cb286706fb14d3857d7ef116c189cc707 Mon Sep 17 00:00:00 2001 From: Stephen Bloch Date: Sun, 2 Oct 2011 22:54:34 -0400 Subject: [PATCH] Fixed error messages, added tests for type errors. --- collects/picturing-programs/private/tiles.rkt | 28 +++++++++++--- .../tests/tiles-error-tests.rkt | 37 +++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) create mode 100644 collects/picturing-programs/tests/tiles-error-tests.rkt diff --git a/collects/picturing-programs/private/tiles.rkt b/collects/picturing-programs/private/tiles.rkt index 281d554d2c..b858db49e1 100644 --- a/collects/picturing-programs/private/tiles.rkt +++ b/collects/picturing-programs/private/tiles.rkt @@ -81,18 +81,28 @@ (rotate 45 (flip-vertical (rotate -45 picture)))) ; synonyms - (define (flip-main picture) (reflect-main-diag picture)) - (define (flip-other picture) (reflect-other-diag picture)) + (define (flip-main picture) + (check-image 'flip-main picture "first") + (reflect-main-diag picture)) + (define (flip-other picture) + (check-image 'flip-other picture "first") + (reflect-other-diag picture)) ; natural-number? anything -> boolean (define (natural-number? x) (and (integer? x) (>= x 0))) + ; natural-bounded? natural anything -> boolean + (define (natural-bounded? max x) + (and (natural-number? x) (<= x max))) + ; crop-left : image natural-number -> image ; deletes that many pixels from left edge of image (define (crop-left picture pixels) (check-image 'crop-left picture "first") - (check-arg 'crop-left (natural-number? pixels) "natural number" "second" pixels) + (check-arg 'crop-left (natural-bounded? (image-width picture) pixels) + (format "natural number <= ~a" (image-width picture)) + "second" pixels) (crop pixels 0 (- (image-width picture) pixels) (image-height picture) picture)) @@ -101,7 +111,9 @@ ; deletes that many pixels from top edge of image (define (crop-top picture pixels) (check-image 'crop-top picture "first") - (check-arg 'crop-top (natural-number? pixels) "natural number" "second" pixels) + (check-arg 'crop-top (natural-bounded? (image-height picture) pixels) + (format "natural number <= ~a" (image-height picture)) + "second" pixels) (crop 0 pixels (image-width picture) (- (image-height picture) pixels) picture)) @@ -110,7 +122,9 @@ ; deletes that many pixels from right edge of image (define (crop-right picture pixels) (check-image 'crop-right picture "first") - (check-arg 'crop-right (natural-number? pixels) "natural number" "second" pixels) + (check-arg 'crop-right (natural-bounded? (image-width picture) pixels) + (format "natural number <= ~a" (image-width picture)) + "second" pixels) (crop 0 0 (- (image-width picture) pixels) (image-height picture) @@ -120,7 +134,9 @@ ; deletes that many pixels from bottom edge of image (define (crop-bottom picture pixels) (check-image 'crop-bottom picture "first") - (check-arg 'crop-bottom (natural-number? pixels) "natural number" "second" pixels) + (check-arg 'crop-bottom (natural-bounded? (image-height picture) pixels) + (format "natural number <= ~a" (image-height picture)) + "second" pixels) (crop 0 0 (image-width picture) (- (image-height picture) pixels) diff --git a/collects/picturing-programs/tests/tiles-error-tests.rkt b/collects/picturing-programs/tests/tiles-error-tests.rkt new file mode 100644 index 0000000000..ffcb0464ec --- /dev/null +++ b/collects/picturing-programs/tests/tiles-error-tests.rkt @@ -0,0 +1,37 @@ +;; The first three lines of this file were inserted by DrRacket. They record metadata +;; about the language level of this file in a form that our tools can easily process. +#reader(lib "htdp-beginner-reader.ss" "lang")((modname tiles-error-tests) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) +(require picturing-programs) + +(check-error (reflect-horiz 17) + "reflect-horiz: expected as first argument, given: 17") +(check-error (reflect-vert "hello") + "reflect-vert: expected as first argument, given: \"hello\"") +(check-error (reflect-main-diag true) + "reflect-main-diag: expected as first argument, given: true") +(check-error (reflect-other-diag false) + "reflect-other-diag: expected as first argument, given: false") +(check-error (flip-main 'blue) + "flip-main: expected as first argument, given: 'blue") +(check-error (flip-other "snark") + "flip-other: expected as first argument, given: \"snark\"") +(check-error (crop-left pic:hacker 50) + "crop-left: expected as second argument, given: 50") +(check-error (crop-right pic:bloch 100) + "crop-right: expected as second argument, given: 100") +(check-error (crop-top pic:book 56) + "crop-top: expected as second argument, given: 56") +(check-error (crop-bottom pic:hacker 56) + "crop-bottom: expected as second argument, given: 56") +(check-error (crop-left pic:hacker -3) + "crop-left: expected as second argument, given: -3") +(check-error (crop-top pic:book 3.2) + "crop-top: expected as second argument, given: 3.2") +(check-error (crop-bottom pic:book pic:book) + "crop-bottom: expected as second argument, given: #") +(check-error (rotate-cw 17) + "rotate-cw: expected as first argument, given: 17") +(check-error (rotate-ccw true) + "rotate-ccw: expected as first argument, given: true") +(check-error (rotate-180 "goodbye") + "rotate-180: expected as first argument, given: \"goodbye\"") \ No newline at end of file