From f1195365abea6f3e9ac99c16b53bc4f54a7c37c2 Mon Sep 17 00:00:00 2001 From: Lucas Tan Date: Sun, 9 Mar 2014 13:53:19 +0800 Subject: [PATCH] added jinja2 tests --- compressor/tests/test_offline.py | 83 ++++++++++++++++--- .../test_complex/test_compressor_offline.html | 12 +++ .../basic/test_compressor_offline.html | 8 ++ .../test_block_super/base.html | 15 ++++ .../test_compressor_offline.html | 12 +++ .../test_block_super_extra/base.html | 15 ++++ .../test_compressor_offline.html | 18 ++++ .../test_block_super_multiple/base.html | 15 ++++ .../test_block_super_multiple/base2.html | 3 + .../test_compressor_offline.html | 10 +++ .../base.html | 15 ++++ .../base2.html | 3 + .../test_compressor_offline.html | 10 +++ .../test_complex/test_compressor_offline.html | 14 ++++ .../test_compressor_offline.html | 7 ++ .../test_error_handling/buggy_extends.html | 9 ++ .../test_error_handling/buggy_template.html | 10 +++ .../test_error_handling/missing_extends.html | 9 ++ .../test_compressor_offline.html | 8 ++ .../with_coffeescript.html | 5 ++ .../test_compressor_offline.html | 7 ++ .../test_compressor_offline.html | 6 ++ .../test_compressor_offline.html | 7 ++ .../test_compressor_offline.html | 7 ++ 24 files changed, 296 insertions(+), 12 deletions(-) create mode 100644 compressor/tests/test_templates/test_complex/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/basic/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super/base.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super_extra/base.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super_extra/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super_multiple/base.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super_multiple/base2.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super_multiple/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/base.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/base2.html create mode 100644 compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_complex/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_condition/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_error_handling/buggy_extends.html create mode 100644 compressor/tests/test_templates_jinja2/test_error_handling/buggy_template.html create mode 100644 compressor/tests/test_templates_jinja2/test_error_handling/missing_extends.html create mode 100644 compressor/tests/test_templates_jinja2/test_error_handling/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_error_handling/with_coffeescript.html create mode 100644 compressor/tests/test_templates_jinja2/test_inline_non_ascii/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_static_templatetag/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_templatetag/test_compressor_offline.html create mode 100644 compressor/tests/test_templates_jinja2/test_with_context/test_compressor_offline.html diff --git a/compressor/tests/test_offline.py b/compressor/tests/test_offline.py index 4f1ae06..d73b38f 100644 --- a/compressor/tests/test_offline.py +++ b/compressor/tests/test_offline.py @@ -60,8 +60,8 @@ class OfflineTestCaseMixin(object): if default_storage.exists(manifest_path): default_storage.delete(manifest_path) - def test_offline(self): - count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity) + def _test_offline(self, engine): + count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine) self.assertEqual(1, count) self.assertEqual([ '' % (self.expected_hash, ), @@ -69,16 +69,36 @@ class OfflineTestCaseMixin(object): rendered_template = self.template.render(Context(settings.COMPRESS_OFFLINE_CONTEXT)) self.assertEqual(rendered_template, "".join(result) + "\n") + def test_offline_django(self): + settings.TEMPLATE_DIRS = ( + os.path.join(settings.TEST_DIR, 'test_templates', self.templates_dir), + ) + self._test_offline(engine="django") + + def test_offline_jinja2(self): + settings.TEMPLATE_DIRS = ( + os.path.join(settings.TEST_DIR, 'test_templates_jinja2', self.templates_dir), + ) + self._test_offline(engine="jinja2") + class OfflineGenerationBlockSuperTestCase(OfflineTestCaseMixin, TestCase): templates_dir = "test_block_super" expected_hash = "7c02d201f69d" + def test_offline_jinja2(self): + # Block.super not supported for Jinja2 yet. + pass + class OfflineGenerationBlockSuperMultipleTestCase(OfflineTestCaseMixin, TestCase): templates_dir = "test_block_super_multiple" expected_hash = "2f6ef61c488e" + def test_offline_jinja2(self): + # Block.super not supported for Jinja2 yet. + pass + class OfflineGenerationBlockSuperMultipleWithCachedLoaderTestCase(OfflineTestCaseMixin, TestCase): templates_dir = "test_block_super_multiple_cached" @@ -98,12 +118,16 @@ class OfflineGenerationBlockSuperMultipleWithCachedLoaderTestCase(OfflineTestCas super(OfflineGenerationBlockSuperMultipleWithCachedLoaderTestCase, self).tearDown() settings.TEMPLATE_LOADERS = self._old_template_loaders + def test_offline_jinja2(self): + # Block.super not supported for Jinja2 yet. + pass + class OfflineGenerationBlockSuperTestCaseWithExtraContent(OfflineTestCaseMixin, TestCase): templates_dir = "test_block_super_extra" - def test_offline(self): - count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity) + def _test_offline(self, engine): + count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine) self.assertEqual(2, count) self.assertEqual([ '', @@ -112,6 +136,10 @@ class OfflineGenerationBlockSuperTestCaseWithExtraContent(OfflineTestCaseMixin, rendered_template = self.template.render(Context(settings.COMPRESS_OFFLINE_CONTEXT)) self.assertEqual(rendered_template, "".join(result) + "\n") + def test_offline_jinja2(self): + # Block.super not supported for Jinja2 yet. + pass + class OfflineGenerationConditionTestCase(OfflineTestCaseMixin, TestCase): templates_dir = "test_condition" @@ -158,9 +186,21 @@ class OfflineGenerationTestCaseWithContext(OfflineTestCaseMixin, TestCase): class OfflineGenerationTestCaseErrors(OfflineTestCaseMixin, TestCase): templates_dir = "test_error_handling" - def test_offline(self): - count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity) - self.assertEqual(2, count) + def _test_offline(self, engine): + count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine) + + if engine == "django": + self.assertEqual(2, count) + else: + # Because we use env.parse in Jinja2Parser, the engine does not + # actually load the "extends" and "includes" templates, and so + # it is unable to detect that they are missing. So all the "compress" + # nodes are processed correctly. + self.assertEqual(4, count) + self.assertEqual(engine, "jinja2") + self.assertIn('', result) + self.assertIn('', result) + self.assertIn('', result) self.assertIn('', result) @@ -173,7 +213,7 @@ class OfflineGenerationTestCaseWithError(OfflineTestCaseMixin, TestCase): settings.COMPRESS_PRECOMPILERS = (('text/coffeescript', 'non-existing-binary'),) super(OfflineGenerationTestCaseWithError, self).setUp() - def test_offline(self): + def _test_offline(self, engine): """ Test that a CommandError is raised with DEBUG being False as well as True, as otherwise errors in configuration will never show in @@ -183,10 +223,10 @@ class OfflineGenerationTestCaseWithError(OfflineTestCaseMixin, TestCase): try: settings.DEBUG = True - self.assertRaises(CommandError, CompressCommand().compress) + self.assertRaises(CommandError, CompressCommand().compress, engine=engine) settings.DEBUG = False - self.assertRaises(CommandError, CompressCommand().compress) + self.assertRaises(CommandError, CompressCommand().compress, engine=engine) finally: settings.DEBUG = self._old_debug @@ -257,7 +297,26 @@ class OfflineGenerationInlineNonAsciiTestCase(OfflineTestCaseMixin, TestCase): self.COMPRESS_OFFLINE_CONTEXT = self.old_offline_context super(OfflineGenerationInlineNonAsciiTestCase, self).tearDown() - def test_offline(self): - count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity) + def _test_offline(self, engine): + count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine) rendered_template = self.template.render(Context(settings.COMPRESS_OFFLINE_CONTEXT)) self.assertEqual(rendered_template, "".join(result) + "\n") + + +class OfflineGenerationComplexTestCase(OfflineTestCaseMixin, TestCase): + templates_dir = "test_complex" + expected_hash = "0e8807bebcee" + + def setUp(self): + self.old_offline_context = settings.COMPRESS_OFFLINE_CONTEXT + settings.COMPRESS_OFFLINE_CONTEXT = { + 'condition': 'OK!', + # Django templating does not allow definition of tuples in the + # templates. Make sure this is same as test_templates_jinja2/test_complex. + 'my_names': ("js/one.js", "js/nonasc.js"), + } + super(OfflineGenerationComplexTestCase, self).setUp() + + def tearDown(self): + self.COMPRESS_OFFLINE_CONTEXT = self.old_offline_context + super(OfflineGenerationComplexTestCase, self).tearDown() diff --git a/compressor/tests/test_templates/test_complex/test_compressor_offline.html b/compressor/tests/test_templates/test_complex/test_compressor_offline.html new file mode 100644 index 0000000..99f75c6 --- /dev/null +++ b/compressor/tests/test_templates/test_complex/test_compressor_offline.html @@ -0,0 +1,12 @@ +{% load compress static %}{% spaceless %} + +{% if condition %} + {% compress js%} + + {% with names=my_names %} + {% for name in names %} + + {% endfor %} + {% endwith %} + {% endcompress %} +{% endif %}{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/basic/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/basic/test_compressor_offline.html new file mode 100644 index 0000000..6e89ed2 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/basic/test_compressor_offline.html @@ -0,0 +1,8 @@ +{% spaceless %} + +{% compress js %} + +{% endcompress %} +{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super/base.html b/compressor/tests/test_templates_jinja2/test_block_super/base.html new file mode 100644 index 0000000..e9ca3ad --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super/base.html @@ -0,0 +1,15 @@ +{% spaceless %} +{% block js %} + +{% endblock %} + +{% block css %} + +{% endblock %} +{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_block_super/test_compressor_offline.html new file mode 100644 index 0000000..e1fabd8 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super/test_compressor_offline.html @@ -0,0 +1,12 @@ +{% extends "base.html" %} + +{% block js %}{% spaceless %} + {% compress js %} + {{ super() }} + + {% endcompress %} +{% endspaceless %}{% endblock %} + +{% block css %}{% endblock %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super_extra/base.html b/compressor/tests/test_templates_jinja2/test_block_super_extra/base.html new file mode 100644 index 0000000..e9ca3ad --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super_extra/base.html @@ -0,0 +1,15 @@ +{% spaceless %} +{% block js %} + +{% endblock %} + +{% block css %} + +{% endblock %} +{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super_extra/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_block_super_extra/test_compressor_offline.html new file mode 100644 index 0000000..328ccb9 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super_extra/test_compressor_offline.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} + +{% block js %}{% spaceless %} + {% compress js %} + + {% endcompress %} + + {% compress js %} + {{ super() }} + + {% endcompress %} +{% endspaceless %}{% endblock %} + +{% block css %}{% endblock %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super_multiple/base.html b/compressor/tests/test_templates_jinja2/test_block_super_multiple/base.html new file mode 100644 index 0000000..c9ee6cc --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super_multiple/base.html @@ -0,0 +1,15 @@ +{% spaceless %} +{% block js %} + +{% endblock %} + +{% block css %} + +{% endblock %} +{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super_multiple/base2.html b/compressor/tests/test_templates_jinja2/test_block_super_multiple/base2.html new file mode 100644 index 0000000..b0b2fef --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super_multiple/base2.html @@ -0,0 +1,3 @@ +{% extends "base.html" %} + +{% block css %}{% endblock %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super_multiple/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_block_super_multiple/test_compressor_offline.html new file mode 100644 index 0000000..accd76d --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super_multiple/test_compressor_offline.html @@ -0,0 +1,10 @@ +{% extends "base2.html" %} + +{% block js %}{% spaceless %} + {% compress js %} + {{ super() }} + + {% endcompress %} +{% endspaceless %}{% endblock %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/base.html b/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/base.html new file mode 100644 index 0000000..c9ee6cc --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/base.html @@ -0,0 +1,15 @@ +{% spaceless %} +{% block js %} + +{% endblock %} + +{% block css %} + +{% endblock %} +{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/base2.html b/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/base2.html new file mode 100644 index 0000000..b0b2fef --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/base2.html @@ -0,0 +1,3 @@ +{% extends "base.html" %} + +{% block css %}{% endblock %} diff --git a/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/test_compressor_offline.html new file mode 100644 index 0000000..accd76d --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_block_super_multiple_cached/test_compressor_offline.html @@ -0,0 +1,10 @@ +{% extends "base2.html" %} + +{% block js %}{% spaceless %} + {% compress js %} + {{ super() }} + + {% endcompress %} +{% endspaceless %}{% endblock %} diff --git a/compressor/tests/test_templates_jinja2/test_complex/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_complex/test_compressor_offline.html new file mode 100644 index 0000000..24eeb8e --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_complex/test_compressor_offline.html @@ -0,0 +1,14 @@ +{% spaceless %} + +{% if condition %} + {% compress js%} + + {% with names=[] %} + {% do names.append("js/one.js") %} + {% do names.append("js/nonasc.js") %} + {% for name in names %} + + {% endfor %} + {% endwith %} + {% endcompress %} +{% endif %}{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_condition/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_condition/test_compressor_offline.html new file mode 100644 index 0000000..bd1adb8 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_condition/test_compressor_offline.html @@ -0,0 +1,7 @@ +{% spaceless %} + +{% if condition %} + {% compress js%} + + {% endcompress %} +{% endif %}{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_error_handling/buggy_extends.html b/compressor/tests/test_templates_jinja2/test_error_handling/buggy_extends.html new file mode 100644 index 0000000..72513f7 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_error_handling/buggy_extends.html @@ -0,0 +1,9 @@ +{% extends "buggy_template.html" %} + +{% compress css %} + +{% endcompress %} diff --git a/compressor/tests/test_templates_jinja2/test_error_handling/buggy_template.html b/compressor/tests/test_templates_jinja2/test_error_handling/buggy_template.html new file mode 100644 index 0000000..a01b899 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_error_handling/buggy_template.html @@ -0,0 +1,10 @@ +{% compress css %} + +{% endcompress %} + + +{% fail %} diff --git a/compressor/tests/test_templates_jinja2/test_error_handling/missing_extends.html b/compressor/tests/test_templates_jinja2/test_error_handling/missing_extends.html new file mode 100644 index 0000000..dc76034 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_error_handling/missing_extends.html @@ -0,0 +1,9 @@ +{% extends "missing.html" %} + +{% compress css %} + +{% endcompress %} diff --git a/compressor/tests/test_templates_jinja2/test_error_handling/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_error_handling/test_compressor_offline.html new file mode 100644 index 0000000..3ecffa5 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_error_handling/test_compressor_offline.html @@ -0,0 +1,8 @@ +{% spaceless %} + +{% compress js %} + +{% endcompress %} +{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_error_handling/with_coffeescript.html b/compressor/tests/test_templates_jinja2/test_error_handling/with_coffeescript.html new file mode 100644 index 0000000..8a53e44 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_error_handling/with_coffeescript.html @@ -0,0 +1,5 @@ +{% compress js %} + +{% endcompress %} diff --git a/compressor/tests/test_templates_jinja2/test_inline_non_ascii/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_inline_non_ascii/test_compressor_offline.html new file mode 100644 index 0000000..c03b191 --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_inline_non_ascii/test_compressor_offline.html @@ -0,0 +1,7 @@ +{% spaceless %} + +{% compress js, inline %} + +{% endcompress %}{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_static_templatetag/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_static_templatetag/test_compressor_offline.html new file mode 100644 index 0000000..ed7238c --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_static_templatetag/test_compressor_offline.html @@ -0,0 +1,6 @@ +{% spaceless %} + +{% compress js %} + + +{% endcompress %}{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_templatetag/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_templatetag/test_compressor_offline.html new file mode 100644 index 0000000..96d8d3f --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_templatetag/test_compressor_offline.html @@ -0,0 +1,7 @@ +{% spaceless %} + +{% compress js %} + +{% endcompress %}{% endspaceless %} diff --git a/compressor/tests/test_templates_jinja2/test_with_context/test_compressor_offline.html b/compressor/tests/test_templates_jinja2/test_with_context/test_compressor_offline.html new file mode 100644 index 0000000..2289a5f --- /dev/null +++ b/compressor/tests/test_templates_jinja2/test_with_context/test_compressor_offline.html @@ -0,0 +1,7 @@ +{% spaceless %} + +{% compress js %} + +{% endcompress %}{% endspaceless %}