Skip to content
Myr

ported the tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 5f51488 · Browse files · 1 file+32 added−77 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+32 added−77 removed

  • other:+32 added−77 removed

1 file+32 added−77 removed

modified:jinja2/tests.py+32 added-77 removed
7 unmodified lines
8 :copyright: 2007 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11import re12from jinja2.runtime import Undefined131415number_re = re.compile(r'^-?\d+(\.\d+)?$')16regex_type = type(number_re)171818def test_odd():19 """20 Return true if the variable is odd.21 """22 return lambda e, c, v: v % 2 == 119def test_odd(value):20 """Return true if the variable is odd."""21 return value % 2 == 1222325def test_even():26 """27 Return true of the variable is even.28 """29 return lambda e, c, v: v % 2 == 024def test_even(value):25 """Return true of the variable is even."""26 return value % 2 == 0272832def test_defined():33 """34 Return true if the variable is defined:29def test_defined(value):30 """Return true if the variable is defined:3132 .. sourcecode:: jinja3334 {% if variable is defined %}
3 unmodified lines
38 {% endif %}3940 See also the ``default`` filter.41 """46 return lambda e, c, v: v is not e.undefined_singleton42 return not isinstance(value, Undefined)434449def test_lower():50 """51 Return true if the variable is lowercase.52 """53 return lambda e, c, v: isinstance(v, basestring) and v.islower()45def test_lower(value):46 """Return true if the variable is lowercase."""47 return unicode(value).islower()484956def test_upper():57 """58 Return true if the variable is uppercase.59 """60 return lambda e, c, v: isinstance(v, basestring) and v.isupper()50def test_upper(value):51 """Return true if the variable is uppercase."""52 return unicode(value).isupper()535463def test_numeric():64 """65 Return true if the variable is numeric.66 """67 return lambda e, c, v: isinstance(v, (int, long, float)) or (68 isinstance(v, basestring) and69 number_re.match(v) is not None)55def test_numeric(value):56 """Return true if the variable is numeric."""57 return isinstance(value, (int, long, float)) or (58 isinstance(value, basestring) and59 number_re.match(value) is not None)606172def test_sequence():73 """74 Return true if the variable is a sequence. Sequences are variables62def test_sequence(value):63 """Return true if the variable is a sequence. Sequences are variables64 that are iterable.65 """77 def wrapped(environment, context, value):78 try:79 len(value)80 value.__getitem__81 except:82 return False83 return True66 try:67 len(value)68 value.__getitem__69 except:70 return False71 return True84 return wrapped727387def test_matching(regex):88 r"""89 Test if the variable matches the regular expression given. Note that90 you have to escape special chars using *two* backslashes, these are91 *not* raw strings.9293 .. sourcecode:: jinja9495 {% if var is matching @/^\d+$/ %}96 var looks like a number97 {% else %}98 var doesn't really look like a number99 {% endif %}100 """101 def wrapped(environment, context, value):102 if type(regex) is regex_type:103 regex_ = regex104 else:105 if environment.disable_regexps:106 raise RuntimeError('regular expressions disabled.')107 if isinstance(regex, unicode):108 regex_ = re.compile(regex, re.U)109 elif isinstance(regex, str):110 regex_ = re.compile(regex)111 else:112 return False113 return regex_.search(value) is not None114 return wrapped115116117def test_sameas(other):118 """119 Check if an object points to the same memory address than another74def test_sameas(value, other):75 """Check if an object points to the same memory address than another76 object:7778 .. sourcecode:: jinja79
2 unmodified lines
82 {% endif %}8384 *New in Jinja 1.2*85 """130 return lambda e, c, v: v is other86 return value is other878889TESTS = {90 'odd': test_odd,
2 unmodified lines
93 'lower': test_lower,94 'upper': test_upper,95 'numeric': test_numeric,96 'sequence': test_sequence,141 'matching': test_matching,97 'sameas': test_sameas98}