Ticket #8462: add_template_length_filters_tests.diff
File add_template_length_filters_tests.diff, 2.2 KB (added by , 16 years ago) |
---|
-
django_trunk/django/template/defaultfilters.py
507 507 508 508 def length(value): 509 509 """Returns the length of the value - useful for lists.""" 510 return len(value) 510 try: 511 return len(value) 512 except (ValueError,TypeError): 513 return '' 511 514 length.is_safe = True 512 515 513 516 def length_is(value, arg): 514 517 """Returns a boolean of whether the value's length is the argument.""" 515 return len(value) == int(arg) 518 try: 519 return len(value) == int(arg) 520 except (ValueError,TypeError): 521 return '' 516 522 length_is.is_safe = False 517 523 518 524 def random(value): -
django_trunk/tests/regressiontests/templates/tests.py
811 811 'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')), 812 812 'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')), 813 813 814 # usage of length and length_is filters 815 'length01': ('{{ lengthless_number|length }}', {'lengthless_number': 7}, ''), 816 'length02': ('{{ some_list|length }}', {'some_list': ['4', None, True, {}]}, '4'), 817 'length03': ('{{ some_list|length }}', {'some_list': []}, '0'), 818 'length04': ('{{ None|length }}', {'None': None}, ''), 819 'length_is01': ('{{ lengthless_number|length_is:"1" }}', {'lengthless_number': 7}, ''), 820 'length_is02': ('{% if some_list|length_is:"4" %}Four{% endif %}', {'some_list': ['4', None, True, {}]}, 'Four'), 821 'length_is03': ('{% if some_list|length_is:"4" %}Four{% else %}Not Four{% endif %}', {'some_list': ['4', None, True, {}, 17]}, 'Not Four'), 822 'length_is04': ('{% if nested_list|length_is:"4" %}Four{% endif %}', {'nested_list': ['4', [4, 3, 1], True, ['a', None]]}, 'Four'), 823 814 824 ### MULTILINE ############################################################# 815 825 816 826 'multiline01': ("""