Ticket #8462: add_template_length_filters_tests.diff

File add_template_length_filters_tests.diff, 2.2 KB (added by rob, 16 years ago)

keeping the filter diffs and test diffs together

  • django_trunk/django/template/defaultfilters.py

     
    507507
    508508def length(value):
    509509    """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 ''
    511514length.is_safe = True
    512515
    513516def length_is(value, arg):
    514517    """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 ''
    516522length_is.is_safe = False
    517523
    518524def random(value):
  • django_trunk/tests/regressiontests/templates/tests.py

     
    811811            'invalidstr05': ('{{ var }}', {}, ('', 'INVALID %s', 'var')),
    812812            'invalidstr06': ('{{ var.prop }}', {'var': {}}, ('', 'INVALID %s', 'var.prop')),
    813813
     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
    814824            ### MULTILINE #############################################################
    815825
    816826            'multiline01': ("""
Back to Top