Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ python:
- "3.2"
- "3.3"
- "3.4"
- "3.5"

cache: pip

Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def get_version():
install_requires=[
'six',
'unittest2',
'jinja2 == 2.6', # specify library version to support Python 3.2
],
tests_require=[
],
Expand Down
2 changes: 1 addition & 1 deletion src/mog_commons/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.6'
__version__ = '0.1.7'
34 changes: 31 additions & 3 deletions src/mog_commons/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import sys
import six
import io
import tempfile
from contextlib import contextmanager
import jinja2

# unittest
if sys.version_info < (2, 7):
import unittest2 as base_unittest
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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions tests/mog_commons/test_unittest.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -10,13 +12,15 @@ 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):
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)
Expand All @@ -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))
1 change: 1 addition & 0 deletions tests/resources/utf8_ja.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
あいうえお
2 changes: 2 additions & 0 deletions tests/resources/utf8_ja_template.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ text }}