Skip to content

Commit

Permalink
Pass all specs in CLojure CLR
Browse files Browse the repository at this point in the history
  • Loading branch information
brandoncorrea committed Nov 17, 2024
1 parent f0731bf commit 97da244
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 68 deletions.
4 changes: 3 additions & 1 deletion deps-clr.edn
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{:paths ["src/clj" "src/cljc" "src/cljr"]
:deps {
io.github.clojure/clr.tools.namespace {:git/tag "v1.5.2" :git/sha "d540923"}
;io.github.clojure/clr.tools.namespace {:git/tag "v1.5.2" :git/sha "d540923"}
;; TODO [BAC]: Use clojure tools once fork has been merged
io.github.brandoncorrea/clr.tools.namespace {:git/sha "6aaae5337f510365c5712a71a0274c8cf81d931f"}
}
:aliases {:spec {:extra-paths ["spec/clj" "spec/cljc" "spec/cljr"]
:main-opts ["-m" "speclj.main" "-c" "spec/clj" "spec/cljc" "spec/cljr"]}}}
4 changes: 2 additions & 2 deletions examples/failures/storm.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
(should-throw (+ 1 1)))

(it "fails to throw the right error"
(should-throw Exception (throw (Error. "oops"))))
(should-throw clojure.lang.ExceptionInfo (throw (Exception. "oops"))))

(it "fails to throw error with the right message"
(should-throw Exception "Howdy" (throw (Exception. "Hiya")))))


(run-specs)
(run-specs)
4 changes: 3 additions & 1 deletion examples/prime_factors/prime_factors.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
(recur (conj factors divisor) divisor (/ n divisor))
(recur factors (inc divisor) n)))))

(defn pow [x n] (apply * (repeat n (bigdec x))))

(describe "prime factors"
(it "factors 1"
(should= [] (factors-of 1)))
Expand Down Expand Up @@ -38,7 +40,7 @@
(should= [3 3] (factors-of 9)))

(it "factors 2^100"
(should= (repeat 100 2) (factors-of (Math/pow 2 100))))
(should= (repeat 100 2) (factors-of (pow 2 100))))

; MDM - This one takes a bit too long to participate in the spec suite
; (it "factors 2^19-1"
Expand Down
7 changes: 4 additions & 3 deletions spec/cljc/speclj/platform_spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

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

(describe "try-catch-anything"
(let #?(:clj [throwable (Throwable. "welp")]
:cljs [throwable "welp"])
(let [throwable #?(:clj (Throwable. "welp")
:cljs "welp"
:cljr (Exception. "welp"))]
(it "catches anything"
(try-catch-anything
(throw throwable)
Expand Down
133 changes: 77 additions & 56 deletions spec/cljc/speclj/should_spec.cljc
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
(ns speclj.should-spec
(#?(:clj :require :cljs :require-macros)
[speclj.core :refer [it
context
describe
should should-not
should= should-not=
should== should-not==
should-be should-not-be
should-be-a should-not-be-a
should-be-nil should-not-be-nil
should-be-same should-not-be-same
should-contain should-not-contain
should-have-count should-not-have-count
should-start-with should-not-start-with
should-end-with should-not-end-with
should-throw should-not-throw
should< should<=
should> should>=
should-fail
-to-s -new-throwable -new-exception]]
[speclj.spec-helper :refer [should-fail! should-pass! failure-message]])
(:require [speclj.platform :refer [endl exception type-name throwable]]
[speclj.run.standard :as standard]))
(:require [speclj.core #?(:cljs :refer-macros :default :refer) [it
context
describe
should should-not
should= should-not=
should== should-not==
should-be should-not-be
should-be-a should-not-be-a
should-be-nil should-not-be-nil
should-be-same should-not-be-same
should-contain should-not-contain
should-have-count should-not-have-count
should-start-with should-not-start-with
should-end-with should-not-end-with
should-throw should-not-throw
should< should<=
should> should>=
should-fail
-to-s -new-throwable -new-exception]]
[speclj.spec-helper #?(:cljs :refer-macros :default :refer) [should-fail! should-pass! failure-message]]
[speclj.platform :refer [endl exception type-name throwable]]
[speclj.run.standard :as standard]
[clojure.string :as str]))

(describe "Should Assertions: "
(context "should"
Expand Down Expand Up @@ -100,8 +100,8 @@
(should-pass! (should-be-same "foo" "foo"))
(should-pass! (should-be-same 1 1))
(should-fail! (should-be-same [] ()))
#?(:clj
(should-fail! (should-be-same 1 1.0)))
#?(:cljs (do)
:default (should-fail! (should-be-same 1 1.0)))
)

(it "failure message is nice"
Expand All @@ -114,8 +114,8 @@
(should-fail! (should-not-be-same 1 1))
(should-pass! (should-not-be-same [] ()))

#?(:clj
(should-pass! (should-not-be-same 1 1.0)))
#?(:cljs (do)
:default (should-pass! (should-not-be-same 1 1.0)))
)

