Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
Armin Ronacher | 26c0f51 | 2008-04-17 11:16:01 +0200 | [diff] [blame] | 3 | jinja2.tests |
| 4 | ~~~~~~~~~~~~ |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 5 | |
| 6 | Jinja test functions. Used with the "is" operator. |
| 7 | |
Armin Ronacher | 8ebf1f9 | 2007-03-03 11:22:18 +0100 | [diff] [blame] | 8 | :copyright: 2007 by Armin Ronacher. |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 9 | :license: BSD, see LICENSE for more details. |
| 10 | """ |
| 11 | import re |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 12 | from jinja2.runtime import Undefined |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 13 | |
| 14 | |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 15 | number_re = re.compile(r'^-?\d+(\.\d+)?$') |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 16 | regex_type = type(number_re) |
| 17 | |
| 18 | |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 19 | def test_odd(value): |
| 20 | """Return true if the variable is odd.""" |
| 21 | return value % 2 == 1 |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 22 | |
| 23 | |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 24 | def test_even(value): |
| 25 | """Return true of the variable is even.""" |
| 26 | return value % 2 == 0 |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 27 | |
| 28 | |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 29 | def test_divisibleby(value, num): |
| 30 | """Check if a variable is divisible by a number.""" |
| 31 | return value % num == 0 |
| 32 | |
| 33 | |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 34 | def test_defined(value): |
| 35 | """Return true if the variable is defined: |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 36 | |
Armin Ronacher | 37a8851 | 2007-03-02 20:42:18 +0100 | [diff] [blame] | 37 | .. sourcecode:: jinja |
| 38 | |
| 39 | {% if variable is defined %} |
| 40 | value of variable: {{ variable }} |
| 41 | {% else %} |
| 42 | variable is not defined |
| 43 | {% endif %} |
| 44 | |
Armin Ronacher | 26c0f51 | 2008-04-17 11:16:01 +0200 | [diff] [blame] | 45 | See the ``default`` filter for a simple way to set undefined |
| 46 | variables. |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 47 | """ |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 48 | return not isinstance(value, Undefined) |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 49 | |
| 50 | |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 51 | def test_undefined(value): |
| 52 | """Like `defined` but the other way round.""" |
| 53 | return isinstance(value, Undefined) |
| 54 | |
| 55 | |
| 56 | def test_none(value): |
| 57 | """Return true if the variable is none.""" |
| 58 | return value is None |
| 59 | |
| 60 | |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 61 | def test_lower(value): |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 62 | """Return true if the variable is lowercased.""" |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 63 | return unicode(value).islower() |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 64 | |
| 65 | |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 66 | def test_upper(value): |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 67 | """Return true if the variable is uppercased.""" |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 68 | return unicode(value).isupper() |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 69 | |
| 70 | |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 71 | def test_string(value): |
| 72 | """Return true if the object is a string.""" |
| 73 | return isinstance(value, basestring) |
| 74 | |
| 75 | |
| 76 | def test_number(value): |
| 77 | """Return true if the variable is a number.""" |
| 78 | return isinstance(value, (int, long, float, complex)) |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 79 | |
| 80 | |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 81 | def test_sequence(value): |
| 82 | """Return true if the variable is a sequence. Sequences are variables |
Armin Ronacher | 37a8851 | 2007-03-02 20:42:18 +0100 | [diff] [blame] | 83 | that are iterable. |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 84 | """ |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 85 | try: |
| 86 | len(value) |
| 87 | value.__getitem__ |
| 88 | except: |
| 89 | return False |
| 90 | return True |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 91 | |
| 92 | |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 93 | def test_sameas(value, other): |
| 94 | """Check if an object points to the same memory address than another |
Armin Ronacher | 69ddc58 | 2007-06-24 12:37:13 +0200 | [diff] [blame] | 95 | object: |
| 96 | |
| 97 | .. sourcecode:: jinja |
| 98 | |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 99 | {% if foo.attribute is sameas false %} |
Armin Ronacher | 69ddc58 | 2007-06-24 12:37:13 +0200 | [diff] [blame] | 100 | the foo attribute really is the `False` singleton |
| 101 | {% endif %} |
Armin Ronacher | 69ddc58 | 2007-06-24 12:37:13 +0200 | [diff] [blame] | 102 | """ |
Armin Ronacher | 577ad38 | 2008-04-16 15:36:49 +0200 | [diff] [blame] | 103 | return value is other |
Armin Ronacher | 69ddc58 | 2007-06-24 12:37:13 +0200 | [diff] [blame] | 104 | |
| 105 | |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 106 | def test_iterable(value): |
| 107 | """Check if it's possible to iterate over an object.""" |
| 108 | try: |
| 109 | iter(value) |
| 110 | except TypeError: |
| 111 | return False |
| 112 | return True |
| 113 | |
| 114 | |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 115 | def test_escaped(value): |
| 116 | """Check if the value is escaped.""" |
| 117 | return hasattr(value, '__html__') |
| 118 | |
| 119 | |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 120 | TESTS = { |
| 121 | 'odd': test_odd, |
| 122 | 'even': test_even, |
Armin Ronacher | f59bac2 | 2008-04-20 13:11:43 +0200 | [diff] [blame] | 123 | 'divisibleby': test_divisibleby, |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 124 | 'defined': test_defined, |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 125 | 'undefined': test_undefined, |
| 126 | 'none': test_none, |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 127 | 'lower': test_lower, |
| 128 | 'upper': test_upper, |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 129 | 'string': test_string, |
| 130 | 'number': test_number, |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 131 | 'sequence': test_sequence, |
Armin Ronacher | 5304229 | 2008-04-26 18:30:19 +0200 | [diff] [blame] | 132 | 'iterable': test_iterable, |
| 133 | 'callable': callable, |
Armin Ronacher | 3c8b7ad | 2008-04-28 13:52:21 +0200 | [diff] [blame] | 134 | 'sameas': test_sameas, |
| 135 | 'escaped': test_escaped |
Armin Ronacher | 5887538 | 2007-02-27 19:40:14 +0100 | [diff] [blame] | 136 | } |