Skip to content

Commit

Permalink
Modify exception checking in tests
Browse files Browse the repository at this point in the history
Refactored the way exceptions are checked in tests across various modules. This was done by changing from direct exception checks to using the `is-exception?` macro. This improves code readability and makes the test scripts more maintainable.
  • Loading branch information
Denis Smet committed Feb 11, 2024
1 parent be20ab3 commit 73ee58c
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 28 deletions.
16 changes: 8 additions & 8 deletions src/sicp/misc.clj
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,11 @@
(if (nil? ~exception-type)
(is false "Execution of the code expects any type of exception to be thrown")
(is false (str "Expected exception " ~exception-type " was not thrown")))
(catch Exception e#
(when ~exception-type
(is (= (class e#) ~exception-type)
(str "Expected exception type " ~exception-type ", but got " (class e#))))
(when ~expected-message
(is (= (.getMessage e#) ~expected-message)
(str "Expected message: " ~expected-message ", but got: " (.getMessage e#))))
true)))
(catch Exception exception#
(cond
(not (nil? ~exception-type)) (is (= (class exception#) ~exception-type)
(str "Expected exception type " ~exception-type ", but got " (class exception#)))
(not (nil? ~exception-type)) (is (= (.getMessage exception#) ~expected-message)
(str "Expected message: " ~expected-message ", but got: " (.getMessage exception#)))
; For valid test cases, the expected exception type and message should be nil
:else true))))
4 changes: 1 addition & 3 deletions test/sicp/chapter_1/part_3/book_1_3_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@
(is (= 6.103515625E-5 (b13/half-interval-method m/cube -1.0 9.0)))
(is (= 3.14111328125 (b13/half-interval-method #(Math/sin %) 2.0 4.0)))
(is (= 1.89306640625 (b13/half-interval-method #(- (m/cube %) (* 2 %) 3) 1.0 2.0)))
(is (m/is-exception?
(b13/half-interval-method m/cube 1 1)
"Values are not of opposite sign 1 1")))
(is (= true (m/is-exception? (b13/half-interval-method m/cube 1 1) "Values are not of opposite sign 1 1"))))

(deftest fixed-point-test
(is (= 0.7390822985224024 (b13/fixed-point #(Math/cos %) -1.0)))
Expand Down
4 changes: 1 addition & 3 deletions test/sicp/chapter_2/part_1/book_2_1_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,4 @@
(deftest pair-alt-test
(is (= 1 (b21/car-alt (b21/pair-alt 1 2))))
(is (= 2 (b21/cdr-alt (b21/pair-alt 1 2))))
(is (m/is-exception?
((b21/pair-alt 1 2) 2)
"Argument not 0 or 1: CONS")))
(is (= true (m/is-exception? ((b21/pair-alt 1 2) 2) "Argument not 0 or 1: CONS"))))
7 changes: 2 additions & 5 deletions test/sicp/chapter_2/part_1/ex_2_10_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@
[sicp.misc :as m]))

(deftest div-interval-test
(is (= [-1.5 -0.5]
(div-interval (m/make-interval 10 15) (m/make-interval -10 -20))))
(is (m/is-exception?
(div-interval (m/make-interval 1 2) (m/make-interval -1 2))
"Interval-2 is spanning zero")))
(is (= [-1.5 -0.5] (div-interval (m/make-interval 10 15) (m/make-interval -10 -20))))
(is (= true (m/is-exception? (div-interval (m/make-interval 1 2) (m/make-interval -1 2)) "Interval-2 is spanning zero"))))
4 changes: 1 addition & 3 deletions test/sicp/chapter_2/part_3/ex_2_68_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
(is (= b23/huffman-B (encode-symbol :B b23/huffman-tree)))
(is (= b23/huffman-C (encode-symbol :C b23/huffman-tree)))
(is (= b23/huffman-D (encode-symbol :D b23/huffman-tree)))
(is (m/is-exception?
(encode-symbol :Z b23/huffman-tree)
"Symbol not found in tree :Z")))
(is (= true (m/is-exception? (encode-symbol :Z b23/huffman-tree) "Symbol not found in tree :Z"))))

(deftest encode-test
(is (= '(0 ; A
Expand Down
4 changes: 1 addition & 3 deletions test/sicp/chapter_2/part_4/book_2_4_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,4 @@
(is (= y ((b24/make-from-real-imag x y) :imag-part)))
(is (= radius ((b24/make-from-real-imag x y) :magnitude)))
(is (= angle-60 ((b24/make-from-real-imag x y) :angle)))
(is (m/is-exception?
((b24/make-from-real-imag x y) :undefined)
"Unknown op: MAKE-FROM-REAL-IMAG :undefined")))
(is (= true (m/is-exception? ((b24/make-from-real-imag x y) :undefined) "Unknown op: MAKE-FROM-REAL-IMAG :undefined"))))
6 changes: 3 additions & 3 deletions test/sicp/misc_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
(comment "Chapter #1 The Elements of Programming -------------------------------------------------")

(deftest error-test
(is (m/is-exception? (m/error "123")))
(is (m/is-exception? (m/error "123") "123"))
(is (m/is-exception? (m/error "123") "123" Exception)))
(is (= true (m/is-exception? (m/error "123"))))
(is (= true (m/is-exception? (m/error "123") "123")))
(is (= true (m/is-exception? (m/error "123") "123" Exception))))

(deftest square-test
(is (= 4 (m/square 2)))
Expand Down

0 comments on commit 73ee58c

Please sign in to comment.