6a851206801b5073b576b9ba62523a0ee0220afd
[scons.git] / src / engine / SCons / Scanner / CTests.py
1 __revision__ = "Scanner/CTests.py __REVISION__ __DATE__ __DEVELOPER__"
2
3 import TestCmd
4 import SCons.Scanner.C
5 import unittest
6 import sys
7
8 test = TestCmd.TestCmd(workdir = '')
9
10 # create some source files and headers:
11
12 test.write('f1.cpp',"""
13 #include \"f1.h\"
14 #include <f2.h>
15
16 int main()
17 {
18    return 0;
19 }
20 """)
21
22 test.write('f2.cpp',"""
23 #include \"d1/f1.h\"
24 #include <d2/f1.h>
25 #include \"f1.h\"
26 #include <f4.h>
27
28 int main()
29 {
30    return 0;
31 }
32 """)
33
34 test.write('f3.cpp',"""
35 #include \t "f1.h"
36    \t #include "f2.h"
37 #   \t include "f3.h"
38
39 #include \t <d1/f1.h>
40    \t #include <d1/f2.h>
41 #   \t include <d1/f3.h>
42
43 // #include "never.h"
44
45 const char* x = "#include <never.h>"
46
47 int main()
48 {
49    return 0;
50 }
51 """)
52
53
54 # for Emacs -> "
55
56 test.subdir('d1', ['d1', 'd2'])
57
58 headers = ['f1.h','f2.h', 'f3.h', 'never.h',
59            'd1/f1.h', 'd1/f2.h', 'd1/f3.h',
60            'd1/d2/f1.h', 'd1/d2/f2.h', 'd1/d2/f3.h', 'd1/d2/f4.h']
61
62 for h in headers:
63     test.write(h, " ")
64
65 # define some helpers:
66
67 class DummyEnvironment:
68     pass
69
70 def deps_match(deps, headers):
71     return deps.sort() == map(test.workpath, headers).sort()
72
73 # define some tests:
74
75 class CScannerTestCase1(unittest.TestCase):
76     def runTest(self):
77         env = DummyEnvironment
78         s = SCons.Scanner.C.CScan()
79         deps = s.scan(test.workpath('f1.cpp'), env)
80         self.failUnless(deps_match(deps, ['f1.h', 'f2.h']))
81
82 class CScannerTestCase2(unittest.TestCase):
83     def runTest(self):
84         env = DummyEnvironment
85         env.CPPPATH = [test.workpath("d1")]
86         s = SCons.Scanner.C.CScan()
87         deps = s.scan(test.workpath('f1.cpp'), env)
88         headers = ['f1.h', 'd1/f2.h']
89         self.failUnless(deps_match(deps, headers)) 
90
91 class CScannerTestCase3(unittest.TestCase):
92     def runTest(self):
93         env = DummyEnvironment
94         env.CPPPATH = [test.workpath("d1")]
95         s = SCons.Scanner.C.CScan()
96         deps = s.scan(test.workpath('f2.cpp'), env)
97         headers = ['f1.h', 'd1/f2.h', 'd1/d2/f1.h']
98         self.failUnless(deps_match(deps, headers))
99                   
100
101 class CScannerTestCase4(unittest.TestCase):
102     def runTest(self):
103         env = DummyEnvironment
104         env.CPPPATH = [test.workpath("d1"), test.workpath("d1/d2")]
105         s = SCons.Scanner.C.CScan()
106         deps = s.scan(test.workpath('f2.cpp'), env)
107         headers =  ['f1.h', 'd1/f2.h', 'd1/d2/f1.h', 'd1/d2/f4.h']
108         self.failUnless(deps_match(deps, headers))
109         
110 class CScannerTestCase5(unittest.TestCase):
111     def runTest(self):
112         env = DummyEnvironment
113         s = SCons.Scanner.C.CScan()
114         deps = s.scan(test.workpath('f3.cpp'), env)
115         headers =  ['f1.h', 'f2.h', 'f3.h', 'd1/f1.h', 'd1/f2.h', 'd1/f3.h']
116         self.failUnless(deps_match(deps, headers))
117
118 def suite():
119     suite = unittest.TestSuite()
120     suite.addTest(CScannerTestCase1())
121     suite.addTest(CScannerTestCase2())
122     suite.addTest(CScannerTestCase3())
123     suite.addTest(CScannerTestCase4())
124     suite.addTest(CScannerTestCase5())
125     return suite
126
127 if __name__ == "__main__":
128     runner = unittest.TextTestRunner()
129     result = runner.run(suite())
130     if not result.wasSuccessful():
131         sys.exit(1)