DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5350524bb654)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
import pymake.data, pymake.functions, pymake.util
import unittest
import re
from cStringIO import StringIO

def multitest(cls):
    for name in cls.testdata.iterkeys():
        def m(self, name=name):
            return self.runSingle(*self.testdata[name])

        setattr(cls, 'test_%s' % name, m)
    return cls

class SplitWordsTest(unittest.TestCase):
    testdata = (
        (' test test.c test.o ', ['test', 'test.c', 'test.o']),
        ('\ttest\t  test.c \ntest.o', ['test', 'test.c', 'test.o']),
    )

    def runTest(self):
        for s, e in self.testdata:
            w = s.split()
            self.assertEqual(w, e, 'splitwords(%r)' % (s,))

class GetPatSubstTest(unittest.TestCase):
    testdata = (
        ('%.c', '%.o', ' test test.c test.o ', 'test test.o test.o'),
        ('%', '%.o', ' test.c test.o ', 'test.c.o test.o.o'),
        ('foo', 'bar', 'test foo bar', 'test bar bar'),
        ('foo', '%bar', 'test foo bar', 'test %bar bar'),
        ('%', 'perc_%', 'path', 'perc_path'),
        ('\\%', 'sub%', 'p %', 'p sub%'),
        ('%.c', '\\%%.o', 'foo.c bar.o baz.cpp', '%foo.o bar.o baz.cpp'),
    )

    def runTest(self):
        for s, r, d, e in self.testdata:
            words = d.split()
            p = pymake.data.Pattern(s)
            a = ' '.join((p.subst(r, word, False)
                          for word in words))
            self.assertEqual(a, e, 'Pattern(%r).subst(%r, %r)' % (s, r, d))

class LRUTest(unittest.TestCase):
    # getkey, expected, funccount, debugitems
    expected = (
        (0, '', 1, (0,)),
        (0, '', 2, (0,)),
        (1, ' ', 3, (1, 0)),
        (1, ' ', 3, (1, 0)),
        (0, '', 4, (0, 1)),
        (2, '  ', 5, (2, 0, 1)),
        (1, ' ', 5, (1, 2, 0)),
        (3, '   ', 6, (3, 1, 2)),
    )

    def spaceFunc(self, l):
        self.funccount += 1
        return ''.ljust(l)

    def runTest(self):
        self.funccount = 0
        c = pymake.util.LRUCache(3, self.spaceFunc, lambda k, v: k % 2)
        self.assertEqual(tuple(c.debugitems()), ())

        for i in xrange(0, len(self.expected)):
            k, e, fc, di = self.expected[i]

            v = c.get(k)
            self.assertEqual(v, e)
            self.assertEqual(self.funccount, fc,
                             "funccount, iteration %i, got %i expected %i" % (i, self.funccount, fc))
            goti = tuple(c.debugitems())
            self.assertEqual(goti, di,
                             "debugitems, iteration %i, got %r expected %r" % (i, goti, di))

class EqualityTest(unittest.TestCase):
    def test_string_expansion(self):
        s1 = pymake.data.StringExpansion('foo bar', None)
        s2 = pymake.data.StringExpansion('foo bar', None)

        self.assertEqual(s1, s2)

    def test_expansion_simple(self):
        s1 = pymake.data.Expansion(None)
        s2 = pymake.data.Expansion(None)

        self.assertEqual(s1, s2)

        s1.appendstr('foo')
        s2.appendstr('foo')
        self.assertEqual(s1, s2)

    def test_expansion_string_finish(self):
        """Adjacent strings should normalize to same value."""
        s1 = pymake.data.Expansion(None)
        s2 = pymake.data.Expansion(None)

        s1.appendstr('foo')
        s2.appendstr('foo')

        s1.appendstr(' bar')
        s1.appendstr(' baz')
        s2.appendstr(' bar baz')

        self.assertEqual(s1, s2)

    def test_function(self):
        s1 = pymake.data.Expansion(None)
        s2 = pymake.data.Expansion(None)

        n1 = pymake.data.StringExpansion('FOO', None)
        n2 = pymake.data.StringExpansion('FOO', None)

        v1 = pymake.functions.VariableRef(None, n1)
        v2 = pymake.functions.VariableRef(None, n2)

        s1.appendfunc(v1)
        s2.appendfunc(v2)

        self.assertEqual(s1, s2)


