From 90c8b7e8c50de3b77b492b4b0ef2ff855845fdf6 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Tue, 22 Dec 2009 00:24:03 +0000 Subject: [PATCH] extend test coverage to ensure borg state is unique between borg types. add some debugging info and support the ability to have multiple borg classes via an ugly parameter to __init__() --- terminatorlib/borg.py | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/terminatorlib/borg.py b/terminatorlib/borg.py index ded16c8b..3e4233cf 100755 --- a/terminatorlib/borg.py +++ b/terminatorlib/borg.py @@ -21,8 +21,19 @@ >>> obj2.attribute = 54321 >>> obj1.attribute 54321 +>>> obj3 = TestBorg2() +>>> obj3.attribute +1 +>>> obj4 = TestBorg2() +>>> obj3.attribute = 98765 +>>> obj4.attribute +98765 +>>> + """ +from util import dbg + # pylint: disable-msg=R0903 # pylint: disable-msg=R0921 class Borg: @@ -35,7 +46,7 @@ class Borg: attribute = None def __init__(self): - Borg.__init__(self) + Borg.__init__(self, self.__class__.__name__) def prepare_attributes(self): if not self.attribute: @@ -51,11 +62,16 @@ class Borg: if necessary.""" __shared_state = {} - def __init__(self): + def __init__(self, borgtype=None): """Class initialiser. Overwrite our class dictionary with the shared state. This makes us identical to every other instance of this class type.""" - self.__dict__ = self.__shared_state + if borgtype is None: + raise TypeError('Borg::__init__: You must pass a borgtype') + if not self.__shared_state.has_key(borgtype): + dbg('Borg::__init__: Preparing borg state for %s' % borgtype) + self.__shared_state[borgtype] = {} + self.__dict__ = self.__shared_state[borgtype] def prepare_attributes(self): """This should be used to prepare any attributes of the borg class.""" @@ -66,13 +82,24 @@ if __name__ == '__main__': attribute = None def __init__(self): - Borg.__init__(self) + Borg.__init__(self, self.__class__.__name__) self.prepare_attributes() def prepare_attributes(self): if not self.attribute: self.attribute = 0 + class TestBorg2(Borg): + attribute = None + + def __init__(self): + Borg.__init__(self, self.__class__.__name__) + self.prepare_attributes() + + def prepare_attributes(self): + if not self.attribute: + self.attribute = 1 + import doctest (failed, attempted) = doctest.testmod() print "%d/%d tests failed" % (failed, attempted)