[Zope3-checkins] CVS: Products3/z3checkins/tests - test_message.py:1.3

Marius Gedminas mgedmin@codeworks.lt
Thu, 3 Apr 2003 07:08:25 -0500


Update of /cvs-repository/Products3/z3checkins/tests
In directory cvs.zope.org:/tmp/cvs-serv24870/z3checkins/tests

Modified Files:
	test_message.py 
Log Message:
- Plain text view for checkin messages (called 'index.txt')
- Next/Previous/First/Last navigation via <link> elements
- Tabs and trailing whitespace are highlighted in diffs
- Message parsing uses less regexes (old way triggered stack overflows)



=== Products3/z3checkins/tests/test_message.py 1.2 => 1.3 ===
--- Products3/z3checkins/tests/test_message.py:1.2	Thu Apr  3 04:17:47 2003
+++ Products3/z3checkins/tests/test_message.py	Thu Apr  3 07:08:24 2003
@@ -10,12 +10,14 @@
 from difflib import SequenceMatcher
 from StringIO import StringIO
 from datetime import datetime, timedelta
-from zope.interface.verify import verifyObject
 from zope.app.tests.placelesssetup import PlacelessSetup
 from zope.component import getService
+from zope.interface.verify import verifyObject
 from zope.proxy.context import getWrapperContext, getWrapperData
+from zope.proxy.context import ContextWrapper
 
 from zopeproducts.z3checkins.interfaces import ICheckinMessage, ICheckinMessageParser
+from zopeproducts.z3checkins.interfaces import ICheckinMessageArchive
 
 
 class TestFixedTimezone(unittest.TestCase):
@@ -186,12 +188,75 @@
 
     __implements__ = ICheckinMessage
 
-    message_id = "<message@id>"
-
-    def __init__(self, data=None, date=None, body=None):
+    def __init__(self, data=None, date=None, body=None,
+                       message_id="<message@id>"):
         self.data = data
         self.date = date
         self.body = body
+        self.message_id = message_id
+
+
+class TestCheckinMessageAdapter(unittest.TestCase):
+
+    def test_interface(self):
+        from zopeproducts.z3checkins.message import CheckinMessageAdapter
+        verifyObject(ICheckinMessageArchive, CheckinMessageAdapter({}))
+
+    def test_len(self):
+        from zopeproducts.z3checkins.message import CheckinMessageAdapter
+        a = CheckinMessageAdapter({})
+        self.assertEquals(len(a), 0)
+        a = CheckinMessageAdapter({'1': 2, '3': 'abc'})
+        self.assertEquals(len(a), 0)
+        a = CheckinMessageAdapter({'1': 2, '3': 'abc', 4: MessageStub()})
+        self.assertEquals(len(a), 1)
+
+    def test_getitem(self):
+        from zopeproducts.z3checkins.message import CheckinMessageAdapter
+        a = CheckinMessageAdapter({'1': 2, '3': 'abc',
+                                   '4': MessageStub(date=1, message_id='1'),
+                                   '5': MessageStub(date=4, message_id='2'),
+                                   '6': MessageStub(date=3, message_id='3'),
+                                   '7': MessageStub(date=2, message_id='4')})
+        self.assertEquals(a[0].message_id, '1')
+        self.assertEquals(a[1].message_id, '4')
+        self.assertEquals(a[2].message_id, '3')
+        self.assertEquals(a[3].message_id, '2')
+        self.assertEquals(a[-1].message_id, '2')
+        self.assertRaises(IndexError, a.__getitem__, 4)
+        self.assertRaises(IndexError, a.__getitem__, -5)
+        self.assertRaises(TypeError, a.__getitem__, 'xyzzy')
+        self.assertRaises(TypeError, a.__getitem__, None)
+        self.assertEquals(len(a[1:3]), 2)
+        self.assertEquals(len(a[1:-1]), 2)
+        self.assertEquals(len(a[3:1]), 0)
+
+    def test_iter(self):
+        from zopeproducts.z3checkins.message import CheckinMessageAdapter
+        a = CheckinMessageAdapter({'1': 2, '3': 'abc',
+                                   '4': MessageStub(date=1, message_id='1'),
+                                   '5': MessageStub(date=4, message_id='2'),
+                                   '6': MessageStub(date=3, message_id='3'),
+                                   '7': MessageStub(date=2, message_id='4')})
+        b = [x.message_id for x in a]
+        self.assertEquals(b, ['1', '4', '3', '2'])
+        self.assert_(MessageStub(message_id='5') not in a)
+        self.assert_(a.context['6'] in a)
+
+    def test_index(self):
+        from zopeproducts.z3checkins.message import CheckinMessageAdapter
+        m1 = MessageStub(date=1, message_id='1')
+        m2 = MessageStub(date=4, message_id='2')
+        m3 = MessageStub(date=3, message_id='3')
+        m4 = MessageStub(date=2, message_id='4')
+        a = CheckinMessageAdapter({'1': 2, '3': 'abc',
+                                   '4': m1, '5': m2, '6': m3, '7': m4})
+        self.assertEquals(a.index(m1), 0)
+        self.assertEquals(a.index(m4), 1)
+        self.assertEquals(a.index(m3), 2)
+        self.assertEquals(a.index(m2), 3)
+        self.assertRaises(ValueError, a.index, MessageStub)
+
 
 class ParserStub:
 
