1. Chris Turner
  2. Clojure-spec

Commits

Chris Turner  committed 6bfbc77

Refactored parse-individual-spec to return a spec-result instead of throwing the spec failure exception directly. Working towards showing all spec failures in one error message

  • Participants
  • Parent commits 151f890
  • Branches default

Comments (0)

Files changed (1)

File src/clojure/spec/spec.clj

View file
  • Ignore whitespace
 exception "
 ")))
 
+(defstruct spec-result :fname :args :let-bindings :spec :sysout :actual-result :test-result)
+
 (defmacro parse-individual-spec [fname args let-bindings spec]
-  `(fn []
-     (let ~let-bindings
-       (let [sysout# (new java.io.StringWriter)
-             actual# (testit ~fname ~args sysout#)]
-         (if (let [~'value     (actual# :value)
-                   ~'exception (actual# :exception)
-                   ~'sysout    (str sysout#)]
-                ~spec)
-           true
-           (throw (new Exception (pr-failure '~fname '~args '~let-bindings '~spec actual# sysout#))))))))
+  `(let ~let-bindings
+     (let [sysout# (new java.io.StringWriter)
+           actual# (testit ~fname ~args sysout#)
+           test-result# (let [~'value     (actual# :value)
+                              ~'exception (actual# :exception)
+                              ~'sysout    (str sysout#)]
+                          ~spec)]
+       (struct spec-result '~fname '~args '~let-bindings '~spec (str sysout#) actual# test-result#))))
 
 (defmacro parse-spec [fname & args]
   (let [[let-bindings spec] (if (vector? (first args))
         all-specs (map (fn [sp] `(parse-individual-spec ~fname ~spec-args ~let-bindings ~sp))
                        (rest spec))]
     `(fn []
-       (doseq spec# (list ~@all-specs)
-         (spec#))
+       (doseq verification-value# (list ~@all-specs)
+         (when-not (:test-result verification-value#)
+           (throw (new Exception
+                       (pr-failure '~fname '~spec-args '~let-bindings (:spec verification-value#)
+                                   (:actual-result verification-value#) (:sysout verification-value#))))))
        true)))
 
 
 
 (defspec parse-individual-spec
   ([+ [] [] (= value 0)]
-      (didPass value))
+      (= (value :sysout) "")
+      (= (-> value :actual-result :value) 0)
+      (= (value :test-result) true))
   ([+ [1] [] (= value 0)]
-      (didFail value))
+      (= (value :sysout) "")
+      (= (-> value :actual-result :value) 1)
+      (= (value :test-result) false))
   ;Supports let bindings
   ([+ [] [a 0] (= value a)]
-      (didPass value))
+      (= (value :sysout) "")
+      (= (-> value :actual-result :value) 0)
+      (= (value :test-result) true))
   ([+ [] [a 1] (= value a)]
-      (didFail value))
+      (= (value :sysout) "")
+      (= (-> value :actual-result :value) 0)
+      (= (value :test-result) false))
   )
 
 (defspec parse-spec