| | 1 | from unittest import TestCase |
| | 2 | from sys import version_info |
| | 3 | |
| | 4 | from django import template |
| | 5 | |
| | 6 | register = template.Library() |
| | 7 | |
| | 8 | # Very simple tag, with no parameters. |
| | 9 | def a_simple_tag_without_parameters(arg): |
| | 10 | """Expected __doc__""" |
| | 11 | return "Expected result" |
| | 12 | a_simple_tag_without_parameters.anything = "Expected __dict__" |
| | 13 | |
| | 14 | # Tag that takes a context. |
| | 15 | def a_simple_tag_with_context(context, arg): |
| | 16 | """Expected __doc__""" |
| | 17 | return "Expected result" |
| | 18 | a_simple_tag_with_context.anything = "Expected __dict__" |
| | 19 | |
| | 20 | # Tag that takes a nodelist. |
| | 21 | def a_simple_tag_with_block(nodelist, arg): |
| | 22 | """Expected __doc__""" |
| | 23 | return "Expected result" |
| | 24 | a_simple_tag_with_block.anything = "Expected __dict__" |
| | 25 | |
| | 26 | # Tag that takes both a context and a nodlist. |
| | 27 | def a_simple_tag_with_context_and_block(context, nodelist, arg): |
| | 28 | """Expected __doc__""" |
| | 29 | return "Expected result" |
| | 30 | a_simple_tag_with_context_and_block.anything = "Expected __dict__" |
| | 31 | |
| | 32 | # Tag that *wants* to take both a context and a nodelist, but that has arguments in wrong order. |
| | 33 | def a_simple_tag_with_context_and_block_wrong_order(nodelist, context, arg): |
| | 34 | """Expected __doc__""" |
| | 35 | return "Expected result" |
| | 36 | a_simple_tag_with_context_and_block_wrong_order.anything = "Expected __dict__" |
| | 37 | |
| | 38 | |
| | 39 | |
| | 40 | |
| | 41 | class DecoratorsTest(TestCase): |
| | 42 | def verify_decorator(self, decorator, func_name): |
| | 43 | # Only check __name__ on Python 2.4 or later since __name__ can't be |
| | 44 | # assigned to in earlier Python versions. |
| | 45 | if version_info[0] >= 2 and version_info[1] >= 4: |
| | 46 | self.assertEquals(decorator.__name__, func_name) |
| | 47 | self.assertEquals(decorator.__doc__, 'Expected __doc__') |
| | 48 | self.assertEquals(decorator.__dict__['anything'], 'Expected __dict__') |
| | 49 | |
| | 50 | def test_simple_tag(self): |
| | 51 | # Test that the decorators preserve the decorated function's docstring, name and attributes. |
| | 52 | decorator = register.simple_tag(a_simple_tag_without_parameters) |
| | 53 | self.verify_decorator(decorator, 'a_simple_tag_without_parameters') |
| | 54 | |
| | 55 | decorator = register.simple_tag(takes_context=True)(a_simple_tag_with_context) |
| | 56 | self.verify_decorator(decorator, 'a_simple_tag_with_context') |
| | 57 | |
| | 58 | decorator = register.simple_tag(takes_nodelist=True)(a_simple_tag_with_block) |
| | 59 | self.verify_decorator(decorator, 'a_simple_tag_with_block') |
| | 60 | |
| | 61 | decorator = register.simple_tag(takes_context=True, takes_nodelist=True)(a_simple_tag_with_context_and_block) |
| | 62 | self.verify_decorator(decorator, 'a_simple_tag_with_context_and_block') |
| | 63 | |
| | 64 | # Now test that 'context' and 'nodelist' arguments and their order are correct. |
| | 65 | decorator = register.simple_tag(takes_context=True) |
| | 66 | self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters) |
| | 67 | |
| | 68 | decorator = register.simple_tag(takes_nodelist=True) |
| | 69 | self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters) |
| | 70 | |
| | 71 | decorator = register.simple_tag(takes_nodelist=True, takes_context=True) |
| | 72 | self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_with_context_and_block_wrong_order) |
| | 73 | from unittest import TestCase |
| | 74 | from sys import version_info |
| | 75 | |
| | 76 | from django import template |
| | 77 | |
| | 78 | register = template.Library() |
| | 79 | |
| | 80 | # Very simple tag, with no parameters. |
| | 81 | def a_simple_tag_without_parameters(arg): |
| | 82 | """Expected __doc__""" |
| | 83 | return "Expected result" |
| | 84 | a_simple_tag_without_parameters.anything = "Expected __dict__" |
| | 85 | |
| | 86 | # Tag that takes the context. |
| | 87 | def a_simple_tag_with_context(context, arg): |
| | 88 | """Expected __doc__""" |
| | 89 | return "Expected result" |
| | 90 | a_simple_tag_with_context.anything = "Expected __dict__" |
| | 91 | |
| | 92 | # Tag that takes the inner block. |
| | 93 | def a_simple_tag_with_block(nodelist, arg): |
| | 94 | """Expected __doc__""" |
| | 95 | return "Expected result" |
| | 96 | a_simple_tag_with_block.anything = "Expected __dict__" |
| | 97 | |
| | 98 | # Tag that takes both the context and the inner block. |
| | 99 | def a_simple_tag_with_context_and_block(context, nodelist, arg): |
| | 100 | """Expected __doc__""" |
| | 101 | return "Expected result" |
| | 102 | a_simple_tag_with_context_and_block.anything = "Expected __dict__" |
| | 103 | |
| | 104 | # Tag that *wants* to take both the context and the inner block, but that has arguments in wrong order. |
| | 105 | def a_simple_tag_with_context_and_block_wrong_order(nodelist, context, arg): |
| | 106 | """Expected __doc__""" |
| | 107 | return "Expected result" |
| | 108 | a_simple_tag_with_context_and_block_wrong_order.anything = "Expected __dict__" |
| | 109 | |
| | 110 | |
| | 111 | |
| | 112 | |
| | 113 | class DecoratorsTest(TestCase): |
| | 114 | def verify_decorator(self, decorator, func_name): |
| | 115 | # Only check __name__ on Python 2.4 or later since __name__ can't be |
| | 116 | # assigned to in earlier Python versions. |
| | 117 | if version_info[0] >= 2 and version_info[1] >= 4: |
| | 118 | self.assertEquals(decorator.__name__, func_name) |
| | 119 | self.assertEquals(decorator.__doc__, 'Expected __doc__') |
| | 120 | self.assertEquals(decorator.__dict__['anything'], 'Expected __dict__') |
| | 121 | |
| | 122 | def test_simple_tag(self): |
| | 123 | # Test that the decorators preserve the decorated function's docstring, name and attributes. |
| | 124 | decorator = register.simple_tag(a_simple_tag_without_parameters) |
| | 125 | self.verify_decorator(decorator, 'a_simple_tag_without_parameters') |
| | 126 | |
| | 127 | decorator = register.simple_tag(takes_context=True)(a_simple_tag_with_context) |
| | 128 | self.verify_decorator(decorator, 'a_simple_tag_with_context') |
| | 129 | |
| | 130 | decorator = register.simple_tag(takes_nodelist=True)(a_simple_tag_with_block) |
| | 131 | self.verify_decorator(decorator, 'a_simple_tag_with_block') |
| | 132 | |
| | 133 | decorator = register.simple_tag(takes_context=True, takes_nodelist=True)(a_simple_tag_with_context_and_block) |
| | 134 | self.verify_decorator(decorator, 'a_simple_tag_with_context_and_block') |
| | 135 | |
| | 136 | # Now test that 'context' and 'nodelist' arguments and their order are correct. |
| | 137 | decorator = register.simple_tag(takes_context=True) |
| | 138 | self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters) |
| | 139 | |
| | 140 | decorator = register.simple_tag(takes_nodelist=True) |
| | 141 | self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters) |
| | 142 | |
| | 143 | decorator = register.simple_tag(takes_nodelist=True, takes_context=True) |
| | 144 | self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_with_context_and_block_wrong_order) |