Kaydet (Commit) 8ada1774 authored tarafından Georg Brandl's avatar Georg Brandl

Fix unittest conversion breakage.

üst 212861c5
...@@ -2,16 +2,6 @@ from test import test_support ...@@ -2,16 +2,6 @@ from test import test_support
import types import types
import unittest import unittest
def cannot_set_attr(obj, name, value, exceptions):
# This method is not called as a test (name doesn't start with 'test'),
# but may be used by other tests.
try: setattr(obj, name, value)
except exceptions: pass
else: self.fail("shouldn't be able to set %s to %r" % (name, value))
try: delattr(obj, name)
except exceptions: pass
else: self.fail("shouldn't be able to del %s" % name)
class FuncAttrsTest(unittest.TestCase): class FuncAttrsTest(unittest.TestCase):
def setUp(self): def setUp(self):
class F: class F:
...@@ -23,6 +13,17 @@ class FuncAttrsTest(unittest.TestCase): ...@@ -23,6 +13,17 @@ class FuncAttrsTest(unittest.TestCase):
self.fi = F() self.fi = F()
self.b = b self.b = b
def cannot_set_attr(self,obj, name, value, exceptions):
# This method is not called as a test (name doesn't start with 'test'),
# but may be used by other tests.
try: setattr(obj, name, value)
except exceptions: pass
else: self.fail("shouldn't be able to set %s to %r" % (name, value))
try: delattr(obj, name)
except exceptions: pass
else: self.fail("shouldn't be able to del %s" % name)
class FunctionPropertiesTest(FuncAttrsTest): class FunctionPropertiesTest(FuncAttrsTest):
# Include the external setUp method that is common to all tests # Include the external setUp method that is common to all tests
def test_module(self): def test_module(self):
...@@ -56,7 +57,7 @@ class FunctionPropertiesTest(FuncAttrsTest): ...@@ -56,7 +57,7 @@ class FunctionPropertiesTest(FuncAttrsTest):
def test_func_globals(self): def test_func_globals(self):
self.assertEqual(self.b.func_globals, globals()) self.assertEqual(self.b.func_globals, globals())
cannot_set_attr(self.b, 'func_globals', 2, TypeError) self.cannot_set_attr(self.b, 'func_globals', 2, TypeError)
def test_func_name(self): def test_func_name(self):
self.assertEqual(self.b.__name__, 'b') self.assertEqual(self.b.__name__, 'b')
...@@ -68,8 +69,8 @@ class FunctionPropertiesTest(FuncAttrsTest): ...@@ -68,8 +69,8 @@ class FunctionPropertiesTest(FuncAttrsTest):
self.assertEqual(self.b.__name__, 'd') self.assertEqual(self.b.__name__, 'd')
self.assertEqual(self.b.func_name, 'd') self.assertEqual(self.b.func_name, 'd')
# __name__ and func_name must be a string # __name__ and func_name must be a string
cannot_set_attr(self.b, '__name__', 7, TypeError) self.cannot_set_attr(self.b, '__name__', 7, TypeError)
cannot_set_attr(self.b, 'func_name', 7, TypeError) self.cannot_set_attr(self.b, 'func_name', 7, TypeError)
# __name__ must be available when in restricted mode. Exec will raise # __name__ must be available when in restricted mode. Exec will raise
# AttributeError if __name__ is not available on f. # AttributeError if __name__ is not available on f.
s = """def f(): pass\nf.__name__""" s = """def f(): pass\nf.__name__"""
...@@ -77,8 +78,8 @@ class FunctionPropertiesTest(FuncAttrsTest): ...@@ -77,8 +78,8 @@ class FunctionPropertiesTest(FuncAttrsTest):
# Test on methods, too # Test on methods, too
self.assertEqual(self.f.a.__name__, 'a') self.assertEqual(self.f.a.__name__, 'a')
self.assertEqual(self.fi.a.__name__, 'a') self.assertEqual(self.fi.a.__name__, 'a')
cannot_set_attr(self.f.a, "__name__", 'a', AttributeError) self.cannot_set_attr(self.f.a, "__name__", 'a', AttributeError)
cannot_set_attr(self.fi.a, "__name__", 'a', AttributeError) self.cannot_set_attr(self.fi.a, "__name__", 'a', AttributeError)
def test_func_code(self): def test_func_code(self):
num_one, num_two = 7, 8 num_one, num_two = 7, 8
...@@ -135,21 +136,21 @@ class ImplicitReferencesTest(FuncAttrsTest): ...@@ -135,21 +136,21 @@ class ImplicitReferencesTest(FuncAttrsTest):
def test_im_class(self): def test_im_class(self):
self.assertEqual(self.f.a.im_class, self.f) self.assertEqual(self.f.a.im_class, self.f)
self.assertEqual(self.fi.a.im_class, self.f) self.assertEqual(self.fi.a.im_class, self.f)
cannot_set_attr(self.f.a, "im_class", self.f, TypeError) self.cannot_set_attr(self.f.a, "im_class", self.f, TypeError)
cannot_set_attr(self.fi.a, "im_class", self.f, TypeError) self.cannot_set_attr(self.fi.a, "im_class", self.f, TypeError)
def test_im_func(self): def test_im_func(self):
self.f.b = self.b self.f.b = self.b
self.assertEqual(self.f.b.im_func, self.b) self.assertEqual(self.f.b.im_func, self.b)
self.assertEqual(self.fi.b.im_func, self.b) self.assertEqual(self.fi.b.im_func, self.b)
cannot_set_attr(self.f.b, "im_func", self.b, TypeError) self.cannot_set_attr(self.f.b, "im_func", self.b, TypeError)
cannot_set_attr(self.fi.b, "im_func", self.b, TypeError) self.cannot_set_attr(self.fi.b, "im_func", self.b, TypeError)
def test_im_self(self): def test_im_self(self):
self.assertEqual(self.f.a.im_self, None) self.assertEqual(self.f.a.im_self, None)
self.assertEqual(self.fi.a.im_self, self.fi) self.assertEqual(self.fi.a.im_self, self.fi)
cannot_set_attr(self.f.a, "im_self", None, TypeError) self.cannot_set_attr(self.f.a, "im_self", None, TypeError)
cannot_set_attr(self.fi.a, "im_self", self.fi, TypeError) self.cannot_set_attr(self.fi.a, "im_self", self.fi, TypeError)
def test_im_func_non_method(self): def test_im_func_non_method(self):
# Behavior should be the same when a method is added via an attr # Behavior should be the same when a method is added via an attr
...@@ -162,8 +163,8 @@ class ImplicitReferencesTest(FuncAttrsTest): ...@@ -162,8 +163,8 @@ class ImplicitReferencesTest(FuncAttrsTest):
except AttributeError: pass except AttributeError: pass
else: self.fail("using unknown attributes should raise AttributeError") else: self.fail("using unknown attributes should raise AttributeError")
# Test assignment and deletion # Test assignment and deletion
cannot_set_attr(self.f.id, 'unknown_attr', 2, AttributeError) self.cannot_set_attr(self.f.id, 'unknown_attr', 2, AttributeError)
cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError) self.cannot_set_attr(self.fi.id, 'unknown_attr', 2, AttributeError)
def test_implicit_method_properties(self): def test_implicit_method_properties(self):
self.f.a.im_func.known_attr = 7 self.f.a.im_func.known_attr = 7
...@@ -202,12 +203,12 @@ class ArbitraryFunctionAttrTest(FuncAttrsTest): ...@@ -202,12 +203,12 @@ class ArbitraryFunctionAttrTest(FuncAttrsTest):
class FunctionDictsTest(FuncAttrsTest): class FunctionDictsTest(FuncAttrsTest):
def test_setting_dict_to_invalid(self): def test_setting_dict_to_invalid(self):
cannot_set_attr(self.b, '__dict__', None, TypeError) self.cannot_set_attr(self.b, '__dict__', None, TypeError)
cannot_set_attr(self.b, 'func_dict', None, TypeError) self.cannot_set_attr(self.b, 'func_dict', None, TypeError)
from UserDict import UserDict from UserDict import UserDict
d = UserDict({'known_attr': 7}) d = UserDict({'known_attr': 7})
cannot_set_attr(self.f.a.im_func, '__dict__', d, TypeError) self.cannot_set_attr(self.f.a.im_func, '__dict__', d, TypeError)
cannot_set_attr(self.fi.a.im_func, '__dict__', d, TypeError) self.cannot_set_attr(self.fi.a.im_func, '__dict__', d, TypeError)
def test_setting_dict_to_valid(self): def test_setting_dict_to_valid(self):
d = {'known_attr': 7} d = {'known_attr': 7}
...@@ -259,8 +260,8 @@ class FunctionDocstringTest(FuncAttrsTest): ...@@ -259,8 +260,8 @@ class FunctionDocstringTest(FuncAttrsTest):
self.assertEqual(self.b.func_doc, docstr) self.assertEqual(self.b.func_doc, docstr)
self.assertEqual(self.f.a.__doc__, docstr) self.assertEqual(self.f.a.__doc__, docstr)
self.assertEqual(self.fi.a.__doc__, docstr) self.assertEqual(self.fi.a.__doc__, docstr)
cannot_set_attr(self.f.a, "__doc__", docstr, AttributeError) self.cannot_set_attr(self.f.a, "__doc__", docstr, AttributeError)
cannot_set_attr(self.fi.a, "__doc__", docstr, AttributeError) self.cannot_set_attr(self.fi.a, "__doc__", docstr, AttributeError)
def test_delete_docstring(self): def test_delete_docstring(self):
self.b.__doc__ = "The docstring" self.b.__doc__ = "The docstring"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment