David Chambers avatar David Chambers committed d766195

Removed template from `mango.tests` to improve readability and flexibility.

Comments (0)

Files changed (1)

tests/__init__.py

 
 from django.core import mail
 from django.core.urlresolvers import reverse
-from django.template import Context, Template
 from django.test import TestCase
 from django.test.client import Client
 
 from mango.templatetags.mango import month
 from mango.utils import primary_author_email
 
-document_template = Template(
-'''{% load mango %}{% autoescape off %}{% if meta == None %}date: 29 September 2010
-time: 1:30am
-zone: Pacific/Auckland
-tags: Markdown, Django, Mango
-{% else %}{% for k, v in meta %}{{ k }}: {{ v }}
-{% endfor %}{% endif %}
-{% if title == None %}
-Introducing Mango
-=================
-{% else %}
-{{ title }}
-===========
-{% endif %}
-{% firstof body 'Mango attempts to make blogging fun again.' %}
-{% endautoescape %}
-''')
 
-def create_document(filename='test.text', context=None, body=None):
-    if context is None:
-        context = {}
-    if body is not None:
-        context['body'] = '\n'.join([l.lstrip() for l in body.splitlines()])
+def create_document(source='', filename='test.text'):
+    source = '\n'.join((l.lstrip() for l in source.strip().splitlines()))
     filepath = os.path.join(mango.settings.MANGO_PATH, 'examples', filename)
     with open(filepath, 'w') as f:
-        f.write(document_template.render(Context(context)).encode('utf-8'))
+        f.write(source.encode('utf-8'))
     document = Document(filepath)
     os.remove(filepath)
     return document
 
-def md(body):
-    return lambda fn: lambda self: fn(self, create_document(body=body))
+def md(source):
+    return lambda fn: lambda self: fn(self, create_document(source))
 
 
 class DocumentTests(unittest.TestCase):
         self.assertEqual(mail.outbox[0].subject, subject)
         self.assertEqual(mail.outbox[0].body, message)
         self.assertEqual(len(mail.outbox[0].alternatives), 1)
-        self.assertEqual(len(mail.outbox[0].alternatives[0]), 2)
-        self.assertEqual(mail.outbox[0].alternatives[0][0], '<p><strong>Hello!</strong></p>')
-        self.assertEqual(mail.outbox[0].alternatives[0][1], 'text/html')
+        self.assertEqual(list(mail.outbox[0].alternatives[0]),
+                         ['<p><strong>Hello!</strong></p>', 'text/html'])
 
         data['cc_sender'] = 'on'
         client.post(path, data)
 
 class EmbedTests(unittest.TestCase):
 
-    def test_github(self):
-        text = '[First ever gist](https://gist.github.com/1)'
-        html = '<script src="https://gist.github.com/1.js"></script>\n'
-        self.assertEqual(create_document(context={'body': text}).html, html)
+    @md('''
+        # First ever gist
+        
+        [First ever gist](https://gist.github.com/1)
+        ''')
+    def test_github(self, document):
+        self.assertEqual(document.html,
+                         '<script src="https://gist.github.com/1.js">'
+                         '</script>\n')
 
-    def test_vimeo(self):
-        text = '[Getting truth out of the DOM](http://vimeo.com/22687694)'
-        html = ('<iframe class="vimeo video" type="text/html" src='
-                '"http://player.vimeo.com/video/22687694"></iframe>\n')
-        self.assertEqual(create_document(context={'body': text}).html, html)
+    @md('''
+        # Getting truth out of the DOM
+        
+        [Getting truth out of the DOM](http://vimeo.com/22687694)
+        ''')
+    def test_vimeo(self, document):
+        self.assertEqual(document.html,
+                         '<iframe class="vimeo video" type="text/html" '
+                         'src="http://player.vimeo.com/video/22687694">'
+                         '</iframe>\n')
 
