Commits

Steve Losh committed 7736316

Fix some test issues

Comments (0)

Files changed (5)

 Red Tape
 ========
 
-Red Tape is a Clojure library for processing form data.  It's heavily inspired
-by Django's forms (the good parts), with some Clojurey twists.
+Red Tape is a Clojure library for processing user-submitted data.  It's heavily
+inspired by Django's forms (the good parts).
 
 Read [the documentation][docs] for more information.
 
 [docs]: http://sjl.bitbucket.org/red-tape/
 
+**License:** MIT/X11  
+**Documentation:** <http://sjl.bitbucket.org/red-tape/>  
+**Changelog:** <http://sjl.bitbucket.org/red-tape/changelog/>  
+**Issues:** <http://github.com/sjl/red-tape/issues/>  
+**Mercurial:** <http://bitbucket.org/sjl/red-tape/>  
+**Git:** <http://github.com/sjl/red-tape/>
+
 ## License
 
 Copyright 2013 Steve Losh and contributors.

docs/index.markdown

-Red Tape is a Clojure library for processing form data.  It's heavily inspired
-by Django's forms (the good parts), with some Clojurey twists.
+Red Tape is a Clojure library for processing user-submitted data.  It's heavily
+inspired by Django's forms (the good parts).
 
 **License:** MIT/X11  
 **Documentation:** <http://sjl.bitbucket.org/red-tape/>  

src/red_tape/cleaners.clj

   [value pred error-message]
   `(let [v# ~value]
      (if (~pred v#)
-       (throw+ ~msg)
+       (throw+ ~error-message)
        v#)))
 
 

src/red_tape/core.clj

 
   "
   [fields data]
-  (for [[field-name cleaners] fields]
-    [field-name (get data (keyword field-name) "") cleaners]))
+  (for [[field-name cleaners] fields
+        :let [data (map-for [[k v] data]
+                            [(keyword k) v])]]
+    [field-name (get data field-name "") cleaners]))
 
 (defn process-fields
   "Process the zipped fields and return a map of the outcome.

test/red_tape/core_test.clj

   :n [cs/to-long])
 
 (defform numbers-form {}
-  :n [cs/to-long]
-  :m [cs/to-long])
+  :n [#(cs/to-long % "bad")]
+  :m [#(cs/to-long % "bad")])
 
 (defform stripping-number-form {}
   :n [clojure.string/trim cs/to-long])
   :state [clojure.string/trim (partial cs/choices states)])
 
 (defform initial-form {:initial {:x "42"}}
-  :x []
-  :y [])
+  :x [cs/to-long]
+  :y [cs/to-long])
 
 (defform dynamic-initial-form
   {:arguments [x]
 
 
 (deftest test-number-form
-  (is (= (number-form)
-         (fresh-form {:n ""})))
+  (testing
+    "fresh vanilla form structure"
+    (is (= (number-form)
+           (fresh-form {:n ""}))))
 
-  (is (= (:results (number-form {"n" "10"})))
-      10)
+  (testing
+    "forms can take string keys"
+    (is (= (:results (number-form {"n" "10"}))
+        {:n 10}))) 
 
-  (are [n result]
-       (= (:results (number-form {:n n}))
-          {:n result})
-       "10" 10
-       "1" 1
-       "-42" -42))
+  (testing
+    "successful forms return results"
+    (are [n result]
+         (= (number-form {:n n})
+            {:valid true
+             :fresh false
+             :data {:n n}
+             :results {:n result}
+             :arguments {}
+             :errors nil})
+         "10" 10
+         "1" 1
+         "-42" -42
+         "0" 0))
+
+  (testing
+    "unsuccessful forms return errors"
+    (are [n]
+         (let [form (number-form {:n n})]
+           (and
+             (= (:fresh form) false)
+             (= (:valid form) false)
+             (= (:results form) nil)
+             (= (:errors form) {:n "Please enter a whole number."})
+             (= (:data form) {:n n})))
+         ""
+         "1 + 1"
+         "1dogs0"
+         "zero")))
 
 (deftest test-numbers-form
-  (is (= (numbers-form)
-         (fresh-form {:n ""
-                      :m ""})))
+  (testing
+    "fresh form structure"
+    (is (= (numbers-form)
+           (fresh-form {:n ""
+                        :m ""})))) 
 
-  (are [n m rn rm]
-       (= (:results (numbers-form {:m m :n n}))
-          {:n rn :m rm})
-       "10" "0" 10 0
-       "1" "2" 1 2))
+  (testing "multiple field results"
+    (are [n m rn rm]
+         (= (:results (numbers-form {:m m :n n}))
+            {:n rn :m rm})
+         "10" "0" 10 0
+         "1" "2" 1 2))
+
+  (testing
+    "multiple field errors"
+    (are [n m errors]
+         (= (numbers-form {:n n :m m})
+            {:fresh false
+             :valid false
+             :results nil
+             :errors errors
+             :data {:n n :m m}
+             :arguments {}})
+         "1" "dogs" {:m "bad"}
+         "cats" "1" {:n "bad"}
+         "cats" "dogs" {:n "bad" :m "bad"})))
 
 (deftest test-stripping-number-form
   (are [n result]
        "   -42  " -42))
 
 (deftest test-state-form
-  (are [available-states data result]
-       (= (:results (state-form available-states {:state data}))
-          {:state result})
-       #{"pa" "ny"} " ny"   "ny"
-       #{"ny"}      " ny  " "ny"
-       #{"ny"}      "ny"    "ny")
+  (testing
+    "choices loops things up"
+    (are [available-states data result]
+         (= (:results (state-form available-states {:state data}))
+            {:state result})
+         #{"pa" "ny"} " ny"   "ny"
+         #{"ny"}      " ny  " "ny"
+         #{"ny"}      "ny"    "ny")) 
 
-  (are [available-states data errors]
-       (= (:errors (state-form available-states {:state data}))
-          errors)
-       #{"pa" "ny"} "nj" {:state "Invalid choice."}))
+  (testing "choices excludes nonmembers, is case sensitive"
+    (are [available-states data errors]
+         (= (:errors (state-form available-states {:state data}))
+            errors)
+         #{"pa" "ny"} "nj" {:state "Invalid choice."}
+         #{"pa" "ny"} "NY" {:state "Invalid choice."})))
 
 (deftest test-initial-form
-  ; Initial data should be passed through to the initial :data map.
-  (is (= (initial-form)
-         (fresh-form {:x "42"
-                      :y ""})))
+  (testing
+    "initial data should be passed through to the initial :data map"
+    (is (= (initial-form)
+           (fresh-form {:x "42"
+                        :y ""})))) 
 
-  ; Initial data should be ignored when we have real data.
-  (is (= (initial-form {:x "1" :y "2"})
-         {:valid true
-          :fresh false
-          :data {:x "1" :y "2"}
-          :results {:x "1" :y "2"}
-          :arguments {}
-          :errors nil})))
+  (testing
+    "ignore initial data when we have real data, regardless of success"
+    (is (= (initial-form {:x "1" :y "2"})
+           {:valid true
+            :fresh false
+            :data {:x "1" :y "2"}
+            :results {:x 1 :y 2}
+            :arguments {}
+            :errors nil}))
+    (is (= (initial-form {:x "dogs" :y "2"})
+           {:valid false
+            :fresh false
+            :data {:x "dogs" :y "2"}
+            :results nil
+            :arguments {}
+            :errors {:x "Please enter a whole number."}}))))
 
 (deftest test-dynamic-initial-form
   (is (= (dynamic-initial-form ["1" "2"])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.