diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-04-28 17:03:37 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-04-28 17:03:37 -0400 |
| commit | 70981099103828afbe33eb4d1f668dc20894b399 (patch) | |
| tree | 4b9c0a98829b4d409211a8d536ca4e94e7450c8d /test | |
| parent | d1f6d7f3e7e698eff2c7774b63531f151d908209 (diff) | |
| download | external_python_mako-70981099103828afbe33eb4d1f668dc20894b399.tar.gz external_python_mako-70981099103828afbe33eb4d1f668dc20894b399.tar.bz2 external_python_mako-70981099103828afbe33eb4d1f668dc20894b399.zip | |
- switch to argparse for cmdline template runner
- write a test suite for cmdline
- start using context manager helpers in tests. intrinsic here is that
we're going to go 1.0 and drop at least 2.4 and probably 2.5
- update .gitignore
Diffstat (limited to 'test')
| -rw-r--r-- | test/__init__.py | 28 | ||||
| -rw-r--r-- | test/templates/cmd_good.mako | 1 | ||||
| -rw-r--r-- | test/templates/cmd_runtime.mako | 1 | ||||
| -rw-r--r-- | test/templates/cmd_syntax.mako | 1 | ||||
| -rw-r--r-- | test/test_cmd.py | 73 |
5 files changed, 93 insertions, 11 deletions
diff --git a/test/__init__.py b/test/__init__.py index 64dde8e..91ff54e 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -2,11 +2,12 @@ from mako.template import Template import unittest import os from mako.compat import py3k, py26, py25 +from mako import compat from mako.util import update_wrapper import re from mako.cache import CacheImpl, register_plugin from nose import SkipTest -import sys +import contextlib template_base = os.path.join(os.path.dirname(__file__), 'templates') module_base = os.path.join(template_base, 'modules') @@ -61,24 +62,29 @@ def teardown(): import shutil shutil.rmtree(module_base, True) -def assert_raises(except_cls, callable_, *args, **kw): +@contextlib.contextmanager +def raises(except_cls, message=None): try: - callable_(*args, **kw) + yield success = False - except except_cls: + except except_cls as e: + if message: + assert re.search(message, compat.text_type(e), re.UNICODE), \ + "%r !~ %s" % (message, e) + print(compat.text_type(e).encode('utf-8')) success = True # assert outside the block so it works for AssertionError too ! assert success, "Callable did not raise an exception" + +def assert_raises(except_cls, callable_, *args, **kw): + with raises(except_cls): + return callable_(*args, **kw) + def assert_raises_message(except_cls, msg, callable_, *args, **kwargs): - try: - callable_(*args, **kwargs) - assert False, "Callable did not raise an exception" - except except_cls: - e = sys.exc_info()[1] - assert re.search(msg, str(e)), "%r !~ %s" % (msg, e) - print(str(e)) + with raises(except_cls, msg): + return callable_(*args, **kwargs) def skip_if(predicate, reason=None): """Skip a test if predicate is true.""" diff --git a/test/templates/cmd_good.mako b/test/templates/cmd_good.mako new file mode 100644 index 0000000..68ebec4 --- /dev/null +++ b/test/templates/cmd_good.mako @@ -0,0 +1 @@ +hello world ${x}
\ No newline at end of file diff --git a/test/templates/cmd_runtime.mako b/test/templates/cmd_runtime.mako new file mode 100644 index 0000000..6c2675b --- /dev/null +++ b/test/templates/cmd_runtime.mako @@ -0,0 +1 @@ +${q}
\ No newline at end of file diff --git a/test/templates/cmd_syntax.mako b/test/templates/cmd_syntax.mako new file mode 100644 index 0000000..d2117db --- /dev/null +++ b/test/templates/cmd_syntax.mako @@ -0,0 +1 @@ +${x
\ No newline at end of file diff --git a/test/test_cmd.py b/test/test_cmd.py new file mode 100644 index 0000000..d7e07ae --- /dev/null +++ b/test/test_cmd.py @@ -0,0 +1,73 @@ +from __future__ import with_statement +from contextlib import contextmanager +from test import TemplateTest, eq_, raises, template_base +import os +import mock +from mako.cmd import cmdline + +class CmdTest(TemplateTest): + @contextmanager + def _capture_output_fixture(self, stream="stdout"): + with mock.patch("sys.%s" % stream) as stdout: + yield stdout + + def test_stdin_success(self): + with self._capture_output_fixture() as stdout: + with mock.patch("sys.stdin", mock.Mock( + read=mock.Mock(return_value="hello world ${x}"))): + cmdline(["--var", "x=5", "-"]) + + eq_(stdout.write.mock_calls[0][1][0], "hello world 5") + + def test_stdin_syntax_err(self): + with mock.patch("sys.stdin", mock.Mock( + read=mock.Mock(return_value="${x"))): + with self._capture_output_fixture("stderr") as stderr: + with raises(SystemExit): + cmdline(["--var", "x=5", "-"]) + + assert "SyntaxException: Expected" in \ + stderr.write.mock_calls[0][1][0] + assert "Traceback" in stderr.write.mock_calls[0][1][0] + + + def test_stdin_rt_err(self): + with mock.patch("sys.stdin", mock.Mock( + read=mock.Mock(return_value="${q}"))): + with self._capture_output_fixture("stderr") as stderr: + with raises(SystemExit): + cmdline(["--var", "x=5", "-"]) + + assert "NameError: Undefined" in stderr.write.mock_calls[0][1][0] + assert "Traceback" in stderr.write.mock_calls[0][1][0] + + def test_file_success(self): + with self._capture_output_fixture() as stdout: + cmdline(["--var", "x=5", + os.path.join(template_base, "cmd_good.mako")]) + + eq_(stdout.write.mock_calls[0][1][0], "hello world 5") + + def test_file_syntax_err(self): + with self._capture_output_fixture("stderr") as stderr: + with raises(SystemExit): + cmdline(["--var", "x=5", + os.path.join(template_base, "cmd_syntax.mako")]) + + assert "SyntaxException: Expected" in stderr.write.mock_calls[0][1][0] + assert "Traceback" in stderr.write.mock_calls[0][1][0] + + def test_file_rt_err(self): + with self._capture_output_fixture("stderr") as stderr: + with raises(SystemExit): + cmdline(["--var", "x=5", + os.path.join(template_base, "cmd_runtime.mako")]) + + assert "NameError: Undefined" in stderr.write.mock_calls[0][1][0] + assert "Traceback" in stderr.write.mock_calls[0][1][0] + + + def test_file_notfound(self): + with raises(SystemExit, "error: can't find fake.lalala"): + cmdline(["--var", "x=5", "fake.lalala"]) + |
