PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/templates/custom.py

https://code.google.com/p/mango-py/
Python | 80 lines | 57 code | 19 blank | 4 comment | 0 complexity | cb19c15e786c1556e120e2265c010dde MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django import template
  2. from django.utils.unittest import TestCase
  3. from templatetags import custom
  4. class CustomFilterTests(TestCase):
  5. def test_filter(self):
  6. t = template.Template("{% load custom %}{{ string|trim:5 }}")
  7. self.assertEqual(
  8. t.render(template.Context({"string": "abcdefghijklmnopqrstuvwxyz"})),
  9. u"abcde"
  10. )
  11. class CustomTagTests(TestCase):
  12. def verify_tag(self, tag, name):
  13. self.assertEqual(tag.__name__, name)
  14. self.assertEqual(tag.__doc__, 'Expected %s __doc__' % name)
  15. self.assertEqual(tag.__dict__['anything'], 'Expected %s __dict__' % name)
  16. def test_simple_tags(self):
  17. c = template.Context({'value': 42})
  18. t = template.Template('{% load custom %}{% no_params %}')
  19. self.assertEqual(t.render(c), u'no_params - Expected result')
  20. t = template.Template('{% load custom %}{% one_param 37 %}')
  21. self.assertEqual(t.render(c), u'one_param - Expected result: 37')
  22. t = template.Template('{% load custom %}{% explicit_no_context 37 %}')
  23. self.assertEqual(t.render(c), u'explicit_no_context - Expected result: 37')
  24. t = template.Template('{% load custom %}{% no_params_with_context %}')
  25. self.assertEqual(t.render(c), u'no_params_with_context - Expected result (context value: 42)')
  26. t = template.Template('{% load custom %}{% params_and_context 37 %}')
  27. self.assertEqual(t.render(c), u'params_and_context - Expected result (context value: 42): 37')
  28. def test_simple_tag_registration(self):
  29. # Test that the decorators preserve the decorated function's docstring, name and attributes.
  30. self.verify_tag(custom.no_params, 'no_params')
  31. self.verify_tag(custom.one_param, 'one_param')
  32. self.verify_tag(custom.explicit_no_context, 'explicit_no_context')
  33. self.verify_tag(custom.no_params_with_context, 'no_params_with_context')
  34. self.verify_tag(custom.params_and_context, 'params_and_context')
  35. def test_simple_tag_missing_context(self):
  36. # That the 'context' parameter must be present when takes_context is True
  37. def a_simple_tag_without_parameters(arg):
  38. """Expected __doc__"""
  39. return "Expected result"
  40. register = template.Library()
  41. decorator = register.simple_tag(takes_context=True)
  42. self.assertRaises(template.TemplateSyntaxError, decorator, a_simple_tag_without_parameters)
  43. def test_inclusion_tags(self):
  44. c = template.Context({'value': 42})
  45. t = template.Template('{% load custom %}{% inclusion_no_params %}')
  46. self.assertEquals(t.render(c), u'inclusion_no_params - Expected result\n')
  47. t = template.Template('{% load custom %}{% inclusion_one_param 37 %}')
  48. self.assertEquals(t.render(c), u'inclusion_one_param - Expected result: 37\n')
  49. t = template.Template('{% load custom %}{% inclusion_explicit_no_context 37 %}')
  50. self.assertEquals(t.render(c), u'inclusion_explicit_no_context - Expected result: 37\n')
  51. t = template.Template('{% load custom %}{% inclusion_no_params_with_context %}')
  52. self.assertEquals(t.render(c), u'inclusion_no_params_with_context - Expected result (context value: 42)\n')
  53. t = template.Template('{% load custom %}{% inclusion_params_and_context 37 %}')
  54. self.assertEquals(t.render(c), u'inclusion_params_and_context - Expected result (context value: 42): 37\n')
  55. def test_inclusion_tag_registration(self):
  56. # Test that the decorators preserve the decorated function's docstring, name and attributes.
  57. self.verify_tag(custom.inclusion_no_params, 'inclusion_no_params')
  58. self.verify_tag(custom.inclusion_one_param, 'inclusion_one_param')
  59. self.verify_tag(custom.inclusion_explicit_no_context, 'inclusion_explicit_no_context')
  60. self.verify_tag(custom.inclusion_no_params_with_context, 'inclusion_no_params_with_context')
  61. self.verify_tag(custom.inclusion_params_and_context, 'inclusion_params_and_context')