[Checkins] SVN: five.grok/trunk/src/five/grok/ add new README te(s/x)t; add a way to handle grok directive and doctest code within one docfilesuite; fix i18n warning

Jean-Fran�ois Roche jfroche at jfroche.be
Fri Jul 18 12:30:31 EDT 2008


Log message for revision 88531:
  add new README te(s/x)t; add a way to handle grok directive and doctest code within one docfilesuite; fix i18n warning

Changed:
  U   five.grok/trunk/src/five/grok/README.txt
  U   five.grok/trunk/src/five/grok/configure.zcml
  U   five.grok/trunk/src/five/grok/ftests/test_grok_functional.py
  U   five.grok/trunk/src/five/grok/testing.py
  A   five.grok/trunk/src/five/grok/tests/all/
  A   five.grok/trunk/src/five/grok/tests/all/__init__.py
  A   five.grok/trunk/src/five/grok/tests/all/all_test_templates/
  A   five.grok/trunk/src/five/grok/tests/all/all_test_templates/grokvillageview.pt
  A   five.grok/trunk/src/five/grok/tests/doctest.py
  U   five.grok/trunk/src/five/grok/tests/test_all.py

-=-
Modified: five.grok/trunk/src/five/grok/README.txt
===================================================================
--- five.grok/trunk/src/five/grok/README.txt	2008-07-18 16:26:21 UTC (rev 88530)
+++ five.grok/trunk/src/five/grok/README.txt	2008-07-18 16:30:30 UTC (rev 88531)
@@ -1,14 +1,211 @@
-five.grok Package Readme
-=========================
+five.grok
+=========
 
 Overview
 --------
 
+This package is meant to provide all the grok functionalities into Zope 2.
 
+How-to
+------
 
+This readme try to explain you how to use grok in Zope 2.
 
-Your tests here
----------------
+    <<< from five import grok
+    <<< from OFS.ObjectManager import ObjectManager
+    <<< from OFS.SimpleItem import Item
 
