Skip to content

Commit

Permalink
move tests to separate directory; fix build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Jun 13, 2016
1 parent 56d52b6 commit 6c9f7dd
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 160 deletions.
11 changes: 0 additions & 11 deletions src/cljs/ulysses/db.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
(ns ulysses.db
(:require-macros [cljs.test :refer [deftest testing are is]])
(:require [ulysses.utils :refer [document-size window-size window-scroll-position]]))

(def default-db
Expand Down Expand Up @@ -104,13 +103,3 @@
"given a db, set the given keys to their original state"
[db keys]
(merge db (select-keys default-db keys)))

;; ----------------------------------------------------------------------------
;; tests
;; ----------------------------------------------------------------------------

(deftest test-refresh-keys
(testing "refresh-keys"
(are [keys db] (= default-db (refresh-keys db keys))
[:active-page] (assoc default-db :active-page :builder)
[:page-current] (assoc default-db :page-current 2))))
136 changes: 1 addition & 135 deletions src/cljs/ulysses/utils.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(ns ulysses.utils
(:require-macros [cljs.test :refer [deftest testing are is]]
[cljs.core.async.macros :refer [go-loop]])
(:require-macros [cljs.core.async.macros :refer [go-loop]])
(:require [clojure.string :as string]
[clojure.walk :refer [keywordize-keys]]
[clojure.set :refer [rename-keys]]
Expand Down Expand Up @@ -531,136 +530,3 @@

(defn scroll-to [y]
(.scrollTo js/window 0 y))

;; ----------------------------------------------------------------------------
;; tests
;; ----------------------------------------------------------------------------

(deftest test-noop
(is (nil? (noop)))
(is (nil? (noop 1)))
(is (nil? (noop 1 2))))

(deftest test-boolean?
(is (= true (boolean? true)))
(is (= true (boolean? false)))
(is (not (boolean? nil)))
(is (not (boolean? "")))
(is (not (boolean? 0))))

; missing: ptr

; missing: ptr-first

(deftest test-metas-equal?
(is (metas-equal? [(with-meta {:a 1} {:key 1})] [(with-meta {:a 2} {:key 1})]))
(is (not (metas-equal? [(with-meta {:a 1} {:key 1})] [(with-meta {:a 2} {:key 2})]))))

(deftest test-remap-and-variations
(let [from {:a 1 :b 2 :c 3}]
(testing "remap"
(is (= {:a 2 :b 4 :c 6} (remap (partial * 2) from))))
(testing "remap-k"
(is (= {:a1 1 :b2 2 :c3 3}
(remap-k
(fn [k v] (keyword (str (name k) v)))
from))))
(testing "remap-v"
(is (= {:a :a1 :b :b2 :c :c3}
(remap-v
(fn [k v] (keyword (str (name k) v)))
from))))))

(deftest test-hyphenate-keys
(is (= {:a-b 1 :c-d 2} (hyphenate-keys {:a_b 1 :c_d 2}))))

(deftest test-map-subels
(testing "when values have ids"
(let [in [{:id 23 :value 3} {:id 95 :value 4}]
out (map-subels :div in)
expected (list (with-meta [:div (first in)] {:key 23})
(with-meta [:div (second in)] {:key 95}))]
(is (= out expected))
(is (metas-equal? out expected))))
(testing "when values don't have ids"
(let [in [3 4]
out (map-subels :div in)
expected (list (with-meta [:div (first in)] {:key "default-key-0"})
(with-meta [:div (second in)] {:key "default-key-1"}))]
(is (= out expected))
(is (metas-equal? out expected))))
(testing "when values have ids sometimes"
(let [in [{:id 23 :value 3} 4]
out (map-subels :div in)
expected (list (with-meta [:div (first in)] {:key 23})
(with-meta [:div (second in)] {:key "default-key-1"}))]
(is (= out expected))
(is (metas-equal? out expected)))))

(deftest test-str->int
(testing "successful cases"
(is (= 0 (str->int "0")))
(is (= -5 (str->int "-5")))
(is (= 1 (str->int "1")))
(is (= 20003018 (str->int "20003018")))
(is (= 0 (str->int 0)))
(is (= -5 (str->int -5)))
(is (= 1 (str->int 1)))
(is (= 20003018 (str->int 20003018))))
(testing "unsuccessful cases"
(is (nil? (str->int "")))
(is (nil? (str->int "0.0")))
(is (nil? (str->int "1.0")))
(is (nil? (str->int "123.123")))
(is (nil? (str->int "garbage")))
(is (nil? (str->int "12moregarbage12")))
(is (nil? (str->int 123.123)))
(is (nil? (str->int nil)))))

(deftest test-clj->json
(is (= "null" (clj->json nil)))
(is (= "true" (clj->json true)))
(is (= "-3" (clj->json -3)))
(is (= "[1,2,3]" (clj->json [1 2 3])))
(is (= "[\"one\",\"three\",\"two\"]" (clj->json (sorted-set :one :two :three)))))

(deftest test-json->clj
(testing "basic values"
(is (= nil (json->clj "null")))
(is (= true (json->clj "true")))
(is (= -3 (json->clj "-3")))
(is (= [1 2 3] (json->clj "[1,2,3]")))
(is (= ["one" "two" "three"] (json->clj "[\"one\",\"two\",\"three\"]"))))
(testing "maps: no keywordizing"
(is (= {"a" 1 "b" 2} (json->clj "{\"a\": 1, \"b\": 2}"))))
(testing "maps: keywordizing"
(is (= {:a 1 :b 2} (json->clj "{\"a\": 1, \"b\": 2}" :keywords))))
(testing "exception throwing"
(is (thrown? js/SyntaxError (json->clj "asdf")))
(is (thrown? js/SyntaxError (json->clj "[1,2")))))

(deftest test-url-encode-component
(is (= "foo" (url-encode-component "foo")))
(is (= "foo%20bar" (url-encode-component "foo bar")))
(is (= "%3F%26%3F%26" (url-encode-component "?&?&"))))

(deftest test-truncate-with-ellipsis
(testing "normal"
(is (= "foo..." (truncate-with-ellipsis "foo bar" 3))))
(testing "custom ellipsis"
(is (= "foo[..]" (truncate-with-ellipsis "foo bar" 3 "[..]")))))

(deftest test-find-by-id
(let [ms [{:id 1} {:id 2} {:id 94}]]
(testing "found (type int)"
(is (= (first ms) (find-by-id 1 ms)))
(is (= (second ms) (find-by-id 2 ms)))
(is (= (last ms) (find-by-id 94 ms))))
(testing "found (type int-string)"
(is (= (first ms) (find-by-id "1" ms)))
(is (= (second ms) (find-by-id "2" ms)))
(is (= (last ms) (find-by-id "94" ms))))
(testing "not found"
(is (nil? (find-by-id 33 ms)))
(is (nil? (find-by-id "33" ms)))
(is (nil? (find-by-id "1.0" ms))))))
8 changes: 4 additions & 4 deletions src/js/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ ulysses.extra = (function() {
},

fit: function(blocks) {
var n, node, block, final;
var n, node, block, packed;

final = [];
packed = [];

for (n = 0; n < blocks.length; n++) {
block = blocks[n];

if (node = this.findNode(this.root, block.w, block.h)) {
block.fit = this.splitNode(node, block.w, block.h);
final.push(block);
packed.push(block);
}
}

return final;
return packed;
},

findNode: function(root, w, h) {
Expand Down
8 changes: 1 addition & 7 deletions test/cljs/ulysses/core_test.cljs
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
(ns ulysses.core-test
(:require [cljs.test :refer-macros [deftest testing is]]
[ulysses.core :as core]))

;; NOTE
;; runner.cljs is currently set up to run tests in
;; all namespaces that match ulysses.*, which mostly
;; voids the need for this auxillary test directory
(:require [cljs.test :refer-macros [deftest testing are is]]))

(deftest fake-test
(testing "description"
Expand Down
9 changes: 9 additions & 0 deletions test/cljs/ulysses/db_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(ns ulysses.db-test
(:require [cljs.test :refer-macros [deftest testing are is]]
[ulysses.db :as db]))

(deftest test-refresh-keys
(testing "refresh-keys"
(are [keys db-state] (= db/default-db (db/refresh-keys db-state keys))
[:active-page] (assoc db/default-db :active-page :builder)
[:page-current] (assoc db/default-db :page-current 2))))
8 changes: 5 additions & 3 deletions test/cljs/ulysses/runner.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(ns ulysses.runner
(:require [doo.runner :refer-macros [doo-tests doo-all-tests]]
[ulysses.core-test]))
(:require [doo.runner :refer-macros [doo-all-tests]]
[ulysses.core-test]
[ulysses.utils-test]
[ulysses.db-test]))

;; run all tests in all namespaces matching ulysses.*
(doo.runner/doo-all-tests #"ulysses.*")
(doo-all-tests #"ulysses.*")
134 changes: 134 additions & 0 deletions test/cljs/ulysses/utils_test.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
(ns ulysses.utils-test
(:require [cljs.test :refer-macros [deftest testing are is]]
[ulysses.utils :as utils]))

(deftest test-noop
(is (nil? (utils/noop)))
(is (nil? (utils/noop 1)))
(is (nil? (utils/noop 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))))

; missing: ptr

; missing: ptr-first

(deftest test-metas-equal?
(is (utils/metas-equal? [(with-meta {:a 1} {:key 1})]
[(with-meta {:a 2} {:key 1})]))
(is (not (utils/metas-equal? [(with-meta {:a 1} {:key 1})]
[(with-meta {:a 2} {:key 2})]))))

(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))))
(testing "remap-k"
(is (= {:a1 1 :b2 2 :c3 3}
(utils/remap-k
(fn [k v] (keyword (str (name k) v)))
from))))
(testing "remap-v"
(is (= {:a :a1 :b :b2 :c :c3}
(utils/remap-v
(fn [k v] (keyword (str (name k) v)))
from))))))

(deftest test-hyphenate-keys
(is (= {:a-b 1 :c-d 2} (utils/hyphenate-keys {:a_b 1 :c_d 2}))))

(deftest test-map-subels
(testing "when values have ids"
(let [in [{:id 23 :value 3} {:id 95 :value 4}]
out (utils/map-subels :div in)
expected (list (with-meta [:div (first in)] {:key 23})
(with-meta [:div (second in)] {:key 95}))]
(is (= out expected))
(is (utils/metas-equal? out expected))))
(testing "when values don't have ids"
(let [in [3 4]
out (utils/map-subels :div in)
expected (list (with-meta [:div (first in)] {:key "default-key-0"})
(with-meta [:div (second in)] {:key "default-key-1"}))]
(is (= out expected))
(is (utils/metas-equal? out expected))))
(testing "when values have ids sometimes"
(let [in [{:id 23 :value 3} 4]
out (utils/map-subels :div in)
expected (list (with-meta [:div (first in)] {:key 23})
(with-meta [:div (second in)] {:key "default-key-1"}))]
(is (= out expected))
(is (utils/metas-equal? out expected)))))

(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))))
(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)))))

(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)))))

(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\"]"))))
(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")))))

(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 "?&?&"))))

(deftest test-truncate-with-ellipsis
(testing "normal"
(is (= "foo..." (utils/truncate-with-ellipsis "foo bar" 3))))
(testing "custom ellipsis"
(is (= "foo[..]" (utils/truncate-with-ellipsis "foo bar" 3 "[..]")))))

(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))))
(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))))
(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))))))

0 comments on commit 6c9f7dd

Please sign in to comment.