-    def test_youtube(self):
-        text = '[Soda Pop Stop](http://www.youtube.com/watch?v=gPbh6Ru7VVM)'
-        html = ('<iframe class="youtube video" type="text/html" src='
-                '"http://www.youtube.com/embed/gPbh6Ru7VVM?rel=0"></iframe>\n')
-        self.assertEqual(create_document(context={'body': text}).html, html)
+    @md('''
+        # Soda Pop Stop
+        
+        [Soda Pop Stop](http://www.youtube.com/watch?v=gPbh6Ru7VVM)
+        ''')
+    def test_youtube(self, document):
+        self.assertEqual(document.html,
+                         '<iframe class="youtube video" type="text/html" '
+                         'src="http://www.youtube.com/embed/gPbh6Ru7VVM?rel=0"'
+                         '></iframe>\n')
 
 
 class ExcerptTests(unittest.TestCase):
 
     def setUp(self):
         self.mount_point = reverse('mango.views.index')
-        mango.settings.UNIX_DOCUMENTS_PATH = os.path.join(mango.settings.MANGO_PATH, 'examples')
+        path = os.path.join(mango.settings.MANGO_PATH, 'examples')
+        mango.settings.UNIX_DOCUMENTS_PATH = path
 
     def test_separator(self):
-        urls = create_document('1=test.text').urls
+        urls = create_document(filename='1=test.text').urls
         self.assertEqual((urls['canon']['rel'], urls['short']['rel']),
                 ('%stest/' % self.mount_point, '%s1/' % self.mount_point))
 
     def test_padded_numeric_separator(self):
-        urls = create_document('01=test.text').urls
+        urls = create_document(filename='01=test.text').urls
         self.assertEqual(urls['short']['rel'], '%s1/' % self.mount_point)
 
     def test_equals_sign_in_slug(self):
-        urls = create_document('1=mangoes=tasty.text').urls
-        self.assertEqual(urls['canon']['rel'], '%smangoes=tasty/' % self.mount_point)
+        urls = create_document(filename='1=mangoes=tasty.text').urls
+        self.assertEqual(urls['canon']['rel'],
+                         '%smangoes=tasty/' % self.mount_point)
 
     def test_escapement(self):
-        urls = create_document('=2+2=5.text').urls
+        urls = create_document(filename='=2+2=5.text').urls
         self.assertEqual(urls['canon']['rel'], '%s2+2=5/' % self.mount_point)
 
     def test_no_extension(self):
-        urls = create_document('test').urls
+        urls = create_document(filename='test').urls
         self.assertEqual(urls['canon']['rel'], '%stest/' % self.mount_point)
 
     def test_multiple_full_stops(self):
-        urls = create_document('mango-0.5-released.text').urls
-        self.assertEqual(urls['canon']['rel'], '%smango-0.5-released/' % self.mount_point)
+        urls = create_document(filename='mango-0.5-released.text').urls
+        self.assertEqual(urls['canon']['rel'],
+                         '%smango-0.5-released/' % self.mount_point)
 
 
 class RelatedDocumentsTests(unittest.TestCase):
 
 class TimeZoneTests(unittest.TestCase):
 
-    def test_converts_to_utc(self):
-        self.assertEqual(create_document().datetime.tzinfo, pytz.utc)
+    @md('''
+        date: 29 September 2010
+        time: 1:30am
+        zone: Pacific/Auckland
+        ''')
+    def test_converts_to_utc(self, document):
+        self.assertEqual(document.datetime.tzinfo, pytz.utc)
 
-    def test_date(self):
-        self.assertEqual(create_document().datetime.day, 28)
+    @md('''
+        date: 29 September 2010
+        time: 1:30am
+        zone: Pacific/Auckland
+        ''')
+    def test_date(self, document):
+        self.assertEqual(document.datetime.day, 28)
 
-    def test_hour(self):
-        self.assertEqual(create_document().datetime.hour, 12)
+    @md('''
+        date: 29 September 2010
+        time: 1:30am
+        zone: Pacific/Auckland
+        ''')
+    def test_hour(self, document):
+        self.assertEqual(document.datetime.hour, 12)
 
 
 class MetaListsTests(unittest.TestCase):
 
     def test_in_list(self):
         mango.settings.META_LISTS = ('tags',)