-    >>> 1 + 1
-    3
+    <<< class SimpleFolder(ObjectManager, Item):
+    ...     def __init__(self, id=None):
+    ...         if id is not None:
+    ...             self.id = str(id)
+
+    <<< class GrokVillage(SimpleFolder):
+    ...
+    ...     def addCave(self, id):
+    ...         cave = Cave(id)
+    ...         self._setObject(id, cave)
+    ...         return cave
+
+    <<< grok.templatedir('tests/all/all_test_templates')
+    <<< class GrokVillageView(grok.View):
+    ...     grok.context(GrokVillage)
+    ...
+    ...     def getCaves(self):
+    ...         cavesInfos = []
+    ...         for cave in self.context.objectValues():
+    ...             caveInfo = dict(id=cave.id, caveWomen=cave.numberOfCaveWomen())
+    ...             cavesInfos.append(caveInfo)
+    ...         return cavesInfos
+
+    <<< class Cave(SimpleFolder):
+    ...
+    ...     def numberOfCaveWomen(self):
+    ...         return len(self.objectIds())
+
+    <<< class CaveView(grok.View):
+    ...     grok.context(Cave)
+    ...
+    ...     def render(self):
+    ...         return 'This is the %s cave, there is %s cavewomen in this cave.' %\
+    ...                                 (self.context.id,
+    ...                                  self.context.numberOfCaveWomen())
+
+    <<< class AddCaveWoman(grok.View):
+    ...     grok.context(Cave)
+    ...     grok.name(u'cave-woman-add')
+    ...
+    ...     def render(self):
+    ...         return 'Add a Cave Woman ...'
+    ...
+    ...     def update(self, id=None, name=None,
+    ...                age=None, hairType=None, size=0,
+    ...                weight=0):
+    ...         self.context._setObject(id, CaveWoman(name, age, hairType,
+    ...                                               size, weight))
+
+    <<< class CaveWoman(grok.Model):
+    ...
+    ...     def __init__(self, name, age, hairType, size,
+    ...                  weight):
+    ...         self.name = name
+    ...         self.age = age
+    ...         self.hairType = hairType
+    ...         self.size = size
+    ...         self.weight = weight
+
+    <<< from zope.interface import Interface
+
+    <<< class ICaveWomanSummarizer(Interface):
+    ...
+    ...     def info():
+    ...         """
+    ...         return filtered informations
+    ...         """
+
+    <<< class CaveWomanFaceBookProfile(grok.Adapter):
+    ...     grok.context(CaveWoman)
+    ...     grok.provides(ICaveWomanSummarizer)
+    ...
+    ...     def info(self):
+    ...         return {'hair': self.context.hairType,
+    ...                 'weight': self.context.weight,
+    ...                 'size': self.context.size}
+
+    <<< from zope.app.container.interfaces import IObjectAddedEvent
+    <<< from zope.component import getUtility
+
+    <<< class ICaveInformations(Interface):
+    ...
+    ...     def getHairStatistics(cave):
+    ...         """
+    ...         return the statistics about the cavewoman's hair in the cave
+    ...         """
+
+    <<< class CaveInformations(grok.GlobalUtility):
+    ...     grok.implements(ICaveInformations)
+    ...     grok.provides(ICaveInformations)
+    ...
+    ...     def getHairStatistics(self, cave):
+    ...         browns = 0
+    ...         blonds = 0
+    ...         for caveWoman in cave.objectValues():
+    ...             if caveWoman.hairType == 'blond':
+    ...                 blonds += 1
+    ...             elif caveWoman.hairType == 'brown':
+    ...                 browns += 1
+    ...         return blonds, browns
+
+    <<< from Acquisition import aq_parent
+    <<< @grok.subscribe(CaveWoman, IObjectAddedEvent)
+    ... def handle(obj, event):
+    ...     profile = ICaveWomanSummarizer(obj)
+    ...     caveInfos = getUtility(ICaveInformations)
+    ...     village = aq_parent(obj)
+    ...     nbrOfBlond, nbrOfBrown = caveInfos.getHairStatistics(village)
+    ...     if nbrOfBlond >= nbrOfBrown:
+    ...         obj.hairType = 'brown'
+    ...     else:
+    ...         obj.hairType = 'blond'
+    ...     print """Hey caveman there is a new cavewoman in the cave, here
+    ... are the most important informations about her:
+    ...  * Hair Type: %(hair)s
+    ...  * Weight: %(weight)s
+    ...  * Size: %(size)s""" % profile.info()
+
+    >>> from zope.component import getUtility
+    >>> from five.grok.README import *
+    >>> from Testing.ZopeTestCase import ZopeLite as Zope2
+    >>> app= Zope2.app()
+    >>> #grok.testing.grok(__name__)
+    >>> from zope.publisher.browser import TestRequest
+    >>> from OFS.Folder import Folder
+    >>> request = TestRequest()
+    >>> village = GrokVillage(id='amsterdam')
+    >>> app._setObject('amsterdam', village)
+    'amsterdam'
+    >>> from zope.component import queryMultiAdapter
+
+    >>> martijnCave = village.addCave('martijn-cave')
+    >>> belgianCave = village.addCave('belgian-cave')
+    >>> queryMultiAdapter((martijnCave, request), name='caveview')()
+    'This is the martijn-cave cave, there is 0 cavewomen in this cave.'
+
+    >>> queryMultiAdapter((belgianCave, request), name='caveview')()
+    'This is the belgian-cave cave, there is 0 cavewomen in this cave.'
+
+    >>> addView = queryMultiAdapter((belgianCave, request),
+    ...                             name=u'cave-woman-add')
+
+    >>> addView.update(id='emma', name='Emma', age=22,
+    ...                size=160, weight=47)
+    Hey caveman there is a new cavewoman in the cave, here
+    are the most important informations about her:
+      * Hair Type: brown
+      * Weight: 47
+      * Size: 160
+
+    >>> addView.update(id='carla', name='Carla', age=23,
+    ...                size=160, weight=47)
+    Hey caveman there is a new cavewoman in the cave, here
+    are the most important informations about her:
+      * Hair Type: blond
+      * Weight: 47
+      * Size: 160
+
+    >>> addView.update(id='layla', name='Layla', age=24,
+    ...                size=164, weight=56)
+    Hey caveman there is a new cavewoman in the cave, here
+    are the most important informations about her:
+      * Hair Type: brown
+      * Weight: 56
+      * Size: 164
+
+    >>> addView = queryMultiAdapter((martijnCave, request),
+    ...                             name=u'cave-woman-add')
+
+    >>> addView.update(id='betty', name='Betty', age=52,
+    ...                size=160, weight=90)
+    Hey caveman there is a new cavewoman in the cave, here
+    are the most important informations about her:
+      * Hair Type: brown
+      * Weight: 90
+      * Size: 160
+
+    >>> queryMultiAdapter((martijnCave, request), name='caveview')()
+    'This is the martijn-cave cave, there is 1 cavewomen in this cave.'
+
+    >>> queryMultiAdapter((belgianCave, request), name='caveview')()
+    'This is the belgian-cave cave, there is 3 cavewomen in this cave.'
+    >>> print queryMultiAdapter((village, request), name='grokvillageview')()
+    <html>
+    <body>
+    <div>
+    In cave martijn-cave there is 1 cavewomen.
+    </div>
+    <div>
+    In cave belgian-cave there is 3 cavewomen.
+    </div>
+    </body>
+    </html>
+    <BLANKLINE>

