[Opensrf-commits] r2217 - in trunk/src/javascript: . tests (dbs)
svn at svn.open-ils.org
svn at svn.open-ils.org
Mon Mar 28 11:22:57 EDT 2011
Author: dbs
Date: 2011-03-28 11:22:54 -0400 (Mon, 28 Mar 2011)
New Revision: 2217
Added:
trunk/src/javascript/tests/
trunk/src/javascript/tests/README
trunk/src/javascript/tests/module.js
trunk/src/javascript/tests/testJSON_v1.js
Log:
Add DOH-based unit test harness for OpenSRF JavaScript
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.
Added: trunk/src/javascript/tests/README
===================================================================
--- trunk/src/javascript/tests/README (rev 0)
+++ trunk/src/javascript/tests/README 2011-03-28 15:22:54 UTC (rev 2217)
@@ -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
+------------------------------------------------------------------------------
Added: trunk/src/javascript/tests/module.js
===================================================================
--- trunk/src/javascript/tests/module.js (rev 0)
+++ trunk/src/javascript/tests/module.js 2011-03-28 15:22:54 UTC (rev 2217)
@@ -0,0 +1,8 @@
+dojo.provide("opensrf.tests.module");
+dojo.require("DojoSRF");
+
+try{
+ dojo.require("opensrf.tests.testJSON_v1");
+}catch(e){
+ doh.debug(e);
+}
Added: trunk/src/javascript/tests/testJSON_v1.js
===================================================================
--- trunk/src/javascript/tests/testJSON_v1.js (rev 0)
+++ trunk/src/javascript/tests/testJSON_v1.js 2011-03-28 15:22:54 UTC (rev 2217)
@@ -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");
+ }
+]);
More information about the opensrf-commits
mailing list