Add DOH-based unit test harness for OpenSRF JavaScript
authordbs <dbs@9efc2488-bf62-4759-914b-345cdb29e865>
Mon, 28 Mar 2011 15:22:54 +0000 (15:22 +0000)
committerdbs <dbs@9efc2488-bf62-4759-914b-345cdb29e865>
Mon, 28 Mar 2011 15:22:54 +0000 (15:22 +0000)
The Dojo Objective Harness enables us to run unit tests from the command
line or within a browser. Running tests from a command line makes it easier
to fold into the continuous integration server. Included in this commit
is a small README for setting up command-line testing and a sample set
of unit tests that exercise parts of JSON_v1.js.

git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@2217 9efc2488-bf62-4759-914b-345cdb29e865

src/javascript/tests/README [new file with mode: 0644]
src/javascript/tests/module.js [new file with mode: 0644]
src/javascript/tests/testJSON_v1.js [new file with mode: 0644]

diff --git a/src/javascript/tests/README b/src/javascript/tests/README
new file mode 100644 (file)
index 0000000..d46224a
--- /dev/null
@@ -0,0 +1,31 @@
+Running JavaScript tests
+========================
+
+These tests have been created using the
+http://dojotoolkit.org/reference-guide/util/doh.html[Dojo Objective Harness
+(DOH)] testing framework.
+
+To run these tests from the command line:
+
+  1. Download and extract a Dojo Toolkit source tarball:
++
+--------------------------------------------------------------------------------
+wget http://download.dojotoolkit.org/release-1.6.0/dojo-release-1.6.0-src.tar.gz
+tar xzf dojo-release-1.6.0-src.tar.gz
+------------------------------------------------------------------------------
++
+  2. Copy the OpenSRF JavaScript directory into the Dojo source tree and move
+     the 'DojoSRF.js' file into place:
++
+------------------------------------------------------------------------------
+cp -r src/javascript dojo-release-1.6.0-src/opensrf
+mv dojo-release-1.6.0-src/opensrf/DojoSRF.js dojo-release-1.6.0-src/.
+------------------------------------------------------------------------------
++
+  3. Run the tests from the Dojo 'util/doh' directory, specifying the OpenSRF
+     test module:
++
+------------------------------------------------------------------------------
+cd dojo-release-1.6.0-src/util/doh
+sh runner.sh testModule=opensrf.tests.module
+------------------------------------------------------------------------------
diff --git a/src/javascript/tests/module.js b/src/javascript/tests/module.js
new file mode 100644 (file)
index 0000000..947d6c5
--- /dev/null
@@ -0,0 +1,8 @@
+dojo.provide("opensrf.tests.module");
+dojo.require("DojoSRF");
+
+try{
+       dojo.require("opensrf.tests.testJSON_v1");
+}catch(e){
+       doh.debug(e);
+}
diff --git a/src/javascript/tests/testJSON_v1.js b/src/javascript/tests/testJSON_v1.js
new file mode 100644 (file)
index 0000000..d6d3b07
--- /dev/null
@@ -0,0 +1,68 @@
+dojo.provide('opensrf.tests.testJSON_v1');
+
+dojo.require('DojoSRF');
+
+doh.register("JSONTests", [
+    function test_version() {
+        doh.assertTrue(JSON_version() == 'wrapper');
+    },
+    function test_js2JSON() {
+        // Solo nulls and booleans are stringified XXX
+        doh.assertTrue(js2JSON(null) === "null");
+        doh.assertTrue(js2JSON(true) === "true");
+        doh.assertTrue(js2JSON(false) === "false");
+        doh.assertTrue(js2JSON(0) === 0);
+        doh.assertTrue(js2JSON(1.5) === 1.5);
+        doh.assertTrue(js2JSON(.7) === .7);
+        doh.assertTrue(js2JSON("") == '""');
+        doh.assertTrue(js2JSON("foo") == '"foo"');
+        // Escape sequences
+        doh.assertTrue(js2JSON("foo\n\t\n") == '"foo\\n\\t\\n"');
+        doh.assertTrue(js2JSON({"foo":"bar"}) == '{"foo":"bar"}');
+        doh.assertTrue(js2JSON({"foo":true}) == '{"foo":true}');
+        doh.assertTrue(js2JSON({"foo":0}) == '{"foo":0}');
+        doh.assertTrue(js2JSON([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]');
+        // Order of object attributes is not guaranteed
+        doh.assertTrue(js2JSON({"foo":{"one":null,"two":2}}) == '{"foo":{"two":2,"one":null}}');
+    },
+    function test_js2JSONRaw() {
+        // Solo nulls and booleans are stringified XXX
+        doh.assertTrue(js2JSONRaw(null) === "null");
+        doh.assertTrue(js2JSONRaw(true) === "true");
+        doh.assertTrue(js2JSONRaw(false) === "false");
+        doh.assertTrue(js2JSONRaw(0) === 0);
+        doh.assertTrue(js2JSONRaw(1.5) === 1.5);
+        doh.assertTrue(js2JSONRaw(.7) === .7);
+        doh.assertTrue(js2JSONRaw("") == '""');
+        doh.assertTrue(js2JSONRaw("foo") == '"foo"');
+        // Escape sequences
+        doh.assertTrue(js2JSONRaw("foo\n\t\n") == '"foo\\n\\t\\n"');
+        doh.assertTrue(js2JSONRaw({"foo":"bar"}) == '{"foo":"bar"}');
+        doh.assertTrue(js2JSONRaw({"foo":true}) == '{"foo":true}');
+        doh.assertTrue(js2JSONRaw({"foo":0}) == '{"foo":0}');
+        doh.assertTrue(js2JSONRaw([0,"foo",null,"true",true]) === '[0,"foo",null,"true",true]');
+        // Order of object attributes is not guaranteed
+        doh.assertTrue(js2JSONRaw({"foo":{"one":null,"two":2}}) == '{"foo":{"two":2,"one":null}}');
+    },
+    function test_JSON2js() {
+        // Standalone quoted nulls and booleans are converted to primitives
+        doh.assertTrue(JSON2js(null) === null);
+        doh.assertTrue(JSON2js("null") === null);
+        doh.assertTrue(JSON2js(true) === true);
+        doh.assertTrue(JSON2js("true") === true);
+        doh.assertTrue(JSON2js(false) === false);
+        doh.assertTrue(JSON2js("false") === false);
+        // Zero is zero and only zero
+        doh.assertTrue(JSON2js(0) === 0);
+        // Empty string
+        doh.assertTrue(JSON2js('""') === "");
+        // String
+        doh.assertTrue(JSON2js('"foo"') == "foo");
+        // Array; access an index
+        doh.assertTrue(JSON2js('[0,1,2,3,4,5]')[1] == 1);
+        // Object; access a key
+        doh.assertTrue(JSON2js('{"foo":"bar"}').foo == "bar");
+        doh.assertTrue(JSON2js('{"foo":{"two":2,"one":null}}').foo.one === null);
+        doh.assertTrue(JSON2js('{"foo":{"two":2,"one":"null"}}').foo.one === "null");
+    }
+]);