Modified: five.grok/trunk/src/five/grok/configure.zcml
===================================================================
--- five.grok/trunk/src/five/grok/configure.zcml	2008-07-18 16:26:21 UTC (rev 88530)
+++ five.grok/trunk/src/five/grok/configure.zcml	2008-07-18 16:30:30 UTC (rev 88531)
@@ -2,7 +2,8 @@
     xmlns="http://namespaces.zope.org/zope"
     xmlns:meta="http://namespaces.zope.org/meta"
     xmlns:browser="http://namespaces.zope.org/browser"
-    xmlns:grok="http://namespaces.zope.org/grok">
+    xmlns:grok="http://namespaces.zope.org/grok"
+    i18n_domain="five.grok">
 
   <grok:grok package=".meta" />
 

Modified: five.grok/trunk/src/five/grok/ftests/test_grok_functional.py
===================================================================
--- five.grok/trunk/src/five/grok/ftests/test_grok_functional.py	2008-07-18 16:26:21 UTC (rev 88530)
+++ five.grok/trunk/src/five/grok/ftests/test_grok_functional.py	2008-07-18 16:30:30 UTC (rev 88531)
@@ -1,10 +1,7 @@
-import re
 import unittest
-from five import grok
-import os.path
 
 from pkg_resources import resource_listdir
-from zope.testing import doctest, renormalizing
+from zope.testing import doctest
 from zope.app.testing.functional import HTTPCaller
 from five.grok.testing import GrokFunctionalLayer
 from Testing.ZopeTestCase.zopedoctest.functional import getRootFolder, sync
@@ -13,6 +10,7 @@
 from Testing.ZopeTestCase import installProduct
 installProduct('PageTemplates')
 
+
 def http_call(method, path, data=None, **kw):
     """Function to help make RESTful calls.
 
@@ -21,7 +19,7 @@
     data - (body) data to submit
     kw - any request parameters
     """
-    
+
     if path.startswith('http://localhost'):
         path = path[len('http://localhost'):]
     request_string = '%s %s HTTP/1.1\n' % (method, path)
@@ -32,6 +30,7 @@
         request_string += data
     return HTTPCaller()(request_string, handle_errors=False)
 
