1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.github.maven_nar.cpptasks;
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27
28 import com.github.maven_nar.cpptasks.compiler.CommandLineCompilerConfiguration;
29 import com.github.maven_nar.cpptasks.compiler.Compiler;
30 import com.github.maven_nar.cpptasks.compiler.LinkType;
31 import com.github.maven_nar.cpptasks.gcc.GccCCompiler;
32 import com.github.maven_nar.cpptasks.msvc.MsvcCCompiler;
33 import com.github.maven_nar.cpptasks.types.CompilerArgument;
34 import com.github.maven_nar.cpptasks.types.ConditionalPath;
35 import com.github.maven_nar.cpptasks.types.DefineArgument;
36 import com.github.maven_nar.cpptasks.types.DefineSet;
37 import com.github.maven_nar.cpptasks.types.IncludePath;
38 import com.github.maven_nar.cpptasks.types.SystemIncludePath;
39 import com.github.maven_nar.cpptasks.types.UndefineArgument;
40
41
42
43
44 public final class TestCompilerDef extends TestProcessorDef {
45
46
47
48
49
50
51
52
53 private static void setCompilerName(final CompilerDef compiler, final String name) {
54 final CompilerEnum compilerName = new CompilerEnum();
55 compilerName.setValue(name);
56 compiler.setName(compilerName);
57 }
58
59
60
61
62
63
64
65 public TestCompilerDef(final String name) {
66 super(name);
67 }
68
69
70
71
72
73
74 @Override
75 protected ProcessorDef create() {
76 return new CompilerDef();
77 }
78
79
80
81
82
83
84
85
86
87
88
89 @Override
90 protected String[] getPreArguments(final ProcessorDef processor) {
91 final String[] result = ((CommandLineCompilerConfiguration) getConfiguration(processor)).getPreArguments();
92
93
94 int j = -1;
95 for (int i = 0; i < result.length; i++) {
96 if (result[i].startsWith("-m")) {
97 continue;
98 }
99 if (i != ++j) {
100 result[j] = result[i];
101 }
102 }
103 if (++j == result.length) {
104 return result;
105 }
106 final String[] filtered = new String[j];
107 System.arraycopy(result, 0, filtered, 0, j);
108 return filtered;
109 }
110
111
112
113
114 public void testExtendsClassname() {
115 final CompilerDef baseCompiler = new CompilerDef();
116 baseCompiler.setClassname("com.github.maven_nar.cpptasks.msvc.MsvcCCompiler");
117 final CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(baseCompiler);
118 extendedCompiler.setExceptions(true);
119 final String[] preArgs = getPreArguments(extendedCompiler);
120 assertEquals("/EHsc", preArgs[2]);
121 }
122
123
124
125
126
127 public void testExtendsCompilerArgs() {
128 final CompilerDef baseLinker = new CompilerDef();
129 final CompilerArgument linkerArg = new CompilerArgument();
130 linkerArg.setValue("/base");
131 baseLinker.addConfiguredCompilerArg(linkerArg);
132 final CompilerDef extendedLinker = (CompilerDef) createExtendedProcessorDef(baseLinker);
133 final String[] preArgs = getPreArguments(extendedLinker);
134
135 assertEquals(3, preArgs.length);
136 assertEquals("/base", preArgs[0]);
137 }
138
139
140
141
142
143 public void testExtendsDefineSet() {
144 final CompilerDef baseCompiler = new CompilerDef();
145 final DefineSet defSet = new DefineSet();
146 final DefineArgument define = new DefineArgument();
147 define.setName("foo");
148 define.setValue("bar");
149 defSet.addDefine(define);
150 baseCompiler.addConfiguredDefineset(defSet);
151 final CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(baseCompiler);
152 final String[] preArgs = getPreArguments(extendedCompiler);
153
154 assertEquals(3, preArgs.length);
155 assertEquals("-Dfoo=bar", preArgs[2]);
156
157 }
158
159
160
161
162
163 public void testExtendsExceptions() {
164 final CompilerDef baseCompiler = new CompilerDef();
165 baseCompiler.setExceptions(true);
166 final CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(baseCompiler);
167 setCompilerName(extendedCompiler, "msvc");
168 final String[] preArgs = getPreArguments(extendedCompiler);
169 assertEquals("/EHsc", preArgs[2]);
170 }
171
172
173
174
175
176
177
178 public void testExtendsFileSet() throws IOException {
179 super.testExtendsFileSet(File.createTempFile("cpptaskstest", ".cpp"));
180 }
181
182
183
184
185
186 public void testExtendsIncludePath() {
187 final CompilerDef baseCompiler = new CompilerDef();
188 final CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(baseCompiler);
189 final IncludePath path = baseCompiler.createIncludePath();
190 path.setPath("/tmp");
191 final String[] preArgs = getPreArguments(extendedCompiler);
192
193 assertEquals(3, preArgs.length);
194 assertEquals("-I", preArgs[2].substring(0, 2));
195
196 }
197
198
199
200
201
202 public void testExtendsMultithreaded() {
203 final CompilerDef baseCompiler = new CompilerDef();
204 baseCompiler.setMultithreaded(false);
205 final CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(baseCompiler);
206 setCompilerName(extendedCompiler, "msvc");
207 final CCTask cctask = new CCTask();
208 final LinkType linkType = new LinkType();
209 final File objDir = new File("dummy");
210 cctask.setObjdir(objDir);
211 linkType.setStaticRuntime(true);
212 final CommandLineCompilerConfiguration config = (CommandLineCompilerConfiguration) extendedCompiler
213 .createConfiguration(cctask, linkType, null, null, null);
214 final String[] preArgs = config.getPreArguments();
215 assertEquals("/ML", preArgs[3]);
216 }
217
218
219
220
221 public void testExtendsName() {
222 final CompilerDef baseCompiler = new CompilerDef();
223 setCompilerName(baseCompiler, "msvc");
224 final CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(baseCompiler);
225 extendedCompiler.setExceptions(true);
226 final String[] preArgs = getPreArguments(extendedCompiler);
227 assertEquals("/EHsc", preArgs[2]);
228 }
229
230
231
232
233
234
235 public void testExtendsRebuild() {
236 testExtendsRebuild(new CompilerDef());
237 }
238
239
240
241
242
243 public void testExtendsSysIncludePath() {
244 final CompilerDef baseCompiler = new CompilerDef();
245 final CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(baseCompiler);
246 final SystemIncludePath path = baseCompiler.createSysIncludePath();
247 path.setPath("/tmp");
248 final String[] preArgs = getPreArguments(extendedCompiler);
249 System.out.println("Class: " + baseCompiler + " and: " + extendedCompiler);
250
251 assertEquals(3, preArgs.length);
252 assertEquals("-isystem", preArgs[2].substring(0, 8));
253
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268 public void testGetActiveDefines() {
269 final Project project = new org.apache.tools.ant.Project();
270 final CompilerDef def = new CompilerDef();
271 def.setProject(project);
272 final DefineSet defset = new DefineSet();
273 final DefineArgument arg1 = new DefineArgument();
274 arg1.setName("DEBUG");
275 arg1.setIf("debug");
276 defset.addDefine(arg1);
277 final DefineArgument arg2 = new DefineArgument();
278 arg2.setName("NDEBUG");
279 arg2.setUnless("debug");
280 defset.addDefine(arg2);
281 def.addConfiguredDefineset(defset);
282
283
284
285 UndefineArgument[] activeArgs = def.getActiveDefines();
286 assertEquals(1, activeArgs.length);
287 assertEquals("NDEBUG", activeArgs[0].getName());
288
289
290
291 project.setProperty("debug", "");
292 activeArgs = def.getActiveDefines();
293 assertEquals(1, activeArgs.length);
294 assertEquals("DEBUG", activeArgs[0].getName());
295 }
296
297
298
299
300
301
302
303
304
305
306 public void testGetActiveIncludePaths() {
307 final Project project = new org.apache.tools.ant.Project();
308 final CompilerDef def = new CompilerDef();
309 def.setProject(project);
310 final ConditionalPath path = def.createIncludePath();
311 path.setLocation(new File(".."));
312 path.setIf("debug");
313
314
315
316 String[] includePaths = def.getActiveIncludePaths();
317 assertEquals(0, includePaths.length);
318
319
320
321 project.setProperty("debug", "");
322 includePaths = def.getActiveIncludePaths();
323 assertEquals(1, includePaths.length);
324 }
325
326
327
328
329 public void testGetGcc() {
330 final CompilerDef compilerDef = (CompilerDef) create();
331 compilerDef.setClassname("com.github.maven_nar.cpptasks.gcc.GccCCompiler");
332 final Compiler comp = (Compiler) compilerDef.getProcessor();
333 assertNotNull(comp);
334 assertSame(GccCCompiler.getInstance(), comp);
335 }
336
337
338
339
340 public void testGetMSVC() {
341 final CompilerDef compilerDef = (CompilerDef) create();
342 compilerDef.setClassname("com.github.maven_nar.cpptasks.msvc.MsvcCCompiler");
343 final Compiler comp = (Compiler) compilerDef.getProcessor();
344 assertNotNull(comp);
345 assertSame(MsvcCCompiler.getInstance(), comp);
346 }
347
348
349
350
351
352 public void testUnknownClass() {
353 final CompilerDef compilerDef = (CompilerDef) create();
354 try {
355 compilerDef.setClassname("com.github.maven_nar.cpptasks.bogus.BogusCompiler");
356 } catch (final BuildException ex) {
357 return;
358 }
359 fail("Exception not thrown");
360 }
361
362
363
364
365
366
367 public void testWrongType() {
368 final CompilerDef compilerDef = (CompilerDef) create();
369 try {
370 compilerDef.setClassname("com.github.maven_nar.cpptasks.msvc.MsvcLinker");
371 } catch (final BuildException ex) {
372 return;
373 }
374 fail("Exception not thrown");
375 }
376 }