Skip to content

Commit

Permalink
Merge pull request #19 from racehub/feature/types_testing
Browse files Browse the repository at this point in the history
tests for the types namespace
  • Loading branch information
Sam Ritchie committed Sep 3, 2014
2 parents 31336d2 + 9f7aca0 commit ea3c4da
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 21 deletions.
36 changes: 18 additions & 18 deletions src/om_bootstrap/types.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
(:require [schema.core :as s])
(:require-macros [schema.macros :as sm]))

;; ## Schema Utilities

(sm/defn schema-keys
"Returns all keys from a schema."
[schema :- {s/Any s/Any}]
(map (fn [k]
(if (s/optional-key? k) (:k k) k))
(keys schema)))

(sm/defn at-least
"Returns a map schema that accepts the supplied map schema, plus any
other optional keys that show up in the map. Such a schema can only
enforce that required keys are missing."
[schema]
(assoc schema s/Any s/Any))

;; ## Schema

(def Component
Expand Down Expand Up @@ -68,24 +84,8 @@
(keys)
(map s/optional-key)
(apply dissoc BootstrapClass))]
(assoc (merge bootstrap-schema schema)
s/Any s/Any)))

;; ## Schema Utilities

(sm/defn schema-keys
"Returns all keys from a schema."
[schema :- {s/Any s/Any}]
(map (fn [k]
(if (s/optional-key? k) (:k k) k))
(keys schema)))

(sm/defn at-least
"Returns a map schema that accepts the supplied map schema, plus any
other optional keys that show up in the map. Such a schema can only
enforce that required keys are missing."
[schema]
(assoc schema s/Any s/Any))
(at-least
(merge bootstrap-schema schema))))

;; ## Public API
;;
Expand Down
33 changes: 30 additions & 3 deletions test/om_bootstrap/types_test.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
(ns om-bootstrap.types-test
(:require [om-bootstrap.types :as t]
[cemerick.cljs.test
(:require [cemerick.cljs.test
:include-macros true
:refer [deftest is]]))
:refer [deftest is]]
[om-bootstrap.types :as t]
[schema.core :as s]))

(deftest schema-keys-test
(is (= #{:a :b :c}
(set (t/schema-keys
{:a "a!"
(s/optional-key :b) "b!"
(s/optional-key :c) "b!"})))
"Schema keys returns all keys with optionals unwrapped."))

(deftest bootstrap-test
(let [attrs {:first-key "ay"
:second-key "bee"}]
(is (= (t/at-least
(merge t/BootstrapClass attrs))
(t/bootstrap attrs))
"Usually, t/bootstrap just tacks on some extra optional fields
and an s/Any s/Any pair so that extra items won't fail
validation.")

(is (= (t/at-least
{:bs-style (s/enum "cake" "face")
(s/optional-key :bs-size) t/BSSize
(s/optional-key :bs-class) t/BSClass})
(t/bootstrap {:bs-style (s/enum "cake" "face")}))
"If the input schema contains one of the optional keys, its
value will override the default optional key.")))

(deftest separate-test
(is (= (t/separate t/BootstrapClass
Expand Down

0 comments on commit ea3c4da

Please sign in to comment.