;=====
; demo of throwing your own errors
;     and testing for them in BSL Racket
;=====
; last modified: 2024-09-24

(require 2htdp/image)

; used in a test below

(define FLOATING-PENGUIN
        (bitmap/url
"https://nrs-projects.humboldt.edu/~st10/f24cs111/floating-penguin.png"))

;=========
; GUESS WHAT?
; you can throw your own errors in your functions
;     in BSL Racket!
;
; (error "desired error message")
;
; and you can test for that error without killing
;    your program with:
;
; (check-error call-to-test
;              "error message you SHOULD get")

;=====
; to DEMO this, let's ADD an error if someone tries to call
;    double-it with an argument that is NOT an AddableType instance

;=====
; DATA DEFINITION
; an AddableType is one of:
;    - number
;    - image
;    - string

;=====
; signature: double-it: AddableType -> AddableType
; purpose: expects an addable thing, and:
;    *   IF it is a number, returns the sum of
;        that number and itself
;    *   IF it is an image, returns the result of
;        copying that image beside itself
;    *   IF it is a string, it returns the result
;        of appending that string to a space and itself

(check-expect (double-it 50)
              100)

(check-expect (double-it FLOATING-PENGUIN)
              (beside FLOATING-PENGUIN FLOATING-PENGUIN))

(check-expect (double-it "poot")
              "poot poot")

;                |||||
;=====           vvvvv
; ADDING a check-error test to see if double-it indeed
;     throws the expected error if called with a
;     non-AddableType argument

(check-error (double-it #true)
             "double-it: argument must be an AddableType")

(define (double-it an-addable-thing)
   (cond
     [(number? an-addable-thing)
          (+ an-addable-thing an-addable-thing)]
     [(image? an-addable-thing)
          (beside an-addable-thing an-addable-thing)]
     [(string? an-addable-thing)
          (string-append an-addable-thing " " an-addable-thing)]
     [else
          (error "double-it: argument must be an AddableType")]
   )
)

(double-it 50)
(double-it FLOATING-PENGUIN)
(double-it "poot")

; uncomment to see your error message!
;
;(double-it #true)