@@ -212,8 +277,8 @@
 
     def setUp(self):
         PlacelessSetup.setUp(self)
-        getService(None,'Utilities').provideUtility(ICheckinMessageParser,
-                                                    ParserStub())
+        getService(None, 'Utilities').provideUtility(ICheckinMessageParser,
+                                                     ParserStub())
 
     def test_createAndAdd(self):
         from zopeproducts.z3checkins.message import MessageUpload
@@ -238,7 +303,14 @@
         self.assertEquals(view.context.added.data, "Ipsum suum")
 
 
-class TestContainerView(unittest.TestCase):
+class TestContainerView(PlacelessSetup, unittest.TestCase):
+
+    def setUp(self):
+        PlacelessSetup.setUp(self)
+        from zopeproducts.z3checkins.message import CheckinMessageAdapter
+        getService(None, 'Adapters').provideAdapter(None,
+                                                    ICheckinMessageArchive,
+                                                    CheckinMessageAdapter)
 
     def test_last_checkins(self):
         from zopeproducts.z3checkins.message import ContainerView
@@ -272,6 +344,7 @@
         self.assertEquals(getWrapperData(res[1])['name'], 'a')
 
 
+
 def diff(a, b):
     "Compare the differences of two sequences of strings"
 
@@ -298,7 +371,14 @@
             raise ValueError, 'unknown tag ' + `tag`
     return "\n".join(diff)
 
-class TestCheckinMessageView(unittest.TestCase):
+class TestCheckinMessageView(PlacelessSetup, unittest.TestCase):
+
+    def setUp(self):
+        PlacelessSetup.setUp(self)
+        from zopeproducts.z3checkins.message import CheckinMessageAdapter
+        getService(None, 'Adapters').provideAdapter(None,
+                                                    ICheckinMessageArchive,
+                                                    CheckinMessageAdapter)
 
     def test_body_strange(self):
         from zopeproducts.z3checkins.message import CheckinMessageView
@@ -334,10 +414,10 @@
                     '<p>Blurb blurb</p>'
                     '<p>blurb.</p>'
                     '</div>'
-                    '<pre>'
+                    '<pre>\n'
                     '<div class="file">=== foo.py: 1.2 -&gt; 1.3 ===\n</div>'
-                    '<div class="oldfile">--- foo.py:1.2\tdatetime\n</div>'
-                    '<div class="newfile">+++ foo.py\tdatetime\n</div>'
+                    '<div class="oldfile">--- foo.py:1.2<span class="tab">>--</span>datetime\n</div>'
+                    '<div class="newfile">+++ foo.py<span class="tab">>------</span>datetime\n</div>'
                     '<div class="chunk">@@@ -123,4 +567,8 @@@\n</div>'
                     ' fwoosh &lt;&gt;&amp;&quot;\n'
                     '<div class="old">-fouoww\n</div>'
@@ -364,9 +444,11 @@
                           "+++ foo.py\tdatetime\r\n"
                           "@@@ -123,4 +567,8 @@@\r\n"
                           " fwoosh <>&\"\r\n"
-                          "-fouoww\r\n"
+                          "-fouoww  \r\n"
                           "+fruuuh\r\n"
                           " fargle\r\n"
+                          "   \r\n"
+                          " \r\n"
                           "_______________________________________________\r\n"
                           "signature\r\n")
         result = view.body()
@@ -378,15 +460,17 @@
                     '<p>Blurb blurb</p>'
                     '<p>blurb.</p>'
                     '</div>'
-                    '<pre>'
+                    '<pre>\n'
                     '<div class="file">=== foo.py: 1.2 -&gt; 1.3 ===\n</div>'
-                    '<div class="oldfile">--- foo.py:1.2\tdatetime\n</div>'
-                    '<div class="newfile">+++ foo.py\tdatetime\n</div>'
+                    '<div class="oldfile">--- foo.py:1.2<span class="tab">>--</span>datetime\n</div>'
+                    '<div class="newfile">+++ foo.py<span class="tab">>------</span>datetime\n</div>'
                     '<div class="chunk">@@@ -123,4 +567,8 @@@\n</div>'
                     ' fwoosh &lt;&gt;&amp;&quot;\n'
-                    '<div class="old">-fouoww\n</div>'
+                    '<div class="old">-fouoww<span class="trail">..</span>\n</div>'
                     '<div class="new">+fruuuh\n</div>'
-                    ' fargle'
+                    ' fargle\n'
+                    ' <span class="trail">..</span>\n'
+                    ' '
                     '<div class="signature">\n'
                     '_______________________________________________\n'
                     'signature\n'
@@ -420,10 +504,10 @@
                     '<p>Blurb blurb</p>'
                     '<p>blurb.</p>'
                     '</div>'
