1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
*
* Copyright 2002-2004 The Ant-Contrib project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package net.sf.antcontrib.cpptasks;
import java.io.File;
import java.io.IOException;
import net.sf.antcontrib.cpptasks.compiler.CommandLineCompilerConfiguration;
import net.sf.antcontrib.cpptasks.compiler.Compiler;
import net.sf.antcontrib.cpptasks.compiler.LinkType;
import net.sf.antcontrib.cpptasks.devstudio.DevStudioCCompiler;
import net.sf.antcontrib.cpptasks.gcc.GccCCompiler;
import net.sf.antcontrib.cpptasks.types.CompilerArgument;
import net.sf.antcontrib.cpptasks.types.ConditionalPath;
import net.sf.antcontrib.cpptasks.types.DefineArgument;
import net.sf.antcontrib.cpptasks.types.DefineSet;
import net.sf.antcontrib.cpptasks.types.IncludePath;
import net.sf.antcontrib.cpptasks.types.SystemIncludePath;
import net.sf.antcontrib.cpptasks.types.UndefineArgument;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
/**
* Tests for CompilerDef.
*/
public final class TestCompilerDef
extends TestProcessorDef {
/**
* Constructor.
*
* @param name
* test name
*/
public TestCompilerDef(final String name) {
super(name);
}
/**
* Creates a new processor.
*
* @return new processor
*/
protected ProcessorDef create() {
return new CompilerDef();
}
/**
* This method tests CompilerDef.getActiveDefines.
*
* A CompilerDef is created similar to what would be created for
*
* <cc><defineset><define name="DEBUG" if="debug"/> <define name="NDEBUG"
* unless="debug"/> </defineset> </cc>
*
* Then getActiveDefines is called for a project without and with the
* "debug" property defined. Return value from getActiveDefines should
* contain one member
*/
public void testGetActiveDefines() {
Project project = new org.apache.tools.ant.Project();
CompilerDef def = new CompilerDef();
def.setProject(project);
DefineSet defset = new DefineSet();
DefineArgument arg1 = new DefineArgument();
arg1.setName("DEBUG");
arg1.setIf("debug");
defset.addDefine(arg1);
DefineArgument arg2 = new DefineArgument();
arg2.setName("NDEBUG");
arg2.setUnless("debug");
defset.addDefine(arg2);
def.addConfiguredDefineset(defset);
//
// Evaluate without "debug" set
//
UndefineArgument[] activeArgs = def.getActiveDefines();
assertEquals(1, activeArgs.length);
assertEquals("NDEBUG", activeArgs[0].getName());
//
// Set the "debug" property
//
project.setProperty("debug", "");
activeArgs = def.getActiveDefines();
assertEquals(1, activeArgs.length);
assertEquals("DEBUG", activeArgs[0].getName());
}
/**
* This method tests CompilerDef.getActiveIncludePath.
*
* A CompilerDef is created similar to what would be created for
*
* <cc><includepath location=".." if="debug"/> </cc>
*
* and is evaluate for a project without and without "debug" set
*/
public void testGetActiveIncludePaths() {
Project project = new org.apache.tools.ant.Project();
CompilerDef def = new CompilerDef();
def.setProject(project);
ConditionalPath path = def.createIncludePath();
path.setLocation(new File(".."));
path.setIf("debug");
//
// Evaluate without "debug" set
//
String[] includePaths = def.getActiveIncludePaths();
assertEquals(0, includePaths.length);
//
// Set the "debug" property
//
project.setProperty("debug", "");
includePaths = def.getActiveIncludePaths();
assertEquals(1, includePaths.length);
}
/**
* Tests that setting classname to the Gcc compiler is effective.
*/
public void testGetGcc() {
CompilerDef compilerDef = (CompilerDef) create();
compilerDef.setClassname("net.sf.antcontrib.cpptasks.gcc.GccCCompiler");
Compiler comp = (Compiler) compilerDef.getProcessor();
assertNotNull(comp);
assertSame(GccCCompiler.getInstance(), comp);
}
/**
* Tests that setting classname to the MSVC compiler is effective.
*/
public void testGetMSVC() {
CompilerDef compilerDef = (CompilerDef) create();
compilerDef
.setClassname(
"net.sf.antcontrib.cpptasks.devstudio.DevStudioCCompiler");
Compiler comp = (Compiler) compilerDef.getProcessor();
assertNotNull(comp);
assertSame(DevStudioCCompiler.getInstance(), comp);
}
/**
* Tests that setting classname to an bogus class name results in a
* BuildException.
*/
public void testUnknownClass() {
CompilerDef compilerDef = (CompilerDef) create();
try {
compilerDef
.setClassname("net.sf.antcontrib.cpptasks.bogus.BogusCompiler");
} catch (BuildException ex) {
return;
}
fail("Exception not thrown");
}
/**
* Test that setting classname to a class that doesn't support Compiler
* throws a BuildException.
*
*/
public void testWrongType() {
CompilerDef compilerDef = (CompilerDef) create();
try {
compilerDef
.setClassname("net.sf.antcontrib.cpptasks.devstudio.DevStudioLinker");
} catch (BuildException ex) {
return;
}
fail("Exception not thrown");
}
/**
* Gets the command line arguments that precede filenames.
*
* @param processor
* processor under test
* @return command line arguments
*/
protected String[] getPreArguments(final ProcessorDef processor) {
return ((CommandLineCompilerConfiguration) getConfiguration(processor))
.getPreArguments();
}
/**
* Tests if a fileset enclosed in the base compiler definition is effective.
*
* @throws IOException
* if unable to create or delete a temporary file
*/
public void testExtendsFileSet() throws IOException {
super.testExtendsFileSet(File.createTempFile("cpptaskstest", ".cpp"));
}
/**
* Tests if the rebuild attribute of the base compiler definition is
* effective.
*
*/
public void testExtendsRebuild() {
testExtendsRebuild(new CompilerDef());
}
/**
* Tests that compilerarg's contained in the base compiler definition are
* effective.
*/
public void testExtendsCompilerArgs() {
CompilerDef baseLinker = new CompilerDef();
CompilerArgument linkerArg = new CompilerArgument();
linkerArg.setValue("/base");
baseLinker.addConfiguredCompilerArg(linkerArg);
CompilerDef extendedLinker = (CompilerDef) createExtendedProcessorDef(
baseLinker);
String[] preArgs = getPreArguments(extendedLinker);
assertEquals(2, preArgs.length);
assertEquals("/base", preArgs[0]);
}
/**
* Tests that defineset's contained in the base compiler definition are
* effective.
*/
public void testExtendsDefineSet() {
CompilerDef baseCompiler = new CompilerDef();
DefineSet defSet = new DefineSet();
DefineArgument define = new DefineArgument();
define.setName("foo");
define.setValue("bar");
defSet.addDefine(define);
baseCompiler.addConfiguredDefineset(defSet);
CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(
baseCompiler);
String[] preArgs = getPreArguments(extendedCompiler);
assertEquals(2, preArgs.length);
assertEquals("-Dfoo=bar", preArgs[1]);
}
/**
* Tests that includepath's contained in the base compiler definition are
* effective.
*/
public void testExtendsIncludePath() {
CompilerDef baseCompiler = new CompilerDef();
CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(
baseCompiler);
IncludePath path = baseCompiler.createIncludePath();
path.setPath("/tmp");
String[] preArgs = getPreArguments(extendedCompiler);
assertEquals(2, preArgs.length);
assertEquals("-I", preArgs[1].substring(0, 2));
}
/**
* Tests that sysincludepath's contained in the base compiler definition are
* effective.
*/
public void testExtendsSysIncludePath() {
CompilerDef baseCompiler = new CompilerDef();
CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(
baseCompiler);
SystemIncludePath path = baseCompiler.createSysIncludePath();
path.setPath("/tmp");
String[] preArgs = getPreArguments(extendedCompiler);
assertEquals(2, preArgs.length);
assertEquals("-I", preArgs[1].substring(0, 2));
}
/**
* Sets the name attribute.
*
* @param compiler
* compiler under test
* @param name
* compiler name
*/
private static void setCompilerName(final CompilerDef compiler,
final String name) {
CompilerEnum compilerName = new CompilerEnum();
compilerName.setValue(name);
compiler.setName(compilerName);
}
/**
* Tests that the extend attribute of the base compiler definition is
* effective.
*/
public void testExtendsExceptions() {
CompilerDef baseCompiler = new CompilerDef();
baseCompiler.setExceptions(true);
CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(
baseCompiler);
setCompilerName(extendedCompiler, "msvc");
String[] preArgs = getPreArguments(extendedCompiler);
assertEquals("/EHsc", preArgs[2]);
}
/**
* Tests that the multithread attribute of the base compiler definition is
* effective.
*/
public void testExtendsMultithreaded() {
CompilerDef baseCompiler = new CompilerDef();
baseCompiler.setMultithreaded(false);
CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(
baseCompiler);
setCompilerName(extendedCompiler, "msvc");
CCTask cctask = new CCTask();
LinkType linkType = new LinkType();
linkType.setStaticRuntime(true);
CommandLineCompilerConfiguration config = (CommandLineCompilerConfiguration)
extendedCompiler
.createConfiguration(cctask, linkType, null, null, null);
String[] preArgs = config.getPreArguments();
assertEquals("/ML", preArgs[3]);
}
/**
* Tests that the name attribute in the base compiler is effective.
*/
public void testExtendsName() {
CompilerDef baseCompiler = new CompilerDef();
setCompilerName(baseCompiler, "msvc");
CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(
baseCompiler);
extendedCompiler.setExceptions(true);
String[] preArgs = getPreArguments(extendedCompiler);
assertEquals("/EHsc", preArgs[2]);
}
/**
* Tests that the classname attribute in the base compiler is effective.
*/
public void testExtendsClassname() {
CompilerDef baseCompiler = new CompilerDef();
baseCompiler
.setClassname(
"net.sf.antcontrib.cpptasks.devstudio.DevStudioCCompiler");
CompilerDef extendedCompiler = (CompilerDef) createExtendedProcessorDef(
baseCompiler);
extendedCompiler.setExceptions(true);
String[] preArgs = getPreArguments(extendedCompiler);
assertEquals("/EHsc", preArgs[2]);
}
}
|