util_test.py
Go to the documentation of this file.
00001 # coding: utf-8
00002 from __future__ import absolute_import, division, print_function, with_statement
00003 import sys
00004 
00005 from tornado.escape import utf8
00006 from tornado.util import raise_exc_info, Configurable, u, exec_in, ArgReplacer
00007 from tornado.test.util import unittest
00008 
00009 try:
00010     from cStringIO import StringIO  # py2
00011 except ImportError:
00012     from io import StringIO  # py3
00013 
00014 
00015 class RaiseExcInfoTest(unittest.TestCase):
00016     def test_two_arg_exception(self):
00017         # This test would fail on python 3 if raise_exc_info were simply
00018         # a three-argument raise statement, because TwoArgException
00019         # doesn't have a "copy constructor"
00020         class TwoArgException(Exception):
00021             def __init__(self, a, b):
00022                 super(TwoArgException, self).__init__()
00023                 self.a, self.b = a, b
00024 
00025         try:
00026             raise TwoArgException(1, 2)
00027         except TwoArgException:
00028             exc_info = sys.exc_info()
00029         try:
00030             raise_exc_info(exc_info)
00031             self.fail("didn't get expected exception")
00032         except TwoArgException as e:
00033             self.assertIs(e, exc_info[1])
00034 
00035 
00036 class TestConfigurable(Configurable):
00037     @classmethod
00038     def configurable_base(cls):
00039         return TestConfigurable
00040 
00041     @classmethod
00042     def configurable_default(cls):
00043         return TestConfig1
00044 
00045 
00046 class TestConfig1(TestConfigurable):
00047     def initialize(self, a=None):
00048         self.a = a
00049 
00050 
00051 class TestConfig2(TestConfigurable):
00052     def initialize(self, b=None):
00053         self.b = b
00054 
00055 
00056 class ConfigurableTest(unittest.TestCase):
00057     def setUp(self):
00058         self.saved = TestConfigurable._save_configuration()
00059 
00060     def tearDown(self):
00061         TestConfigurable._restore_configuration(self.saved)
00062 
00063     def checkSubclasses(self):
00064         # no matter how the class is configured, it should always be
00065         # possible to instantiate the subclasses directly
00066         self.assertIsInstance(TestConfig1(), TestConfig1)
00067         self.assertIsInstance(TestConfig2(), TestConfig2)
00068 
00069         obj = TestConfig1(a=1)
00070         self.assertEqual(obj.a, 1)
00071         obj = TestConfig2(b=2)
00072         self.assertEqual(obj.b, 2)
00073 
00074     def test_default(self):
00075         obj = TestConfigurable()
00076         self.assertIsInstance(obj, TestConfig1)
00077         self.assertIs(obj.a, None)
00078 
00079         obj = TestConfigurable(a=1)
00080         self.assertIsInstance(obj, TestConfig1)
00081         self.assertEqual(obj.a, 1)
00082 
00083         self.checkSubclasses()
00084 
00085     def test_config_class(self):
00086         TestConfigurable.configure(TestConfig2)
00087         obj = TestConfigurable()
00088         self.assertIsInstance(obj, TestConfig2)
00089         self.assertIs(obj.b, None)
00090 
00091         obj = TestConfigurable(b=2)
00092         self.assertIsInstance(obj, TestConfig2)
00093         self.assertEqual(obj.b, 2)
00094 
00095         self.checkSubclasses()
00096 
00097     def test_config_args(self):
00098         TestConfigurable.configure(None, a=3)
00099         obj = TestConfigurable()
00100         self.assertIsInstance(obj, TestConfig1)
00101         self.assertEqual(obj.a, 3)
00102 
00103         obj = TestConfigurable(a=4)
00104         self.assertIsInstance(obj, TestConfig1)
00105         self.assertEqual(obj.a, 4)
00106 
00107         self.checkSubclasses()
00108         # args bound in configure don't apply when using the subclass directly
00109         obj = TestConfig1()
00110         self.assertIs(obj.a, None)
00111 
00112     def test_config_class_args(self):
00113         TestConfigurable.configure(TestConfig2, b=5)
00114         obj = TestConfigurable()
00115         self.assertIsInstance(obj, TestConfig2)
00116         self.assertEqual(obj.b, 5)
00117 
00118         obj = TestConfigurable(b=6)
00119         self.assertIsInstance(obj, TestConfig2)
00120         self.assertEqual(obj.b, 6)
00121 
00122         self.checkSubclasses()
00123         # args bound in configure don't apply when using the subclass directly
00124         obj = TestConfig2()
00125         self.assertIs(obj.b, None)
00126 
00127 
00128 class UnicodeLiteralTest(unittest.TestCase):
00129     def test_unicode_escapes(self):
00130         self.assertEqual(utf8(u('\u00e9')), b'\xc3\xa9')
00131 
00132 
00133 class ExecInTest(unittest.TestCase):
00134     # This test is python 2 only because there are no new future imports
00135     # defined in python 3 yet.
00136     @unittest.skipIf(sys.version_info >= print_function.getMandatoryRelease(),
00137                      'no testable future imports')
00138     def test_no_inherit_future(self):
00139         # This file has from __future__ import print_function...
00140         f = StringIO()
00141         print('hello', file=f)
00142         # ...but the template doesn't
00143         exec_in('print >> f, "world"', dict(f=f))
00144         self.assertEqual(f.getvalue(), 'hello\nworld\n')
00145 
00146 
00147 class ArgReplacerTest(unittest.TestCase):
00148     def setUp(self):
00149         def function(x, y, callback=None, z=None):
00150             pass
00151         self.replacer = ArgReplacer(function, 'callback')
00152 
00153     def test_omitted(self):
00154         args = (1, 2)
00155         kwargs = dict()
00156         self.assertIs(self.replacer.get_old_value(args, kwargs), None)
00157         self.assertEqual(self.replacer.replace('new', args, kwargs),
00158                          (None, (1, 2), dict(callback='new')))
00159 
00160     def test_position(self):
00161         args = (1, 2, 'old', 3)
00162         kwargs = dict()
00163         self.assertEqual(self.replacer.get_old_value(args, kwargs), 'old')
00164         self.assertEqual(self.replacer.replace('new', args, kwargs),
00165                          ('old', [1, 2, 'new', 3], dict()))
00166 
00167     def test_keyword(self):
00168         args = (1,)
00169         kwargs = dict(y=2, callback='old', z=3)
00170         self.assertEqual(self.replacer.get_old_value(args, kwargs), 'old')
00171         self.assertEqual(self.replacer.replace('new', args, kwargs),
00172                          ('old', (1,), dict(y=2, callback='new', z=3)))


rosbridge_server
Author(s): Jonathan Mace
autogenerated on Wed Sep 13 2017 03:18:20