# -*- coding: utf-8 -*-
from __future__ import with_statement
from django.test import TestCase
import jinja2
from compressor.conf import settings
from compressor.tests.test_base import css_tag
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):
self.assertRaises(jinja2.exceptions.TemplateSyntaxError,
self.env.from_string, '{% compress %}Foobar{% endcompress %}')
def test_error_raised_if_wrong_kind_given(self):
self.assertRaises(jinja2.exceptions.TemplateSyntaxError,
self.env.from_string, '{% compress foo %}Foobar{% endcompress %}')
def test_error_raised_if_wrong_mode_given(self):
self.assertRaises(jinja2.exceptions.TemplateSyntaxError,
self.env.from_string, '{% compress css foo %}Foobar{% endcompress %}')
def test_compress_is_disabled(self):
org_COMPRESS_ENABLED = settings.COMPRESS_ENABLED
settings.COMPRESS_ENABLED = False
tag_body = '\n'.join([
'',
'',
'',
])
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 = {'STATIC_URL': settings.COMPRESS_URL}
self.assertEqual(u'', template.render(context))
def test_css_tag(self):
template = self.env.from_string(u"""{% compress css -%}
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = css_tag("/static/CACHE/css/e41ba2cc6982.css")
self.assertEqual(out, template.render(context))
def test_nonascii_css_tag(self):
template = self.env.from_string(u"""{% compress css -%}
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = css_tag("/static/CACHE/css/799f6defe43c.css")
self.assertEqual(out, template.render(context))
def test_js_tag(self):
template = self.env.from_string(u"""{% compress js -%}
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = u''
self.assertEqual(out, template.render(context))
def test_nonascii_js_tag(self):
template = self.env.from_string(u"""{% compress js -%}
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = u''
self.assertEqual(out, template.render(context))
def test_nonascii_latin1_js_tag(self):
template = self.env.from_string(u"""{% compress js -%}
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = u''
self.assertEqual(out, template.render(context))
def test_css_inline(self):
template = self.env.from_string(u"""{% compress css, inline -%}
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = '\n'.join([
'',
])
self.assertEqual(out, template.render(context))
def test_js_inline(self):
template = self.env.from_string(u"""{% compress js, inline -%}
{% endcompress %}""")
context = {'STATIC_URL': settings.COMPRESS_URL}
out = ''
self.assertEqual(out, template.render(context))
def test_nonascii_inline_css(self):
org_COMPRESS_ENABLED = settings.COMPRESS_ENABLED
settings.COMPRESS_ENABLED = False
template = self.env.from_string(u'{% compress css %}'
u'{% endcompress %}')
out = u''
settings.COMPRESS_ENABLED = org_COMPRESS_ENABLED
context = {'STATIC_URL': settings.COMPRESS_URL}
self.assertEqual(out, template.render(context))