[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/PageTemplate/tests - SimpleTestView.py:1.1.2.1 __init__.py:1.1.2.1 sample.py:1.1.2.1 test.pt:1.1.2.1 testBoundPageTemplate.py:1.1.2.1 testSimpleViewClass.pt:1.1.2.1 testSimpleViewClass.py:1.1.2.1 testViewZPT.py:1.1.2.1 testZopePythonExpr.py:1.1.2.1 test_binding.py:1.1.2.1
Jim Fulton
jim@zope.com
Thu, 23 May 2002 14:01:16 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/PageTemplate/tests
In directory cvs.zope.org:/tmp/cvs-serv26429/lib/python/Zope/App/PageTemplate/tests
Added Files:
Tag: Zope-3x-branch
SimpleTestView.py __init__.py sample.py test.pt
testBoundPageTemplate.py testSimpleViewClass.pt
testSimpleViewClass.py testViewZPT.py testZopePythonExpr.py
test_binding.py
Log Message:
This all started with wanting to be able to use url;view in a ZPT path. :)
That lead me to:
- Massive traversal refactoring.
Namespace handling is now centralized in Zope.App.Traversing.
- ZPT refactoring, including some renaming that touches pretty much everything. :)
- The application specific ZPT support was moved into
Zope.App.PageTemplate.
- To get page template files (for use in views):
from Zope.App.PageTemplate import ViewPageTemplateFile
- Fixed up security so that ZPT expressions only have access to
safe builtins and so that modules namespace does imports safely.
- Got ZPTPage working!
- renaming url to absolute_url and got absolute_url to work in paths.
- Cleaned up the (as yet unused) RestrictedInterpreter module in
Zope.Security. In particular, changed to use a separate
RestrictedBuiltins module.
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/SimpleTestView.py ===
##############################################################################
#
# 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.
#
##############################################################################
from Zope.App.PageTemplate.SimpleViewClass import SimpleViewClass
SimpleTestView = SimpleViewClass('testSimpleViewClass.pt')
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/__init__.py ===
##############################################################################
#
# Copyright (c) 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
#
##############################################################################
"""
Revision information:
$Id: __init__.py,v 1.1.2.1 2002/05/23 18:01:14 jim Exp $
"""
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/sample.py ===
##############################################################################
#
# 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.
#
##############################################################################
"""
Revision information: $Id: sample.py,v 1.1.2.1 2002/05/23 18:01:14 jim Exp $
"""
import os
from Zope.App.PageTemplate import ViewPageTemplateFile
class C:
index = ViewPageTemplateFile('test.pt')
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/test.pt ===
<html><body></body></html>
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/testBoundPageTemplate.py ===
##############################################################################
#
# 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.
#
##############################################################################
import unittest, sys, os
class Test(unittest.TestCase):
def testAttributes(self):
from sample import C
self.assertRaises(AttributeError, setattr, C.index, 'foo', 1)
self.assertRaises(AttributeError, setattr, C().index, 'foo', 1)
C.index.im_func.foo = 1
self.assertEqual(C.index.foo, 1)
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/testSimpleViewClass.pt ===
<html>
<body>
<p metal:define-macro="test">hello world</p>
</body>
</html>
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/testSimpleViewClass.py ===
##############################################################################
#
# 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.
#
##############################################################################
import unittest, sys
class data: pass
class Test(unittest.TestCase):
def test(self):
from SimpleTestView import SimpleTestView
ob = data()
view = SimpleTestView(ob)
macro = view['test']
out = view(None) # Need to past a, uh, request.
self.assertEqual(out,
'<html>\n'
' <body>\n'
' <p>hello world</p>\n'
' </body>\n</html>\n')
def test_suite():
loader=unittest.TestLoader()
return loader.loadTestsFromTestCase(Test)
if __name__=='__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/testViewZPT.py ===
##############################################################################
#
# 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.
#
##############################################################################
import os, sys, unittest
from Zope.App.PageTemplate import ViewPageTemplateFile
from Zope.Testing.CleanUp import CleanUp
from Zope.ComponentArchitecture import getService
from Interface import Interface
from Zope.App.OFS.ServiceManager.tests.PlacefulSetup\
import PlacefulSetup
class I1(Interface):
pass
class C1:
__implements__ = I1
class InstanceWithContext:
def __init__(self, context):
self.context = context
def getContext(self):
return self.context
class InstanceWithoutContext:
pass
class TestViewZPT(PlacefulSetup, unittest.TestCase):
def setUp(self):
PlacefulSetup.setUp(self)
self.t = ViewPageTemplateFile('test.pt')
self.context = C1()
def checkNamespaceContextAvailable(self):
context = self.context
request = None
namespace = self.t.pt_getContext(InstanceWithContext(context), request)
self.failUnless(namespace['context'] is context)
self.failUnless(namespace.has_key('views'))
def checkNamespaceHereNotAvailable(self):
request = None
self.assertRaises(AttributeError, self.t.pt_getContext,
InstanceWithoutContext(), request)
def checkViewMapper(self):
the_view = "This is the view"
the_view_type = "some view type"
the_view_name = "some view name"
def ViewMaker(*args, **kw):
return the_view
getService(None,"Views").provideView(I1,
name=the_view_name,
type=the_view_type,
maker=ViewMaker)
from Zope.ComponentArchitecture.IViewService import IViewRequest
class MyRequest:
__implements__ = IViewRequest
def getViewType(self):
return the_view_type
def getViewSkin(self):
return "some skin"
request = MyRequest()
namespace = self.t.pt_getContext(InstanceWithContext(self.context),
request)
views = namespace['views']
self.failUnless(the_view is views[the_view_name])
def test_suite():
return unittest.makeSuite(TestViewZPT, 'check')
if __name__ == '__main__':
unittest.TextTestRunner().run(test_suite())
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/testZopePythonExpr.py ===
##############################################################################
#
# 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
#
##############################################################################
"""
Revision information:
$Id: testZopePythonExpr.py,v 1.1.2.1 2002/05/23 18:01:14 jim Exp $
"""
from unittest import TestCase, TestSuite, main, makeSuite
from Zope.Testing.CleanUp import CleanUp # Base class w registry cleanup
class Engine:
def getTypes(self):
return {}
class Context:
_engine = Engine()
def __init__(self, **kw):
self.vars = kw
class Test(CleanUp, TestCase):
def test(self):
from Zope.App.PageTemplate.Engine import ZopePythonExpr
from Zope.Exceptions import Forbidden
expr = ZopePythonExpr('python', 'max(a,b)', Engine())
self.assertEqual(expr(Context(a=1, b=2)), 2)
expr = ZopePythonExpr(
'python', '__import__("sys").__name__', Engine())
self.assertEqual(expr(Context()), 'sys')
expr = ZopePythonExpr('python', '__import__("sys").exit',
Engine())
self.assertRaises(Forbidden, expr, Context())
expr = ZopePythonExpr('python', 'open("x", "w")', Engine())
self.assertRaises(NameError, expr, Context())
def test_suite():
return TestSuite((
makeSuite(Test),
))
if __name__=='__main__':
main(defaultTest='test_suite')
=== Added File Zope3/lib/python/Zope/App/PageTemplate/tests/test_binding.py ===
##############################################################################
#
# 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.
#
##############################################################################
import unittest
from Zope.PageTemplate.tests import util
from Zope.App.PageTemplate.tests.testpackage.content \
import Content, PTComponent
# Wow, this is a lot of work. :(
from Zope.ComponentArchitecture.tests.PlacelessSetup import PlacelessSetup
from Zope.App.Traversing.Traverser import Traverser
from Zope.App.Traversing.ITraverser import ITraverser
from Zope.App.Traversing.DefaultTraversable import DefaultTraversable
from Zope.App.Traversing.ITraversable import ITraversable
from Zope.ComponentArchitecture.GlobalAdapterService import provideAdapter
class BindingTestCase(PlacelessSetup, unittest.TestCase):
def setUp(self):
PlacelessSetup.setUp(self)
provideAdapter(None, ITraverser, Traverser)
provideAdapter(None, ITraversable, DefaultTraversable)
def test_binding(self):
comp = PTComponent(Content())
self.assertEqual(comp.index(), "42\n")
def test_suite():
return unittest.makeSuite(BindingTestCase)
if __name__=='__main__':
unittest.main()