~ubuntu-branches/ubuntu/utopic/mako/utopic-proposed

« back to all changes in this revision

Viewing changes to test/__init__.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2014-06-10 20:38:26 UTC
  • mfrom: (1.4.7)
  • Revision ID: package-import@ubuntu.com-20140610203826-5gtppywd9v3gf14a
Tags: 1.0.0-1
* New upstream release
* Add python-changelog and python-sphinx-paramlinks to Build-Depends
  (needed while rebuilding documentation)
* Enable Python 3.X tests during build (add necessary packages to
  Build-Depends)
* Update links to upstream changelog (now points to changelog.rst)
* Add lintian override for source-is-missing doc/searchindex.js
  (this file is generated by sphinx-build, all sources are in the tarball)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from mako.template import Template
2
2
import unittest
3
3
import os
4
 
from mako.compat import py3k, py26, py25
5
 
from mako.util import function_named
 
4
from mako.compat import py3k, py26, py33
 
5
from mako import compat
 
6
from mako.util import update_wrapper
6
7
import re
7
8
from mako.cache import CacheImpl, register_plugin
8
9
from nose import SkipTest
9
 
import sys
 
10
import contextlib
10
11
 
11
12
template_base = os.path.join(os.path.dirname(__file__), 'templates')
12
13
module_base = os.path.join(template_base, 'modules')
61
62
    import shutil
62
63
    shutil.rmtree(module_base, True)
63
64
 
64
 
def assert_raises(except_cls, callable_, *args, **kw):
 
65
if py33:
 
66
    from unittest import mock
 
67
else:
 
68
    import mock
 
69
 
 
70
@contextlib.contextmanager
 
71
def raises(except_cls, message=None):
65
72
    try:
66
 
        callable_(*args, **kw)
 
73
        yield
67
74
        success = False
68
 
    except except_cls:
 
75
    except except_cls as e:
 
76
        if message:
 
77
            assert re.search(message, compat.text_type(e), re.UNICODE), \
 
78
                            "%r !~ %s" % (message, e)
 
79
            print(compat.text_type(e).encode('utf-8'))
69
80
        success = True
70
81
 
71
82
    # assert outside the block so it works for AssertionError too !
72
83
    assert success, "Callable did not raise an exception"
73
84
 
 
85
 
 
86
def assert_raises(except_cls, callable_, *args, **kw):
 
87
    with raises(except_cls):
 
88
        return callable_(*args, **kw)
 
89
 
74
90
def assert_raises_message(except_cls, msg, callable_, *args, **kwargs):
75
 
    try:
76
 
        callable_(*args, **kwargs)
77
 
        assert False, "Callable did not raise an exception"
78
 
    except except_cls:
79
 
        e = sys.exc_info()[1]
80
 
        assert re.search(msg, str(e)), "%r !~ %s" % (msg, e)
81
 
        print(str(e))
 
91
    with raises(except_cls, msg):
 
92
        return callable_(*args, **kwargs)
82
93
 
83
94
def skip_if(predicate, reason=None):
84
95
    """Skip a test if predicate is true."""
93
104
                raise SkipTest(msg)
94
105
            else:
95
106
                return fn(*args, **kw)
96
 
        return function_named(maybe, fn_name)
 
107
        return update_wrapper(maybe, fn)
97
108
    return decorate
98
109
 
 
110
def requires_python_3(fn):
 
111
    return skip_if(lambda: not py3k, "Requires Python 3.xx")(fn)
 
112
 
99
113
def requires_python_2(fn):
100
114
    return skip_if(lambda: py3k, "Requires Python 2.xx")(fn)
101
115
 
102
116
def requires_python_26_or_greater(fn):
103
117
    return skip_if(lambda: not py26, "Requires Python 2.6 or greater")(fn)
104
118
 
105
 
def requires_python_25_or_greater(fn):
106
 
    return skip_if(lambda: not py25, "Requires Python 2.5 or greater")(fn)
107
 
 
108
119
def requires_pygments_14(fn):
109
120
    try:
110
121
        import pygments
121
132
            return fn(*arg, **kw)
122
133
        finally:
123
134
            exceptions._install_highlighting()
124
 
    return function_named(go, fn.__name__)
 
135
    return update_wrapper(go, fn)
125
136
 
126
137
class PlainCacheImpl(CacheImpl):
127
138
    """Simple memory cache impl so that tests which