[Author: aa]
[google-gears.git] / gears / tools / mkdepend_unittest.py
blobe04d61f3afeb7aae9a25e93340e633c41924c985
1 #!/usr/bin/python2.4
3 # Copyright 2007, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
8 # 1. Redistributions of source code must retain the above copyright notice,
9 # this list of conditions and the following disclaimer.
10 # 2. Redistributions in binary form must reproduce the above copyright notice,
11 # this list of conditions and the following disclaimer in the documentation
12 # and/or other materials provided with the distribution.
13 # 3. Neither the name of Google Inc. nor the names of its contributors may be
14 # used to endorse or promote products derived from this software without
15 # specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20 # EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 """Unit tests for mkdepend.py
29 """
32 import unittest
33 import mkdepend
35 class TestMkDepend(unittest.TestCase):
37 def testGrepForIncludes(self):
38 file = """/* some comments */
39 #include "foo.h"
40 #include"baz.h"
41 #include_ "foo.h"
42 #include <baz.h>
43 #include "bar.h"
44 """
45 self.assertEquals(mkdepend.grepForIncludes(file), ['foo.h', 'bar.h'])
47 def testResolve(self):
48 file_exists = {}
49 def f_file_exists(f):
50 if file_exists.has_key(f):
51 return True
52 return False
54 file_exists['foo.h'] = True
55 file_exists[r'..\baz.h'] = True
56 self.assertEquals(mkdepend.resolve('foo.h', f_file_exists), 'foo.h')
57 self.assertEquals(mkdepend.resolve('bar.h', f_file_exists), None)
58 self.assertEquals(mkdepend.resolve('baz.h', f_file_exists), r'..\baz.h')
60 def testGetResolvedIncludes(self):
62 def mockResolve(filename):
63 files = {'a':'a', 'b':'../b', 'd': 'd'}
64 if files.has_key(filename):
65 return files[filename]
66 return None
68 def mockGetIncludes(filename):
69 return ['a', 'a', 'b', 'c', 'd']
71 self.assertEquals(mkdepend.getResolvedIncludes('foo.c',
72 mockResolve,
73 mockGetIncludes),
74 ['a', 'a', '../b', 'd'])
76 def testGetDependencies(self):
77 def mock_getIncludes(filename):
78 if dependencies.has_key(filename):
79 return dependencies[filename]
80 return []
82 dependencies = {}
83 # No dependencies.
84 self.assertEquals(mkdepend.getDependencies(['a.cpp'], mock_getIncludes),
85 ['a.cpp'])
87 # One dependency.
88 dependencies['a.cpp'] = ['a.h']
89 self.assertEquals(mkdepend.getDependencies(['a.cpp'], mock_getIncludes),
90 ['a.cpp', 'a.h'])
92 # Multiple dependencies.
93 dependencies['a.cpp'] = ['a.h', 'b.h']
94 dependencies['b.h'] = ['bb.h', 'bc.h', 'bd.h']
95 dependencies['bc.h'] = ['bca.h']
96 self.assertEquals(mkdepend.getDependencies(['a.cpp'], mock_getIncludes),
97 ['a.cpp', 'a.h', 'b.h', 'bb.h', 'bc.h', 'bca.h', 'bd.h'])
99 def testSortAndRemoveDupes(self):
100 self.assertEquals(mkdepend.sortAndRemoveDupes(['b', 'a', 'a']), ['a', 'b'])
102 def testSortDependencies(self):
103 # No deps.
104 self.assertEquals(mkdepend.sortDependencies(['main.cc']), ['main.cc'])
106 # One dep.
107 self.assertEquals(mkdepend.sortDependencies(['main.cc', 'foo.h']), ['main.cc', 'foo.h'])
109 # Multiple deps.
110 self.assertEquals(mkdepend.sortDependencies(['main.cc', 'foo.h', 'baz.h', 'bar.h']),
111 ['main.cc', 'bar.h', 'baz.h', 'foo.h'])
113 def testPrintDependencies(self):
114 output = ['']
115 def mock_write(s):
116 output[0] = output[0] + s
118 mkdepend.printDependencies(['a.c', 'a.h', 'b.h'], 'a.obj', mock_write)
119 self.assertEquals(output[0], 'a.obj : a.c a.h b.h ')
121 def testCheckArgs(self):
122 output = ['']
123 def mock_write(s):
124 output[0] = output[0] + s
126 usage_string = "Usage: %s target source"
127 self.assertEquals(mkdepend.checkArgs(
128 ['mkdepends.py', 'main.cc', 'main.obj'], usage_string, mock_write),
129 True)
130 self.assertEquals(output[0], '')
132 self.assertEquals(mkdepend.checkArgs(
133 ['mkdepends.py'], usage_string, mock_write),
134 False)
135 self.assertEquals(output[0], 'Usage: mkdepends.py target source')
138 if __name__ == '__main__':
139 unittest.main()