From 63ff8bab7cf4b818da6e50251f8a79cf1e1209d8 Mon Sep 17 00:00:00 2001
From: Sean Brant
Date: Sun, 10 Mar 2013 22:48:15 -0500
Subject: Adds tests for django template tag
---
 tests/templates/django.html                        | 18 +++++
 tests/test_contrib/__init__.py                     |  0
 tests/test_contrib/test_django/__init__.py         |  0
 .../test_django/test_templatetags/__init__.py      |  0
 .../test_django/test_templatetags/test_pykss.py    | 93 ++++++++++++++++++++++
 5 files changed, 111 insertions(+)
 create mode 100644 tests/templates/django.html
 create mode 100644 tests/test_contrib/__init__.py
 create mode 100644 tests/test_contrib/test_django/__init__.py
 create mode 100644 tests/test_contrib/test_django/test_templatetags/__init__.py
 create mode 100644 tests/test_contrib/test_django/test_templatetags/test_pykss.py
(limited to 'tests')
diff --git a/tests/templates/django.html b/tests/templates/django.html
new file mode 100644
index 0000000..7c52f40
--- /dev/null
+++ b/tests/templates/django.html
@@ -0,0 +1,18 @@
+{# it's probably a terrible idea to render JSON by hand... #}
+{
+    "section": "{{ section.section }}",
+    "filename": "{{ section.filename }}",
+    "description": "{{ section.description }}",
+    "modifiers": [
+        {% for modifier in section.modifiers %}
+            ["{{ modifier.name }}","{{ modifier.description }}"]{% if not forloop.last %},{% endif %}
+        {% endfor %}
+    ],
+    "example_html": "{{ example_html|escapejs }}",
+    "modifier_examples": [
+        {% for example in modifier_examples %}
+            ["{{ example.modifier.name }}","{{ example.html|escapejs }}"]{% if not forloop.last %},{% endif %}
+        {% endfor %}
+    ],
+    "escaped_html": "{{ escaped_html|escapejs }}"
+}
diff --git a/tests/test_contrib/__init__.py b/tests/test_contrib/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_contrib/test_django/__init__.py b/tests/test_contrib/test_django/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_contrib/test_django/test_templatetags/__init__.py b/tests/test_contrib/test_django/test_templatetags/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/test_contrib/test_django/test_templatetags/test_pykss.py b/tests/test_contrib/test_django/test_templatetags/test_pykss.py
new file mode 100644
index 0000000..1f0d967
--- /dev/null
+++ b/tests/test_contrib/test_django/test_templatetags/test_pykss.py
@@ -0,0 +1,93 @@
+import os
+
+from django.conf import settings
+from django.template import Context, Template, TemplateSyntaxError
+from django.test import TestCase
+from django.utils import simplejson
+
+from mock import patch, ANY
+
+from pykss.exceptions import SectionDoesNotExist
+from pykss.parser import Parser
+
+
+class StyleGuideBlockTestCase(TestCase):
+
+    def setUp(self):
+        css = os.path.join(settings.PROJECT_ROOT, 'tests', 'fixtures', 'css')
+        self.styleguide = Parser(css)
+
+    def test_when_section_does_not_exist_and_in_debug(self):
+        template = Template("""
+            {% load pykss %}
+            {% styleguideblock styleguide "99" %}
+            {% endstyleguideblock %}
+        """)
+        context = Context({'styleguide': self.styleguide})
+        self.assertRaises(SectionDoesNotExist, template.render, context)
+
+    def test_when_section_does_not_exist_and_not_in_debug(self):
+        with self.settings(TEMPLATE_DEBUG=False):
+            template = Template("""
+                {% load pykss %}
+                {% styleguideblock styleguide "99" %}
+                {% endstyleguideblock %}
+            """)
+            context = Context({'styleguide': self.styleguide})
+            results = template.render(context)
+            self.assertEqual(results.strip(), '')
+
+    @patch('pykss.contrib.django.templatetags.pykss.render_to_string')
+    def test_uses_default_template(self, mock_render_to_string):
+        template = Template("""
+            {% load pykss %}
+            {% styleguideblock styleguide "2.1.1" %}
+            {% endstyleguideblock %}
+        """)
+        context = Context({'styleguide': self.styleguide})
+        template.render(context)
+        mock_render_to_string.assert_called_with('pykss/styleguideblock.html', ANY)
+
+    @patch('pykss.contrib.django.templatetags.pykss.render_to_string')
+    def test_allows_overiding_template(self, mock_render_to_string):
+        template = Template("""
+            {% load pykss %}
+            {% styleguideblock styleguide "2.1.1" using "custom.html" %}
+            {% endstyleguideblock %}
+        """)
+        context = Context({'styleguide': self.styleguide})
+        template.render(context)
+        mock_render_to_string.assert_called_with('custom.html', ANY)
+
+    def test_when_using_argument_is_wrong(self):
+        text = """
+            {% load pykss %}
+            {% styleguideblock styleguide "2.1.1" "custom.html" %}
+            {% endstyleguideblock %}
+        """
+        self.assertRaises(TemplateSyntaxError, Template, text)
+
+    def test_when_using_is_defined_without_template(self):
+        text = """
+            {% load pykss %}
+            {% styleguideblock styleguide "2.1.1" using %}
+            {% endstyleguideblock %}
+        """
+        self.assertRaises(TemplateSyntaxError, Template, text)
+
+    def test_renders_correctly(self):
+        template = Template("""
+            {% load pykss %}
+            {% styleguideblock styleguide "2.1.1" using "django.html" %}
+                
+            {% endstyleguideblock %}
+        """)
+        context = Context({'styleguide': self.styleguide})
+        results = simplejson.loads(template.render(context))
+        self.assertEqual(results['section'], '2.1.1')
+        self.assertEqual(results['filename'], 'buttons.css')
+        self.assertEqual(results['description'], 'Your standard form button.')
+        self.assertEqual(results['modifiers'][0], [':hover', 'Highlights when hovering.'])
+        self.assertEqual(results['example_html'], '')
+        self.assertEqual(results['modifier_examples'][0], [':hover', ''])
+        self.assertEqual(results['escaped_html'], '<i class=""></i>')
-- 
cgit v1.2.3