changeset 3947:7626f4760d9f

macro._tests: refactored duplicated code to common methods
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Sat, 16 Aug 2008 10:18:23 +0200
parents 54fffaea4e22
children 62b9f61eb526
files MoinMoin/_tests/__init__.py MoinMoin/macro/_tests/test_Action.py MoinMoin/macro/_tests/test_EmbedObject.py MoinMoin/macro/_tests/test_FootNote.py MoinMoin/macro/_tests/test_GetVal.py MoinMoin/macro/_tests/test_Hits.py MoinMoin/macro/_tests/test_PageHits.py MoinMoin/macro/_tests/test_StatsChart.py MoinMoin/macro/_tests/test_macro.py
diffstat 9 files changed, 55 insertions(+), 130 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/_tests/__init__.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/_tests/__init__.py	Sat Aug 16 10:18:23 2008 +0200
@@ -8,11 +8,13 @@
 """
 
 import os, shutil
-
+from MoinMoin.parser.text import Parser
+from MoinMoin.formatter.text_html import Formatter
 from MoinMoin.Page import Page
 from MoinMoin.PageEditor import PageEditor
 from MoinMoin.util import random_string
 from MoinMoin import caching, user
+
 # Promoting the test user -------------------------------------------
 # Usually the tests run as anonymous user, but for some stuff, you
 # need more privs...
@@ -82,6 +84,12 @@
     page.saveText(content, 0)
     return page
 
+def nuke_eventlog(request):
+    """ removes event-log file """
+    fpath = request.rootpage.getPagePath('event-log', isfile=1)
+    if os.path.exists(fpath):
+        os.remove(fpath)
+
 def nuke_page(request, pagename):
     """ completely delete a page, everything in the pagedir """
     page = PageEditor(request, pagename, do_editor_backup=False)
@@ -94,3 +102,17 @@
     """ creates a list of random strings """
     chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
     return [u"%s" % random_string(length, chars) for counter in range(count)]
+
+def make_macro(request, page):
+    """ creates the macro """
+    from MoinMoin import macro
+    p = Parser("##\n", request)
+    p.formatter = Formatter(request)
+    p.formatter.page = page
+    request.page = page
+    request.formatter = p.formatter
+    p.form = request.form
+    m = macro.Macro(p)
+    return m
+
+
--- a/MoinMoin/macro/_tests/test_Action.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/macro/_tests/test_Action.py	Sat Aug 16 10:18:23 2008 +0200
@@ -12,31 +12,19 @@
 from MoinMoin.Page import Page
 from MoinMoin.PageEditor import PageEditor
 
-from MoinMoin._tests import become_trusted, create_page, nuke_page
+from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_page
 
 class TestAction:
     """ testing macro Action calling action raw """
     pagename = u'AutoCreatedMoinMoinTemporaryTestPageForAction'
 
-    def _make_macro(self):
-        """Test helper"""
-        from MoinMoin.parser.text import Parser
-        from MoinMoin.formatter.text_html import Formatter
-        p = Parser("##\n", self.request)
-        p.formatter = Formatter(self.request)
-        p.formatter.page = self.page
-        self.request.formatter = p.formatter
-        p.form = self.request.form
-        m = macro.Macro(p)
-        return m
-
     def testActionCallingRaw(self):
         """ module_tested: executes raw by macro Action on existing page"""
         request = self.request
         become_trusted(request)
 
         self.page = create_page(request, self.pagename, u'= title1 =\n||A||B||\n')
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         result = Action.macro_Action(m, 'raw')
         nuke_page(request, self.pagename)
 
--- a/MoinMoin/macro/_tests/test_EmbedObject.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/macro/_tests/test_EmbedObject.py	Sat Aug 16 10:18:23 2008 +0200
@@ -13,7 +13,7 @@
 from MoinMoin import macro
 from MoinMoin.action import AttachFile
 
-from MoinMoin._tests import become_trusted, create_page, nuke_page
+from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_page
 
 class TestEmbedObject:
     """ testing macro Action calling action raw """
@@ -40,18 +40,6 @@
     def teardown_class(self):
         nuke_page(self.request, self.pagename)
 
-    def _make_macro(self):
-        """ Test helper """
-        from MoinMoin.parser.text import Parser
-        from MoinMoin.formatter.text_html import Formatter
-        p = Parser("##\n", self.request)
-        p.formatter = Formatter(self.request)
-        p.formatter.page = self.page
-        self.request.formatter = p.formatter
-        p.form = self.request.form
-        m = macro.Macro(p)
-        return m
-
     def testEmbedObjectMimetype(self):
         """ tests defined mimetyes """
         tests = [
@@ -61,13 +49,13 @@
             (u'test.mp3', 'audio/mpeg'),
         ]
         for filename, mimetype in tests:
-            m = self._make_macro()
+            m = make_macro(self.request, self.page)
             result = m.execute('EmbedObject', filename)
             assert mimetype in result
 
     def testEmbedObjectDefaultValues(self):
         """ tests default values of macro EmbedObject """
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         filename = 'test.mpg'
         result = m.execute('EmbedObject', u'%s' % filename)
         assert '<object data="./AutoCreatedMoinMoinTemporaryTestPageForEmbedObject?action=AttachFile&amp;do=get&amp;target=test.mpg"' in result
@@ -76,7 +64,7 @@
 
     def testEmbedObjectPercentHeight(self):
         """ tests a unit value for macro EmbedObject """
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         filename = 'test.mpg'
         height = '50 %' # also tests that space is allowed in there
         result = m.execute('EmbedObject', u'target=%s, height=%s' % (filename, height))
@@ -86,7 +74,7 @@
 
     def testEmbedObjectFromUrl(self):
         """ tests using a URL for macro EmbedObject """
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         target = 'http://localhost/%s?action=AttachFile&do=view&target=test.mpg' % self.pagename
         result = m.execute('EmbedObject', u'target=%s, url_mimetype=video/mpeg' % target)
         assert '<object data="http://localhost/AutoCreatedMoinMoinTemporaryTestPageForEmbedObject?action=AttachFile&amp;do=view&amp;target=test.mpg" type="video/mpeg"' in result
--- a/MoinMoin/macro/_tests/test_FootNote.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/macro/_tests/test_FootNote.py	Sat Aug 16 10:18:23 2008 +0200
@@ -12,7 +12,7 @@
 from MoinMoin.Page import Page
 from MoinMoin.PageEditor import PageEditor
 
-from MoinMoin._tests import become_trusted, create_page, nuke_page
+from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_page
 
 class TestFootNote:
     """ testing macro Action calling action raw """
@@ -25,21 +25,9 @@
     def teardown_class(self):
         nuke_page(self.request, self.pagename)
 
-    def _make_macro(self):
-        """Test helper"""
-        from MoinMoin.parser.text import Parser
-        from MoinMoin.formatter.text_html import Formatter
-        p = Parser("##\n", self.request)
-        p.formatter = Formatter(self.request)
-        p.formatter.page = self.page
-        self.request.formatter = p.formatter
-        p.form = self.request.form
-        m = macro.Macro(p)
-        return m
-
     def test_enumbering(self):
         """ module_tested: enumbering of Footnotes"""
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         text = 'a'
         FootNote.execute(m, text)
         text = 'b'
--- a/MoinMoin/macro/_tests/test_GetVal.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/macro/_tests/test_GetVal.py	Sat Aug 16 10:18:23 2008 +0200
@@ -12,7 +12,7 @@
 from MoinMoin.Page import Page
 from MoinMoin.PageEditor import PageEditor
 
-from MoinMoin._tests import become_trusted, create_page, nuke_page
+from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_page
 
 class TestGetVal:
     """GetVal: testing getVal macro """
@@ -25,43 +25,24 @@
     def teardown_class(self):
         nuke_page(self.request, self.pagename)
 
-    def _make_macro(self):
-        """Test helper"""
-        from MoinMoin.parser.text import Parser
-        from MoinMoin.formatter.text_html import Formatter
-        p = Parser("##\n", self.request)
-        p.formatter = Formatter(self.request)
-        p.formatter.page = self.page
-        self.request.formatter = p.formatter
-        p.form = self.request.form
-        m = macro.Macro(p)
-        return m
-
     def _test_macro(self, name, args):
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         return m.execute(name, args)
 
     def testGetValNoACLs(self):
         """ macro GetVal test: 'reads VAR' """
-
         self.page = create_page(self.request, self.pagename, u' VAR:: This is an example')
-
         result = self._test_macro(u'GetVal', "%s,%s" % (self.pagename, u'VAR'))
-
         assert result == "This is an example"
 
     def testGetValAfterADictPageIsDeleted(self):
         """ macro GetVal test: 'reads Dict var after another Dict is removed' """
         request = self.request
-
         page = create_page(request, u'SomeDict', u" EXAMPLE:: This is an example text")
         page.deletePage()
-
         page = create_page(request, self.pagename, u' VAR:: This is a brand new example')
         result = self._test_macro(u'GetVal', "%s,%s" % (self.pagename, u'VAR'))
-
         nuke_page(request, u'SomeDict')
-
         assert result == "This is a brand new example"
 
     def testGetValACLs(self):
--- a/MoinMoin/macro/_tests/test_Hits.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/macro/_tests/test_Hits.py	Sat Aug 16 10:18:23 2008 +0200
@@ -12,7 +12,7 @@
 from MoinMoin.PageEditor import PageEditor
 from MoinMoin.Page import Page
 
-from MoinMoin._tests import become_trusted, create_page, nuke_page
+from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_eventlog, nuke_page
 
 class TestHits:
     """Hits: testing Hits macro """
@@ -24,9 +24,7 @@
         self.page = create_page(request, self.pagename, u"Foo!")
 
         # for that test eventlog needs to be empty
-        fpath = request.rootpage.getPagePath('event-log', isfile=1)
-        if os.path.exists(fpath):
-            os.remove(fpath)
+        nuke_eventlog(request)
 
         # hits is based on hitcounts which reads the cache
         caching.CacheEntry(request, 'charts', 'hitcounts', scope='wiki').remove()
@@ -34,27 +32,13 @@
     def teardown_class(self):
         nuke_page(self.request, self.pagename)
 
-    def _make_macro(self):
-        """Test helper"""
-        from MoinMoin.parser.text import Parser
-        from MoinMoin.formatter.text_html import Formatter
-        p = Parser("##\n", self.request)
-        p.formatter = Formatter(self.request)
-        p.formatter.page = self.page
-        self.request.formatter = p.formatter
-        p.form = self.request.form
-        m = macro.Macro(p)
-        return m
-
     def _test_macro(self, name, args):
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         return m.execute(name, args)
 
     def _cleanStats(self):
         # cleans all involved cache and log files
-        fpath = self.request.rootpage.getPagePath('event-log', isfile=1)
-        if os.path.exists(fpath):
-            os.remove(fpath)
+        nuke_eventlog(self.request)
         # hits is based on hitcounts which reads the cache
         caching.CacheEntry(self.request, 'charts', 'hitcounts', scope='wiki').remove()
         arena = Page(self.request, self.pagename)
--- a/MoinMoin/macro/_tests/test_PageHits.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/macro/_tests/test_PageHits.py	Sat Aug 16 10:18:23 2008 +0200
@@ -13,7 +13,7 @@
 from MoinMoin.PageEditor import PageEditor
 from MoinMoin.Page import Page
 
-from MoinMoin._tests import become_trusted, create_page, nuke_page
+from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_eventlog, nuke_page
 
 class TestHits:
     """Hits: testing Hits macro """
@@ -25,9 +25,7 @@
         self.page = create_page(request, self.pagename, u"Foo!")
 
         # for that test eventlog needs to be empty
-        fpath = request.rootpage.getPagePath('event-log', isfile=1)
-        if os.path.exists(fpath):
-            os.remove(fpath)
+        nuke_eventlog(self.request)
 
         # hits is based on hitcounts which reads the cache
         caching.CacheEntry(request, 'charts', 'pagehits', scope='wiki').remove()
@@ -36,20 +34,8 @@
     def teardown_class(self):
         nuke_page(self.request, self.pagename)
 
-    def _make_macro(self):
-        """Test helper"""
-        from MoinMoin.parser.text import Parser
-        from MoinMoin.formatter.text_html import Formatter
-        p = Parser("##\n", self.request)
-        p.formatter = Formatter(self.request)
-        p.formatter.page = self.page
-        self.request.formatter = p.formatter
-        p.form = self.request.form
-        m = macro.Macro(p)
-        return m
-
     def _test_macro(self, name, args):
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         return m.execute(name, args)
 
     def testPageHits(self):
--- a/MoinMoin/macro/_tests/test_StatsChart.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/macro/_tests/test_StatsChart.py	Sat Aug 16 10:18:23 2008 +0200
@@ -12,7 +12,7 @@
 from MoinMoin.logfile import eventlog
 from MoinMoin.PageEditor import PageEditor
 from MoinMoin.Page import Page
-from MoinMoin._tests import become_trusted, create_page, nuke_page
+from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_page
 
 class TestStatsCharts:
     """StartsChart: testing StatsChart macro """
@@ -28,21 +28,8 @@
     def teardown_class(self):
         nuke_page(self.request, self.pagename)
 
-    def _make_macro(self):
-        """Test helper"""
-        from MoinMoin.parser.text import Parser
-        from MoinMoin.formatter.text_html import Formatter
-        p = Parser("##\n", self.request)
-        p.formatter = Formatter(self.request)
-        p.formatter.page = self.page
-        self.request.page = self.page
-        self.request.formatter = p.formatter
-        p.form = self.request.form
-        m = macro.Macro(p)
-        return m
-
     def _test_macro(self, name, args):
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         return m.execute(name, args)
 
     def testStatsChart_useragents(self):
--- a/MoinMoin/macro/_tests/test_macro.py	Fri Aug 15 23:21:10 2008 +0200
+++ b/MoinMoin/macro/_tests/test_macro.py	Sat Aug 16 10:18:23 2008 +0200
@@ -10,25 +10,26 @@
 from MoinMoin import macro
 from MoinMoin.parser.text import Parser
 from MoinMoin.formatter.text_html import Formatter
-
+from MoinMoin._tests import become_trusted, create_page, make_macro, nuke_page
 
 class TestMacro:
+    pagename = u'AutoCreatedMoinMoinTemporaryTestPageForTestMacro'
+
+    def setup_class(self):
+        request = self.request
+        become_trusted(request)
+        self.page = create_page(request, self.pagename, u"Foo!")
+
+    def teardown_class(self):
+        nuke_page(self.request, self.pagename)
+
     def testTrivialMacro(self):
         """macro: trivial macro works"""
-        m = self._make_macro()
+        m = make_macro(self.request, self.page)
         expected = m.formatter.linebreak(0)
         result = m.execute("BR", "")
         assert result == expected
 
-    def _make_macro(self):
-        """Test helper"""
-        p = Parser("##\n", self.request)
-        p.formatter = Formatter(self.request)
-        self.request.formatter = p.formatter
-        p.form = self.request.form
-        m = macro.Macro(p)
-        return m
-
 
 coverage_modules = ['MoinMoin.macro']