Added tests for jinja2 extension after tests refactor (#92)
This commit is contained in:

committed by
Jannis Leidel

parent
26fa0d4296
commit
a203d6bb2f
@@ -4,7 +4,7 @@ from jinja2.exceptions import TemplateSyntaxError
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from compressor.conf import settings
|
||||
from compressor.utils import get_class
|
||||
from compressor.templatetags.compress import COMPRESSORS, OUTPUT_FILE
|
||||
from compressor.templatetags.compress import OUTPUT_FILE
|
||||
from compressor.cache import (cache_get, cache_set,
|
||||
get_templatetag_cachekey)
|
||||
|
||||
@@ -13,6 +13,13 @@ class CompressorExtension(Extension):
|
||||
|
||||
tags = set(['compress'])
|
||||
|
||||
@property
|
||||
def compressors(self):
|
||||
return {
|
||||
'js': settings.COMPRESS_JS_COMPRESSOR,
|
||||
'css': settings.COMPRESS_CSS_COMPRESSOR,
|
||||
}
|
||||
|
||||
def parse(self, parser):
|
||||
lineno = parser.stream.next().lineno
|
||||
kindarg = parser.parse_expression()
|
||||
@@ -20,9 +27,9 @@ class CompressorExtension(Extension):
|
||||
if isinstance(kindarg, nodes.Name):
|
||||
kindarg = nodes.Const(kindarg.name)
|
||||
args = [kindarg]
|
||||
if args[0].value not in COMPRESSORS:
|
||||
raise TemplateSyntaxError('compress kind may be "css" or "js"',
|
||||
lineno)
|
||||
if args[0].value not in self.compressors:
|
||||
raise TemplateSyntaxError('compress kind may be one of: %s'
|
||||
% (', '.join(self.compressors.keys())), lineno)
|
||||
if parser.stream.skip_if('comma'):
|
||||
modearg = parser.parse_expression()
|
||||
# Allow mode to be defined as jinja2 name node
|
||||
@@ -37,7 +44,7 @@ class CompressorExtension(Extension):
|
||||
|
||||
def _compress(self, kind, mode, caller):
|
||||
mode = mode or OUTPUT_FILE
|
||||
Compressor = get_class(COMPRESSORS.get(kind),
|
||||
Compressor = get_class(self.compressors.get(kind),
|
||||
exception=ImproperlyConfigured)
|
||||
original_content = caller()
|
||||
compressor = Compressor(original_content)
|
||||
|
@@ -1,5 +1,6 @@
|
||||
from .base import CompressorTestCase, CssMediaTestCase, VerboseTestCase, CacheBackendTestCase
|
||||
from .filters import CssTidyTestCase, PrecompilerTestCase, CssMinTestCase, CssAbsolutizingTestCase, CssDataUriTestCase
|
||||
from .jinja2ext import TestJinja2CompressorExtension
|
||||
from .offline import OfflineGenerationTestCase
|
||||
from .parsers import LxmlParserTests, Html5LibParserTests, BeautifulSoupParserTests, HtmlParserTests
|
||||
from .signals import PostCompressSignalTestCase
|
||||
|
133
tests/tests/jinja2ext.py
Normal file
133
tests/tests/jinja2ext.py
Normal file
@@ -0,0 +1,133 @@
|
||||
from django.test import TestCase
|
||||
from compressor.conf import settings
|
||||
from unittest2 import skipIf
|
||||
from .base import css_tag
|
||||
|
||||
|
||||
try:
|
||||
import jinja2
|
||||
except ImportError:
|
||||
jinja2 = None
|
||||
|
||||
|
||||
class TestJinja2CompressorExtension(TestCase):
|
||||
"""
|
||||
Test case for jinja2 extension.
|
||||
|
||||
.. note::
|
||||
At tests we need to make some extra care about whitespace. Please note
|
||||
that we use jinja2 specific controls (*minus* character at block's
|
||||
beginning or end). For more information see jinja2 documentation.
|
||||
"""
|
||||
|
||||
def assertStrippedEqual(self, result, expected):
|
||||
self.assertEqual(result.strip(), expected.strip(), "%r != %r" % (
|
||||
result.strip(), expected.strip()))
|
||||
|
||||
def setUp(self):
|
||||
from compressor.contrib.jinja2ext import CompressorExtension
|
||||
self.env = jinja2.Environment(extensions=[CompressorExtension])
|
||||
|
||||
def test_error_raised_if_no_arguments_given(self):
|
||||
with self.assertRaises(jinja2.exceptions.TemplateSyntaxError):
|
||||
self.env.from_string('{% compress %}Foobar{% endcompress %}')
|
||||
|
||||
def test_error_raised_if_wrong_kind_given(self):
|
||||
with self.assertRaises(jinja2.exceptions.TemplateSyntaxError):
|
||||
self.env.from_string('{% compress foo %}Foobar{% endcompress %}'
|
||||
).render()
|
||||
|
||||
def test_error_raised_if_wrong_mode_given(self):
|
||||
with self.assertRaises(jinja2.exceptions.TemplateSyntaxError):
|
||||
self.env.from_string('{% compress css foo %}Foobar{% endcompress %}'
|
||||
).render()
|
||||
|
||||
def test_compress_is_disabled(self):
|
||||
org_COMPRESS_ENABLED = settings.COMPRESS_ENABLED
|
||||
settings.COMPRESS_ENABLED = False
|
||||
tag_body = '\n'.join([
|
||||
'<link rel="stylesheet" href="css/one.css" type="text/css" charset="utf-8">',
|
||||
'<style type="text/css">p { border:5px solid green;}</style>',
|
||||
'<link rel="stylesheet" href="css/two.css" type="text/css" charset="utf-8">',
|
||||
])
|
||||
template_string = '{% compress css %}' + tag_body + '{% endcompress %}'
|
||||
template = self.env.from_string(template_string)
|
||||
self.assertEqual(tag_body, template.render())
|
||||
settings.COMPRESS_ENABLED = org_COMPRESS_ENABLED
|
||||
|
||||
def test_empty_tag(self):
|
||||
template = self.env.from_string(u"""{% compress js %}{% block js %}
|
||||
{% endblock %}{% endcompress %}""")
|
||||
context = {'MEDIA_URL': settings.COMPRESS_URL }
|
||||
self.assertEqual(u'', template.render(context))
|
||||
|
||||
def test_css_tag(self):
|
||||
template = self.env.from_string(u"""{% compress css -%}
|
||||
<link rel="stylesheet" href="{{ MEDIA_URL }}css/one.css" type="text/css" charset="utf-8">
|
||||
<style type="text/css">p { border:5px solid green;}</style>
|
||||
<link rel="stylesheet" href="{{ MEDIA_URL }}css/two.css" type="text/css" charset="utf-8">
|
||||
{%- endcompress %}""")
|
||||
context = {'MEDIA_URL': settings.COMPRESS_URL }
|
||||
out = css_tag("/media/CACHE/css/e41ba2cc6982.css")
|
||||
self.assertEqual(out, template.render(context))
|
||||
|
||||
def test_nonascii_css_tag(self):
|
||||
template = self.env.from_string(u"""{% compress css -%}
|
||||
<link rel="stylesheet" href="{{ MEDIA_URL }}css/nonasc.css" type="text/css" charset="utf-8">
|
||||
<style type="text/css">p { border:5px solid green;}</style>
|
||||
{%- endcompress %}""")
|
||||
context = {'MEDIA_URL': settings.COMPRESS_URL }
|
||||
out = css_tag("/media/CACHE/css/799f6defe43c.css")
|
||||
self.assertEqual(out, template.render(context))
|
||||
|
||||
def test_js_tag(self):
|
||||
template = self.env.from_string(u"""{% compress js -%}
|
||||
<script src="{{ MEDIA_URL }}js/one.js" type="text/javascript" charset="utf-8"></script>
|
||||
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
|
||||
{%- endcompress %}""")
|
||||
context = {'MEDIA_URL': settings.COMPRESS_URL }
|
||||
out = u'<script type="text/javascript" src="/media/CACHE/js/066cd253eada.js"></script>'
|
||||
self.assertEqual(out, template.render(context))
|
||||
|
||||
def test_nonascii_js_tag(self):
|
||||
template = self.env.from_string(u"""{% compress js -%}
|
||||
<script src="{{ MEDIA_URL }}js/nonasc.js" type="text/javascript" charset="utf-8"></script>
|
||||
<script type="text/javascript" charset="utf-8">var test_value = "\u2014";</script>
|
||||
{%- endcompress %}""")
|
||||
context = {'MEDIA_URL': settings.COMPRESS_URL }
|
||||
out = u'<script type="text/javascript" src="/media/CACHE/js/e214fe629b28.js"></script>'
|
||||
self.assertEqual(out, template.render(context))
|
||||
|
||||
def test_nonascii_latin1_js_tag(self):
|
||||
template = self.env.from_string(u"""{% compress js -%}
|
||||
<script src="{{ MEDIA_URL }}js/nonasc-latin1.js" type="text/javascript" charset="latin-1"></script>
|
||||
<script type="text/javascript">var test_value = "\u2014";</script>
|
||||
{%- endcompress %}""")
|
||||
context = {'MEDIA_URL': settings.COMPRESS_URL }
|
||||
out = u'<script type="text/javascript" src="/media/CACHE/js/be9e078b5ca7.js"></script>'
|
||||
self.assertEqual(out, template.render(context))
|
||||
|
||||
def test_css_inline(self):
|
||||
template = self.env.from_string(u"""{% compress css, inline -%}
|
||||
<link rel="stylesheet" href="{{ MEDIA_URL }}css/one.css" type="text/css" charset="utf-8">
|
||||
<style type="text/css">p { border:5px solid green;}</style>
|
||||
{%- endcompress %}""")
|
||||
context = {'MEDIA_URL': settings.COMPRESS_URL }
|
||||
out = '\n'.join([
|
||||
'<style type="text/css">body { background:#990; }',
|
||||
'p { border:5px solid green;}</style>',
|
||||
])
|
||||
self.assertEqual(out, template.render(context))
|
||||
|
||||
def test_js_inline(self):
|
||||
template = self.env.from_string(u"""{% compress js, inline -%}
|
||||
<script src="{{ MEDIA_URL }}js/one.js" type="text/css" type="text/javascript" charset="utf-8"></script>
|
||||
<script type="text/javascript" charset="utf-8">obj.value = "value";</script>
|
||||
{%- endcompress %}""")
|
||||
context = {'MEDIA_URL': settings.COMPRESS_URL }
|
||||
out = '<script type="text/javascript">obj={};obj.value="value";</script>'
|
||||
self.assertEqual(out, template.render(context))
|
||||
|
||||
TestJinja2CompressorExtension = skipIf(jinja2 is None, 'jinja2 not found')(
|
||||
TestJinja2CompressorExtension)
|
||||
|
Reference in New Issue
Block a user