Skip to content

Commit

Permalink
Validate try-catch-anything
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelidlessness committed Jan 9, 2016
1 parent 047e621 commit 3cd704a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
40 changes: 40 additions & 0 deletions spec/speclj/platform_spec.cljc
@@ -0,0 +1,40 @@
(ns speclj.platform-spec
(#?(:clj :require :cljs :require-macros)
[speclj.core :refer [describe it should= should-throw]]
[speclj.platform :refer [if-cljs try-catch-anything]])
(:require
[speclj.run.standard :refer [run-specs]]))

(defmacro which-env []
`(if-cljs :cljs :clj))

(describe "platform-specific bits"
#?(:cljs
(it "javascript object stays pristine"
(should= {} (js->clj (js-obj)))))

(describe "if-cljs"
(it "conditionally compiles a macro"
(should= #?(:clj :clj :cljs :cljs) (which-env))))

(describe "try-catch-anything"
(let #?(:clj [throwable (Throwable. "welp")]
:cljs [throwable "welp"])
(it "catches anything"
(try-catch-anything
(throw throwable)
(catch e
(should= e throwable))))

(it "throws if the last form is not a catch"
(should-throw
(try-catch-anything
:nope)))

(it "throws if the binding is not a symbol"
(should-throw
(try-catch-anything
:yep
(catch :nope 'whatever)))))))

(run-specs)
10 changes: 0 additions & 10 deletions spec/speclj/platform_spec.cljs

This file was deleted.

16 changes: 10 additions & 6 deletions src/speclj/platform.clj
Expand Up @@ -14,12 +14,16 @@
`java.lang.Throwable` is used in Clojure JVM."
[& forms]
(let [body (butlast forms)
[catch-sym binding & catch-forms] (last forms)]
`(if-cljs
(try ~@body
(catch :default ~binding ~@catch-forms))
(try ~@body
(catch java.lang.Throwable ~binding ~@catch-forms)))))
catch-form (last forms)
[catch-sym binding & catch-forms] (if (sequential? catch-form) catch-form [nil nil nil])
catch-valid? (and (= 'catch catch-sym) (symbol? binding))]
(if catch-valid?
`(if-cljs
(try ~@body
(catch :default ~binding ~@catch-forms))
(try ~@body
(catch java.lang.Throwable ~binding ~@catch-forms)))
`(throw (ex-info "Invalid catch form" {:catch '~catch-form})))))

(def endl (System/getProperty "line.separator"))
(def file-separator (System/getProperty "file.separator"))
Expand Down

0 comments on commit 3cd704a

Please sign in to comment.