Back to index

enigmail  1.4.3
datatests.py
Go to the documentation of this file.
00001 import pymake.data, pymake.util
00002 import unittest
00003 import re
00004 from cStringIO import StringIO
00005 
00006 def multitest(cls):
00007     for name in cls.testdata.iterkeys():
00008         def m(self, name=name):
00009             return self.runSingle(*self.testdata[name])
00010 
00011         setattr(cls, 'test_%s' % name, m)
00012     return cls
00013 
00014 class SplitWordsTest(unittest.TestCase):
00015     testdata = (
00016         (' test test.c test.o ', ['test', 'test.c', 'test.o']),
00017         ('\ttest\t  test.c \ntest.o', ['test', 'test.c', 'test.o']),
00018     )
00019 
00020     def runTest(self):
00021         for s, e in self.testdata:
00022             w = s.split()
00023             self.assertEqual(w, e, 'splitwords(%r)' % (s,))
00024 
00025 class GetPatSubstTest(unittest.TestCase):
00026     testdata = (
00027         ('%.c', '%.o', ' test test.c test.o ', 'test test.o test.o'),
00028         ('%', '%.o', ' test.c test.o ', 'test.c.o test.o.o'),
00029         ('foo', 'bar', 'test foo bar', 'test bar bar'),
00030         ('foo', '%bar', 'test foo bar', 'test %bar bar'),
00031         ('%', 'perc_%', 'path', 'perc_path'),
00032         ('\\%', 'sub%', 'p %', 'p sub%'),
00033         ('%.c', '\\%%.o', 'foo.c bar.o baz.cpp', '%foo.o bar.o baz.cpp'),
00034     )
00035 
00036     def runTest(self):
00037         for s, r, d, e in self.testdata:
00038             words = d.split()
00039             p = pymake.data.Pattern(s)
00040             a = ' '.join((p.subst(r, word, False)
00041                           for word in words))
00042             self.assertEqual(a, e, 'Pattern(%r).subst(%r, %r)' % (s, r, d))
00043 
00044 class LRUTest(unittest.TestCase):
00045     # getkey, expected, funccount, debugitems
00046     expected = (
00047         (0, '', 1, (0,)),
00048         (0, '', 2, (0,)),
00049         (1, ' ', 3, (1, 0)),
00050         (1, ' ', 3, (1, 0)),
00051         (0, '', 4, (0, 1)),
00052         (2, '  ', 5, (2, 0, 1)),
00053         (1, ' ', 5, (1, 2, 0)),
00054         (3, '   ', 6, (3, 1, 2)),
00055     )
00056 
00057     def spaceFunc(self, l):
00058         self.funccount += 1
00059         return ''.ljust(l)
00060 
00061     def runTest(self):
00062         self.funccount = 0
00063         c = pymake.util.LRUCache(3, self.spaceFunc, lambda k, v: k % 2)
00064         self.assertEqual(tuple(c.debugitems()), ())
00065 
00066         for i in xrange(0, len(self.expected)):
00067             k, e, fc, di = self.expected[i]
00068 
00069             v = c.get(k)
00070             self.assertEqual(v, e)
00071             self.assertEqual(self.funccount, fc,
00072                              "funccount, iteration %i, got %i expected %i" % (i, self.funccount, fc))
00073             goti = tuple(c.debugitems())
00074             self.assertEqual(goti, di,
00075                              "debugitems, iteration %i, got %r expected %r" % (i, goti, di))
00076 
00077 if __name__ == '__main__':
00078     unittest.main()