Skip to content
Browse files

Convert all tests to use Midje (Issue #70).

  • Loading branch information...
1 parent d2835b0 commit 581677318816cc13684953c49a7bb8a633c92a44 @emezeske committed Apr 8, 2012
View
2 bin/test-install.sh
@@ -14,7 +14,7 @@ pushd $project_root
rm -rf ~/.m2/repository/lein-cljsbuild ~/.m2/repository/cljsbuild/
for d in support plugin; do
pushd $d
- lein install, test
+ lein install, midje
popd
done
for d in $projects; do
View
2 example-projects/advanced/src-clj/example/crossover/shared.clj
@@ -4,4 +4,4 @@
(defn make-example-text []
(macros/reverse-eval
- ("code" "shared " "from the " "Hello " str)))
+ ("code" "shared " "from the " "Hello " str)))
View
4 example-projects/advanced/src-cljs/example/hello.cljs
@@ -2,8 +2,8 @@
(:require
[example.crossover.shared :as shared]))
-(defn ^:export say-hello []
- (js/alert (shared/make-example-text)))
+(defn ^:export say-hello []
+ (js/alert (shared/make-example-text)))
(defn add-some-numbers [& numbers]
(apply + numbers))
View
4 example-projects/advanced/test-cljs/example/test/hello.cljs
@@ -2,6 +2,6 @@
(:use [example.hello :only [add-some-numbers]]))
(defn run []
- (assert (= (add-some-numbers 2 2) 4))
- (assert (= (add-some-numbers 1 2 3) 6))
+ (assert (= (add-some-numbers 2 2) 4))
+ (assert (= (add-some-numbers 1 2 3) 6))
(assert (= (add-some-numbers 4 5 6) 15)))
View
4 plugin/project.clj
@@ -5,4 +5,8 @@
:url "http://www.eclipse.org/legal/epl-v10.html"
:distribution :repo}
:dependencies [[fs "1.1.2"]]
+ :dev-dependencies [[midje "1.3.1"]
+ ; NOTE: lein-midje requires different versions to be
+ ; installed for lein1 vs lein2 compatibility :(.
+ [lein-midje "1.0.9"]]
:eval-in-leiningen true)
View
3 plugin/src/leiningen/cljsbuild.clj
@@ -56,7 +56,8 @@
'~crossovers))]
(copy-crossovers#)
(when ~watch?
- (cljsbuild.util/once-every 1000 "copying crossovers" copy-crossovers#))
+ (future
+ (cljsbuild.util/once-every 1000 "copying crossovers" copy-crossovers#)))
(cljsbuild.util/in-threads
(fn [opts#]
(cljsbuild.compiler/run-compiler
View
118 plugin/test/leiningen/test/cljsbuild/config.clj
@@ -1,66 +1,84 @@
(ns leiningen.test.cljsbuild.config
(:use
leiningen.cljsbuild.config
- clojure.test))
+ midje.sweet))
-(deftest test-backwards-compatibility
+(fact
(let [config-0-0-x-early {:source-path "a"
:compiler {:output-to "hello.js"}}
config-0-0-x-late [{:source-path "a"
:compiler {:output-to "hello.js"}}]
config-backwards {:builds
[{:source-path "a"
:compiler {:output-to "hello.js"}}]}]
- (is (= config-backwards (backwards-compat config-0-0-x-early)))
- (is (= config-backwards (backwards-compat config-0-0-x-late)))))
+ (backwards-compat config-0-0-x-early) => config-backwards
+ (backwards-compat config-0-0-x-late) => config-backwards))
-(deftest test-ids
+(fact
(let [config-vec {:builds [{:id "a"} {:id "b"}]}
config-map {:builds {:a {} :b {}}}]
- (is (= config-vec (convert-builds-map config-vec)))
- (is (= config-vec (convert-builds-map config-map)))))
+ (convert-builds-map config-vec) => config-vec
+ (convert-builds-map config-map) => config-vec))
-(deftest test-shell-command
- (is (= {:shell []}
- (parse-shell-command [])))
- (is (= {:shell ["a"]}
- (parse-shell-command ["a"])))
- (is (= {:shell ["a" "b" "c"] :x 1 :y 2}
- (parse-shell-command ["a" "b" "c" :x 1 :y 2]))))
+(fact
+ (parse-shell-command []) => {:shell []}
+ (parse-shell-command ["a"]) => {:shell ["a"]}
+ (parse-shell-command ["a" "b" "c" :x 1 :y 2]) => {:shell ["a" "b" "c"] :x 1 :y 2})
-(deftest test-default-options
- (let [config-in {:repl-launch-commands {:a ["a"]}
- :repl-listen-port 10000
- :test-commands {:b ["b"]}
- :crossover-path "c"
- :crossover-jar true
- :crossovers ["d" "e"]
- :builds
- '({:source-path "f"
- :jar true
- :notify-command ["g"]
- :warn-on-undeclared false
- :compiler
- {:output-to "h"
- :output-dir "i"
- :optimizations :advanced
- :pretty-print false}})}]
- ; Ensure that none of our custom settings are overwritten by defaults.
- (is (= config-in (set-default-options config-in)))
- ; Ensure that if any custom setting is missing, a default is provided.
- (doseq [option (keys config-in)]
- (is (contains? (set-default-options (dissoc config-in option)) option)))
- (let [build (first (:builds config-in))]
- (doseq [build-option (keys build)]
- (let [defaulted (set-default-options
- (assoc config-in :builds
- (list (dissoc build build-option))))]
- (is (contains? (first (:builds defaulted)) build-option))))
- (let [compiler (:compiler build)]
- (doseq [compiler-option (keys compiler)]
- (let [defaulted (set-default-options
- (assoc config-in :builds
- (list
- (assoc build :compiler
- (dissoc compiler :compiler-option)))))]
- (is (contains? (:compiler (first (:builds defaulted))) compiler-option))))))))
+(def config-in
+ {:repl-launch-commands {:a ["a"]}
+ :repl-listen-port 10000
+ :test-commands {:b ["b"]}
+ :crossover-path "c"
+ :crossover-jar true
+ :crossovers ["d" "e"]
+ :builds
+ '({:source-path "f"
+ :jar true
+ :notify-command ["g"]
+ :warn-on-undeclared false
+ :compiler
+ {:output-to "h"
+ :output-dir "i"
+ :optimizations :advanced
+ :pretty-print false}})})
+
+(fact "custom settings are not overwritten by defaults"
+ (set-default-options config-in) => config-in)
+
+(fact "missing settings have defaults provided"
+ (doseq [option (keys config-in)]
+ (set-default-options (dissoc config-in option)) => (contains {option anything})))
+
+(defn- get-build [config]
+ (first (:builds config)))
+
+(defn- default-build-option [config build option]
+ (set-default-options
+ (assoc config :builds
+ (list (dissoc build option)))))
+
+(fact "missing build settings have defaults provided"
+ (let [build (get-build config-in)]
+ (doseq [build-option (keys build)]
+ (let [defaulted (default-build-option config-in build build-option)]
+ (get-build defaulted) => (contains {build-option anything})))))
+
+(defn- get-compiler [config]
+ (:compiler (get-build config)))
+
+(defn- default-compiler-option [config compiler option]
+ (set-default-options
+ (assoc config :builds
+ (list
+ (assoc compiler :compiler
+ (dissoc compiler option))))))
+
+(fact "missing compiler settings have defaults provided"
+ (let [compiler (:compiler (get-build config-in))]
+ (doseq [compiler-option (keys compiler)]
+ (let [defaulted (default-compiler-option config-in compiler compiler-option)]
+ (get-compiler defaulted) => (contains {compiler-option anything})))))
+
+(fact
+ (extract-options {:cljsbuild config-in}) => config-in)
View
12 plugin/test/leiningen/test/cljsbuild/jar.clj
@@ -1,12 +1,12 @@
(ns leiningen.test.cljsbuild.jar
(:use
leiningen.cljsbuild.jar
- clojure.test))
+ midje.sweet))
-(deftest test-relative-path
- (is (= "a" (relative-path "/" "/a")))
- (is (= "d/e" (relative-path "/a/b/c" "/a/b/c/d/e")))
- (is (thrown? Exception (relative-path "" "a")))
- (is (thrown? Exception (relative-path "/a/b/c" "/a/b"))))
+(fact
+ (relative-path "/" "/a") => "a"
+ (relative-path "/a/b/c" "/a/b/c/d/e") => "d/e"
+ (relative-path "" "a") => (throws Exception)
+ (relative-path "/a/b/c" "/a/b") => (throws Exception))
; TODO: More tests!
View
70 plugin/test/leiningen/test/cljsbuild/subproject.clj
@@ -1,22 +1,28 @@
(ns leiningen.test.cljsbuild.subproject
(:use
leiningen.cljsbuild.subproject
- clojure.test))
+ midje.sweet))
-(deftest test-check-clojure-version
- (are [x] (= nil x)
- (check-clojure-version {'org.clojure/clojure ["1.3.0"]})
- (check-clojure-version {'org.clojure/clojure ["1.4.0"]}))
- (are [x] (thrown? Exception x)
- (check-clojure-version {'org.clojure/clojure ["1.2.0"]})
- (check-clojure-version {})))
+(fact
+ (check-clojure-version {'org.clojure/clojure ["1.3.0"]}) => nil
+ (check-clojure-version {'org.clojure/clojure ["1.4.0"]}) => nil
+ (check-clojure-version {'org.clojure/clojure ["1.2.0"]}) => (throws Exception)
+ (check-clojure-version {}) => (throws Exception))
-(deftest test-merge-dependencies
- (are [x] (apply =
- (map dependency-map [(concat x cljsbuild-dependencies)
- (merge-dependencies x)]))
- [['org.clojure/clojure "1.3.0"] ['a "1"] ['b "2"]]
- [['org.clojure/clojure "1.3.0"] ['cljsbuild "9.9.9"]]))
+(defn- merge-dependencies-map [dependencies]
+ (-> dependencies
+ merge-dependencies
+ dependency-map))
+
+(defn- expected-dependencies-map [dependencies]
+ (-> dependencies
+ (concat cljsbuild-dependencies)
+ dependency-map))
+
+(fact
+ (for [dependencies [[['org.clojure/clojure "1.3.0"] ['a "1"] ['b "2"]]
+ [['org.clojure/clojure "1.3.0"] ['cljsbuild "9.9.9"]]]]
+ (merge-dependencies-map dependencies) => (expected-dependencies-map dependencies)))
(def lein-crossover ".crossovers")
(def lein-build-path "src-cljs-a")
@@ -34,16 +40,15 @@
:extra-classpath-dirs lein-extra-classpath-dirs
:repositories lein-repositories})
-(deftest test-make-subproject-lein1
+(fact
(let [subproject (make-subproject-lein1 lein1-project lein-crossover lein-builds)]
(doseq [dir (concat lein-extra-classpath-dirs [lein-build-path lein-crossover])]
- (is (some #{dir} (:extra-classpath-dirs subproject))))
- (is (= lein-source-path (:source-path subproject)))
- (is (:local-repo-classpath subproject))
- (is (= lein-dev-dependencies (:dev-dependencies subproject)))
- (is (= lein-repositories (:repositories subproject)))
- (is (apply = (map dependency-map [(concat lein-dependencies cljsbuild-dependencies)
- (:dependencies subproject)])))))
+ (:extra-classpath-dirs subproject) => (contains dir))
+ (:source-path subproject) => lein-source-path
+ (:local-repo-classpath subproject) => truthy
+ (:dev-dependencies subproject) => lein-dev-dependencies
+ (:repositories subproject) => lein-repositories
+ (dependency-map (:dependencies subproject)) => (expected-dependencies-map lein-dependencies)))
(def lein2-metadata {:test-metadata "testing 1 2 3"})
(def lein2-eval-in :trampoline)
@@ -54,19 +59,18 @@
:dev-dependencies lein-dev-dependencies
:source-paths (concat [lein-source-path] lein-extra-classpath-dirs)
:repositories lein-repositories
- :eval-in lein2-eval-in
+ :eval-in lein2-eval-in
:resources-path lein2-resources-path}
lein2-metadata))
-(deftest test-make-subproject-lein2
+(fact
(let [subproject (make-subproject-lein2 lein2-project lein-crossover lein-builds)]
- (is (= lein2-metadata (meta subproject)))
+ (meta subproject) => lein2-metadata
(doseq [dir (concat lein-extra-classpath-dirs [lein-source-path lein-build-path lein-crossover])]
- (is (some #{dir} (:source-paths subproject))))
- (is (:local-repo-classpath subproject))
- (is (= lein-dev-dependencies (:dev-dependencies subproject)))
- (is (= lein-repositories (:repositories subproject)))
- (is (= lein2-eval-in (:eval-in subproject)))
- (is (= lein2-resources-path (:resources-path subproject)))
- (is (apply = (map dependency-map [(concat lein-dependencies cljsbuild-dependencies)
- (:dependencies subproject)])))))
+ (:source-paths subproject) => (contains dir))
+ (:local-repo-classpath subproject)
+ (:dev-dependencies subproject) => lein-dev-dependencies
+ (:repositories subproject) => lein-repositories
+ (:eval-in subproject) => lein2-eval-in
+ (:resources-path subproject) => lein2-resources-path
+ (dependency-map (:dependencies subproject)) => (expected-dependencies-map lein-dependencies)))
View
6 support/project.clj
@@ -8,4 +8,8 @@
[org.clojure/clojurescript "0.0-1011"
:exclusions [org.apache.ant/ant]]
[fs "1.1.2"]
- [clj-stacktrace "0.2.4"]])
+ [clj-stacktrace "0.2.4"]]
+ :dev-dependencies [[midje "1.3.1"]
+ ; NOTE: lein-midje requires different versions to be
+ ; installed for lein1 vs lein2 compatibility :(.
+ [lein-midje "1.0.9"]])
View
2 support/src/cljsbuild/crossover.clj
@@ -95,7 +95,7 @@
(let [temp-file (str to-file ".tmp")]
; Write a temp file and atomically rename to the real file
; to prevent the compiler from reading a half-written file
- (spit temp-file (filtered-crossover-file from-resource))
+ (spit temp-file (filtered-crossover-file from-resource))
(fs/rename temp-file to-file)
; Mark the file as read-only, to hopefully warn the user not to modify it.
(fs/chmod "-w" to-file))))))
View
17 support/src/cljsbuild/util.clj
@@ -30,14 +30,19 @@ and returns a seq of the results. Launches all the threads at once."
[f s]
(doall (map deref (doall (map #(future (f %)) s)))))
-(defn once-every [ms desc f]
- (future
- (while true
- (Thread/sleep ms)
+(defn sleep [ms]
+ (Thread/sleep ms))
+
+(defn once-every
+ ([ms desc f keep-going]
+ (while (keep-going)
(try
(f)
(catch Exception e
- (println (str "Error " desc ": " e)))))))
+ (println (str "Error " desc ": " e))))
+ (sleep ms)))
+ ([ms desc f]
+ (once-every ms desc f (fn [] true))))
(defn- pump-file [reader out]
(let [buffer (make-array Character/TYPE 1000)]
@@ -56,7 +61,7 @@ and returns a seq of the results. Launches all the threads at once."
(.join pump-out)
(.join pump-err))))
-(defn- maybe-writer [file fallback]
+(defn maybe-writer [file fallback]
(if file
(do
(fs/delete file)
View
13 support/test/cljsbuild/test/clean.clj
@@ -0,0 +1,13 @@
+(ns cljsbuild.test.clean
+ (:use
+ cljsbuild.clean
+ midje.sweet)
+ (:require
+ [fs.core :as fs]))
+
+(fact
+ (let [output-to "a"
+ output-dir "b"]
+ (cleanup-files {:output-to output-to :output-dir output-dir}) => nil
+ (provided (fs/delete output-to) => nil :times 1)
+ (provided (fs/delete-dir output-dir) => nil :times 1)))
View
24 support/test/cljsbuild/test/test.clj
@@ -0,0 +1,24 @@
+(ns cljsbuild.test.test
+ (:use
+ cljsbuild.test
+ midje.sweet)
+ (:require
+ [cljsbuild.util :as util]))
+
+(fact
+ (let [command-1 {:shell ["command1"]}
+ command-2 {:shell ["command2"]}
+ command-3 {:shell ["command3"]}
+ commands [command-1 command-2 command-3]]
+ (run-tests commands) => nil
+ (provided (util/sh command-1) => 0 :times 1)
+ (provided (util/sh command-2) => 0 :times 1)
+ (provided (util/sh command-3) => 0 :times 1)))
+
+(fact
+ (let [command-1 {:shell ["command1"]}
+ command-2 {:shell ["command2"]}
+ commands [command-1 command-2]]
+ (run-tests commands) => (throws Exception)
+ (provided (util/sh command-1) => 0 :times 1)
+ (provided (util/sh command-2) => 1 :times 1)))
View
70 support/test/cljsbuild/test/util.clj
@@ -1,20 +1,62 @@
(ns cljsbuild.test.util
(:use
cljsbuild.util
- clojure.test))
+ midje.sweet)
+ (:require
+ [clojure.java.io :as io]
+ [fs.core :as fs]))
-(deftest test-join-paths
- (is (= "") (join-paths))
- (is (= "a") (join-paths "a"))
- (is (= "a/b/c") (join-paths "a" "b" "c")))
+(fact
+ (join-paths) => ""
+ (join-paths "a") => "a"
+ (join-paths "a" "b" "c") => "a/b/c")
-(deftest test-filter-by-ext
+(fact
(let [files ["a.a" "b.b" "c.c" "d.d" "e.d" "f.d" "1/2/3/4/5.e"]]
- (are [x y] (= x y)
- [] (filter-by-ext files #{"q" "r" "s"})
- ["a.a"] (filter-by-ext files #{"a"})
- ["d.d" "e.d" "f.d"] (filter-by-ext files #{"d"})
- ["1/2/3/4/5.e"] (filter-by-ext files #{"e"})
- files (filter-by-ext files #{"a" "b" "c" "d" "e"}))))
-
-; TODO: More tests!
+ (filter-by-ext files #{"q" "r" "s"}) => []
+ (filter-by-ext files #{"a"}) => ["a.a"]
+ (filter-by-ext files #{"d"}) => ["d.d" "e.d" "f.d"]
+ (filter-by-ext files #{"e"}) => ["1/2/3/4/5.e"]
+ (filter-by-ext files #{"a" "b" "c" "d" "e"}) => files))
+
+(fact
+ (find-files "a" #{"z"}) => ["a/j.z" "a/k.z" "a/l.z" "a/b/m.z" "a/b/n.z"]
+ (provided (fs/iterate-dir "a") =>
+ [["a" "" ["i.a" "j.z" "k.z" "l.z"]]
+ ["a/b" "" ["m.z" "n.z" "o.b"]]]
+ :times 1))
+
+(unfinished call-in-threads)
+
+(fact
+ (in-threads call-in-threads [1 2 3]) => [nil nil nil]
+ (provided (call-in-threads 1) => nil :times 1)
+ (provided (call-in-threads 2) => nil :times 1)
+ (provided (call-in-threads 3) => nil :times 1))
+
+(unfinished call-once-every)
+(unfinished keep-going)
+
+(fact
+ (once-every 1000 "description" call-once-every keep-going) => nil
+ (provided (keep-going) =streams=> [true true true false] :times 4)
+ (provided (call-once-every) => nil :times 3)
+ (provided (sleep 1000) => nil :times 3))
+
+(fact
+ (maybe-writer "filename" *out*) => :success
+ (provided (fs/delete "filename") => nil :times 1)
+ (provided (io/writer "filename") => :success :times 1))
+
+(fact
+ (maybe-writer nil *out*) => *out*)
+
+; TODO: It would be nice to test process-start, but it does a lot of Java
+; interop so I'm not sure how to go about that just yet. Maybe if
+; it was switched to using "conch" instead of raw interop it would
+; be easier to test?
+
+(fact
+ (let [command {:shell ["command" "arg1" "arg2"]}]
+ (sh command) => 0
+ (provided (process-start command) => {:wait (fn [] 0)} :times 1)))

0 comments on commit 5816773

Please sign in to comment.
Something went wrong with that request. Please try again.