+
 def suiteFromPackage(name):
     files = resource_listdir(__name__, name)
     suite = unittest.TestSuite()
@@ -50,16 +49,16 @@
                             sync=sync),
             optionflags=(doctest.ELLIPSIS+
                          doctest.NORMALIZE_WHITESPACE+
-                         doctest.REPORT_NDIFF)
-            )
+                         doctest.REPORT_NDIFF))
         test.layer = GrokFunctionalLayer
 
         suite.addTest(test)
     return suite
 
+
 def test_suite():
     suite = unittest.TestSuite()
-    for name in ['view',]:
+    for name in ['view']:
         suite.addTest(suiteFromPackage(name))
     return suite
 

Modified: five.grok/trunk/src/five/grok/testing.py
===================================================================
--- five.grok/trunk/src/five/grok/testing.py	2008-07-18 16:26:21 UTC (rev 88530)
+++ five.grok/trunk/src/five/grok/testing.py	2008-07-18 16:30:30 UTC (rev 88531)
@@ -22,6 +22,8 @@
 def grok(module_name):
     config = ConfigurationMachine()
     zcml.do_grok('grokcore.component.meta', config)
+    zcml.do_grok('grokcore.view.meta', config)
+    zcml.do_grok('grokcore.view.templatereg', config)
     zcml.do_grok('five.grok.meta', config)
     zcml.do_grok(module_name, config)
     config.execute_actions()
@@ -116,4 +118,4 @@
 GrokFunctionalLayer = Layer
 
 
- 
\ No newline at end of file
+ 

Added: five.grok/trunk/src/five/grok/tests/all/__init__.py
===================================================================


Property changes on: five.grok/trunk/src/five/grok/tests/all/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id

Added: five.grok/trunk/src/five/grok/tests/all/all_test_templates/grokvillageview.pt
===================================================================
--- five.grok/trunk/src/five/grok/tests/all/all_test_templates/grokvillageview.pt	                        (rev 0)
+++ five.grok/trunk/src/five/grok/tests/all/all_test_templates/grokvillageview.pt	2008-07-18 16:30:30 UTC (rev 88531)
@@ -0,0 +1,7 @@
+<html>
+<body>
+<div tal:repeat="cave context/getCaves">
+In cave <span tal:replace="cave/id"/> there is <span tal:replace="cave/caveWomen"/> cavewomen.
+</div>
+</body>
+</html>

Added: five.grok/trunk/src/five/grok/tests/doctest.py
===================================================================
--- five.grok/trunk/src/five/grok/tests/doctest.py	                        (rev 0)
+++ five.grok/trunk/src/five/grok/tests/doctest.py	2008-07-18 16:30:30 UTC (rev 88531)
@@ -0,0 +1,43 @@
+# -*- coding: utf-8 -*-
+"""
+five.grok
+
+$Id$
+"""
+import re
+import os.path
+
+
+def doctestToPython(filenameInput, filenameOutput):
+    assert os.path.exists(filenameInput)
+    docFileR = open(filenameInput, 'r')
+    newLines = []
+    originalLines = []
+    for line in docFileR.readlines():
+        originalLines.append(line)
+        if '<<<' in line:
+            match = re.match(re.compile('(\s+<<<\s)(.*)'), line)
+            if match:
+                grokCodeFlag = True
+                newLines.append("%s\n" % match.groups()[1])
+        elif '...' in line and grokCodeFlag == True:
+            match = re.match(re.compile('(\s+\.\.\.\s)(.*)'), line)
+            if match:
+                newLines.append("%s\n" % match.groups()[1])
+        elif '<<<' not in line or '...' not in line: # handle comments
+            grokCodeFlag = False
+            newLines.append('#%s' % line)
+
+    docFileR.close()
+
+    docFileW = open(filenameOutput, 'w')
+    for newLine in newLines:
+        if newLine.strip() != '#':
+            docFileW.write('%s' % newLine)
+        else:
+            docFileW.write('\n')
+    docFileW.close()
+
+
+if __name__ == '__main__':
+    doctestToPython('../README.txt', '../README.txt.out')


