Skip to content

Commit

Permalink
Leave nil values untouched in conversions.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyclemson committed Jun 2, 2024
1 parent 6b7fdb8 commit c422b71
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/configurati/conversions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

(defmethod convert-to :boolean [_ value]
(cond
(nil? value) nil
(or (= "true" value) (true? value)) true
(or (= "false" value) (false? value)) false
:else
Expand Down
8 changes: 7 additions & 1 deletion test/configurati/conversions_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
(conf-conv/convert-to :integer Integer/MAX_VALUE)))
(is (= Integer/MIN_VALUE
(conf-conv/convert-to :integer Integer/MIN_VALUE))))
(testing "leaves nil untouched"
(is (= nil (conf-conv/convert-to :integer nil))))
(testing "throws for non-integral values"
(is (thrown? Exception
(conf-conv/convert-to :integer "hello there")))
Expand All @@ -37,7 +39,9 @@
(testing "leaves string untouched"
(is (= "hello" (conf-conv/convert-to :string "hello")))
(is (= "5" (conf-conv/convert-to :string "5")))
(is (= "true" (conf-conv/convert-to :string "true")))))
(is (= "true" (conf-conv/convert-to :string "true"))))
(testing "leaves nil untouched"
(is (= nil (conf-conv/convert-to :string nil)))))

(deftest boolean-conversion
(testing "converts values to boolean"
Expand All @@ -46,6 +50,8 @@
(testing "leaves booleans untouched"
(is (= true (conf-conv/convert-to :boolean true)))
(is (= false (conf-conv/convert-to :boolean false))))
(testing "leaves nil untouched"
(is (= nil (conf-conv/convert-to :boolean nil))))
(testing "throws for non-boolean values"
(is (thrown? Exception
(conf-conv/convert-to :boolean "spinach"))))
Expand Down

0 comments on commit c422b71

Please sign in to comment.