(it "failure message is nice"
Expand All @@ -142,10 +142,10 @@
(let [error (str "Expected: 1" endl " got: 2 (using ==)")]
(should= error (failure-message (should== 1 2)))))

#?(:clj
(it "reports the error with floats"
(let [error (str "Expected: 1.0" endl " got: 2 (using ==)")]
(should= error (failure-message (should== 1.0 2))))))
(it "reports the error with floats"
(let [error (str "Expected: 1.0" endl " got: 2 (using ==)")]
#?(:cljs (do)
:default (should= error (failure-message (should== 1.0 2))))))

)

Expand Down Expand Up @@ -206,12 +206,12 @@
(it "checks equality of maps"
(should-pass! (should== {:a 1} {:a 1}))
(should-fail! (should== {:a 1} {:a 1 :b 2}))
(let [lines (.split (failure-message (should== {:a 1} {:a 1 :b 2})) endl)]
(should= "Expected contents: {:a 1}" (aget lines 0))
(should-contain ":b 2" (aget lines 1))
(should-contain ":a 1" (aget lines 1))
(should= " missing: nil" (aget lines 2))
(should= " extra: {:b 2}" (aget lines 3))
(let [lines (str/split-lines (failure-message (should== {:a 1} {:a 1 :b 2})))]
(should= "Expected contents: {:a 1}" (nth lines 0))
(should-contain ":b 2" (nth lines 1))
(should-contain ":a 1" (nth lines 1))
(should= " missing: nil" (nth lines 2))
(should= " extra: {:b 2}" (nth lines 3))
)
; (should= (str "Expected contents: {:a 1}" endl " got: {:b 2, :a 1}" endl " missing: nil" endl " extra: {:b 2}")
; (failure-message (should== {:a 1} {:a 1 :b 2})))
Expand All @@ -233,10 +233,10 @@
(let [error (str " Expected: 1" endl "not to ==: 1 (using ==)")]
(should= error (failure-message (should-not== 1 1)))))

#?(:clj
(it "reports the error with floats"
(let [error (str " Expected: 1.0" endl "not to ==: 1 (using ==)")]
(should= error (failure-message (should-not== 1.0 1))))))
(it "reports the error with floats"
(let [error (str " Expected: 1.0" endl "not to ==: 1 (using ==)")]
#?(:cljs (do)
:default (should= error (failure-message (should-not== 1.0 1))))))

)

Expand Down Expand Up @@ -490,35 +490,52 @@
(should-not-end-with 1 2))))

(context "should-throw"

#?(:clj
(it "tests that any Throwable is thrown"
(should-pass! (should-throw (throw (java.lang.Throwable. "error"))))
(should-fail! (should-throw (+ 1 1)))
(should= (str "Expected " (type-name java.lang.Throwable) " thrown from: (+ 1 1)" endl
(apply str (take (count (type-name java.lang.Throwable)) (repeat " "))) " but got: <nothing thrown>")
(failure-message (should-throw (+ 1 1)))))
:cljr
(it "tests that any Throwable is thrown"
(should-pass! (should-throw (throw (Exception. "error"))))
(should-fail! (should-throw (+ 1 1)))
(should= (str "Expected " (type-name Exception) " thrown from: (+ 1 1)" endl
(apply str (take (count (type-name Exception)) (repeat " "))) " but got: <nothing thrown>")
(failure-message (should-throw (+ 1 1))))))

(it "can test an expected throwable type"
(should-pass! (should-throw exception (throw (-new-exception))))

#?(:clj
(should-pass! (should-throw java.lang.Object (throw (java.lang.Exception.)))))
#?(:cljs (do)
:default (should-pass! (should-throw Object (throw (Exception.)))))

(should-fail! (should-throw exception (throw (-new-throwable))))
#?(:cljr (should-fail! (should-throw SystemException (throw (-new-throwable))))
:default (should-fail! (should-throw exception (throw (-new-throwable)))))
(should-fail! (should-throw exception (+ 1 1)))
(should= (str "Expected " (type-name exception) " thrown from: (+ 1 1)" endl
(apply str (take (count (type-name exception)) (repeat " "))) " but got: <nothing thrown>")
(failure-message (should-throw exception (+ 1 1))))
#?(:cljs
(should=
(str "Expected nothing thrown from: " (pr-str '(throw (-new-throwable "some message"))) endl " but got: #object[String some message]")
(str "Expected nothing thrown from: " (pr-str '(throw (-new-throwable "some message"))) endl
" but got: #object[String some message]")
(failure-message (should-not-throw (throw (-new-throwable "some message")))))
:clj
(should-contain
(str "Expected " (type-name exception) " thrown from: (throw (-new-throwable \"some message\"))" endl
(apply str (take (count (type-name exception)) (repeat " ")))
(apply str (repeat (count (type-name exception)) " "))
" but got: #error {\n :cause \"some message\"\n :via\n [{:type java.lang.Throwable\n")
(failure-message (should-throw exception (throw (-new-throwable "some message"))))))
(failure-message (should-throw exception (throw (-new-throwable "some message")))))
:cljr
(should-contain
(str "Expected System.SystemException thrown from: (throw (-new-throwable \"some message\"))" endl
(apply str (repeat (count "System.SystemException") " "))
" but got: #error {\n :cause \"some message\"\n :via\n [{:type System.Exception\n")
(failure-message (should-throw SystemException (throw (-new-throwable "some message"))))))

)

