Skip to content

Commit

Permalink
refactor some tests to use macro; update figwheel
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Jun 14, 2016
1 parent 6c9f7dd commit 7e14b60
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
100 changes: 51 additions & 49 deletions test/cljs/ulysses/utils_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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))))

0 comments on commit 7e14b60

Please sign in to comment.