Debian Codegraph @

src/zc/customdoctests/spidermonkey.txt

raw
1 Javascript and Python-Spidermonkey support
2 ------------------------------------------
3
4 .. This file shows some examples of using spidermonkey APIs in doctests.
5
6 To wire this up, you'd use something like::
7
8 import doctest, zc.customdoctests.js
9
10 test_suite = doctest.DocTestSuite(
11 parser=zc.customdoctests.js.parser,
12 setUp=zc.customdoctests.js.spidermonkeySetUp)
13
14 Or, with manuel::
15
16 test_suite = manuel.testing.TestSuite(
17 manuel.doctest.Manuel(parser=zc.customdoctests.js.parser) +
18 manuel.doctest.Manuel(parser=zc.customdoctests.js.eq_parser) +
19 manuel.doctest.Manuel() +
20 manuel.capture.Manuel(),
21 'spidermonkey.txt',
22 setUp=zc.customdoctests.js.spidermonkeySetUp)
23
24 Note that zc.customdoctests doesn't require spidermonkey, so you need
25 to install spidermonkey seperately if you want to use it.
26
27 An advantage of using manuel is that you can use multiple parsers in
28 the same document. In the example, above, 2 javascript example
29 syntaxes (described below) as well as the standard doctest syntax are
30 supported. This document is run with manuel to allow all 3 syntaxes.
31
32 For the rest of this document, we'll show examples of JavaScript
33 doctests as well as helper APIs used to support JavaScript and to
34 integrate JavaScript and Python.
35
36 Javascript doctests use a "js>" prompt (as used in rhino and the
37 spidermonkey interpreter)::
38
39 js> 2 +
40 ... 'Hi world' // doctest: +ELLIPSIS
41 u'2Hi...
42
43 Assignments return values. This can generate annoying output
44 in doctests::
45
46 js> ob = {a: 1, b: 2}
47 [object Object]
48
49 If you're using manuel, you can avoid this by using js!::
50
51 js! x = 3
52
53 which suppresses expression values.
54
55 load and print functions (similar to those found in rhino) are
56 provided. For example, given a javascript file, double.js::
57
58 function double (x) {
59 return x*2;
60 }
61
62 .. -> src
63
64 >>> with open('double.js', 'w') as f:
65 ... f.write(src)
66
67 We can load the file::
68
69 js> load('double.js')
70 js> double(10)
71 20
72
73 We can print values::
74
75 js> print('Hi')
76 Hi
77
78 A python object provides access to the open function and the os
79 module::
80
81 js> python.os.path.exists('double.js')
82 True
83
84 js! f = python.open('double.js')
85 js> print(f.read())
86 function double (x) {
87 return x*2;
88 }
89 <BLANKLINE>
90
91 js> f.close()
92
93
94 If you're using manuel, you can intermix Python and and JavaScript
95 examples and there are a number of APIs to facilitate using Python and
96 JavaScript together.
97
98 There's an add_js_global function to copy data from Python::
99
100 >>> add_js_global('y', 1)
101
102 js> y
103 1
104
105 There's also a js object that provides attribute access to js globals::
106
107 >>> js.x
108 3
109
110 >>> js.z = 4
111
112 js> z
113 4
114
115 You can also call this to run JS code without returning the resulting value::
116
117 >>> js('a = x + y')
118
119 js> a
120 4