1 |
# Copyright (c) 2004 by Intevation GmbH |
2 |
# Authors: |
3 |
# Martin Schulze <[email protected]> |
4 |
# |
5 |
# This program is free software; you can redistribute it and/or modify |
6 |
# it under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 2 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# This program is distributed in the hope that it will be useful, |
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
|
19 |
""" |
20 |
Test case for the Thuban ClassMapper. |
21 |
|
22 |
""" |
23 |
|
24 |
__version__ = "$Revision$" |
25 |
# $Source$ |
26 |
# $Id$ |
27 |
|
28 |
|
29 |
import unittest |
30 |
|
31 |
import support |
32 |
support.initthuban() |
33 |
|
34 |
from Thuban.Lib.classmapper import ClassMapper |
35 |
|
36 |
class TestMapping(unittest.TestCase): |
37 |
|
38 |
def test_mapper(self): |
39 |
""" |
40 |
Test ClassMapper |
41 |
""" |
42 |
|
43 |
class MyClass: |
44 |
pass |
45 |
|
46 |
class MySecondClass: |
47 |
def hello(self): |
48 |
return "Hello World!" |
49 |
|
50 |
class MyThirdClass: |
51 |
def hello(self): |
52 |
return "Hello Earth!" |
53 |
|
54 |
mapping = ClassMapper() |
55 |
instance = MyClass() |
56 |
|
57 |
# See if an empty mapping really returns False |
58 |
# |
59 |
self.assertEqual(mapping.get(instance), None) |
60 |
self.assertEqual(mapping.has(instance), False) |
61 |
|
62 |
# See if an installed mapping works |
63 |
# |
64 |
mapping.add(MyClass, MySecondClass) |
65 |
self.assertEqual(mapping.get(instance), MySecondClass) |
66 |
self.assertEqual(mapping.has(instance), True) |
67 |
|
68 |
# Ensure that it's really the class we put in and the method |
69 |
# is available as expected. |
70 |
# |
71 |
myinst = mapping.get(instance)() |
72 |
self.assertEqual(myinst.hello(), "Hello World!") |
73 |
|
74 |
second = ClassMapper() |
75 |
|
76 |
# Test if a second mapper gets mixed ubp with the first one. |
77 |
# |
78 |
self.assertEqual(second.get(instance), None) |
79 |
|
80 |
second.add(MyClass, MyThirdClass) |
81 |
self.assertEqual(second.get(instance), MyThirdClass) |
82 |
self.assertEqual(second.has(instance), True) |
83 |
|
84 |
# Ensure that it's really the class we put in and the method |
85 |
# is available as expected. |
86 |
# |
87 |
myinst = second.get(instance)() |
88 |
self.assertEqual(myinst.hello(), "Hello Earth!") |
89 |
|
90 |
|
91 |
if __name__ == "__main__": |
92 |
support.run_tests() |