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
Provenance
- Commit
577ad38- Tree
9192268- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5f51488
1 file changed+32 added−77 removed
modified:jinja2/tests.py+32 added-77 removed
7 unmodified lines8 :copyright: 2007 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10 """11 import re12+from jinja2.runtime import Undefined13 14 15 number_re = re.compile(r'^-?\d+(\.\d+)?$')16 regex_type = type(number_re)17 18 18-def test_odd():19- """20- Return true if the variable is odd.21- """22- return lambda e, c, v: v % 2 == 119+def test_odd(value):20+ """Return true if the variable is odd."""21+ return value % 2 == 122 23 25-def test_even():26- """27- Return true of the variable is even.28- """29- return lambda e, c, v: v % 2 == 024+def test_even(value):25+ """Return true of the variable is even."""26+ return value % 2 == 027 28 32-def test_defined():33- """34- Return true if the variable is defined:29+def test_defined(value):30+ """Return true if the variable is defined:31 32 .. sourcecode:: jinja33 34 {% if variable is defined %}3 unmodified lines38 {% endif %}39 40 See also the ``default`` filter.41 """46- return lambda e, c, v: v is not e.undefined_singleton42+ return not isinstance(value, Undefined)43 44 49-def test_lower():50- """51- Return true if the variable is lowercase.52- """53- return lambda e, c, v: isinstance(v, basestring) and v.islower()45+def test_lower(value):46+ """Return true if the variable is lowercase."""47+ return unicode(value).islower()48 49 56-def test_upper():57- """58- Return true if the variable is uppercase.59- """60- return lambda e, c, v: isinstance(v, basestring) and v.isupper()50+def test_upper(value):51+ """Return true if the variable is uppercase."""52+ return unicode(value).isupper()53 54 63-def 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)55+def 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)60 61 72-def test_sequence():73- """74- Return true if the variable is a sequence. Sequences are variables62+def 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 wrapped72 73 87-def 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.92-93- .. sourcecode:: jinja94-95- {% 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 wrapped115-116-117-def test_sameas(other):118- """119- Check if an object points to the same memory address than another74+def test_sameas(value, other):75+ """Check if an object points to the same memory address than another76 object:77 78 .. sourcecode:: jinja79 2 unmodified lines82 {% endif %}83 84 *New in Jinja 1.2*85 """130- return lambda e, c, v: v is other86+ return value is other87 88 89 TESTS = {90 'odd': test_odd,2 unmodified lines93 'lower': test_lower,94 'upper': test_upper,95 'numeric': test_numeric,96 'sequence': test_sequence,141- 'matching': test_matching,97 'sameas': test_sameas98 }