[Checkins] SVN: zope.testbrowser/trunk/ Add Ellipsis assertion helper which is useful for using testbrowser with unittest.TestCase instead of doctests

Wolfgang Schnerring wosc at wosc.de
Wed Oct 26 14:27:33 UTC 2011


Log message for revision 123163:
  Add Ellipsis assertion helper which is useful for using testbrowser with unittest.TestCase instead of doctests
  

Changed:
  U   zope.testbrowser/trunk/CHANGES.txt
  A   zope.testbrowser/trunk/src/zope/testbrowser/assertion.py
  A   zope.testbrowser/trunk/src/zope/testbrowser/tests/test_assertion.py

-=-
Modified: zope.testbrowser/trunk/CHANGES.txt
===================================================================
--- zope.testbrowser/trunk/CHANGES.txt	2011-10-26 08:33:26 UTC (rev 123162)
+++ zope.testbrowser/trunk/CHANGES.txt	2011-10-26 14:27:32 UTC (rev 123163)
@@ -6,12 +6,15 @@
 ------------------
 
 - When ``Browser.handleErrors`` is False, also add ``x-wsgiorg.throw_errors``
-  to the environment. http://wsgi.org/wsgi/Specifications/throw_errors 
+  to the environment. http://wsgi.org/wsgi/Specifications/throw_errors
 
 - Prevent WebTest from always sending ``paste.throw_errors=True`` in the
   environment by setting it to ``None`` when ``Browser.handleErrors`` is
   ``True``.  This makes it easier to test error pages.
 
+- Add assertion helper that provides ``assertEllipsis`` and
+  ``assertNotEllipsis``.
+
 4.0.2 (2011-05-25)
 ------------------
 

Added: zope.testbrowser/trunk/src/zope/testbrowser/assertion.py
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/assertion.py	                        (rev 0)
+++ zope.testbrowser/trunk/src/zope/testbrowser/assertion.py	2011-10-26 14:27:32 UTC (rev 123163)
@@ -0,0 +1,50 @@
+#############################################################################
+#
+# 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 difflib
+import doctest
+
+
+class Ellipsis(object):
+    """Assertion helper that provides doctest-style ellipsis matching.
+
+    Inherit from this class in additition to unittest.TestCase.
+
+    For convenience when using this with a zope.testbrowser, if no ``actual``
+    value is provided, ``self.browser.contents`` is used.
+    """
+
+    def assertEllipsis(self, expected, actual=None):
+        if actual is None:
+            actual = self.browser.contents
+        # normalize whitespace
+        norm_expected = ' '.join(expected.split())
+        norm_actual = ' '.join(actual.split())
+        if doctest._ellipsis_match(norm_expected, norm_actual):
+            return True
+        # report ndiff
+        engine = difflib.Differ(charjunk=difflib.IS_CHARACTER_JUNK)
+        diff = list(engine.compare(expected.splitlines(True),
+                                   actual.splitlines(True)))
+        kind = 'ndiff with -expected +actual'
+        diff = [line.rstrip() + '\n' for line in diff]
+        self.fail('Differences (%s):\n' % kind + ''.join(diff))
+
+    def assertNotEllipsis(self, expected, actual=None):
+        try:
+            self.assertEllipsis(expected, actual)
+        except AssertionError:
+            pass
+        else:
+            self.fail('Value unexpectedly matches expression %r.' % expected)

Added: zope.testbrowser/trunk/src/zope/testbrowser/tests/test_assertion.py
===================================================================
--- zope.testbrowser/trunk/src/zope/testbrowser/tests/test_assertion.py	                        (rev 0)
+++ zope.testbrowser/trunk/src/zope/testbrowser/tests/test_assertion.py	2011-10-26 14:27:32 UTC (rev 123163)
@@ -0,0 +1,54 @@
+#############################################################################
+#
+# 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 unittest
+import zope.testbrowser.assertion
+
+
+class EllipsisTest(unittest.TestCase, zope.testbrowser.assertion.Ellipsis):
+
+    def test_match_found_nothing_happens(self):
+        # assert nothing is raised
+        self.assertEllipsis('...bar...', 'foo bar baz')
+
+    def test_no_match_found_fails(self):
+        try:
+            self.assertEllipsis('foo', 'bar')
+        except AssertionError, e:
+            self.assertEqual(
+                'Differences (ndiff with -expected +actual):\n- foo\n+ bar\n',
+                str(e))
+        else:
+            self.fail('nothing raised')
+
+    def test_unicode_matches_encoded(self):
+        # assert nothing is raised
+        self.assertEllipsis(u'...bar...', u'foo bar baz'.encode('utf-8'))
+
+    def test_encoded_matches_unicode(self):
+        # assert nothing is raised
+        self.assertEllipsis(u'...bar...'.encode('utf-8'), u'foo bar baz')
+
+    def test_inverse_assertion(self):
+        # assert nothing is raised
+        self.assertNotEllipsis('foo', 'bar')
+
+        try:
+            self.assertNotEllipsis('...bar...', 'foo bar baz')
+        except AssertionError, e:
+            self.assertEqual(
+                "Value unexpectedly matches expression '...bar...'.",
+                str(e))
+        else:
+            self.fail('nothing raised')



More information about the checkins mailing list