class StringExpansionTest(unittest.TestCase):
    def test_base_expansion_interface(self):
        s1 = pymake.data.StringExpansion('FOO', None)

        self.assertTrue(s1.is_static_string)

        funcs = list(s1.functions())
        self.assertEqual(len(funcs), 0)

        funcs = list(s1.functions(True))
        self.assertEqual(len(funcs), 0)

        refs = list(s1.variable_references())
        self.assertEqual(len(refs), 0)


class ExpansionTest(unittest.TestCase):
    def test_is_static_string(self):
        e1 = pymake.data.Expansion()
        e1.appendstr('foo')

        self.assertTrue(e1.is_static_string)

        e1.appendstr('bar')
        self.assertTrue(e1.is_static_string)

        vname = pymake.data.StringExpansion('FOO', None)
        func = pymake.functions.VariableRef(None, vname)

        e1.appendfunc(func)

        self.assertFalse(e1.is_static_string)

    def test_get_functions(self):
        e1 = pymake.data.Expansion()
        e1.appendstr('foo')

        vname1 = pymake.data.StringExpansion('FOO', None)
        vname2 = pymake.data.StringExpansion('BAR', None)

        func1 = pymake.functions.VariableRef(None, vname1)
        func2 = pymake.functions.VariableRef(None, vname2)

        e1.appendfunc(func1)
        e1.appendfunc(func2)

        funcs = list(e1.functions())
        self.assertEqual(len(funcs), 2)

        func3 = pymake.functions.SortFunction(None)
        func3.append(vname1)

        e1.appendfunc(func3)

        funcs = list(e1.functions())
        self.assertEqual(len(funcs), 3)

        refs = list(e1.variable_references())
        self.assertEqual(len(refs), 2)

    def test_get_functions_descend(self):
        e1 = pymake.data.Expansion()
        vname1 = pymake.data.StringExpansion('FOO', None)
        func1 = pymake.functions.VariableRef(None, vname1)
        e2 = pymake.data.Expansion()
        e2.appendfunc(func1)

        func2 = pymake.functions.SortFunction(None)
        func2.append(e2)

        e1.appendfunc(func2)

        funcs = list(e1.functions())
        self.assertEqual(len(funcs), 1)

        funcs = list(e1.functions(True))
        self.assertEqual(len(funcs), 2)

        self.assertTrue(isinstance(funcs[0], pymake.functions.SortFunction))

    def test_is_filesystem_dependent(self):
        e = pymake.data.Expansion()
        vname1 = pymake.data.StringExpansion('FOO', None)
        func1 = pymake.functions.VariableRef(None, vname1)
        e.appendfunc(func1)

        self.assertFalse(e.is_filesystem_dependent)

        func2 = pymake.functions.WildcardFunction(None)
        func2.append(vname1)
        e.appendfunc(func2)

        self.assertTrue(e.is_filesystem_dependent)

    def test_is_filesystem_dependent_descend(self):
        sort = pymake.functions.SortFunction(None)
        wildcard = pymake.functions.WildcardFunction(None)

        e = pymake.data.StringExpansion('foo/*', None)
        wildcard.append(e)

        e = pymake.data.Expansion(None)
        e.appendfunc(wildcard)

        sort.append(e)

        e = pymake.data.Expansion(None)
        e.appendfunc(sort)

        self.assertTrue(e.is_filesystem_dependent)


if __name__ == '__main__':
    unittest.main()