[Zope3-dev] Re: help with doctests

Marius Gedminas mgedmin at b4net.lt
Fri Jul 20 12:05:40 EDT 2007


On Fri, Jul 20, 2007 at 07:57:12AM -0400, Jim Fulton wrote:
> On Jul 20, 2007, at 5:27 AM, Philipp von Weitershausen wrote:
> >I suggesting using the regex-normalizer [1]. There are many  
> >packages out there that use it if you're looking for examples.
> >
> >http://svn.zope.org/zope.testing/trunk/src/zope/testing/renormalizing.py?rev=66267&view=auto
> 
> BTW, a small useful easy project that I don't have time for would be  
> to generalize the renormalizer to allow other objects besides regexes.

It already does, in a way.  It doesn't call re.compile by itself, so
you can pass it anything that implements a ``sub`` method, not
necessarily regexes.

    class LowercaseTransformer(object):
        def sub(replacement, text):
            return text.lower()

    checker = RENormalizing([(LowercaseTransformer(), None)])

> You now give this a sequences of regex/replacement pairs.
> 
> It would be nice to allow callables in addition to regexs.
> Basically, allow items in the sequence to be either:
> 
> - a regex+replacement tuple, or
> 
> - a callable transformer that reads the text and returns new text.

So essentially you want nicer syntax for the above.  Should be easy to do:

Index: renormalizing.py
===================================================================
--- renormalizing.py    (revision 78151)
+++ renormalizing.py    (working copy)
@@ -181,15 +181,21 @@ class RENormalizing(doctest.OutputChecke
     """
 
     def __init__(self, patterns):
-        self.patterns = patterns
+        self.transformers = map(self._cook, patterns)
+
+    def _cook(self, pattern):
+        if callable(pattern):
+            return pattern
+        regexp, replacement = pattern
+        return lambda text: regexp.sub(replacement, text)
 
     def check_output(self, want, got, optionflags):
         if got == want:
             return True
 
-        for pattern, repl in self.patterns:
-            want = pattern.sub(repl, want)
-            got = pattern.sub(repl, got)
+        for transformer in self.transformers:
+            want = transformer(want)
+            got = transformer(got)
 
         return doctest.OutputChecker.check_output(self, want, got, optionflags)
 
@@ -208,9 +214,9 @@ class RENormalizing(doctest.OutputChecke
         # Dang, this isn't as easy to override as we might wish
         original = want
 
-        for pattern, repl in self.patterns:
-            want = pattern.sub(repl, want)
-            got = pattern.sub(repl, got)
+        for transformer in self.transformers:
+            want = transformer(want)
+            got = transformer(got)
 
         # temporarily hack example with normalized want:
         example.want = want


Do you want me to write unit tests for this and commit?

Marius Gedminas
-- 
C is quirky, flawed, and an enormous success.
        -- Dennis M. Ritchie
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.zope.org/pipermail/zope3-dev/attachments/20070720/44261986/attachment.bin


More information about the Zope3-dev mailing list