[Checkins] SVN: Sandbox/J1m/customdoctests/ initial

Jim Fulton jim at zope.com
Wed Jan 19 20:07:22 EST 2011


Log message for revision 119741:
  initial

Changed:
  U   Sandbox/J1m/customdoctests/README.txt
  U   Sandbox/J1m/customdoctests/setup.py
  A   Sandbox/J1m/customdoctests/src/zc/customdoctests/
  A   Sandbox/J1m/customdoctests/src/zc/customdoctests/__init__.py
  A   Sandbox/J1m/customdoctests/src/zc/customdoctests/js.py

-=-
Modified: Sandbox/J1m/customdoctests/README.txt
===================================================================
--- Sandbox/J1m/customdoctests/README.txt	2011-01-20 00:51:14 UTC (rev 119740)
+++ Sandbox/J1m/customdoctests/README.txt	2011-01-20 01:07:22 UTC (rev 119741)
@@ -1,9 +1,15 @@
-Title Here
-**********
+zc.customdoctests -- customized doctest implementations
+*******************************************************
 
+doctest (and recently manuel) provide hooks for using custo doctest
+parsers. This enables writing JavaScript doctests, as in::
 
-To learn more, see
+    js> function double (x) { return x*2; }
+    js> double(2)
+    4
 
+And with manuel, it facilitates doctests that mix multiple languages,
+such as Python, JavaScript, and sh.
 
 Changes
 *******

Modified: Sandbox/J1m/customdoctests/setup.py
===================================================================
--- Sandbox/J1m/customdoctests/setup.py	2011-01-20 00:51:14 UTC (rev 119740)
+++ Sandbox/J1m/customdoctests/setup.py	2011-01-20 01:07:22 UTC (rev 119741)
@@ -11,9 +11,9 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-name, version = 'zc.', '0'
+name, version = 'zc.customdoctests', '0'
 
-install_requires = ['setuptools']
+install_requires = ['setuptools', 'python-spidermonkey']
 extras_require = dict(test=['zope.testing'])
 
 entry_points = """

Added: Sandbox/J1m/customdoctests/src/zc/customdoctests/__init__.py
===================================================================
--- Sandbox/J1m/customdoctests/src/zc/customdoctests/__init__.py	                        (rev 0)
+++ Sandbox/J1m/customdoctests/src/zc/customdoctests/__init__.py	2011-01-20 01:07:22 UTC (rev 119741)
@@ -0,0 +1,47 @@
+##############################################################################
+#
+# Copyright (c) 2011 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+import doctest
+import re
+import sys
+
+class DocTestParser(doctest.DocTestParser):
+    "Doctest parser that creates calls into JavaScript."
+
+    def __init__(self, *args, **kw):
+        ps1 = kw.pop('ps1', '>>>')
+        ps2 = kw.pop('ps2', r'\.\.\.')
+        self.handler_name = kw.pop('handler_name', '')
+        getattr(doctest.DocTestParser, '__init__', lambda : None)(*args, **kw)
+
+        self._EXAMPLE_RE = re.compile(
+            r'''
+            # Source consists of a PS1 line followed by zero or more PS2 lines.
+            (?P<source>
+                (?:^(?P<indent> [ ]*) %(ps1)s    .*)    # PS1 line
+                (?:\n           [ ]*  %(ps2)s    .*)*)  # PS2 lines
+            \n?
+            # Want consists of any non-blank lines that do not start with PS1.
+            (?P<want> (?:(?![ ]*$)        # Not a blank line
+                         (?![ ]*%(ps1)s)  # Not a line starting with PS1
+                         .*$\n?           # But any other line
+                      )*)
+        ''' % dict(ps1=ps1, ps2=ps2), re.MULTILINE | re.VERBOSE)
+
+    def parse(self, string, name='<string>'):
+        r =doctest.DocTestParser.parse(self, string, name)
+        for s in r:
+            if isinstance(s, doctest.Example):
+                s.source = "%s(%r)\n" % (self.handler_name, s.source)
+        return r


Property changes on: Sandbox/J1m/customdoctests/src/zc/customdoctests/__init__.py
___________________________________________________________________
Added: svn:keywords
   + Id
Added: svn:eol-style
   + native

Added: Sandbox/J1m/customdoctests/src/zc/customdoctests/js.py
===================================================================
--- Sandbox/J1m/customdoctests/src/zc/customdoctests/js.py	                        (rev 0)
+++ Sandbox/J1m/customdoctests/src/zc/customdoctests/js.py	2011-01-20 01:07:22 UTC (rev 119741)
@@ -0,0 +1,49 @@
+##############################################################################
+#
+# Copyright (c) 2011 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+import doctest
+import re
+import spidermonkey
+import sys
+import zc.customdoctests
+
+run_time = None
+
+parser = zc.customdoctests.DocTestParser(ps1='js>', handler_name='JS')
+
+def setUp(test_or_self):
+    globs = getattr(test_or_self, 'globs', test_or_self.__dict__)
+
+    global run_time
+    if run_time is None:
+        run_time = spidermonkey.Runtime()
+
+    cx = run_time.new_context()
+    globs['JS'] = JS = cx.execute
+    globs['add_global'] = cx.add_global
+
+
+    # Emulate rhino load:
+    def load(name):
+        JS(open(name).read(), name)
+
+    cx.add_global('load', load)         # rhino compat
+    cx.add_global('print',
+                  lambda s: sys.stdout.write('%s\n' % (s, ))) # rhino compat
+
+    cx.add_global('console', dict(
+        error = lambda s: sys.stdout.write('Error: %s\n' % (s, )),
+        info = lambda s: sys.stdout.write('Info: %s\n' % (s, )),
+        log = lambda s: sys.stdout.write('%s\n' % (s, )),
+        ))


Property changes on: Sandbox/J1m/customdoctests/src/zc/customdoctests/js.py
___________________________________________________________________
Added: svn:keywords
   + Id
Added: svn:eol-style
   + native



More information about the checkins mailing list