Skip to content

Commit

Permalink
Refactor exception handling across multiple test files
Browse files Browse the repository at this point in the history
Refactored exception handling in tests across multiple modules, replacing previous implementations with the centralized and enhanced `is-exception?` macro. This macro captures the exception type and message and eliminates repeated code, enhancing code cleanliness and maintainability.
  • Loading branch information
Denis Smet committed Feb 11, 2024
1 parent 63efebd commit be20ab3
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/sicp/chapter_2/part_1/ex_2_10.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(if (< (* (m/lower-bound interval-2)
(m/upper-bound interval-2))
0)
(m/error "interval-2 is spanning zero")
(m/error "Interval-2 is spanning zero")
(m/mul-interval
interval-1
(m/make-interval (/ 1.0 (m/lower-bound interval-2))
Expand Down
20 changes: 19 additions & 1 deletion src/sicp/misc.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(ns sicp.misc)
(ns sicp.misc
(:require
[clojure.test :refer [is]]))

(comment "Chapter #1 The Elements of Programming -------------------------------------------------")

Expand Down Expand Up @@ -191,3 +193,19 @@
(if (empty? list1)
(if (empty? list2) '() list2)
(cons (first list1) (append (rest list1) list2))))

(defmacro is-exception?
[test-code & [expected-message exception-type]]
`(try
~test-code
(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)))
7 changes: 3 additions & 4 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,10 +87,9 @@
(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 (thrown-with-msg?
Exception
#"Values are not of opposite sign 1 1"
(b13/half-interval-method m/cube 1 1))))
(is (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
10 changes: 5 additions & 5 deletions test/sicp/chapter_2/part_1/book_2_1_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns sicp.chapter-2.part-1.book-2-1-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-2.part-1.book-2-1 :as b21]))
[sicp.chapter-2.part-1.book-2-1 :as b21]
[sicp.misc :as m]))

(comment "2 Building Abstractions with Data ------------------------------------------------------")

Expand Down Expand Up @@ -76,7 +77,6 @@
(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 (thrown-with-msg?
Exception
#"Argument not 0 or 1: CONS"
((b21/pair-alt 1 2) 2))))
(is (m/is-exception?
((b21/pair-alt 1 2) 2)
"Argument not 0 or 1: CONS")))
12 changes: 4 additions & 8 deletions test/sicp/chapter_2/part_1/ex_2_10_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@

(deftest div-interval-test
(is (= [-1.5 -0.5]
(div-interval (m/make-interval 10 15) (m/make-interval -10 -20)))))

(deftest exception-with-message-test
(try
(div-interval (m/make-interval 1 2) (m/make-interval -1 2))
(is false "Exception not thrown")
(catch Exception e
(is (= (.getMessage e) "interval-2 is spanning zero")))))
(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")))
21 changes: 6 additions & 15 deletions test/sicp/chapter_2/part_3/ex_2_68_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,17 @@
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-2.part-3.book-2-3 :as b23]
[sicp.chapter-2.part-3.ex-2-68 :refer [encode encode-symbol]]))
[sicp.chapter-2.part-3.ex-2-68 :refer [encode encode-symbol]]
[sicp.misc :as m]))

(deftest encode-symbol-test
(is (= b23/huffman-A (encode-symbol :A b23/huffman-tree)))
(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))))

(deftest encode-symbol-exception-test
(is (thrown-with-msg?
Exception
#"Symbol not found in tree :Z"
(encode-symbol :Z b23/huffman-tree))))

(deftest encode-exception-test
(try
(encode-symbol :Z b23/huffman-tree)
(is false "Exception not thrown")
(catch Exception e
(is (= (.getMessage e) "Symbol not found in tree :Z")))))
(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")))

(deftest encode-test
(is (= '(0 ; A
Expand Down
8 changes: 6 additions & 2 deletions test/sicp/chapter_2/part_4/book_2_4_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns sicp.chapter-2.part-4.book-2-4-test
(:require
[clojure.test :refer [deftest is]]
[sicp.chapter-2.part-4.book-2-4 :as b24]))
[sicp.chapter-2.part-4.book-2-4 :as b24]
[sicp.misc :as m]))

(def angle-60 (/ Math/PI 3))
(def radius 2.0)
Expand Down Expand Up @@ -127,4 +128,7 @@
(is (= x ((b24/make-from-real-imag x y) :real-part)))
(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 (= 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")))
7 changes: 3 additions & 4 deletions test/sicp/misc_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
(comment "Chapter #1 The Elements of Programming -------------------------------------------------")

(deftest error-test
(try
(m/error "123")
(catch Exception e
(is (= (.getMessage e) "123")))))
(is (m/is-exception? (m/error "123")))
(is (m/is-exception? (m/error "123") "123"))
(is (m/is-exception? (m/error "123") "123" Exception)))

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

0 comments on commit be20ab3

Please sign in to comment.