summaryrefslogtreecommitdiff
path: root/compojure-3.2/test/compojure/validation_test.clj
blob: c203a81055f0ef21d553d27de31db2162808e718 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(ns compojure.validation-test
  (:use compojure.html.form-helpers
        compojure.validation
        clojure.contrib.test-is))

(deftest passes-validate
  (is (= (validate {:a 1} :a (constantly true) "fail")
         {})))

(deftest fails-validate
  (is (= (validate {:a 1} :a (constantly false) "fail")
         {:a ["fail"]})))

(deftest error-class-errors
  (binding [*errors* {:foo "bar"}]
    (is (= ((error-class text-field) :foo)
           [:div.error (text-field :foo)]))))

(deftest error-class-no-errors
  (binding [*errors* {}]
    (is (= ((error-class text-field) :foo)
           (text-field :foo)))))

(deftest merge-errors-test
  (are (= (apply merge-errors _1) _2)
    [{}]                     {}
    [{} {} {}]               {}
    [{:a ["f"]}]             {:a ["f"]}
    [{:a ["f"]} {:b ["g"]}]  {:a ["f"], :b ["g"]}
    [{:a ["f"]} {:a ["g"]}]  {:a ["f" "g"]}))

(deftest validation-test
  (let [params {:a 1, :b 2}
        pred   #(= % 2)
        mesg   "isn't 2"]
    (is (= (validation params [:a pred mesg] [:b pred mesg])
           {:a ["isn't 2"]}))))