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
35 class TestMkDepend(unittest
.TestCase
):
37 def testGrepForIncludes(self
):
38 file = """/* some comments */
45 self
.assertEquals(mkdepend
.grepForIncludes(file), ['foo.h', 'bar.h'])
47 def testResolve(self
):
50 if file_exists
.has_key(f
):
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
]
68 def mockGetIncludes(filename
):
69 return ['a', 'a', 'b', 'c', 'd']
71 self
.assertEquals(mkdepend
.getResolvedIncludes('foo.c',
74 ['a', 'a', '../b', 'd'])
76 def testGetDependencies(self
):
77 def mock_getIncludes(filename
):
78 if dependencies
.has_key(filename
):
79 return dependencies
[filename
]
84 self
.assertEquals(mkdepend
.getDependencies(['a.cpp'], mock_getIncludes
),
88 dependencies
['a.cpp'] = ['a.h']
89 self
.assertEquals(mkdepend
.getDependencies(['a.cpp'], mock_getIncludes
),
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
):
104 self
.assertEquals(mkdepend
.sortDependencies(['main.cc']), ['main.cc'])
107 self
.assertEquals(mkdepend
.sortDependencies(['main.cc', 'foo.h']), ['main.cc', 'foo.h'])
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
):
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
):
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
),
130 self
.assertEquals(output
[0], '')
132 self
.assertEquals(mkdepend
.checkArgs(
133 ['mkdepends.py'], usage_string
, mock_write
),
135 self
.assertEquals(output
[0], 'Usage: mkdepends.py target source')
138 if __name__
== '__main__':