From ab373162fafa9e543e0414d81c36a3b294ce0afa Mon Sep 17 00:00:00 2001 From: Yosuke Mizutani Date: Sun, 25 Oct 2015 19:37:08 +0900 Subject: [PATCH] add withAssertOutputFile method to TestCase --- .travis.yml | 1 + README.rst | 3 ++- setup.py | 1 + src/mog_commons/__init__.py | 2 +- src/mog_commons/unittest.py | 34 +++++++++++++++++++++++++--- tests/mog_commons/test_unittest.py | 21 +++++++++++++++++ tests/resources/utf8_ja.txt | 1 + tests/resources/utf8_ja_template.txt | 2 ++ 8 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 tests/resources/utf8_ja.txt create mode 100644 tests/resources/utf8_ja_template.txt diff --git a/.travis.yml b/.travis.yml index 2801c27..b84fe5a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ python: - "3.2" - "3.3" - "3.4" + - "3.5" cache: pip diff --git a/README.rst b/README.rst index a568097..ed94e3c 100644 --- a/README.rst +++ b/README.rst @@ -28,9 +28,10 @@ Common utility library for Python Dependencies ------------ -* Python: 2.6 / 2.7 / 3.2 / 3.3 / 3.4 +* Python: 2.6 / 2.7 / 3.2 / 3.3 / 3.4 / 3.5 * six * unittest2 +* jinja2: 2.6 ------------ Installation diff --git a/setup.py b/setup.py index 30b98a8..f0e24a5 100644 --- a/setup.py +++ b/setup.py @@ -21,6 +21,7 @@ def get_version(): install_requires=[ 'six', 'unittest2', + 'jinja2 == 2.6', # specify library version to support Python 3.2 ], tests_require=[ ], diff --git a/src/mog_commons/__init__.py b/src/mog_commons/__init__.py index 2fb2513..124e462 100644 --- a/src/mog_commons/__init__.py +++ b/src/mog_commons/__init__.py @@ -1 +1 @@ -__version__ = '0.1.6' +__version__ = '0.1.7' diff --git a/src/mog_commons/unittest.py b/src/mog_commons/unittest.py index cbba8c4..0f1f9e3 100644 --- a/src/mog_commons/unittest.py +++ b/src/mog_commons/unittest.py @@ -2,7 +2,10 @@ import sys import six +import io +import tempfile from contextlib import contextmanager +import jinja2 # unittest if sys.version_info < (2, 7): @@ -10,7 +13,7 @@ else: import unittest as base_unittest -from mog_commons.string import to_bytes +from mog_commons.string import to_bytes, to_str class StringBuffer(object): @@ -43,12 +46,15 @@ def assertRaisesRegexp(self, expected_exception, expected_regexp, callable_obj=N We don't use built-in assertRaisesRegexp because it is unicode-unsafe. """ + encoding = 'utf-8' with self.assertRaises(expected_exception) as cm: callable_obj(*args, **kwargs) if six.PY2: - self.assertRegexpMatches(str(cm.exception), expected_regexp) + # to avoid UnicodeEncodeError, use exception.message in Python2 + msg = cm.exception.message or to_str(cm.exception, encoding) + self.assertRegexpMatches(msg, expected_regexp) else: - self.assertRegex(str(cm.exception), expected_regexp) + self.assertRegex(to_str(cm.exception, encoding), expected_regexp) @contextmanager def withOutput(self): @@ -79,6 +85,28 @@ def assertOutput(self, expected_stdout, expected_stderr, function, encoding='utf with self.withAssertOutput(expected_stdout, expected_stderr, encoding) as (out, err): function() + @contextmanager + def withAssertOutputFile(self, expect_file, variables=None, expect_file_encoding='utf-8', output_encoding='utf-8'): + """ + Create a temporary file as output and compare with the file content. + + :param expect_file: string: path to the file which contains the expected output + :param variables: dict: variables for template engine jinja2 + :param expect_file_encoding: string: + :param output_encoding: string: + """ + with tempfile.TemporaryFile() as out: + yield out + + with io.open(expect_file, encoding=expect_file_encoding) as f: + expect = f.read() + if variables: + expect = jinja2.Template(expect).render(**variables) + + out.seek(0) + actual = out.read().decode(output_encoding) + self.assertMultiLineEqual(actual, expect) + def assertSystemExit(self, expected_code, callable_obj=None, *args, **kwargs): """ Fail unless SystemExit is raised by callableObj when invoked with arguments args and keyword arguments kwargs. diff --git a/tests/mog_commons/test_unittest.py b/tests/mog_commons/test_unittest.py index 5ccfa28..d1ba439 100644 --- a/tests/mog_commons/test_unittest.py +++ b/tests/mog_commons/test_unittest.py @@ -1,6 +1,8 @@ +# -*- coding: utf-8 -*- from __future__ import division, print_function, absolute_import, unicode_literals import sys +import os from mog_commons import unittest @@ -10,6 +12,7 @@ def f(): print('abc') print('123') sys.stderr.writelines(['def\n', '456\n']) + self.assertOutput('abc\n123\n', 'def\n456\n', f) def test_assert_output_fail(self): @@ -17,6 +20,7 @@ def f(): print('abc') print('123') sys.stderr.writelines(['def\n', '456\n']) + self.assertRaisesRegexp(AssertionError, 'abc.+ != ', self.assertOutput, '', 'def\n456\n', f) self.assertRaisesRegexp(AssertionError, 'def.+ != ', self.assertOutput, 'abc\n123\n', '', f) self.assertRaisesRegexp(AssertionError, 'def.+ != .+def', self.assertOutput, 'abc\n123\n', 'def\n456\n\n', f) @@ -25,6 +29,23 @@ def test_assert_system_exit(self): self.assertSystemExit(123, lambda: sys.exit(123)) self.assertSystemExit(234, lambda x: sys.exit(x), 234) + def test_with_assert_output_file(self): + def f(text): + with self.withAssertOutputFile(os.path.join('tests', 'resources', 'utf8_ja.txt')) as out: + out.write(text.encode('utf-8')) + + def g(text): + with self.withAssertOutputFile( + os.path.join('tests', 'resources', 'utf8_ja_template.txt'), {'text': 'かきくけこ'} + ) as out: + out.write(text.encode('utf-8')) + + f('あいうえお\n') + self.assertRaisesRegexp(AssertionError, 'あいうえお', f, 'あいうえお') + + g('かきくけこ\n') + self.assertRaisesRegexp(AssertionError, 'かきくけこ', g, 'あいうえお\n') + def test_assert_system_exit_fail(self): self.assertRaisesRegexp(AssertionError, 'SystemExit not raised', self.assertSystemExit, 0, lambda: 0) self.assertRaisesRegexp(AssertionError, '1 != 0', self.assertSystemExit, 0, lambda: sys.exit(1)) diff --git a/tests/resources/utf8_ja.txt b/tests/resources/utf8_ja.txt new file mode 100644 index 0000000..0005c4e --- /dev/null +++ b/tests/resources/utf8_ja.txt @@ -0,0 +1 @@ +あいうえお diff --git a/tests/resources/utf8_ja_template.txt b/tests/resources/utf8_ja_template.txt new file mode 100644 index 0000000..e35830d --- /dev/null +++ b/tests/resources/utf8_ja_template.txt @@ -0,0 +1,2 @@ +{{ text }} +