From 9cc02ff5bca7c32442593b064d08c762d8136227 Mon Sep 17 00:00:00 2001 From: Miloslav Nenadal Date: Sat, 20 Feb 2016 19:26:44 +0100 Subject: [PATCH] Add clojurescript support --- .gitignore | 1 + project.clj | 15 +- resources/test/phantom/run.js | 33 ++++ resources/test/test.html | 6 + src/pjstadig/assert_expr.clj | 23 +++ test/fixtures/test_output | 106 +++++++++++ test/fixtures/test_output_cljs | 171 ++++++++++++++++++ .../humane_test_output/formatting_test.cljs | 24 +++ .../humane_test_output/records_test.cljs | 15 ++ test/pjstadig/macro.clj | 7 + test/pjstadig/run_all.cljs | 64 +++++++ 11 files changed, 464 insertions(+), 1 deletion(-) create mode 100644 resources/test/phantom/run.js create mode 100644 resources/test/test.html create mode 100644 src/pjstadig/assert_expr.clj create mode 100644 test/fixtures/test_output create mode 100644 test/fixtures/test_output_cljs create mode 100644 test/pjstadig/humane_test_output/formatting_test.cljs create mode 100644 test/pjstadig/humane_test_output/records_test.cljs create mode 100644 test/pjstadig/macro.clj create mode 100644 test/pjstadig/run_all.cljs diff --git a/.gitignore b/.gitignore index 3c42b43..87b9f55 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ /pom.xml *jar *.asc +/resources/public/js/compiled/ diff --git a/project.clj b/project.clj index 10a6a11..3310661 100644 --- a/project.clj +++ b/project.clj @@ -4,6 +4,19 @@ :license {:name "Eclipse Public License" :url "http://www.eclipse.org/legal/epl-v10.html"} :deploy-repositories [["releases" :clojars]] - :profiles {:dev {:dependencies [[org.clojure/clojure "1.3.0"]]} + :profiles {:dev {:dependencies [[org.clojure/clojure "1.8.0"] + [org.clojure/clojurescript "1.7.228"] + [org.seleniumhq.selenium/selenium-java "2.52.0"] + [com.codeborne/phantomjsdriver "1.2.1"]] + :plugins [[lein-cljsbuild "1.1.2"]] + :cljsbuild { + :test-commands {"test" ["phantomjs" "resources/test/phantom/run.js" "resources/test/test.html"]} + :builds [{:id "test" + :source-paths ["src" "test"] + :compiler {:main pjstadig.run-all + :asset-path "../public/js/compiled/test/out" + :output-to "resources/public/js/compiled/humanize-test-output-test.js" + :output-dir "resources/public/js/compiled/test/out" + :source-map-timestamp true}}]}} :test {:injections [(require 'pjstadig.humane-test-output) (pjstadig.humane-test-output/activate!)]}}) diff --git a/resources/test/phantom/run.js b/resources/test/phantom/run.js new file mode 100644 index 0000000..ce37ca3 --- /dev/null +++ b/resources/test/phantom/run.js @@ -0,0 +1,33 @@ +var page = require('webpage').create(); +var system = require('system'); + +if (system.args.length !== 2) { + console.log('Expected a target URL parameter.'); + phantom.exit(1); +} + +page.onConsoleMessage = function (message) { + console.log(message); +}; + +var url = system.args[1]; + +page.open(url, function (status) { + if (status !== "success") { + console.log('Failed to open ' + url); + setTimeout(function() { // https://github.com/ariya/phantomjs/issues/12697 + phantom.exit(1); + }, 0); + } + + var exitStatus = page.evaluate(function() { + pjstadig.run_all.run(); + + return window.testStatus; + }); + + setTimeout(function() { // https://github.com/ariya/phantomjs/issues/12697 + phantom.exit(exitStatus); + }, 0); +}); + diff --git a/resources/test/test.html b/resources/test/test.html new file mode 100644 index 0000000..a97191a --- /dev/null +++ b/resources/test/test.html @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/pjstadig/assert_expr.clj b/src/pjstadig/assert_expr.clj new file mode 100644 index 0000000..3714d57 --- /dev/null +++ b/src/pjstadig/assert_expr.clj @@ -0,0 +1,23 @@ +(ns pjstadig.assert-expr + (:require [cljs.test :refer [assert-expr]])) + +(defn =-body + [msg a more] + `(let [a# ~a] + (if-let [more# (seq (list ~@more))] + (let [result# (apply = a# more#)] + (if result# + (cljs.test/do-report {:type :pass, :message ~msg, + :expected a#, :actual more#}) + (cljs.test/do-report {:type :fail, :message ~msg, + :expected a#, :actual more#, + :diffs (map vector + more# + (map #(take 2 (clojure.data/diff a# %)) + more#))})) + result#) + (throw (Exception. "= expects more than one argument"))))) + +(defmethod assert-expr '= [menv msg [_ a & more]] + (=-body msg a more)) + diff --git a/test/fixtures/test_output b/test/fixtures/test_output new file mode 100644 index 0000000..74ea239 --- /dev/null +++ b/test/fixtures/test_output @@ -0,0 +1,106 @@ + +lein test pjstadig.humane-test-output.formatting-test + +lein test :only pjstadig.humane-test-output.formatting-test/t-formatting + +FAIL in (t-formatting) (formatting_test.clj:6) +FIXME, I fail. +expected: {:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"} + actual: {:fo :bar} + diff: - {:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"} + + {:fo :bar} + +lein test :only pjstadig.humane-test-output.formatting-test/t-formatting + +FAIL in (t-formatting) (formatting_test.clj:9) +FIXME, I fail. +expected: {:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"} + actual: {:foo :bar} + diff: - {:baz :quux, + :something "a long string?", + :another-key "and another value"} + +lein test :only pjstadig.humane-test-output.formatting-test/t-formatting + +FAIL in (t-formatting) (formatting_test.clj:12) +FIXME, I fail. +expected: {:foo :bar} + actual: {:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"} + diff: + {:baz :quux, + :something "a long string?", + :another-key "and another value"} + +lein test :only pjstadig.humane-test-output.formatting-test/t-formatting + +FAIL in (t-formatting) (formatting_test.clj:15) +FIXME, I fail. +expected: {:foo :bar, :baz :quux} + actual: {:foo :bar, :baz :quux} + diff: + expected: {:foo :bar, :baz :quux} + actual: {:fo :bar, :baz :quux} + diff: - {:foo :bar} + + {:fo :bar} +expected: {:foo :bar, :baz :quux} + actual: {:fo :bar, :baz :quux} + diff: - {:foo :bar} + + {:fo :bar} + +lein test :only pjstadig.humane-test-output.formatting-test/t-formatting + +FAIL in (t-formatting) (formatting_test.clj:19) +FIXME, I fail. +expected: (list? foo) + actual: (not + (list? + {:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"})) + +lein test :only pjstadig.humane-test-output.formatting-test/t-macro-wrapping + +FAIL in (t-macro-wrapping) (formatting_test.clj:26) +expected: 1 + actual: 2 + diff: - 1 + + 2 + +lein test pjstadig.humane-test-output.records-test + +lein test :only pjstadig.humane-test-output.records-test/t-records + +FAIL in (t-records) (records_test.clj:9) +these should not print as plain maps +expected: #pjstadig.humane_test_output.records_test.ARecord{:foo :foo} + actual: #pjstadig.humane_test_output.records_test.ARecord{:foo :bar} + diff: - {:foo :foo} + + {:foo :bar} + +lein test :only pjstadig.humane-test-output.records-test/t-records + +FAIL in (t-records) (records_test.clj:11) +there should be a diff here +expected: #pjstadig.humane_test_output.records_test.ARecord{:foo :foo} + actual: {:foo :foo} + +lein test :only pjstadig.humane-test-output.records-test/t-records + +FAIL in (t-records) (records_test.clj:13) +and here +expected: #pjstadig.humane_test_output.records_test.ARecord{:foo :foo} + actual: #pjstadig.humane_test_output.records_test.BRecord{:foo :foo} + +Ran 3 tests containing 9 assertions. +9 failures, 0 errors. diff --git a/test/fixtures/test_output_cljs b/test/fixtures/test_output_cljs new file mode 100644 index 0000000..eeea289 --- /dev/null +++ b/test/fixtures/test_output_cljs @@ -0,0 +1,171 @@ +Compiling ClojureScript... +Running ClojureScript test: test + +Testing pjstadig.humane-test-output.formatting-test + +FAIL in (t-formatting) (:) +FIXME, I fail. +expected: +{:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"} + + actual: +{:fo :bar} + + diff: + - +{:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"} + + + +{:fo :bar} + + +FAIL in (t-formatting) (:) +FIXME, I fail. +expected: +{:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"} + + actual: +{:foo :bar} + + diff: + - +{:baz :quux, + :something "a long string?", + :another-key "and another value"} + + + + +FAIL in (t-formatting) (:) +FIXME, I fail. +expected: +{:foo :bar} + + actual: +{:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"} + + diff: + + +{:baz :quux, + :something "a long string?", + :another-key "and another value"} + + +FAIL in (t-formatting) (:) +FIXME, I fail. +expected: +{:foo :bar, :baz :quux} + + actual: +{:foo :bar, :baz :quux} + + diff: + + +expected: +{:foo :bar, :baz :quux} + + actual: +{:fo :bar, :baz :quux} + + diff: + - +{:foo :bar} + + + +{:fo :bar} + +expected: +{:foo :bar, :baz :quux} + + actual: +{:fo :bar, :baz :quux} + + diff: + - +{:foo :bar} + + + +{:fo :bar} + + +FAIL in (t-formatting) (:) +FIXME, I fail. +expected: +(list? foo) + + actual: +(not + (list? + {:foo :bar, + :baz :quux, + :something "a long string?", + :another-key "and another value"})) + + +FAIL in (t-macro-wrapping) (:) + +expected: +(clojure.core/= 1 2) + + actual: +(not (clojure.core/= 1 2)) + + +Testing pjstadig.humane-test-output.records-test + +FAIL in (t-records) (:) +these should not print as plain maps +expected: + +##pjstadig.humane-test-output.records-test.ARecord{:foo :foo}{:foo:foo} + + actual: + +##pjstadig.humane-test-output.records-test.ARecord{:foo :bar}{:foo:bar} + + diff: + - +{:foo :foo} + + + +{:foo :bar} + + +FAIL in (t-records) (:) +there should be a diff here +expected: + +##pjstadig.humane-test-output.records-test.ARecord{:foo :foo}{:foo:foo} + + actual: +{:foo :foo} + + diff: + + + +FAIL in (t-records) (:) +and here +expected: + +##pjstadig.humane-test-output.records-test.ARecord{:foo :foo}{:foo:foo} + + actual: + +##pjstadig.humane-test-output.records-test.BRecord{:foo :foo}{:foo:foo} + + diff: + + + +Ran 3 tests containing 9 assertions. +9 failures, 0 errors. diff --git a/test/pjstadig/humane_test_output/formatting_test.cljs b/test/pjstadig/humane_test_output/formatting_test.cljs new file mode 100644 index 0000000..1aa74b7 --- /dev/null +++ b/test/pjstadig/humane_test_output/formatting_test.cljs @@ -0,0 +1,24 @@ +(ns pjstadig.humane-test-output.formatting-test + (:use [clojure.data :only [diff]]) + (:require [cljs.test :refer [do-report]]) + (:require-macros [cljs.test :refer [deftest testing is]] + [pjstadig.macro :refer [deftest+]])) + +(deftest t-formatting + (testing "FIXME, I fail." + (is (= {:foo :bar :baz :quux :something "a long string?" + :another-key "and another value"} + {:fo :bar})) + (is (= {:foo :bar :baz :quux :something "a long string?" + :another-key "and another value"} + {:foo :bar})) + (is (= {:foo :bar} + {:foo :bar :baz :quux :something "a long string?" + :another-key "and another value"})) + (is (= {:foo :bar :baz :quux} {:foo :bar :baz :quux} {:fo :bar :baz :quux} + {:fo :bar :baz :quux})) + (let [foo {:foo :bar :baz :quux :something "a long string?" + :another-key "and another value"}] + (is (list? foo))))) + +(deftest+ t-macro-wrapping 1 2) diff --git a/test/pjstadig/humane_test_output/records_test.cljs b/test/pjstadig/humane_test_output/records_test.cljs new file mode 100644 index 0000000..18db0bc --- /dev/null +++ b/test/pjstadig/humane_test_output/records_test.cljs @@ -0,0 +1,15 @@ +(ns pjstadig.humane-test-output.records-test + (:use [clojure.data :only [diff]]) + (:require [cljs.test :refer [do-report]]) + (:require-macros [cljs.test :refer [deftest testing is]])) + +(defrecord ARecord [foo]) +(defrecord BRecord [foo]) + +(deftest t-records + (testing "these should not print as plain maps" + (is (= (->ARecord :foo) (->ARecord :bar)))) + (testing "there should be a diff here" + (is (= (->ARecord :foo) {:foo :foo}))) + (testing "and here" + (is (= (->ARecord :foo) (->BRecord :foo))))) diff --git a/test/pjstadig/macro.clj b/test/pjstadig/macro.clj new file mode 100644 index 0000000..c7392e0 --- /dev/null +++ b/test/pjstadig/macro.clj @@ -0,0 +1,7 @@ +(ns pjstadig.macro) + +(defmacro deftest+ + [test-name expected actual] + `(~'deftest ~test-name + (~'is (= ~expected ~actual)))) + diff --git a/test/pjstadig/run_all.cljs b/test/pjstadig/run_all.cljs new file mode 100644 index 0000000..ca73eea --- /dev/null +++ b/test/pjstadig/run_all.cljs @@ -0,0 +1,64 @@ +(ns pjstadig.run-all + (:require [cljs.test + :refer [inc-report-counter! testing-vars-str testing-contexts-str get-current-env] + :refer-macros [run-all-tests run-tests]] + [pjstadig.humane-test-output.formatting-test] + [pjstadig.humane-test-output.records-test] + [cljs.pprint :as pp]) + (:require-macros [pjstadig.assert-expr])) + +(enable-console-print!) + +(def pprint-map (get-method pp/simple-dispatch :map)) + +(defn pprint-record [arec] + (pp/pprint-logical-block + :prefix (str "#" (with-out-str (print arec)) "{") :suffix "}" + (pp/print-length-loop + [aseq (seq arec)] + (when aseq + (pp/pprint-logical-block + (pp/write-out (ffirst aseq)) + (print " ") + (pp/pprint-newline :linear) + ;; [pjs] this is kind of ugly, but it is a private var :( + (set! pp/*current-length* 0) ; always print both parts of the [k v] pair + (pp/write-out (fnext (first aseq)))) + (when (next aseq) + (print ", ") + (pp/pprint-newline :linear) + (recur (next aseq))))))) + +(defmethod pp/simple-dispatch :map [amap] + (if (record? amap) + (pprint-record amap) + (pprint-map amap))) + +(defmethod cljs.test/report [:cljs.test/default :fail] + [{:keys [type expected actual diffs message] :as event}] + (inc-report-counter! :fail) + (println "\nFAIL in" (testing-vars-str event)) + (when (:testing-contexts (get-current-env)) (println (testing-contexts-str))) + (when message (println message)) + (binding [*out* (pp/get-pretty-writer *out*)] + (let [print-expected (fn [actual] + (print "expected: ") + (pp/pprint expected) + (print " actual: ") + (pp/pprint actual))] + (if (seq diffs) + (doseq [[actual [a b]] diffs] + (print-expected actual) + (print " diff:") + (if a + (do (print " - ") + (pp/pprint a) + (print " + ")) + (print " + ")) + (when b + (pp/pprint b))) + (print-expected actual))))) + +(defn ^:export run [] + (run-all-tests #"pjstadig.*-test")) +