From 7e14b605644082ee40c8254bb1b44544c236469a Mon Sep 17 00:00:00 2001 From: Andrew Suzuki Date: Tue, 14 Jun 2016 08:33:42 -0400 Subject: [PATCH] refactor some tests to use macro; update figwheel --- project.clj | 2 +- test/cljs/ulysses/utils_test.cljs | 100 +++++++++++++++--------------- 2 files changed, 52 insertions(+), 50 deletions(-) diff --git a/project.clj b/project.clj index 6440e18..6352070 100644 --- a/project.clj +++ b/project.clj @@ -36,7 +36,7 @@ :profiles { :dev - {:plugins [[lein-figwheel "0.5.2"] + {:plugins [[lein-figwheel "0.5.4"] [lein-doo "0.1.6"]] :cljsbuild {:builds diff --git a/test/cljs/ulysses/utils_test.cljs b/test/cljs/ulysses/utils_test.cljs index f2c7201..4f381b3 100644 --- a/test/cljs/ulysses/utils_test.cljs +++ b/test/cljs/ulysses/utils_test.cljs @@ -3,16 +3,16 @@ [ulysses.utils :as utils])) (deftest test-noop - (is (nil? (utils/noop))) - (is (nil? (utils/noop 1))) - (is (nil? (utils/noop 1 2)))) + (are [x] (nil? (apply utils/noop x)) + [] [1] [1 2])) (deftest test-boolean? - (is (= true (utils/boolean? true))) - (is (= true (utils/boolean? false))) - (is (not (utils/boolean? nil))) - (is (not (utils/boolean? ""))) - (is (not (utils/boolean? 0)))) + (are [x y] (= x (utils/boolean? y)) + true true + true false + false nil + false "" + false 0)) ; missing: ptr @@ -27,7 +27,8 @@ (deftest test-remap-and-variations (let [from {:a 1 :b 2 :c 3}] (testing "remap" - (is (= {:a 2 :b 4 :c 6} (utils/remap (partial * 2) from)))) + (is (= {:a 2 :b 4 :c 6} + (utils/remap (partial * 2) from)))) (testing "remap-k" (is (= {:a1 1 :b2 2 :c3 3} (utils/remap-k @@ -67,50 +68,50 @@ (deftest test-str->int (testing "successful cases" - (is (= 0 (utils/str->int "0"))) - (is (= -5 (utils/str->int "-5"))) - (is (= 1 (utils/str->int "1"))) - (is (= 20003018 (utils/str->int "20003018"))) - (is (= 0 (utils/str->int 0))) - (is (= -5 (utils/str->int -5))) - (is (= 1 (utils/str->int 1))) - (is (= 20003018 (utils/str->int 20003018)))) + (are [x y] (= x (utils/str->int y)) + 0 "0" + -5 "-5" + 1 "1" + 20003018 "20003018" + 0 0 + -5 -5 + 1 1 + 20003018 20003018)) (testing "unsuccessful cases" - (is (nil? (utils/str->int ""))) - (is (nil? (utils/str->int "0.0"))) - (is (nil? (utils/str->int "1.0"))) - (is (nil? (utils/str->int "123.123"))) - (is (nil? (utils/str->int "garbage"))) - (is (nil? (utils/str->int "12moregarbage12"))) - (is (nil? (utils/str->int 123.123))) - (is (nil? (utils/str->int nil))))) + (are [x] (nil? (utils/str->int x)) + "" "0.0" "1.0" "123.123" + "garbage" "12moregarbage12" + 123.123 nil))) (deftest test-clj->json - (is (= "null" (utils/clj->json nil))) - (is (= "true" (utils/clj->json true))) - (is (= "-3" (utils/clj->json -3))) - (is (= "[1,2,3]" (utils/clj->json [1 2 3]))) - (is (= "[\"one\",\"three\",\"two\"]" (utils/clj->json (sorted-set :one :two :three))))) + (are [x y] (= x (utils/clj->json y)) + "null" nil + "true" true + "-3" -3 + "[1,2,3]" [1 2 3] + "[\"one\",\"three\",\"two\"]" (sorted-set :one :two :three))) (deftest test-json->clj (testing "basic values" - (is (= nil (utils/json->clj "null"))) - (is (= true (utils/json->clj "true"))) - (is (= -3 (utils/json->clj "-3"))) - (is (= [1 2 3] (utils/json->clj "[1,2,3]"))) - (is (= ["one" "two" "three"] (utils/json->clj "[\"one\",\"two\",\"three\"]")))) + (are [x y] (= x (utils/json->clj y)) + nil "null" + true "true" + -3 "-3" + [1 2 3] "[1,2,3]" + ["one" "two" "three"] "[\"one\",\"two\",\"three\"]")) (testing "maps: no keywordizing" (is (= {"a" 1 "b" 2} (utils/json->clj "{\"a\": 1, \"b\": 2}")))) (testing "maps: keywordizing" (is (= {:a 1 :b 2} (utils/json->clj "{\"a\": 1, \"b\": 2}" :keywords)))) (testing "exception throwing" - (is (thrown? js/SyntaxError (utils/json->clj "asdf"))) - (is (thrown? js/SyntaxError (utils/json->clj "[1,2"))))) + (are [x] (thrown? js/SyntaxError (utils/json->clj x)) + "asdf" "[1,2"))) (deftest test-url-encode-component - (is (= "foo" (utils/url-encode-component "foo"))) - (is (= "foo%20bar" (utils/url-encode-component "foo bar"))) - (is (= "%3F%26%3F%26" (utils/url-encode-component "?&?&")))) + (are [x y] (= x (utils/url-encode-component y)) + "foo" "foo" + "foo%20bar" "foo bar" + "%3F%26%3F%26" "?&?&")) (deftest test-truncate-with-ellipsis (testing "normal" @@ -121,14 +122,15 @@ (deftest test-find-by-id (let [ms [{:id 1} {:id 2} {:id 94}]] (testing "found (type int)" - (is (= (first ms) (utils/find-by-id 1 ms))) - (is (= (second ms) (utils/find-by-id 2 ms))) - (is (= (last ms) (utils/find-by-id 94 ms)))) + (are [pf y] (= (pf ms) (utils/find-by-id y ms)) + first 1 + second 2 + last 94)) (testing "found (type int-string)" - (is (= (first ms) (utils/find-by-id "1" ms))) - (is (= (second ms) (utils/find-by-id "2" ms))) - (is (= (last ms) (utils/find-by-id "94" ms)))) + (are [pf y] (= (pf ms) (utils/find-by-id y ms)) + first "1" + second "2" + last "94")) (testing "not found" - (is (nil? (utils/find-by-id 33 ms))) - (is (nil? (utils/find-by-id "33" ms))) - (is (nil? (utils/find-by-id "1.0" ms)))))) + (are [x] (nil? (utils/find-by-id x ms)) + 33 "33" "1.0" nil))))