diff options
Diffstat (limited to 'test_six.py')
-rw-r--r-- | test_six.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/test_six.py b/test_six.py index b0ccd8d..f33f882 100644 --- a/test_six.py +++ b/test_six.py @@ -1,6 +1,7 @@ import operator import sys import types +import unittest import py @@ -635,6 +636,17 @@ def test_print_(): out = six.StringIO() six.print_(None, file=out) assert out.getvalue() == "None\n" + class FlushableStringIO(six.StringIO): + def __init__(self): + six.StringIO.__init__(self) + self.flushed = False + def flush(self): + self.flushed = True + out = FlushableStringIO() + six.print_("Hello", file=out) + assert not out.flushed + six.print_("Hello", file=out, flush=True) + assert out.flushed @py.test.mark.skipif("sys.version_info[:2] >= (2, 6)") @@ -701,6 +713,18 @@ def test_wraps(): assert k is original_k assert not hasattr(k, '__wrapped__') + def f(g, assign, update): + def w(): + return 42 + w.glue = {"foo" : "bar"} + return six.wraps(g, assign, update)(w) + k.glue = {"melon" : "egg"} + k.turnip = 43 + k = f(k, ["turnip"], ["glue"]) + assert k.__name__ == "w" + assert k.turnip == 43 + assert k.glue == {"melon" : "egg", "foo" : "bar"} + def test_add_metaclass(): class Meta(type): @@ -773,3 +797,62 @@ def test_add_metaclass(): __slots__ = "__weakref__", MySlotsWeakref = six.add_metaclass(Meta)(MySlotsWeakref) assert type(MySlotsWeakref) is Meta + + +@py.test.mark.skipif("sys.version_info[:2] < (2, 7) or sys.version_info[:2] in ((3, 0), (3, 1))") +def test_assertCountEqual(): + class TestAssertCountEqual(unittest.TestCase): + def test(self): + with self.assertRaises(AssertionError): + six.assertCountEqual(self, (1, 2), [3, 4, 5]) + + six.assertCountEqual(self, (1, 2), [2, 1]) + + TestAssertCountEqual('test').test() + + +@py.test.mark.skipif("sys.version_info[:2] < (2, 7)") +def test_assertRegex(): + class TestAssertRegex(unittest.TestCase): + def test(self): + with self.assertRaises(AssertionError): + six.assertRegex(self, 'test', r'^a') + + six.assertRegex(self, 'test', r'^t') + + TestAssertRegex('test').test() + + +@py.test.mark.skipif("sys.version_info[:2] < (2, 7)") +def test_assertRaisesRegex(): + class TestAssertRaisesRegex(unittest.TestCase): + def test(self): + with six.assertRaisesRegex(self, AssertionError, '^Foo'): + raise AssertionError('Foo') + + with self.assertRaises(AssertionError): + with six.assertRaisesRegex(self, AssertionError, r'^Foo'): + raise AssertionError('Bar') + + TestAssertRaisesRegex('test').test() + + +def test_python_2_unicode_compatible(): + @six.python_2_unicode_compatible + class MyTest(object): + def __str__(self): + return six.u('hello') + + def __bytes__(self): + return six.b('hello') + + my_test = MyTest() + + if six.PY2: + assert str(my_test) == six.b("hello") + assert unicode(my_test) == six.u("hello") + elif six.PY3: + assert bytes(my_test) == six.b("hello") + assert str(my_test) == six.u("hello") + + assert getattr(six.moves.builtins, 'bytes', str)(my_test) == six.b("hello") |