[Zope3-checkins] SVN: zope.testing/trunk/ Make RENormalizer also accept plain Python callables.

Marius Gedminas marius at pov.lt
Sat Jul 21 06:05:30 EDT 2007


Log message for revision 78260:
  Make RENormalizer also accept plain Python callables.
  
  

Changed:
  U   zope.testing/trunk/README.txt
  U   zope.testing/trunk/src/zope/testing/renormalizing.py

-=-
Modified: zope.testing/trunk/README.txt
===================================================================
--- zope.testing/trunk/README.txt	2007-07-21 09:49:11 UTC (rev 78259)
+++ zope.testing/trunk/README.txt	2007-07-21 10:05:29 UTC (rev 78260)
@@ -64,6 +64,16 @@
 New Features
 ------------
 
+- RENormalizer accepts plain Python callables.
+
+- Added --slow-test option.
+
+3.5.0 (2007/07/19)
+==================
+
+New Features
+------------
+
 - The test runner now works on Python 2.5.
 
 - Added support for cProfile.

Modified: zope.testing/trunk/src/zope/testing/renormalizing.py
===================================================================
--- zope.testing/trunk/src/zope/testing/renormalizing.py	2007-07-21 09:49:11 UTC (rev 78259)
+++ zope.testing/trunk/src/zope/testing/renormalizing.py	2007-07-21 10:05:29 UTC (rev 78260)
@@ -171,6 +171,56 @@
         <BLANKLINE>
     <BLANKLINE>
 
+If regular expressions aren't expressive enough, you can use arbitrary Python
+callables to transform the text.  For example, suppose you want to ignore
+case during comparison:
+
+    >>> checker = RENormalizing([
+    ...    lambda s: s.lower(),
+    ...    lambda s: s.replace('<blankline>', '<BLANKLINE>'),
+    ...    ])
+
+    >>> want = '''\
+    ... Usage: thundermonkey [options] [url]
+    ... <BLANKLINE>
+    ... Options:
+    ...     -h    display this help message
+    ... '''
+
+    >>> got = '''\
+    ... usage: thundermonkey [options] [URL]
+    ...
+    ... options:
+    ...     -h    Display this help message
+    ... '''
+
+    >>> checker.check_output(want, got, 0)
+    True
+
+Suppose we forgot that <BLANKLINE> must be in upper case:
+
+    >>> checker = RENormalizing([
+    ...    lambda s: s.lower(),
+    ...    ])
+
+    >>> checker.check_output(want, got, 0)
+    False
+
+The difference would show us that:
+
+    >>> source = '''\
+    ... >>> print_help_message()
+    ... ''' + want
+    >>> example = doctest.Example(source, want)
+    >>> print checker.output_difference(example, got,
+    ...                                 doctest.REPORT_NDIFF),
+    Differences (ndiff with -expected +actual):
+          usage: thundermonkey [options] [url]
+        - <blankline>
+        + <BLANKLINE>
+          options:
+              -h    display this help message
+
 $Id$
 """
 
@@ -181,15 +231,21 @@
     """
 
     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 +264,9 @@
         # 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



More information about the Zope3-Checkins mailing list