1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
from UnitTest import UnitTest
name = 'Name'
prototype = 'Prototype'
call = 'Call'
apply = 'Apply'
constructor = 'Constructor'
class Foo:
a = 1
b = [1,2]
name = "Foo"
def __init__(self, v):
self.v = v
def getV(self):
return self.v
def call(self, name):
name = name.upper()
prototype = self.name
apply = self.name.lower()
return (name, prototype, apply, self.name)
class AttributeTest(UnitTest):
def testHasattr(self):
self.assertEqual(hasattr(self, "getName"), True, "AttrTest should have method 'getName'")
self.assertEqual(hasattr(self, "blah"), False, "AttrTest has no method 'getName'")
def testGetattr(self):
func = getattr(self, "getName")
self.assertEqual(func(), "AttributeTest",
"getattr does not return correct value'")
self.assertEqual(1, getattr(Foo, "notthere", 1))
foo = Foo(1)
self.assertEqual(foo.v, getattr(foo, "v"))
# test on none object type
self.assertEqual(getattr(1, 'x', 2), 2)
self.assertEqual(getattr(None, 'x', 2), 2)
try:
self.assertEqual(1, getattr(foo, "vv"))
except AttributeError, e:
self.assertEqual(e.__class__.__name__, 'AttributeError')
return
self.fail("No AttributeError raised")
def testSetAttr(self):
f1 = Foo(1)
self.assertEqual(f1.getV(), 1)
f2 = Foo(2)
self.assertEqual(f2.getV(), 2)
f3 = Foo(3)
self.assertEqual(f3.getV(), 3)
# bound method
setattr(f1, "getV", getattr(f2, "getV"))
self.assertEqual(f1.getV(), 2)
# unbound method
setattr(f1, "getV", f3.getV) # reeallly need to have __getattr__
self.assertEqual(f1.getV(), 3)
def testDelAttr(self):
foo = Foo(1)
self.assertEqual(hasattr(foo, "v"), True)
delattr(foo, "v")
self.assertEqual(hasattr(foo, "v"), False)
self.assertEqual(hasattr(foo, "getV"), True)
try:
delattr(foo, "getV")
self.fail("No AttributeError raised")
except AttributeError, e:
self.assertEqual(str(e), "Foo instance has no attribute 'getV'")
def testAttrErr(self):
foo = Foo(1)
try:
v = foo.bar
self.fail("No Error raised on foo.bar")
except:
self.assertTrue(True, "Exception raised")
def testInstanceAttr(self):
foo = Foo(1)
foo_fn = foo.getV
try:
t = foo_fn()
except:
t = None
self.assertEqual(t, 1)
foo.getV = 2
try:
t = foo_fn()
except:
t = None
self.assertEqual(t, 1)
t = foo.a
foo.a = 2
self.assertEqual(t, 1)
t = foo.b
foo.b.append(3)
self.assertEqual(t[2], 3)
def testAttributMapping(self):
f = Foo(1)
self.assertEqual(Foo.name, 'Foo')
self.assertEqual(f.name, 'Foo')
name, prototype, apply, constructor = f.call('bAr')
self.assertEqual(name, 'BAR')
self.assertEqual(prototype, 'Foo')
self.assertEqual(apply, 'foo')
self.assertEqual(constructor, 'Foo')
|