[Checkins] SVN: z3c.etestbrowser/trunk/ Added `normalized_contents` attribute for easier-to-read examples in doctests.

Christian Theune ct at gocept.com
Thu May 29 07:40:43 EDT 2008


Log message for revision 87023:
  Added `normalized_contents` attribute for easier-to-read examples in doctests.
  

Changed:
  U   z3c.etestbrowser/trunk/CHANGES.txt
  U   z3c.etestbrowser/trunk/src/z3c/etestbrowser/README.txt
  U   z3c.etestbrowser/trunk/src/z3c/etestbrowser/ftesting.zcml
  U   z3c.etestbrowser/trunk/src/z3c/etestbrowser/testing.py

-=-
Modified: z3c.etestbrowser/trunk/CHANGES.txt
===================================================================
--- z3c.etestbrowser/trunk/CHANGES.txt	2008-05-29 10:57:31 UTC (rev 87022)
+++ z3c.etestbrowser/trunk/CHANGES.txt	2008-05-29 11:40:42 UTC (rev 87023)
@@ -4,3 +4,7 @@
 
 1.2 (unreleased)
 ----------------
+
+- Added `normalized_contents` attribute that reindents and normalizes the
+  etree structure of a document and allows easier to read HTML/XML examples in
+  doctests.

Modified: z3c.etestbrowser/trunk/src/z3c/etestbrowser/README.txt
===================================================================
--- z3c.etestbrowser/trunk/src/z3c/etestbrowser/README.txt	2008-05-29 10:57:31 UTC (rev 87022)
+++ z3c.etestbrowser/trunk/src/z3c/etestbrowser/README.txt	2008-05-29 11:40:42 UTC (rev 87023)
@@ -84,3 +84,38 @@
   unauthenticated principal [Login][1] (image)[2] Location:...[top][3] /
   Navigation
   Loading... ... Name Title Created Modified ...
+
+HTML/XML normalization
+======================
+
+The extended test browser allows normalized output of HTML and XML which makes
+testing examples with HTML or XML a bit easier when unimportant details like
+whitespace are changing:
+
+  >>> browser.open('http://localhost/funny.html')
+  >>> print browser.contents
+  <html>
+    <head>
+      <title>Foo</title>
+  </head>
+      <body>
+            <h1>
+        Title
+      </h1>
+          </body>
+              </html>
+  <BLANKLINE>
+
+versus
+
+  >>> print browser.normalized_contents
+  <html>
+    <head>
+      <title>Foo</title>
+    </head>
+    <body>
+      <h1>
+        Title
+      </h1>
+    </body>
+  </html>

Modified: z3c.etestbrowser/trunk/src/z3c/etestbrowser/ftesting.zcml
===================================================================
--- z3c.etestbrowser/trunk/src/z3c/etestbrowser/ftesting.zcml	2008-05-29 10:57:31 UTC (rev 87022)
+++ z3c.etestbrowser/trunk/src/z3c/etestbrowser/ftesting.zcml	2008-05-29 11:40:42 UTC (rev 87023)
@@ -30,4 +30,11 @@
     permission="zope.View"
     />
 
+  <browser:page
+    name="funny.html"
+    for="*"
+    template="funny.pt"
+    permission="zope.View"
+    />
+
 </configure>

Modified: z3c.etestbrowser/trunk/src/z3c/etestbrowser/testing.py
===================================================================
--- z3c.etestbrowser/trunk/src/z3c/etestbrowser/testing.py	2008-05-29 10:57:31 UTC (rev 87022)
+++ z3c.etestbrowser/trunk/src/z3c/etestbrowser/testing.py	2008-05-29 11:40:42 UTC (rev 87023)
@@ -29,6 +29,22 @@
 RE_CHARSET = re.compile('.*;charset=(.*)')
 
 
+def indent(elem, level=0):
+    i = "\n" + level*"  "
+    if len(elem):
+        if not elem.text or not elem.text.strip():
+            elem.text = i + "  "
+        for e in elem:
+            indent(e, level+1)
+            if not e.tail or not e.tail.strip():
+                e.tail = i + "  "
+        if not e.tail or not e.tail.strip():
+            e.tail = i
+    else:
+        if level and (not elem.tail or not elem.tail.strip()):
+            elem.tail = i
+
+
 class ExtendedTestBrowser(zope.testbrowser.testing.Browser):
     """An extended testbrowser implementation.
 
@@ -39,6 +55,7 @@
     """
 
     xml_strict = False
+    normalized_contents = ''
 
     _etree = None
 
@@ -68,6 +85,9 @@
     def _changed(self):
         super(ExtendedTestBrowser, self)._changed()
         self._etree = None
+        tree = self.etree
+        indent(tree)
+        self.normalized_contents = lxml.etree.tostring(tree, pretty_print=True)
 
     def pretty_print(self):
         """Print a pretty (formatted) version of the HTML content.



More information about the Checkins mailing list