-                    '<pre>'
+                    '<pre>\n'
                     '<div class="file">=== foo.py: 1.2 -&gt; 1.3 ===\n</div>'
-                    '<div class="oldfile">--- foo.py:1.2\tdatetime\n</div>'
-                    '<div class="newfile">+++ foo.py\tdatetime\n</div>'
+                    '<div class="oldfile">--- foo.py:1.2<span class="tab">>--</span>datetime\n</div>'
+                    '<div class="newfile">+++ foo.py<span class="tab">>------</span>datetime\n</div>'
                     '<div class="chunk">@@@ -123,4 +567,8 @@@\n</div>'
                     ' fwoosh &lt;&gt;&amp;&quot;\n'
                     '<div class="old">-fouoww\n</div>'
@@ -459,7 +543,7 @@
                     '<p>Blurb blurb</p>'
                     '<p>blurb.</p>'
                     '</div>'
-                    '<pre>'
+                    '<pre>\n'
                     'Status:\n'
                     '\n'
                     'Vendor Tag:\tnovendor\n'
@@ -472,6 +556,67 @@
                     '</pre>')
         self.assertEquals(result, expected, diff(expected, result))
 
+    def test_markwitespace(self):
+        from zopeproducts.z3checkins.message import CheckinMessageView
+        view = CheckinMessageView()
+        m = view.mark_whitespace
+        self.assertEquals(m(''), '')
+        self.assertEquals(m('xyzzy'), 'xyzzy')
+        self.assertEquals(m('  '), ' <span class="trail">.</span>')
+        self.assertEquals(m('  xy z  '), '  xy z<span class="trail">..</span>')
+        self.assertEquals(m('  xy z \t '), '  xy z<span class="trail">.<span class="tab">>-</span>.</span>')
+        self.assertEquals(m(' \t|'), ' <span class="tab">>-------</span>|')
+        self.assertEquals(m(' |\t|'), ' |<span class="tab">>------</span>|')
+        self.assertEquals(m(' xxxxxx|\t|'), ' xxxxxx|<span class="tab">></span>|')
+        self.assertEquals(m(' x<tag\t>xxxxx|\t|'), ' x<tag\t>xxxxx|<span class="tab">></span>|')
+        self.assertEquals(m(' x&ent;xxxx|\t|'), ' x&ent;xxxx|<span class="tab">></span>|')
+
+    def test_navigation_no_archive(self):
+        from zopeproducts.z3checkins.message import CheckinMessageView
+        view = CheckinMessageView()
+        view.context = MessageStub()
+        self.assertEquals(view.first(), None)
+        self.assertEquals(view.last(), None)
+        self.assertEquals(view.next(), None)
+        self.assertEquals(view.previous(), None)
+
+    def test_navigation_empty_archive(self):
+        from zopeproducts.z3checkins.message import CheckinMessageView
+        view = CheckinMessageView()
+        view.context = ContextWrapper(MessageStub(), {'1': 2})
+        self.assertEquals(view.first(), None)
+        self.assertEquals(view.last(), None)
+        self.assertEquals(view.next(), None)
+        self.assertEquals(view.previous(), None)
+
+    def test_navigation(self):
+        from zopeproducts.z3checkins.message import CheckinMessageView
+        m1 = MessageStub(date=1, message_id='1')
+        m2 = MessageStub(date=2, message_id='2')
+        m3 = MessageStub(date=3, message_id='3')
+        m4 = MessageStub(date=4, message_id='4')
+        folder = {'1': 2, '3': 'abc', '4': m1, '5': m2, '6': m3, '7': m4}
+        view = CheckinMessageView()
+        view.context = ContextWrapper(m3, folder)
+        self.assertEquals(view.first(), m1)
+        self.assertEquals(view.last(), m4)
+        self.assertEquals(view.next(), m4)
+        self.assertEquals(view.previous(), m2)
+
+        view = CheckinMessageView()
+        view.context = ContextWrapper(m1, folder)
+        self.assertEquals(view.first(), m1)
+        self.assertEquals(view.last(), m4)
+        self.assertEquals(view.next(), m2)
+        self.assertEquals(view.previous(), None)
+
+        view = CheckinMessageView()
+        view.context = ContextWrapper(m4, folder)
+        self.assertEquals(view.first(), m1)
+        self.assertEquals(view.last(), m4)
+        self.assertEquals(view.next(), None)
+        self.assertEquals(view.previous(), m3)
+
 
 def test_suite():
     suite = unittest.TestSuite()
@@ -480,6 +625,7 @@
     suite.addTest(unittest.makeSuite(TestISODateTimeFormatter))
     suite.addTest(unittest.makeSuite(TestCheckinMessage))
     suite.addTest(unittest.makeSuite(TestCheckinMessageParser))
+    suite.addTest(unittest.makeSuite(TestCheckinMessageAdapter))
     suite.addTest(unittest.makeSuite(TestMessageUpload))
     suite.addTest(unittest.makeSuite(TestContainerView))
     suite.addTest(unittest.makeSuite(TestCheckinMessageView))