Commits

Rafael Martins committed 1fc9179

added some tests for rst directives

Comments (0)

Files changed (5)

blohg/rst_parser/directives.py

 
         .. math::
 
-            \frac{x^2}{1+x}
+            \\frac{x^2}{1+x}
     """
 
     required_arguments = 0
         return Image.run(self)
 
 
+
 class AttachmentImage(Image):
 
     def run(self):

blohg/tests/__init__.py

 from blohg.tests.app import AppTestCase
 from blohg.tests.ext import BlohgBlueprintTestCase, BlohgExtensionTestCase, \
      ExtensionImporterTestCase
+from blohg.tests.rst_parser.directives import VimeoTestCase, YoutubeTestCase, \
+     SourceCodeTestCase, MathTestCase
 from blohg.tests.vcs_backends.git import GitRepositoryTestCase
 from blohg.tests.vcs_backends.git.changectx import ChangeCtxDefaultTestCase \
      as GitChangeCtxDefaultTestCase, ChangeCtxWorkingDirTestCase as \
     suite.addTest(unittest.makeSuite(BlohgBlueprintTestCase))
     suite.addTest(unittest.makeSuite(BlohgExtensionTestCase))
     suite.addTest(unittest.makeSuite(ExtensionImporterTestCase))
+    suite.addTest(unittest.makeSuite(VimeoTestCase))
+    suite.addTest(unittest.makeSuite(YoutubeTestCase))
+    suite.addTest(unittest.makeSuite(SourceCodeTestCase))
+    suite.addTest(unittest.makeSuite(MathTestCase))
     suite.addTest(unittest.makeSuite(GitRepositoryTestCase))
     suite.addTest(unittest.makeSuite(GitChangeCtxDefaultTestCase))
     suite.addTest(unittest.makeSuite(GitChangeCtxWorkingDirTestCase))

blohg/tests/rst_parser/__init__.py

+# -*- coding: utf-8 -*-
+"""
+    blohg.tests.rst_parser
+    ~~~~~~~~~~~~~~~~~~~~~~
+
+    Package with tests for blohg integration with docutils.
+
+    :copyright: (c) 2010-2013 by Rafael Goncalves Martins
+    :license: GPL-2, see LICENSE for more details.
+"""

blohg/tests/rst_parser/directives.py

+# -*- coding: utf-8 -*-
+"""
+    blohg.tests.rst_parser.directives
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+    Package with tests for the custom blohg reStructuredText directives.
+
+    :copyright: (c) 2010-2013 by Rafael Goncalves Martins
+    :license: GPL-2, see LICENSE for more details.
+"""
+
+import unittest
+from docutils.parsers.rst.directives import _directives, register_directive
+
+from blohg.rst_parser import parser
+from blohg.rst_parser.directives import Vimeo, Youtube, SourceCode, Math
+
+
+class DirectiveTestCase(unittest.TestCase):
+
+    def setUp(self):
+        unittest.TestCase.setUp(self)
+        self.name = 'blohg-%s' % self.directive.__name__.lower()
+        register_directive(self.name, self.directive)
+
+    def tearDown(self):
+        # FIXME: do not use the internal list directly (?)
+        del _directives[self.name]
+        unittest.TestCase.tearDown(self)
+
+
+class VimeoTestCase(DirectiveTestCase):
+
+    directive = Vimeo
+
+    def test_run_with_default_opts(self):
+        content = parser('''\
+asd
+---
+
+.. blohg-vimeo:: 34368016
+''', 3)
+        self.assertIn('http://player.vimeo.com/video/34368016',
+                      content['fragment'])
+        self.assertIn('width: 425px', content['fragment'])
+        self.assertIn('height: 344px', content['fragment'])
+        self.assertIn('align-center', content['fragment'])
+        self.assertIn('frameborder="0"', content['fragment'])
+        self.assertIn('allowfullscreen', content['fragment'])
+
+    def test_run(self):
+        content = parser('''\
+asd
+---
+
+.. blohg-vimeo:: 34368016
+   :align: left
+   :width: 100
+   :height: 200
+   :allowfullscreen: false
+   :border: 1
+''', 3)
+        self.assertIn('http://player.vimeo.com/video/34368016',
+                      content['fragment'])
+        self.assertIn('width: 100px', content['fragment'])
+        self.assertIn('height: 200px', content['fragment'])
+        self.assertIn('align-left', content['fragment'])
+        self.assertIn('frameborder="1"', content['fragment'])
+        self.assertNotIn('allowfullscreen', content['fragment'])
+
+
+class YoutubeTestCase(DirectiveTestCase):
+
+    directive = Youtube
+
+    def test_run_with_default_opts(self):
+        content = parser('''\
+asd
+---
+
+.. blohg-youtube:: ssMfQ9ybTEc
+''', 3)
+        self.assertIn('http://www.youtube.com/embed/ssMfQ9ybTEc',
+                      content['fragment'])
+        self.assertIn('width: 425px', content['fragment'])
+        self.assertIn('height: 344px', content['fragment'])
+        self.assertIn('align-center', content['fragment'])
+        self.assertIn('frameborder="0"', content['fragment'])
+        self.assertIn('allowfullscreen', content['fragment'])
+
+    def test_run(self):
+        content = parser('''\
+asd
+---
+
+.. blohg-youtube:: ssMfQ9ybTEc
+   :align: left
+   :width: 100
+   :height: 200
+   :allowfullscreen: false
+   :border: 1
+''', 3)
+        self.assertIn('http://www.youtube.com/embed/ssMfQ9ybTEc',
+                      content['fragment'])
+        self.assertIn('width: 100px', content['fragment'])
+        self.assertIn('height: 200px', content['fragment'])
+        self.assertIn('align-left', content['fragment'])
+        self.assertIn('frameborder="1"', content['fragment'])
+        self.assertNotIn('allowfullscreen', content['fragment'])
+
+
+class SourceCodeTestCase(DirectiveTestCase):
+
+    directive = SourceCode
+
+    def test_run_with_default_opts(self):
+        content = parser('''\
+asd
+---
+
+.. blohg-sourcecode:: python
+
+    import os
+
+    print os.path
+
+''', 3)
+        self.assertIn('class="highlight"', content['fragment'])
+        self.assertIn('import', content['fragment'])
+
+    def test_run(self):
+        content = parser('''\
+asd
+---
+
+.. blohg-sourcecode:: python
+   :linenos:
+
+    import os
+
+    print os.path
+
+''', 3)
+        self.assertIn('class="lineno">3', content['fragment'])
+        self.assertIn('import', content['fragment'])
+
+
+class MathTestCase(DirectiveTestCase):
+
+    directive = Math
+
+    def test_run_with_default_opts(self):
+        content = parser('''\
+asd
+---
+
+.. blohg-math::
+
+   \\frac{x^2}{1+x}
+''', 3)
+        self.assertIn('https://chart.googleapis.com/chart?cht=tx&chl='
+                      '%5Cfrac%7Bx%5E2%7D%7B1%2Bx%7D', content['images'])
+        self.assertIn('https://chart.googleapis.com/chart?cht=tx&'
+                      'chl=%5Cfrac%7Bx%5E2%7D%7B1%2Bx%7D', content['fragment'])
+        self.assertIn('align-center', content['fragment'])
+
+    def test_run(self):
+        content = parser('''\
+asd
+---
+
+.. blohg-math::
+   :align: left
+
+   \\frac{x^2}{1+x}
+''', 3)
+        self.assertIn('https://chart.googleapis.com/chart?cht=tx&chl='
+                      '%5Cfrac%7Bx%5E2%7D%7B1%2Bx%7D', content['images'])
+        self.assertIn('https://chart.googleapis.com/chart?cht=tx&'
+                      'chl=%5Cfrac%7Bx%5E2%7D%7B1%2Bx%7D', content['fragment'])
+        self.assertIn('align-left', content['fragment'])
 this data, add a reStructuredText comment like this:
 
 .. code-block:: rest
-    
+
     .. author: John <john@example.com>
 
 
 Post aliases
 ------------
 
-When migrating from another blogging system or URL structure, you can have 
-blohg redirect your readers to the new URL's by providing your posts with URL 
+When migrating from another blogging system or URL structure, you can have
+blohg redirect your readers to the new URL's by providing your posts with URL
 aliases. If you need this, insert a reStructuredText comment with a comma
 separated list of the aliases for the post like this:
 
 .. code-block:: rest
-    
+
     .. aliases: /my-old-post-location/,/another-old-location/
 
 By default, blohg will issue a 302 (temporary) redirection. If you want, you
 can have blohg issue a 301 (permanent) redirection instead like this:
 
 .. code-block:: rest
-    
+
     .. aliases: 301:/my-old-post-location/,/another-old-location/
 
 The ``301:`` prefix is per URL and must be repeated for every URL you wish
 
     .. math::
 
-        \frac{x^2}{1+x}
+        \\frac{x^2}{1+x}
 
 
 Directive ``include``