1 | # Copyright Bruno da Silva de Oliveira 2003. Use, modification and |
---|
2 | # distribution is subject to the Boost Software License, Version 1.0. |
---|
3 | # (See accompanying file LICENSE_1_0.txt or copy at |
---|
4 | # http:#www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | import sys |
---|
7 | sys.path.append('../src') |
---|
8 | import unittest |
---|
9 | import tempfile |
---|
10 | import os.path |
---|
11 | from Pyste import GCCXMLParser |
---|
12 | from Pyste.declarations import * |
---|
13 | |
---|
14 | |
---|
15 | class Tester(unittest.TestCase): |
---|
16 | |
---|
17 | def TestConstructor(self, class_, method, visib): |
---|
18 | self.assert_(isinstance(method, Constructor)) |
---|
19 | self.assertEqual(method.FullName(), class_.FullName() + '::' + method.name) |
---|
20 | self.assertEqual(method.result, None) |
---|
21 | self.assertEqual(method.visibility, visib) |
---|
22 | self.assert_(not method.virtual) |
---|
23 | self.assert_(not method.abstract) |
---|
24 | self.assert_(not method.static) |
---|
25 | |
---|
26 | def TestDefaultConstructor(self, class_, method, visib): |
---|
27 | self.TestConstructor(class_, method, visib) |
---|
28 | self.assert_(method.IsDefault()) |
---|
29 | |
---|
30 | def TestCopyConstructor(self, class_, method, visib): |
---|
31 | self.TestConstructor(class_, method, visib) |
---|
32 | self.assertEqual(len(method.parameters), 1) |
---|
33 | param = method.parameters[0] |
---|
34 | self.TestType( |
---|
35 | param, |
---|
36 | ReferenceType, |
---|
37 | class_.FullName(), |
---|
38 | 'const %s&' % class_.FullName(), |
---|
39 | True) |
---|
40 | self.assert_(method.IsCopy()) |
---|
41 | |
---|
42 | |
---|
43 | def TestType(self, type_, classtype_, name, fullname, const): |
---|
44 | self.assert_(isinstance(type_, classtype_)) |
---|
45 | self.assertEqual(type_.name, name) |
---|
46 | self.assertEqual(type_.namespace, None) |
---|
47 | self.assertEqual(type_.FullName(), fullname) |
---|
48 | self.assertEqual(type_.const, const) |
---|
49 | |
---|
50 | |
---|
51 | class ClassBaseTest(Tester): |
---|
52 | |
---|
53 | def setUp(self): |
---|
54 | self.base = GetDecl('Base') |
---|
55 | |
---|
56 | def testClass(self): |
---|
57 | 'test the properties of the class Base' |
---|
58 | self.assert_(isinstance(self.base, Class)) |
---|
59 | self.assert_(self.base.abstract) |
---|
60 | |
---|
61 | |
---|
62 | def testFoo(self): |
---|
63 | 'test function foo in class Base' |
---|
64 | foo = GetMember(self.base, 'foo') |
---|
65 | self.assert_(isinstance(foo, Method)) |
---|
66 | self.assertEqual(foo.visibility, Scope.public) |
---|
67 | self.assert_(foo.virtual) |
---|
68 | self.assert_(foo.abstract) |
---|
69 | self.failIf(foo.static) |
---|
70 | self.assertEqual(foo.class_, 'test::Base') |
---|
71 | self.failIf(foo.const) |
---|
72 | self.assertEqual(foo.FullName(), 'test::Base::foo') |
---|
73 | self.assertEqual(foo.result.name, 'void') |
---|
74 | self.assertEqual(len(foo.parameters), 1) |
---|
75 | param = foo.parameters[0] |
---|
76 | self.TestType(param, FundamentalType, 'int', 'int', False) |
---|
77 | self.assertEqual(foo.namespace, None) |
---|
78 | self.assertEqual( |
---|
79 | foo.PointerDeclaration(1), '(void (test::Base::*)(int) )&test::Base::foo') |
---|
80 | |
---|
81 | def testX(self): |
---|
82 | 'test the member x in class Base' |
---|
83 | x = GetMember(self.base, 'x') |
---|
84 | self.assertEqual(x.class_, 'test::Base') |
---|
85 | self.assertEqual(x.FullName(), 'test::Base::x') |
---|
86 | self.assertEqual(x.namespace, None) |
---|
87 | self.assertEqual(x.visibility, Scope.private) |
---|
88 | self.TestType(x.type, FundamentalType, 'int', 'int', False) |
---|
89 | self.assertEqual(x.static, False) |
---|
90 | |
---|
91 | def testConstructors(self): |
---|
92 | 'test constructors in class Base' |
---|
93 | constructors = GetMembers(self.base, 'Base') |
---|
94 | for cons in constructors: |
---|
95 | if len(cons.parameters) == 0: |
---|
96 | self.TestDefaultConstructor(self.base, cons, Scope.public) |
---|
97 | elif len(cons.parameters) == 1: # copy constructor |
---|
98 | self.TestCopyConstructor(self.base, cons, Scope.public) |
---|
99 | elif len(cons.parameters) == 2: # other constructor |
---|
100 | intp, floatp = cons.parameters |
---|
101 | self.TestType(intp, FundamentalType, 'int', 'int', False) |
---|
102 | self.TestType(floatp, FundamentalType, 'float', 'float', False) |
---|
103 | |
---|
104 | def testSimple(self): |
---|
105 | 'test function simple in class Base' |
---|
106 | simple = GetMember(self.base, 'simple') |
---|
107 | self.assert_(isinstance(simple, Method)) |
---|
108 | self.assertEqual(simple.visibility, Scope.protected) |
---|
109 | self.assertEqual(simple.FullName(), 'test::Base::simple') |
---|
110 | self.assertEqual(len(simple.parameters), 1) |
---|
111 | param = simple.parameters[0] |
---|
112 | self.TestType(param, ReferenceType, 'std::string', 'const std::string&', True) |
---|
113 | self.TestType(simple.result, FundamentalType, 'bool', 'bool', False) |
---|
114 | self.assertEqual( |
---|
115 | simple.PointerDeclaration(1), |
---|
116 | '(bool (test::Base::*)(const std::string&) )&test::Base::simple') |
---|
117 | |
---|
118 | |
---|
119 | def testZ(self): |
---|
120 | z = GetMember(self.base, 'z') |
---|
121 | self.assert_(isinstance(z, Variable)) |
---|
122 | self.assertEqual(z.visibility, Scope.public) |
---|
123 | self.assertEqual(z.FullName(), 'test::Base::z') |
---|
124 | self.assertEqual(z.type.name, 'int') |
---|
125 | self.assertEqual(z.type.const, False) |
---|
126 | self.assert_(z.static) |
---|
127 | |
---|
128 | |
---|
129 | class ClassTemplateTest(Tester): |
---|
130 | |
---|
131 | def setUp(self): |
---|
132 | self.template = GetDecl('Template<int>') |
---|
133 | |
---|
134 | def testClass(self): |
---|
135 | 'test the properties of the Template<int> class' |
---|
136 | self.assert_(isinstance(self.template, Class)) |
---|
137 | self.assert_(not self.template.abstract) |
---|
138 | self.assertEqual(self.template.FullName(), 'Template<int>') |
---|
139 | self.assertEqual(self.template.namespace, '') |
---|
140 | self.assertEqual(self.template.name, 'Template<int>') |
---|
141 | |
---|
142 | def testConstructors(self): |
---|
143 | 'test the automatic constructors of the class Template<int>' |
---|
144 | constructors = GetMembers(self.template, 'Template') |
---|
145 | for cons in constructors: |
---|
146 | if len(cons.parameters) == 0: |
---|
147 | self.TestDefaultConstructor(self.template, cons, Scope.public) |
---|
148 | elif len(cons.parameters) == 1: |
---|
149 | self.TestCopyConstructor(self.template, cons, Scope.public) |
---|
150 | |
---|
151 | |
---|
152 | def testValue(self): |
---|
153 | 'test the class variable value' |
---|
154 | value = GetMember(self.template, 'value') |
---|
155 | self.assert_(isinstance(value, ClassVariable)) |
---|
156 | self.assert_(value.name, 'value') |
---|
157 | self.TestType(value.type, FundamentalType, 'int', 'int', False) |
---|
158 | self.assert_(not value.static) |
---|
159 | self.assertEqual(value.visibility, Scope.public) |
---|
160 | self.assertEqual(value.class_, 'Template<int>') |
---|
161 | self.assertEqual(value.FullName(), 'Template<int>::value') |
---|
162 | |
---|
163 | def testBase(self): |
---|
164 | 'test the superclasses of Template<int>' |
---|
165 | bases = self.template.bases |
---|
166 | self.assertEqual(len(bases), 1) |
---|
167 | base = bases[0] |
---|
168 | self.assert_(isinstance(base, Base)) |
---|
169 | self.assertEqual(base.name, 'test::Base') |
---|
170 | self.assertEqual(base.visibility, Scope.protected) |
---|
171 | |
---|
172 | |
---|
173 | |
---|
174 | class FreeFuncTest(Tester): |
---|
175 | |
---|
176 | def setUp(self): |
---|
177 | self.func = GetDecl('FreeFunc') |
---|
178 | |
---|
179 | def testFunc(self): |
---|
180 | 'test attributes of FreeFunc' |
---|
181 | self.assert_(isinstance(self.func, Function)) |
---|
182 | self.assertEqual(self.func.name, 'FreeFunc') |
---|
183 | self.assertEqual(self.func.FullName(), 'test::FreeFunc') |
---|
184 | self.assertEqual(self.func.namespace, 'test') |
---|
185 | self.assertEqual( |
---|
186 | self.func.PointerDeclaration(1), |
---|
187 | '(const test::Base& (*)(const std::string&, int))&test::FreeFunc') |
---|
188 | |
---|
189 | |
---|
190 | def testResult(self): |
---|
191 | 'test the return value of FreeFunc' |
---|
192 | res = self.func.result |
---|
193 | self.TestType(res, ReferenceType, 'test::Base', 'const test::Base&', True) |
---|
194 | |
---|
195 | def testParameters(self): |
---|
196 | 'test the parameters of FreeFunc' |
---|
197 | self.assertEqual(len(self.func.parameters), 2) |
---|
198 | strp, intp = self.func.parameters |
---|
199 | self.TestType(strp, ReferenceType, 'std::string', 'const std::string&', True) |
---|
200 | self.assertEqual(strp.default, None) |
---|
201 | self.TestType(intp, FundamentalType, 'int', 'int', False) |
---|
202 | self.assertEqual(intp.default, '10') |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | class testFunctionPointers(Tester): |
---|
207 | |
---|
208 | def testMethodPointer(self): |
---|
209 | 'test declaration of a pointer-to-method' |
---|
210 | meth = GetDecl('MethodTester') |
---|
211 | param = meth.parameters[0] |
---|
212 | fullname = 'void (test::Base::*)(int)' |
---|
213 | self.TestType(param, PointerType, fullname, fullname, False) |
---|
214 | |
---|
215 | def testFunctionPointer(self): |
---|
216 | 'test declaration of a pointer-to-function' |
---|
217 | func = GetDecl('FunctionTester') |
---|
218 | param = func.parameters[0] |
---|
219 | fullname = 'void (*)(int)' |
---|
220 | self.TestType(param, PointerType, fullname, fullname, False) |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | # ============================================================================= |
---|
225 | # Support routines |
---|
226 | # ============================================================================= |
---|
227 | |
---|
228 | cppcode = ''' |
---|
229 | namespace std { |
---|
230 | class string; |
---|
231 | } |
---|
232 | namespace test { |
---|
233 | class Base |
---|
234 | { |
---|
235 | public: |
---|
236 | Base(); |
---|
237 | Base(const Base&); |
---|
238 | Base(int, float); |
---|
239 | |
---|
240 | virtual void foo(int = 0.0) = 0; |
---|
241 | static int z; |
---|
242 | protected: |
---|
243 | bool simple(const std::string&); |
---|
244 | private: |
---|
245 | int x; |
---|
246 | }; |
---|
247 | |
---|
248 | void MethodTester( void (Base::*)(int) ); |
---|
249 | void FunctionTester( void (*)(int) ); |
---|
250 | |
---|
251 | |
---|
252 | const Base & FreeFunc(const std::string&, int=10); |
---|
253 | |
---|
254 | } |
---|
255 | |
---|
256 | template <class T> |
---|
257 | struct Template: protected test::Base |
---|
258 | { |
---|
259 | T value; |
---|
260 | virtual void foo(int); |
---|
261 | }; |
---|
262 | |
---|
263 | Template<int> __aTemplateInt; |
---|
264 | ''' |
---|
265 | |
---|
266 | def GetXMLFile(): |
---|
267 | '''Generates an gccxml file using the code from the global cppcode. |
---|
268 | Returns the xml's filename.''' |
---|
269 | # write the code to a header file |
---|
270 | tmpfile = tempfile.mktemp() + '.h' |
---|
271 | f = file(tmpfile, 'w') |
---|
272 | f.write(cppcode) |
---|
273 | f.close() |
---|
274 | # run gccxml |
---|
275 | outfile = tmpfile + '.xml' |
---|
276 | if os.system('gccxml "%s" "-fxml=%s"' % (tmpfile, outfile)) != 0: |
---|
277 | raise RuntimeError, 'Error executing GCCXML.' |
---|
278 | # read the output file into the xmlcode |
---|
279 | f = file(outfile) |
---|
280 | xmlcode = f.read() |
---|
281 | #print xmlcode |
---|
282 | f.close() |
---|
283 | # remove the header |
---|
284 | os.remove(tmpfile) |
---|
285 | return outfile |
---|
286 | |
---|
287 | |
---|
288 | |
---|
289 | def GetDeclarations(): |
---|
290 | 'Uses the GCCXMLParser module to get the declarations.' |
---|
291 | xmlfile = GetXMLFile() |
---|
292 | declarations = GCCXMLParser.ParseDeclarations(xmlfile) |
---|
293 | os.remove(xmlfile) |
---|
294 | return declarations |
---|
295 | |
---|
296 | # the declarations to be analysed |
---|
297 | declarations = GetDeclarations() |
---|
298 | |
---|
299 | |
---|
300 | def GetDecl(name): |
---|
301 | 'returns one of the top declarations given its name' |
---|
302 | for decl in declarations: |
---|
303 | if decl.name == name: |
---|
304 | return decl |
---|
305 | else: |
---|
306 | raise RuntimeError, 'Declaration not found: %s' % name |
---|
307 | |
---|
308 | |
---|
309 | def GetMember(class_, name): |
---|
310 | 'gets the member of the given class by its name' |
---|
311 | |
---|
312 | res = None |
---|
313 | multipleFound = False |
---|
314 | for member in class_: |
---|
315 | if member.name == name: |
---|
316 | if res is not None: |
---|
317 | multipleFound = True |
---|
318 | break |
---|
319 | res = member |
---|
320 | if res is None or multipleFound: |
---|
321 | raise RuntimeError, \ |
---|
322 | 'No member or more than one member found in class %s: %s' \ |
---|
323 | % (class_.name, name) |
---|
324 | return res |
---|
325 | |
---|
326 | |
---|
327 | def GetMembers(class_, name): |
---|
328 | 'gets the members of the given class by its name' |
---|
329 | res = [] |
---|
330 | for member in class_: |
---|
331 | if member.name == name: |
---|
332 | res.append(member) |
---|
333 | if len(res) in (0, 1): |
---|
334 | raise RuntimeError, \ |
---|
335 | 'GetMembers: 0 or 1 members found in class %s: %s' \ |
---|
336 | % (class_.name, name) |
---|
337 | return res |
---|
338 | |
---|
339 | |
---|
340 | if __name__ == '__main__': |
---|
341 | unittest.main() |
---|