-        document = create_document()
-
-        self.assertEqual(len(document.meta['tags']), 3)
-        self.assertEqual(document.meta['tags'][0], 'Markdown')
-        self.assertEqual(document.meta['tags'][1], 'Django')
-        self.assertEqual(document.meta['tags'][2], 'Mango')
-
-        self.assertEqual(len(document.tags()), 3)
-        self.assertEqual(unicode(document.tags()[0]), 'Django')
-        self.assertEqual(unicode(document.tags()[1]), 'Mango')
-        self.assertEqual(unicode(document.tags()[2]), 'Markdown')
+        document = create_document('tags: Markdown, Django, Mango')
+        self.assertEqual(list(document.meta['tags']),
+                         ['Markdown', 'Django', 'Mango'])
+        self.assertEqual([unicode(tag) for tag in document.tags()],
+                         ['Django', 'Mango', 'Markdown'])
 
     def test_not_in_list(self):
         mango.settings.META_LISTS = ()
-        document = create_document()
+        document = create_document('tags: Markdown, Django, Mango')
         self.assertEqual(document.meta['tags'], 'Markdown, Django, Mango')
         self.assertEqual(document.tags(), None)
 
     def test_irregular_spacing(self):
         mango.settings.META_LISTS = ('tags',)
-        document = create_document(context={'meta': [('tags', ' Linux,Mac OS X , Windows ')]})
-        self.assertEqual(len(document.meta['tags']), 3)
-        self.assertEqual(document.meta['tags'][0], 'Linux')
-        self.assertEqual(document.meta['tags'][1], 'Mac OS X')
-        self.assertEqual(document.meta['tags'][2], 'Windows')
+        document = create_document('tags:  Linux,Mac OS X , Windows ')
+        self.assertEqual(list(document.meta['tags']),
+                         ['Linux', 'Mac OS X', 'Windows'])
 
 
 class TemplateFilterTests(unittest.TestCase):
 
     def test_backticks(self):
         title = 'Simulating `nonlocal` in Python 2.x'
-        document = create_document(context={'title': title})
-        self.assertEqual(document.title, 'Simulating <code>nonlocal</code> in Python 2.x')
+        document = create_document('# %s' % title)
+        self.assertEqual(document.title,
+                         'Simulating <code>nonlocal</code> in Python 2.x')
         self.assertEqual(document.title_text, title)
 
     def test_html_chars(self):
         title = '1 > 0 && 1 < 2'
-        document = create_document(context={'title': title})
+        document = create_document('# %s' % title)
         self.assertEqual(document.title, '1 &gt; 0 &amp;&amp; 1 &lt; 2')
         self.assertEqual(document.title_text, title)
 
     def test_meta_title(self):
         title = 'Hello, world!'
-        document = create_document(context={'meta': [('title', title)]})
+        document = create_document('title: %s' % title)
         self.assertEqual(document.title, title)
         self.assertEqual(document.title_text, title)
 
     def test_meta_title_with_backticks(self):
         title = 'Simulating `nonlocal` in Python 2.x'
-        document = create_document(context={'meta': [('title', title)]})
+        document = create_document('title: %s' % title)
         self.assertEqual(document.title, title)
         self.assertEqual(document.title_text, title)
 
     def test_meta_title_with_html_chars(self):
         title = '1 > 0 && 1 < 2'
-        document = create_document(context={'meta': [('title', title)]})
+        document = create_document('title: %s' % title)
         self.assertEqual(document.title, '1 &gt; 0 &amp;&amp; 1 &lt; 2')
         self.assertEqual(document.title_text, title)
 
         self.assertEqual(document.html, u'<p>Text.</p>\n<p>More text.</p>')
 
 
-# This code is taken from http://stackoverflow.com/questions/1615406/configure-django-to-find-all-doctests-in-all-modules
+# This code is taken from http://stackoverflow.com/questions/1615406
 
 # Django already runs these, don't include them again
 ALREADY_RUN = ('models.py',)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.