BlockIt
testTable.py
Go to the documentation of this file.
00001 import unittest
00002 from blockit import *
00003 
00004 class TestSymbolTable(unittest.TestCase):
00005     def setUp(self):
00006         self.table = SymbolTable()
00007         prev = Block('test', 'block-0', None)
00008         prev.setTable(self.table)
00009         self.blocks = [prev]
00010         for x in range(1,5):
00011             typ = 'test'
00012             name = 'block-%s'%(x,)
00013             next = Block('test', 'block-%s'%(x,), None)
00014             prev.setTable(self.table)
00015             prev.addChild(next)
00016             prev = next
00017             self.blocks.append(prev)
00018 
00019     def tearDown(self):
00020         self.table.clear()
00021 
00022     def testBuild(self):
00023         '''Test the registering of blocks into the table
00024 
00025         '''
00026         table = self.table
00027         blocks = self.blocks
00028 
00029         for blk in blocks:
00030             table.register(blk)
00031 
00032         for blk in blocks:
00033             self.assert_(blk.name() in table)
00034         
00035         table.registerAll(blocks[0])
00036         
00037         sym = blocks[0].name()
00038         jc = table.joinCharacter()
00039         for i,blk in enumerate(blocks):
00040             if i > 0: sym = jc.join([blk.name(),sym])
00041             self.assert_(sym in table)
00042 
00043         table.clear()
00044         table.registerAll(blocks[0])
00045         self.assert_(len(table) == len(blocks))
00046 
00047     def testJoinCharacter(self):
00048         '''Test a new join character for the table
00049 
00050         '''
00051         self.table = SymbolTable(':')
00052         self.testBuild()
00053         self.table = SymbolTable()
00054 
00055     def testFilter(self):
00056         '''Test the filter method
00057 
00058         '''
00059         f1 = lambda (k,v): 'block-1' in k
00060         f2 = lambda (k,v): v.parent().name() == 'block-0'
00061 
00062         table = self.table
00063         blocks = self.blocks
00064 
00065         table.registerAll(blocks[0])
00066         a = table.filter(And(f1, f2))
00067         self.assert_(len(a) == 1)
00068         self.assert_('block-1|block-0' in a)
00069 
00070     def testUnion(self):
00071         '''Test the union of two tables
00072 
00073         '''
00074         table = self.table
00075         blocks = self.blocks
00076         table.registerAll(blocks[0])
00077         table_ = SymbolTable()
00078         table_.register(blocks[1])
00079 
00080         table.union(table_)
00081         self.assert_(len(table) == len(blocks)+1)
00082         self.assert_(blocks[1].name() in table)
00083         self.assert_(blocks[0].name() in table)
00084         table_.clear()
00085 
00086     def testIsEmpty(self):
00087         '''Test the isEmpty() method
00088 
00089         '''
00090         self.assert_(self.table.isEmpty())
00091 
00092 if __name__ == '__main__':
00093     suite = unittest.TestLoader().loadTestsFromTestCase(TestSymbolTable)
00094     unittest.TextTestRunner(verbosity=2).run(suite)
00095 
00096     
 All Classes Namespaces Files Functions Variables Properties