From 73ee58c0e13647cfb9cf3f7b6aa670b5e74f2953 Mon Sep 17 00:00:00 2001 From: Denis Smet Date: Mon, 12 Feb 2024 00:27:47 +0400 Subject: [PATCH] Modify exception checking in tests 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. --- src/sicp/misc.clj | 16 ++++++++-------- test/sicp/chapter_1/part_3/book_1_3_test.clj | 4 +--- test/sicp/chapter_2/part_1/book_2_1_test.clj | 4 +--- test/sicp/chapter_2/part_1/ex_2_10_test.clj | 7 ++----- test/sicp/chapter_2/part_3/ex_2_68_test.clj | 4 +--- test/sicp/chapter_2/part_4/book_2_4_test.clj | 4 +--- test/sicp/misc_test.clj | 6 +++--- 7 files changed, 17 insertions(+), 28 deletions(-) diff --git a/src/sicp/misc.clj b/src/sicp/misc.clj index 8b898d7..eed5585 100644 --- a/src/sicp/misc.clj +++ b/src/sicp/misc.clj @@ -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)))) diff --git a/test/sicp/chapter_1/part_3/book_1_3_test.clj b/test/sicp/chapter_1/part_3/book_1_3_test.clj index d17e80d..a72320a 100644 --- a/test/sicp/chapter_1/part_3/book_1_3_test.clj +++ b/test/sicp/chapter_1/part_3/book_1_3_test.clj @@ -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))) diff --git a/test/sicp/chapter_2/part_1/book_2_1_test.clj b/test/sicp/chapter_2/part_1/book_2_1_test.clj index 48b592c..c4822d9 100644 --- a/test/sicp/chapter_2/part_1/book_2_1_test.clj +++ b/test/sicp/chapter_2/part_1/book_2_1_test.clj @@ -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")))) diff --git a/test/sicp/chapter_2/part_1/ex_2_10_test.clj b/test/sicp/chapter_2/part_1/ex_2_10_test.clj index f9f7ac0..1f38c78 100644 --- a/test/sicp/chapter_2/part_1/ex_2_10_test.clj +++ b/test/sicp/chapter_2/part_1/ex_2_10_test.clj @@ -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")))) diff --git a/test/sicp/chapter_2/part_3/ex_2_68_test.clj b/test/sicp/chapter_2/part_3/ex_2_68_test.clj index 20c6910..69776ad 100644 --- a/test/sicp/chapter_2/part_3/ex_2_68_test.clj +++ b/test/sicp/chapter_2/part_3/ex_2_68_test.clj @@ -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 diff --git a/test/sicp/chapter_2/part_4/book_2_4_test.clj b/test/sicp/chapter_2/part_4/book_2_4_test.clj index a45f834..edb86c7 100644 --- a/test/sicp/chapter_2/part_4/book_2_4_test.clj +++ b/test/sicp/chapter_2/part_4/book_2_4_test.clj @@ -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")))) diff --git a/test/sicp/misc_test.clj b/test/sicp/misc_test.clj index 14caefc..10e34ad 100644 --- a/test/sicp/misc_test.clj +++ b/test/sicp/misc_test.clj @@ -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)))