diff --git a/pattern_library/monkey_utils.py b/pattern_library/monkey_utils.py index f00a192f..a030b220 100644 --- a/pattern_library/monkey_utils.py +++ b/pattern_library/monkey_utils.py @@ -25,6 +25,7 @@ def tag_func(parser, token): def node_render(context): if is_pattern_library_context(context): + tag_overridden = False result = '' # Load pattern's config @@ -42,21 +43,23 @@ def node_render(context): # Get config for specific arguments tag_config = tag_config.get(arguments, {}) - # Return raw data (it can be string or a structure), if defined + if 'raw' in tag_config: + # Return raw data (it can be string or a structure), if defined + result = tag_config['raw'] + tag_overridden = True + elif 'template_name' in tag_config: + # Render pattern, if raw string is not defined + template_name = tag_config['template_name'] + request = context.get('request') + result = render_pattern(request, template_name) + tag_overridden = True + # TODO: Allow objects with the __str__ method # In some cases we must return an object that can # be rendered as a string `{{ result }}` # and allow users to access it's attributes `{{ result.url }}` - result = tag_config.get('raw') - - # Render pattern, if raw string is not defined - if not result: - template_name = tag_config.get('template_name') - if template_name: - request = context.get('request') - result = render_pattern(request, template_name) - if result: + if tag_overridden: if isinstance(original_node, SimpleNode): # If it's a SimpleNode try to use it's target_var target_var = original_node.target_var diff --git a/runtests.py b/runtests.py index a00f5866..376a46a5 100755 --- a/runtests.py +++ b/runtests.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +import argparse import os import sys @@ -8,15 +9,32 @@ import coverage +parser = argparse.ArgumentParser() +parser.add_argument( + '-v', '--verbosity', action='store', dest='verbosity', default=1, + type=int, choices=range(4), + help="Verbosity level; 0=minimal output, 1=normal output, 2=verbose output, 3=very verbose output", +) + if __name__ == "__main__": + # Coverage setup cov = coverage.Coverage() cov.start() + + # Django setup os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings' django.setup() + + # Test runner setup TestRunner = get_runner(settings) - test_runner = TestRunner() + TestRunner.add_arguments(parser) + args = parser.parse_args() + test_runner = TestRunner(**vars(args)) failures = test_runner.run_tests(["tests"]) + + # Generate coverage report cov.stop() cov.save() cov.html_report() + sys.exit(bool(failures)) diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html new file mode 100644 index 00000000..09258c7d --- /dev/null +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.html @@ -0,0 +1,5 @@ +{% load test_tags %} + +SAND{% error_tag empty_string %}WICH +SAND{% error_tag none %}WICH +SAND{% error_tag zero %}WICH diff --git a/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml new file mode 100644 index 00000000..0fe3edf6 --- /dev/null +++ b/tests/templates/patterns/atoms/tags_test_atom/tags_test_atom.yaml @@ -0,0 +1,8 @@ +tags: + error_tag: + empty_string: + raw: '' + none: + raw: null + zero: + raw: 0 diff --git a/tests/templatetags/__init__.py b/tests/templatetags/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/templatetags/test_tags.py b/tests/templatetags/test_tags.py new file mode 100644 index 00000000..581edb9d --- /dev/null +++ b/tests/templatetags/test_tags.py @@ -0,0 +1,14 @@ +from django import template + +from pattern_library.monkey_utils import override_tag + +register = template.Library() + + +@register.simple_tag +def error_tag(arg=None): + "Just raise an exception, never do anything" + raise Exception("error_tag raised an exception") + + +override_tag(register, 'error_tag') diff --git a/tests/tests/test_tags.py b/tests/tests/test_tags.py index a525f061..efee2592 100644 --- a/tests/tests/test_tags.py +++ b/tests/tests/test_tags.py @@ -1,5 +1,13 @@ +from django.core.urlresolvers import reverse from django.test import SimpleTestCase class TagsTesCase(SimpleTestCase): - pass + def test_falsey_raw_values_for_tag_output(self): + response = self.client.get(reverse( + 'pattern_library:display_pattern', + kwargs={'template_name': 'patterns/atoms/tags_test_atom/tags_test_atom.html'}, + )) + self.assertContains(response, "SANDWICH") + self.assertContains(response, "SANDNoneWICH") + self.assertContains(response, "SAND0WICH")