Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

from __future__ import with_statement 

from builtins import object 

import unittest 

import lsst.utils.tests 

from lsst.sims.catalogs.decorators import register_class, register_method 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

@register_class 

class ClassA(object): 

 

def call(self, key): 

return self._methodRegistry[key](self) 

 

@register_method('a') 

def _a_method(self): 

return 'a' 

 

 

@register_class 

class ClassB(ClassA): 

 

@register_method('b') 

def _b_method(self): 

return 'b' 

 

 

@register_class 

class ClassC(ClassB): 

 

@register_method('c') 

def _c_method(self): 

return 'c' 

 

 

@register_class 

class ClassD(ClassA): 

 

@register_method('d') 

def _d_method(self): 

return 'd' 

 

 

class MethodRegistryTestCase(unittest.TestCase): 

 

def testMethodInheritance(self): 

""" 

Test that the register_class and register_method decorators 

behave appropriately and preserve inheritance. 

""" 

 

aa = ClassA() 

self.assertEqual(aa.call('a'), 'a') 

 

# below, we test to make sure that methods which 

# should not be in ClassA's _methodRegistry are not 

# spuriously added to the registry 

self.assertRaises(KeyError, aa.call, 'b') 

self.assertRaises(KeyError, aa.call, 'c') 

self.assertRaises(KeyError, aa.call, 'd') 

 

bb = ClassB() 

self.assertEqual(bb.call('a'), 'a') 

self.assertEqual(bb.call('b'), 'b') 

self.assertRaises(KeyError, bb.call, 'c') 

self.assertRaises(KeyError, bb.call, 'd') 

 

cc = ClassC() 

self.assertEqual(cc.call('a'), 'a') 

self.assertEqual(cc.call('b'), 'b') 

self.assertEqual(cc.call('c'), 'c') 

self.assertRaises(KeyError, cc.call, 'd') 

 

dd = ClassD() 

self.assertEqual(dd.call('a'), 'a') 

self.assertEqual(dd.call('d'), 'd') 

self.assertRaises(KeyError, dd.call, 'b') 

self.assertRaises(KeyError, dd.call, 'c') 

 

 

class MemoryTestClass(lsst.utils.tests.MemoryTestCase): 

pass 

 

87 ↛ 88line 87 didn't jump to line 88, because the condition on line 87 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()