1# Copyright 2017 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import unittest 6 7from py_utils import class_util 8 9 10class ClassUtilTest(unittest.TestCase): 11 12 def testClassOverridden(self): 13 class Parent(object): 14 def MethodShouldBeOverridden(self): 15 pass 16 17 class Child(Parent): 18 def MethodShouldBeOverridden(self): 19 pass 20 21 self.assertTrue(class_util.IsMethodOverridden( 22 Parent, Child, 'MethodShouldBeOverridden')) 23 24 def testGrandchildOverridden(self): 25 class Parent(object): 26 def MethodShouldBeOverridden(self): 27 pass 28 29 class Child(Parent): 30 pass 31 32 class Grandchild(Child): 33 def MethodShouldBeOverridden(self): 34 pass 35 36 self.assertTrue(class_util.IsMethodOverridden( 37 Parent, Grandchild, 'MethodShouldBeOverridden')) 38 39 def testClassNotOverridden(self): 40 class Parent(object): 41 def MethodShouldBeOverridden(self): 42 pass 43 44 class Child(Parent): 45 def SomeOtherMethod(self): 46 pass 47 48 self.assertFalse(class_util.IsMethodOverridden( 49 Parent, Child, 'MethodShouldBeOverridden')) 50 51 def testGrandchildNotOverridden(self): 52 class Parent(object): 53 def MethodShouldBeOverridden(self): 54 pass 55 56 class Child(Parent): 57 def MethodShouldBeOverridden(self): 58 pass 59 60 class Grandchild(Child): 61 def SomeOtherMethod(self): 62 pass 63 64 self.assertTrue(class_util.IsMethodOverridden( 65 Parent, Grandchild, 'MethodShouldBeOverridden')) 66 67 def testClassNotPresentInParent(self): 68 class Parent(object): 69 def MethodShouldBeOverridden(self): 70 pass 71 72 class Child(Parent): 73 def MethodShouldBeOverridden(self): 74 pass 75 76 self.assertRaises( 77 AssertionError, class_util.IsMethodOverridden, 78 Parent, Child, 'WrongMethod') 79 80 def testInvalidClass(self): 81 class Foo(object): 82 def Bar(self): 83 pass 84 85 self.assertRaises( 86 AssertionError, class_util.IsMethodOverridden, 'invalid', Foo, 'Bar') 87 88 self.assertRaises( 89 AssertionError, class_util.IsMethodOverridden, Foo, 'invalid', 'Bar') 90 91 def testMultipleInheritance(self): 92 class Aaa(object): 93 def One(self): 94 pass 95 96 class Bbb(object): 97 def Two(self): 98 pass 99 100 class Ccc(Aaa, Bbb): 101 pass 102 103 class Ddd(object): 104 def Three(self): 105 pass 106 107 class Eee(Ddd): 108 def Three(self): 109 pass 110 111 class Fff(Ccc, Eee): 112 def One(self): 113 pass 114 115 class Ggg(object): 116 def Four(self): 117 pass 118 119 class Hhh(Fff, Ggg): 120 def Two(self): 121 pass 122 123 class Iii(Hhh): 124 pass 125 126 class Jjj(Iii): 127 pass 128 129 self.assertFalse(class_util.IsMethodOverridden(Aaa, Ccc, 'One')) 130 self.assertTrue(class_util.IsMethodOverridden(Aaa, Fff, 'One')) 131 self.assertTrue(class_util.IsMethodOverridden(Aaa, Hhh, 'One')) 132 self.assertTrue(class_util.IsMethodOverridden(Aaa, Jjj, 'One')) 133 self.assertFalse(class_util.IsMethodOverridden(Bbb, Ccc, 'Two')) 134 self.assertTrue(class_util.IsMethodOverridden(Bbb, Hhh, 'Two')) 135 self.assertTrue(class_util.IsMethodOverridden(Bbb, Jjj, 'Two')) 136 self.assertFalse(class_util.IsMethodOverridden(Eee, Fff, 'Three')) 137 138 139