comparison 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
comparison
equal deleted inserted replaced
136:5666f217df39 150:5118d0e38ba4
1 Test tools
2 ==========
3
4 To make it easier to test code, and to ensure that example code is
5 correct, there is a small testtools.js file in the work folder.
6
7
8 How to write tests
9 ------------------
10
11 To prepare a test create a file like this. It's also in the work
12 folder. (For real examples the file will give *assert* a more
13 interesting argument, whose truth or falsity is perhaps not obvious.)
14
15 .. literalinclude:: /core-javascript-work/demo_testtools.js
16 :language: javascript
17
18
19 How to run tests
20 ----------------
21
22 Here's how to run the tests at the command line, finishing with a
23 command prompt that allows you inspect the state and also run the test
24 again.
25
26
27 Rhino
28 ^^^^^
29
30 For Rhino use *load* to run the test again.
31
32 .. code-block:: sh
33
34 core-javascript-work$ js -f testtools.js -f demo_testtools.js -f -
35 Testing: all-pass
36 Testing: 2-4-fail
37 ! assert 2 has failed
38 ! assert 4 has failed
39 Rhino 1.7 release 2 2010 01 20
40 js> load('demo_testtools.js') // You type this.
41 Testing: all-pass
42 Testing: 2-4-fail
43 ! assert 2 has failed
44 ! assert 4 has failed
45 js>
46
47
48 JSDB
49 ^^^^
50
51 For JSDB use *run* to run the test again.
52
53 .. code-block:: sh
54
55 C:core-javascript-work> jsdb -load testtools.js -load demo_testtools.js
56 Testing: all-pass
57 Testing: 2-4-fail
58 ! assert 2 has failed
59 ! assert 4 has failed
60 js> load('demo_testtools.js') // You type this.
61 Testing: all-pass
62 Testing: 2-4-fail
63 ! assert 2 has failed
64 ! assert 4 has failed
65 js>
66
67
68 testools.js
69 -----------
70
71 (Optional on a first reading.) Here's the file testools.js. It's
72 short and simple (and has a not-nice dependency on global variables).
73 It also has a branch based on whether it's being run on JSDB or Rhino.
74
75 .. literalinclude:: /core-javascript-work/testtools.js
76 :language: javascript