175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom __future__ import with_statement
275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport unittest2
475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenfrom unittest2.test.support import OldTestResult, catch_warnings
575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenimport warnings
775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen# needed to enable the deprecation warnings
875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenwarnings.simplefilter('default')
975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
1075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenclass TestWith(unittest2.TestCase):
1175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    """Tests that use the with statement live in this
1275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    module so that all other tests can be run with Python 2.4.
1375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    """
1475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
1575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def testAssertRaisesExcValue(self):
1675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class ExceptionMock(Exception):
1775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            pass
1875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
1975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        def Stub(foo):
2075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            raise ExceptionMock(foo)
2175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        v = "particular value"
2275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
2375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        ctx = self.assertRaises(ExceptionMock)
2475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        with ctx:
2575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            Stub(v)
2675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        e = ctx.exception
2775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertIsInstance(e, ExceptionMock)
2875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertEqual(e.args[0], v)
2975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
3075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
3175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_assertRaises(self):
3275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        def _raise(e):
3375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            raise e
3475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertRaises(KeyError, _raise, KeyError)
3575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertRaises(KeyError, _raise, KeyError("key"))
3675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        try:
3775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertRaises(KeyError, lambda: None)
3875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        except self.failureException, e:
3975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertIn("KeyError not raised", e.args)
4075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        else:
4175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.fail("assertRaises() didn't fail")
4275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        try:
4375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertRaises(KeyError, _raise, ValueError)
4475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        except ValueError:
4575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            pass
4675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        else:
4775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.fail("assertRaises() didn't let exception pass through")
4875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        with self.assertRaises(KeyError) as cm:
4975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            try:
5075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                raise KeyError
5175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            except Exception, e:
5275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                raise
5375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertIs(cm.exception, e)
5475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
5575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        with self.assertRaises(KeyError):
5675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            raise KeyError("key")
5775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        try:
5875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            with self.assertRaises(KeyError):
5975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                pass
6075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        except self.failureException, e:
6175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertIn("KeyError not raised", e.args)
6275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        else:
6375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.fail("assertRaises() didn't fail")
6475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        try:
6575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            with self.assertRaises(KeyError):
6675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                raise ValueError
6775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        except ValueError:
6875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            pass
6975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        else:
7075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.fail("assertRaises() didn't let exception pass through")
7175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
7275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_assert_dict_unicode_error(self):
7375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        with catch_warnings(record=True):
7475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            # This causes a UnicodeWarning due to its craziness
7575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            one = ''.join(chr(i) for i in range(255))
7675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            # this used to cause a UnicodeDecodeError constructing the failure msg
7775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            with self.assertRaises(self.failureException):
7875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                self.assertDictContainsSubset({'foo': one}, {'foo': u'\uFFFD'})
7975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
8075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_formatMessage_unicode_error(self):
8175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        with catch_warnings(record=True):
8275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            # This causes a UnicodeWarning due to its craziness
8375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            one = ''.join(chr(i) for i in range(255))
8475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            # this used to cause a UnicodeDecodeError constructing msg
8575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self._formatMessage(one, u'\uFFFD')
8675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
8775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def assertOldResultWarning(self, test, failures):
8875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        with catch_warnings(record=True) as log:
8975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            result = OldTestResult()
9075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            test.run(result)
9175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertEqual(len(result.failures), failures)
9275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            warning, = log
9375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertIs(warning.category, DeprecationWarning)
9475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
9575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_old_testresult(self):
9675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Test(unittest2.TestCase):
9775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def testSkip(self):
9875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                self.skipTest('foobar')
9975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            @unittest2.expectedFailure
10075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def testExpectedFail(self):
10175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                raise TypeError
10275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            @unittest2.expectedFailure
10375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def testUnexpectedSuccess(self):
10475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                pass
10575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
10675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        for test_name, should_pass in (('testSkip', True),
10775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                                       ('testExpectedFail', True),
10875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                                       ('testUnexpectedSuccess', False)):
10975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            test = Test(test_name)
11075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.assertOldResultWarning(test, int(not should_pass))
11175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
11275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_old_testresult_setup(self):
11375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Test(unittest2.TestCase):
11475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def setUp(self):
11575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                self.skipTest('no reason')
11675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def testFoo(self):
11775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                pass
11875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertOldResultWarning(Test('testFoo'), 0)
11975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
12075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def test_old_testresult_class(self):
12175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        class Test(unittest2.TestCase):
12275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            def testFoo(self):
12375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen                pass
12475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        Test = unittest2.skip('no reason')(Test)
12575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        self.assertOldResultWarning(Test('testFoo'), 0)
12675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
12775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    def testPendingDeprecationMethodNames(self):
12875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        """Test fail* methods pending deprecation, they will warn in 3.2.
12975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
13075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        Do not use these methods.  They will go away in 3.3.
13175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        """
13275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen        with catch_warnings(record=True):
13375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.failIfEqual(3, 5)
13475e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.failUnlessEqual(3, 3)
13575e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.failUnlessAlmostEqual(2.0, 2.0)
13675e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.failIfAlmostEqual(3.0, 5.0)
13775e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.failUnless(True)
13875e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.failUnlessRaises(TypeError, lambda _: 3.14 + u'spam')
13975e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen            self.failIf(False)
14075e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
14175e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen
14275e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chenif __name__ == '__main__':
14375e28f942c1b9f9c6d5a0d5f2efd037cbbc9fc74Johnny Chen    unittest2.main()
144