Property changes on: five.grok/trunk/src/five/grok/tests/doctest.py
___________________________________________________________________
Name: svn:keywords
   + Id

Modified: five.grok/trunk/src/five/grok/tests/test_all.py
===================================================================
--- five.grok/trunk/src/five/grok/tests/test_all.py	2008-07-18 16:26:21 UTC (rev 88530)
+++ five.grok/trunk/src/five/grok/tests/test_all.py	2008-07-18 16:30:30 UTC (rev 88531)
@@ -4,13 +4,14 @@
 from zope.testing import doctestunit, doctest
 from zope.component import testing
 
-from Testing import ZopeTestCase
-
 import Products.Five
 from Products.Five import zcml
 
 import five.grok
+from five.grok.tests.doctest import doctestToPython
+import os
 
+
 def setUp(test=None):
     testing.setUp(test)
     zcml.load_config('meta.zcml', package=Products.Five)
@@ -19,7 +20,36 @@
     zcml.load_config('configure.zcml', package=five.grok)
     zcml.load_config('configure.zcml', package=five.grok.tests)
 
-    
+from five.grok.testing import grok
+from zope import component
+
+
+def setUpWithGrokDoctest(test=None):
+    testing.setUp(test)
+    testFile = test.globs.get('__file__')
+    testFileDirName, testFullFileName = os.path.split(testFile)
+    testFileName, testFileExt = os.path.splitext(testFullFileName)
+    pythonTestFile = os.path.join(testFileDirName, testFileName + '.py')
+    doctestToPython(testFile, pythonTestFile)
+    component.eventtesting.setUp()
+    #XXX this should be done by the GrokDocFileSuite
+    grok('five.grok.README')
+    from zope.traversing.adapters import DefaultTraversable
+    component.provideAdapter(DefaultTraversable, [None])
+
+
+def tearDownWithGrokDoctest(test=None):
+    testing.tearDown(test)
+    testFile = test.globs.get('__file__')
+    testFileDirName, testFullFileName = os.path.split(testFile)
+    testFileName, testFileExt = os.path.splitext(testFullFileName)
+    pythonTestFile = os.path.join(testFileDirName, testFileName + '.py')
+    os.remove(pythonTestFile)
+    pythonPycTestFile = os.path.join(testFileDirName, testFileName + '.pyc')
+    if os.path.exists(pythonPycTestFile):
+        os.remove(pythonPycTestFile)
+
+
 def suiteFromPackage(name):
     files = resource_listdir(__name__, name)
     suite = unittest.TestSuite()
@@ -34,16 +64,24 @@
         dottedname = 'five.grok.tests.%s.%s' % (name, filename[:-3])
         test = doctest.DocTestSuite(dottedname,
                                     setUp=setUp,
-                                    tearDown=testing.tearDown, 
+                                    tearDown=testing.tearDown,
                                     optionflags=doctest.ELLIPSIS+
                                                 doctest.NORMALIZE_WHITESPACE)
 
         suite.addTest(test)
     return suite
-    
+
+
 def test_suite():
     return unittest.TestSuite([
 
+        # Unit tests for your API
+        doctestunit.DocFileSuite(
+            'README.txt', package='five.grok',
+            setUp=setUpWithGrokDoctest, tearDown=tearDownWithGrokDoctest,
+            optionflags=doctest.ELLIPSIS+
+                        doctest.NORMALIZE_WHITESPACE),
+
         doctestunit.DocTestSuite(
             module='five.grok.tests.adapters',
             setUp=setUp, tearDown=testing.tearDown),
@@ -59,9 +97,9 @@
         doctestunit.DocTestSuite(
             module='five.grok.tests.subscribers',
             setUp=setUp, tearDown=testing.tearDown),
-        
+
         ])
-        
-        
+
+
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')



More information about the Checkins mailing list