[Zope3-checkins] CVS: Zope3/src/zope/app/zptpage/tests - __init__.py:1.2 test_zptpage.py:1.2 test_zptpageeval.py:1.2

Philipp von Weitershausen philikon at philikon.de
Tue Feb 24 11:50:49 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/zptpage/tests
In directory cvs.zope.org:/tmp/cvs-serv27280/src/zope/app/zptpage/tests

Added Files:
	__init__.py test_zptpage.py test_zptpageeval.py 
Log Message:


Moved the Template Page content type to its own package below
zope.app, including its interfaces and browser views.




=== Zope3/src/zope/app/zptpage/tests/__init__.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:50:49 2004
+++ Zope3/src/zope/app/zptpage/tests/__init__.py	Tue Feb 24 11:50:48 2004
@@ -0,0 +1,2 @@
+#
+# This file is necessary to make this directory a package.


=== Zope3/src/zope/app/zptpage/tests/test_zptpage.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:50:49 2004
+++ Zope3/src/zope/app/zptpage/tests/test_zptpage.py	Tue Feb 24 11:50:48 2004
@@ -0,0 +1,220 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""
+Basic tests for Page Templates used in content-space.
+
+$Id$
+"""
+
+import unittest
+
+from zope.interface.verify import verifyClass
+from zope.exceptions import Forbidden
+
+from zope.app.tests import ztapi
+from zope.app.interfaces.index.text import ISearchableText
+from zope.component import getAdapter, getView
+from zope.publisher.browser import TestRequest, BrowserView
+
+# Wow, this is a lot of work. :(
+from zope.app.tests.placelesssetup import PlacelessSetup
+from zope.app.traversing.adapters import Traverser, DefaultTraversable
+from zope.app.interfaces.traversing import ITraverser
+from zope.app.interfaces.traversing import ITraversable
+from zope.app.tests import ztapi
+from zope.security.checker import NamesChecker, defineChecker
+from zope.app.container.contained import contained
+
+from zope.app.zptpage.interfaces import IZPTPage
+from zope.app.zptpage.zptpage import ZPTPage, ZPTSourceView,\
+     ZPTReadFile, ZPTWriteFile, ZPTFactory
+from zope.app.zptpage.zptpage import Sized, SearchableText
+
+
+class Data(object):
+    def __init__(self, **kw):
+        self.__dict__.update(kw)
+
+
+class ZPTPageTests(PlacelessSetup, unittest.TestCase):
+
+    def setUp(self):
+        super(ZPTPageTests, self).setUp()
+        ztapi.provideAdapter(None, ITraverser, Traverser)
+        ztapi.provideAdapter(None, ITraversable, DefaultTraversable)
+        ztapi.provideAdapter(IZPTPage, ISearchableText, SearchableText)
+        defineChecker(Data, NamesChecker(['URL', 'name']))
+        defineChecker(TestRequest, NamesChecker(['getPresentationSkin']))
+
+    def testSearchableText(self):
+        page = ZPTPage()
+        searchableText = getAdapter(page, ISearchableText)
+
+        utext = u'another test\n' # The source will grow a newline if ommited
+        html = u"<html><body>%s</body></html>\n" % (utext, )
+
+        page.setSource(utext)
+        self.failUnlessEqual(searchableText.getSearchableText(), [utext])
+
+        page.setSource(html, content_type='text/html')
+        self.assertEqual(searchableText.getSearchableText(), [utext+'\n'])
+
+        page.setSource(html, content_type='text/plain')
+        self.assertEqual(searchableText.getSearchableText(), [html])
+
+    def testZPTRendering(self):
+        page = ZPTPage()
+        page.setSource(
+            u''
+            '<html>'
+            '<head><title tal:content="options/title">blah</title></head>'
+            '<body>'
+            '<a href="foo" tal:attributes="href request/URL/1">'
+            '<span tal:replace="container/name">splat</span>'
+            '</a></body></html>'
+            )
+
+        page = contained(page, Data(name='zope'))
+
+        out = page.render(Data(URL={'1': 'http://foo.com/'}),
+                          title="Zope rules")
+        out = ' '.join(out.split())
+
+        self.assertEqual(
+            out,
+            '<html><head><title>Zope rules</title></head><body>'
+            '<a href="http://foo.com/">'
+            'zope'
+            '</a></body></html>'
+            )
+
+    def test_request_protected(self):
+        page = ZPTPage()
+        page.setSource(
+            u'<p tal:content="python: request.__dict__" />'
+            )
+
+        page = contained(page, Data(name='zope'))
+
+        self.assertRaises(Forbidden, page.render, Data())
+
+    def test_template_context_wrapping(self):
+
+        class AU(BrowserView):
+            def __str__(self):
+                name = self.context.__name__
+                if name is None:
+                    return 'None'
+                return name
+
+        from zope.app.traversing.namespace import provideNamespaceHandler
+        from zope.app.traversing.namespace import view
+        provideNamespaceHandler('view', view)
+        ztapi.browserView(IZPTPage, 'name', AU)
+
+        page = ZPTPage()
+        page.setSource(
+            u'<p tal:replace="template/@@name" />'
+            )
+        page = contained(page, None, name='zpt')
+        request = TestRequest()
+        self.assertEquals(page.render(request), 'zpt\n')
+
+
+class DummyZPT:
+
+    def __init__(self, source):
+        self.source = source
+
+    def getSource(self):
+        return self.source
+
+class SizedTests(unittest.TestCase):
+
+    def testInterface(self):
+        from zope.app.interfaces.size import ISized
+        self.failUnless(ISized.isImplementedByInstancesOf(Sized))
+        self.failUnless(verifyClass(ISized, Sized))
+
+    def test_zeroSized(self):
+        s = Sized(DummyZPT(''))
+        self.assertEqual(s.sizeForSorting(), ('line', 0))
+        self.assertEqual(s.sizeForDisplay(), u'${lines} lines')
+        self.assertEqual(s.sizeForDisplay().mapping, {'lines': '0'})
+
+    def test_oneSized(self):
+        s = Sized(DummyZPT('one line'))
+        self.assertEqual(s.sizeForSorting(), ('line', 1))
+        self.assertEqual(s.sizeForDisplay(), u'1 line')
+
+    def test_arbitrarySize(self):
+        s = Sized(DummyZPT('some line\n'*5))
+        self.assertEqual(s.sizeForSorting(), ('line', 5))
+        self.assertEqual(s.sizeForDisplay(), u'${lines} lines')
+        self.assertEqual(s.sizeForDisplay().mapping, {'lines': '5'})
+
+
+class TestFileEmulation(unittest.TestCase):
+
+    def test_ReadFile(self):
+        page = ZPTPage()
+        content = u"<p></p>"
+        page.setSource(content)        
+        f = ZPTReadFile(page)
+        self.assertEqual(f.read(), content)
+        self.assertEqual(f.size(), len(content))
+
+    def test_WriteFile(self):
+        page = ZPTPage()
+        f = ZPTWriteFile(page)
+        content = "<p></p>"
+        f.write(content)
+        self.assertEqual(page.getSource(), content)
+
+    def test_factory(self):
+        content = "<p></p>"
+        page = ZPTFactory(None)('foo', '', content)
+        self.assertEqual(page.getSource(), content)
+
+
+class ZPTSourceTest(PlacelessSetup, unittest.TestCase):
+
+    def setUp(self):
+        super(ZPTSourceTest, self).setUp()
+        ztapi.browserView(IZPTPage, 'source.html', ZPTSourceView)
+
+    def testSourceView(self):
+        page = ZPTPage()
+
+        utext = u'another test\n' # The source will grow a newline if ommited
+        html = u"<html><body>%s</body></html>\n" % (utext, )
+        page.setSource(html, content_type='text/plain')
+        request = TestRequest()
+
+        view = getView(page, 'source.html', request)
+
+        self.assertEqual(str(view), html)
+        self.assertEqual(view(), html)
+
+
+def test_suite():
+    return unittest.TestSuite((
+        unittest.makeSuite(ZPTPageTests),
+        unittest.makeSuite(SizedTests),
+        unittest.makeSuite(TestFileEmulation),
+        unittest.makeSuite(ZPTSourceTest),
+        ))
+
+if __name__=='__main__':
+    unittest.TextTestRunner().run(test_suite())


=== Zope3/src/zope/app/zptpage/tests/test_zptpageeval.py 1.1 => 1.2 ===
--- /dev/null	Tue Feb 24 11:50:49 2004
+++ Zope3/src/zope/app/zptpage/tests/test_zptpageeval.py	Tue Feb 24 11:50:48 2004
@@ -0,0 +1,62 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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
+#
+##############################################################################
+"""
+$Id$
+"""
+
+from unittest import TestCase, main, makeSuite
+from zope.testing.cleanup import CleanUp # Base class w registry cleanup
+from zope.app.container.contained import contained
+from zope.app.zptpage.browser.zptpage import ZPTPageEval
+
+class Test(CleanUp, TestCase):
+
+    def testTemplateRendering(self):
+
+        class Template:
+            def render(self, request, **kw):
+                self.called = request, kw
+                return 42
+
+            content_type = 'text/x-test'
+
+        class Folder:
+            name='zope'
+        folder = Folder()
+
+        class Request(object):
+            def _getResponse(self):
+                return self
+
+            response = property(_getResponse)
+
+            def setHeader(self, name, value):
+                setattr(self, name, value)
+
+        request = Request()
+        template = contained(Template(), folder)
+
+        view = ZPTPageEval()
+        # Do manually, since directive adds BrowserView as base class
+        view.context = template
+        view.request = request
+        self.assertEqual(view.index(), 42)
+        self.assertEqual(template.called, (request, {}))
+        self.assertEqual(getattr(request, 'content-type'), 'text/x-test')
+
+def test_suite():
+    return makeSuite(Test)
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')




More information about the Zope3-Checkins mailing list