diff core-javascript/testtools.rst @ 150:5118d0e38ba4 core-javascript-latex

$ hg merge core-javascript # into core-javascript-latex
author jf3243
date Tue, 01 Nov 2011 08:58:38 +0000
parents 71b7fd796ddf
children b04f509bee83
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/core-javascript/testtools.rst	Tue Nov 01 08:58:38 2011 +0000
@@ -0,0 +1,76 @@
+Test tools
+==========
+
+To make it easier to test code, and to ensure that example code is
+correct, there is a small testtools.js file in the work folder.
+
+
+How to write tests
+------------------
+
+To prepare a test create a file like this.  It's also in the work
+folder.  (For real examples the file will give *assert* a more
+interesting argument, whose truth or falsity is perhaps not obvious.)
+
+.. literalinclude:: /core-javascript-work/demo_testtools.js
+   :language: javascript
+
+
+How to run tests
+----------------
+
+Here's how to run the tests at the command line, finishing with a
+command prompt that allows you inspect the state and also run the test
+again.
+
+
+Rhino
+^^^^^
+
+For Rhino use *load* to run the test again.
+
+.. code-block:: sh
+
+   core-javascript-work$ js -f testtools.js -f demo_testtools.js -f -
+   Testing: all-pass
+   Testing: 2-4-fail
+   ! assert 2 has failed
+   ! assert 4 has failed
+   Rhino 1.7 release 2 2010 01 20
+   js> load('demo_testtools.js')  // You type this.
+   Testing: all-pass
+   Testing: 2-4-fail
+   ! assert 2 has failed
+   ! assert 4 has failed
+   js>
+
+
+JSDB
+^^^^
+
+For JSDB use *run* to run the test again.
+
+.. code-block:: sh
+
+   C:core-javascript-work> jsdb -load testtools.js -load demo_testtools.js
+   Testing: all-pass
+   Testing: 2-4-fail
+   ! assert 2 has failed
+   ! assert 4 has failed
+   js> load('demo_testtools.js')  // You type this.
+   Testing: all-pass
+   Testing: 2-4-fail
+   ! assert 2 has failed
+   ! assert 4 has failed
+   js>
+
+
+testools.js
+-----------
+
+(Optional on a first reading.)  Here's the file testools.js.  It's
+short and simple (and has a not-nice dependency on global variables).
+It also has a branch based on whether it's being run on JSDB or Rhino.
+
+.. literalinclude:: /core-javascript-work/testtools.js
+   :language: javascript