Permalink
Browse files

Added support for testing (#36).

* This required that crossovers be moved out of :builds and into the
  top-level :cljsbuild.

* Improved all of the hooks, and added a "lein test" hook.

* Added a PhantomJS test example to the advanced project.

* Ensure that all :compiler :output-dir options are unique (#32).
  • Loading branch information...
1 parent 92c7dfd commit b8dbbb6d9a575105d6ee67efd8a0f8e565eaacfd @emezeske committed Feb 20, 2012
View
@@ -7,7 +7,7 @@ cljsbuild-*.*.*
example-projects/*/*.jar
example-projects/simple/resources
example-projects/advanced/resources/public/js
-example-projects/*/.clojurescript-output
+example-projects/advanced/resources/private/js
+example-projects/advanced/crossover-cljs
example-projects/*/classes
example-projects/*/lib
-example-projects/*/src-cljs/example/crossover
View
@@ -1,5 +1,7 @@
# Sharing Code Between Clojure and ClojureScript
+**TODO** Fix docs W.R.T. the :crossovers option moving around.
+
Sharing code with lein-cljsbuild is accomplished via the configuration
of "crossovers". A crossover specifies a Clojure namespace, the content
of which should be copied into your ClojureScript project. This can be any
@@ -6,11 +6,19 @@ if (phantom.args.length != 1) {
var page = new WebPage();
var url = phantom.args[0];
+page.onConsoleMessage = function (message) {
+ console.log("App console: " + message);
+};
+
+console.log("Loading URL: " + url);
+
page.open(url, function (status) {
if (status != "success") {
console.log('Failed to open ' + url);
phantom.exit(1);
}
- // TODO: Should anything else happen here?
+ console.log("Loaded successfully.");
+
+ // TODO: Somehow gracefully exit when the REPL is closed?
});
@@ -0,0 +1,39 @@
+if (phantom.args.length != 1) {
+ console.log('Expected a target URL parameter.');
+ phantom.exit(1);
+}
+
+var page = new WebPage();
+var url = phantom.args[0];
+
+page.onConsoleMessage = function (message) {
+ console.log("Test console: " + message);
+};
+
+console.log("Loading URL: " + url);
+
+page.open(url, function (status) {
+ if (status != "success") {
+ console.log('Failed to open ' + url);
+ phantom.exit(1);
+ }
+
+ console.log("Running test.");
+
+ var result = page.evaluate(function() {
+ return example.test.run();
+ });
+
+ // NOTE: PhantomJS 1.4.0 has a bug that prevents the exit codes
+ // below from eing returned properly. :(
+ //
+ // http://code.google.com/p/phantomjs/issues/detail?id=294
+
+ if (result != 0) {
+ console.log("*** Test failed! ***");
+ phantom.exit(1);
+ }
+
+ console.log("Test succeeded.");
+ phantom.exit(0);
+});
@@ -20,22 +20,27 @@
:repl-listen-port 9000
:repl-launch-commands
{"firefox" ["firefox"]
- "firefox-naked" ["firefox" "resources/public/html/naked.html"]
- "phantom" ["phantomjs" "phantom/page-repl.js"]
- "phantom-naked" ["phantomjs" "phantom/page-repl.js" "resources/public/html/naked.html"]}
+ "firefox-naked" ["firefox" "resources/private/html/naked.html"]
+ "phantom" ["phantomjs" "phantom/repl.js"]
+ "phantom-naked" ["phantomjs" "phantom/repl.js" "resources/private/html/naked.html"]}
+ :test-commands
+ {"unit" ["phantomjs" "phantom/unit-test.js" "resources/private/html/unit-test.html"]}
; Configure two separate builds; one with few optimizations for
; development/debugging, and one with many optimizations for
; production use.
+ :crossovers [example.crossover]
:builds [
{:source-path "src-cljs"
:jar true
- :crossovers [example.crossover]
:compiler {:output-to "resources/public/js/main-debug.js"
:optimizations :whitespace
:pretty-print true}}
{:source-path "src-cljs"
- :crossovers [example.crossover]
:compiler {:output-to "resources/public/js/main.js"
:optimizations :advanced
- :pretty-print false}}]}
+ :pretty-print false}}
+ {:source-path "test-cljs"
+ :compiler {:output-to "resources/private/js/unit-test.js"
+ :optimizations :whitespace
+ :pretty-print true}}]}
:ring {:handler example.routes/app})
@@ -2,7 +2,7 @@
<body>
This is just a dummy HTML file to connect to the REPL.
Don't close this window until you're done with the REPL.
- <script src="../js/main-debug.js" type="text/javascript"></script>
+ <script src="../../public/js/main-debug.js" type="text/javascript"></script>
<script type="text/javascript">
example.repl.connect();
</script>
@@ -0,0 +1,8 @@
+<html>
+ <body>
+ This is just a dummy HTML file with which to load the unit tests.
+ This file could be changed to include HTML for the tests to use
+ during their operation.
+ <script src="../js/unit-test.js" type="text/javascript"></script>
+ </body>
+</html>
@@ -4,3 +4,6 @@
(defn ^:export say-hello []
(js/alert (shared/make-example-text)))
+
+(defn add-some-numbers [& numbers]
+ (apply + numbers))
@@ -0,0 +1,9 @@
+(ns example.test
+ (:require [example.test.hello :as hello]))
+
+(def success 0)
+
+(defn ^:export run []
+ (.log js/console "Example test started.")
+ (hello/run)
+ success)
@@ -0,0 +1,7 @@
+(ns example.test.hello
+ (: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 4 5 6) 15)))
Oops, something went wrong.

0 comments on commit b8dbbb6

Please sign in to comment.