Changeset 5

Show
Ignore:
Timestamp:
11/18/07 21:08:56 (1 year ago)
Author:
skit
Message:

* Ajout des classes View et ViewLocator?
* Ajout des classes Model et ModelLocator?
* Modification de la classe CommandLocator?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/Pyxoo/commands/locator.py

    r4 r5  
    3939     
    4040    def locate(self, key): 
    41         """locate(string):list 
    42         Returns commands list associated the passed-in key or None value if key not know by the locator.""" 
     41        """locate(string):Command 
     42        Returns Command associated to the passed-in key or None value if key not know by the locator.""" 
    4343        return self.__commands.get(key) 
    4444     
    45     def registerCommand(self, key, commandClass ): 
    46         """registerCommand(string, class):void 
    47         Registers passed-in command associated to passed-in key.""" 
    48         if not Type.isClass( commandClass ): 
    49             raise TypeError("commandClass param must be a class implementation of Command interface") 
    50         if not issubclass(commandClass, Command): 
    51             raise TypeError("commandClass param must be a class implementation of Command interface") 
    52         if self.isRegistered( key ): 
    53             if not self.__commands.get(key).__contains__(commandClass): 
    54                 self.__commands.get(key).append(commandClass) 
    55         else: 
    56             l = list() 
    57             l.append(commandClass) 
    58             self.__commands.__setitem__(key, l) 
     45    def registerCommand(self, key, command): 
     46        """registerCommand(string, Command):void 
     47        Registers passed-in command associated to passed-in key."""         
     48        if Type.isClass(command): 
     49            if not issubclass(command, Command):             
     50                raise TypeError("command param must be a class implementation of Command interface or Command implementation instance.") 
     51        elif not isinstance(command, Command): 
     52            raise TypeError("command param must be a class implementation of Command interface or Command implementation instance.") 
     53        self.__commands.__setitem__(key, command) 
    5954     
    60     def unregisterCommand(self, key, commandClass): 
    61         """unregisterCommand(string, class):boolean 
    62         Unregisters passed-in command associated to passed-in key.""" 
    63         if not Type.isClass( commandClass ): 
    64             raise TypeError("commandClass param must be a class implementation of Command interface") 
    65         if not issubclass(commandClass, Command): 
    66             raise TypeError("commandClass param must be a class implementation of Command interface") 
    67         if self.isRegistered( key ): 
    68             if self.__commands.get(key).__contains__(commandClass): 
    69                 self.__commands.get(key).remove(commandClass) 
    70                 if self.__commands.get(key).__len__() == 0: 
    71                     self.__commands.__delitem__(key) 
    72                 return True 
    73         return False 
    74      
    75     def unregisterCommands(self, key): 
    76         """unregisterCommands(string):boolean 
    77         Unregisters all commands associated to the passed-in key.""" 
     55    def unregisterCommand(self, key): 
     56        """unregisterCommand(string):void 
     57        Unregisters command associated to passed-in key.""" 
    7858        if self.isRegistered( key ): 
    7959            self.__commands.__delitem__(key) 
    80             return True 
    81         return False 
    8260     
    8361    def __str__(self): 
  • trunk/test/commands/TestCommandLocator.py

    r4 r5  
    2828class MyCommand2(Command): 
    2929    def execute(self, event=None): 
    30         pass   
    31  
     30        pass 
     31   
     32class A:pass 
    3233 
    3334class TestCommandLocator(unittest.TestCase): 
    3435    def setUp(self): 
    3536        self.instance = CommandLocator() 
     37        self.c1 = MyCommand1() 
     38        self.c2 = MyCommand2() 
    3639         
    3740    def testRegisterCommand(self): 
     
    4043        self.assertTrue( self.instance.isRegistered("run") ) 
    4144        self.instance.registerCommand("run", MyCommand2) 
    42         self.assertTrue( self.instance.isRegistered("run") ) 
     45        self.assertTrue( self.instance.isRegistered("run") )         
     46        self.instance.registerCommand("run", self.c1) 
     47        self.assertRaises( TypeError, self.instance.registerCommand, "run", A ) 
     48        self.assertRaises( TypeError, self.instance.registerCommand, "run", A() ) 
    4349         
    4450    def testUnregisterCommand(self): 
    4551        self.instance.registerCommand("run", MyCommand1) 
    46         self.instance.registerCommand("run", MyCommand2) 
    47         self.assertEquals( 2, self.instance.locate("run").__len__() )         
    48         self.assertTrue(self.instance.unregisterCommand("run", MyCommand1)) 
    49         self.assertTrue( self.instance.isRegistered("run") ) 
    50         self.assertFalse(self.instance.unregisterCommand("run", MyCommand1)) 
    51         self.assertEquals( 1, self.instance.locate("run").__len__() ) 
    52         self.assertTrue(self.instance.unregisterCommand("run", MyCommand2)) 
    53         self.assertFalse( self.instance.isRegistered("run") ) 
    54         self.assertFalse(self.instance.unregisterCommand("run", MyCommand2)) 
    55          
    56     def testUnregisterCommands(self): 
    57         self.instance.registerCommand("run", MyCommand1) 
    58         self.instance.registerCommand("run", MyCommand2) 
    59         self.instance.registerCommand("yop", MyCommand1) 
    60         self.instance.registerCommand("yop", MyCommand2)         
    61         self.assertTrue( self.instance.unregisterCommands("run") ) 
    62         self.assertFalse( self.instance.unregisterCommands("run") ) 
    63         self.assertFalse( self.instance.isRegistered("run") ) 
    64         self.assertTrue( self.instance.isRegistered("yop") ) 
     52        self.instance.registerCommand("plop", MyCommand2)   
     53        self.assertTrue( self.instance.isRegistered("run") )      
     54        self.instance.unregisterCommand("run") 
     55        self.assertFalse( self.instance.isRegistered("run") )         
     56        self.assertTrue(self.instance.isRegistered("plop")) 
    6557         
    6658    def testLocate(self): 
    6759        self.instance.registerCommand("run", MyCommand1) 
     60        self.assertEquals( MyCommand1, self.instance.locate("run") ) 
    6861        self.instance.registerCommand("run", MyCommand2) 
    69         self.instance.registerCommand("run", MyCommand2) 
    70         result = self.instance.locate("run")         
    71         self.assertEquals( 2, result.__len__())         
    72         self.assertEquals( MyCommand1, result[0] ) 
    73         self.assertEquals( MyCommand2, result[1] )         
     62        self.assertEquals( MyCommand2, self.instance.locate("run") )       
    7463        self.assertEquals( None, self.instance.locate("plop") ) 
     64         
     65        self.instance.registerCommand("c1", self.c1) 
     66        self.assertEquals( self.c1, self.instance.locate("c1") ) 
    7567         
    7668         
    7769         
    78