(it "can test the message of the exception with regex"
Expand All @@ -545,22 +562,25 @@
(should-fail! (should-not-throw (throw (-new-throwable "error"))))
#?(:cljs
(should=
(str "Expected nothing thrown from: " (pr-str '(throw (-new-throwable "error"))) endl " but got: #object[String error]")
(str "Expected nothing thrown from: " (pr-str '(throw (-new-throwable "error"))) endl
" but got: #object[String error]")
(failure-message (should-not-throw (throw (-new-throwable "error")))))
:clj
:default
(should-contain
(str "Expected nothing thrown from: " (pr-str '(throw (-new-throwable "error"))) endl
" but got: #error {\n :cause \"error\"\n :via\n [{:type java.lang.Throwable\n :message \"error\"\n")
(str " but got: #error {\n :cause \"error\"\n :via\n [{:type " (type-name throwable)
"\n :message \"error\"\n"))
(failure-message (should-not-throw (throw (-new-throwable "error"))))))))


(context "should-be-a"
(it "passes if the actual form is an instance of the expected type"
(should-pass! (should-be-a (type 1) 1)))

#?(:clj
(it "passes if the actual derives from the expected type"
(should-pass! (should-be-a Number (int 1)))))
(it "passes if the actual derives from the expected type"
#?(:cljs (do)
:clj (should-pass! (should-be-a Number (int 1)))
:cljr (should-pass! (should-be-a System.ValueType (int 1)))))

(it "fails if the actual form is not an instance of the expected type"
(should-fail! (should-be-a (type 1) "one")))
Expand All @@ -575,9 +595,10 @@
(it "fails if the actual form is an instance of the expected type"
(should-fail! (should-not-be-a (type 1) 1)))

#?(:clj
(it "fails if the actual derives from the expected type"
(should-fail! (should-not-be-a Number (int 1)))))
(it "fails if the actual derives from the expected type"
#?(:cljs (do)
:clj (should-fail! (should-not-be-a Number (int 1)))
:cljr (should-fail! (should-not-be-a System.ValueType (int 1)))))

(it "passes if the actual form is not an instance of the expected type"
(should-pass! (should-not-be-a (type 1) "one")))
Expand Down
10 changes: 5 additions & 5 deletions src/cljc/speclj/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -301,16 +301,16 @@
(cond
(nil? actual#) (-fail (str "Expected: " (-to-s expected#) speclj.platform/endl "to be in: nil"))
(and (string? expected#) (string? actual#))
(when (= -1 (.indexOf actual# expected#))
(when (nil? (clojure.string/index-of actual# expected#))
(-fail (str "Expected: " (-to-s expected#) speclj.platform/endl "to be in: " (-to-s actual#) " (using .contains)")))
(and (speclj.platform/re? expected#) (string? actual#))
(when (empty? (re-seq expected# actual#))
(-fail (str "Expected: " (-to-s actual#) speclj.platform/endl "to match: " (-to-s expected#) " (using re-seq)")))
(map? actual#)
(when (not (contains? actual# expected#))
(when-not (contains? actual# expected#)
(-fail (str "Expected: " (-to-s expected#) speclj.platform/endl "to be a key in: " (-to-s actual#) " (using contains?)")))
(coll? actual#)
(when (not (some #(= expected# %) actual#))
(when-not (some #(= expected# %) actual#)
(-fail (str "Expected: " (-to-s expected#) speclj.platform/endl "to be in: " (-to-s actual#) " (using =)")))
:else (throw (-new-exception (wrong-types "should-contain" expected# actual#))))))

Expand All @@ -322,10 +322,10 @@
(cond
(nil? actual#) nil ; automatic pass!
(and (string? expected#) (string? actual#))
(when (not (= -1 (.indexOf actual# expected#)))
(when (some? (clojure.string/index-of actual# expected#))
(-fail (str "Expected: " (-to-s expected#) speclj.platform/endl "not to be in: " (-to-s actual#) " (using .contains)")))
(and (speclj.platform/re? expected#) (string? actual#))
(when (not (empty? (re-seq expected# actual#)))
(when (seq (re-seq expected# actual#))
(-fail (str "Expected: " (-to-s actual#) speclj.platform/endl "not to match: " (-to-s expected#) " (using re-seq)")))
(map? actual#)
(when (contains? actual# expected#)
Expand Down

0 comments on commit 97da244

Please sign in to comment.