Link Here
|
15 |
import java.io.IOException; |
15 |
import java.io.IOException; |
16 |
import java.text.SimpleDateFormat; |
16 |
import java.text.SimpleDateFormat; |
17 |
import java.util.ArrayList; |
17 |
import java.util.ArrayList; |
18 |
import java.util.Arrays; |
|
|
19 |
import java.util.Date; |
18 |
import java.util.Date; |
20 |
import java.util.HashMap; |
19 |
import java.util.HashMap; |
21 |
import java.util.List; |
20 |
import java.util.List; |
22 |
import java.util.Map; |
21 |
import java.util.Map; |
23 |
import java.util.StringTokenizer; |
|
|
24 |
|
22 |
|
25 |
import junit.framework.AssertionFailedError; |
23 |
import junit.framework.AssertionFailedError; |
26 |
import junit.framework.ComparisonFailure; |
24 |
import junit.framework.ComparisonFailure; |
Link Here
|
30 |
import org.eclipse.core.runtime.IPath; |
28 |
import org.eclipse.core.runtime.IPath; |
31 |
import org.eclipse.core.runtime.Path; |
29 |
import org.eclipse.core.runtime.Path; |
32 |
import org.eclipse.jdt.core.formatter.CodeFormatter; |
30 |
import org.eclipse.jdt.core.formatter.CodeFormatter; |
33 |
import org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants; |
|
|
34 |
import org.eclipse.jdt.core.tests.model.ModelTestsUtil; |
31 |
import org.eclipse.jdt.core.tests.model.ModelTestsUtil; |
35 |
import org.eclipse.jdt.core.tests.util.Util; |
32 |
import org.eclipse.jdt.core.tests.util.Util; |
36 |
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; |
|
|
37 |
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; |
38 |
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
39 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
33 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
40 |
import org.eclipse.jdt.internal.compiler.parser.Scanner; |
|
|
41 |
import org.eclipse.jdt.internal.core.util.CodeSnippetParsingUtil; |
42 |
import org.eclipse.jdt.internal.core.util.SimpleDocument; |
43 |
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; |
34 |
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatter; |
44 |
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions; |
35 |
import org.eclipse.jdt.internal.formatter.DefaultCodeFormatterOptions; |
45 |
import org.eclipse.text.edits.TextEdit; |
36 |
import org.eclipse.text.edits.TextEdit; |
Link Here
|
102 |
* <code>linesLeading</code> (e.g. ignore white spaces at the beginning of the |
93 |
* <code>linesLeading</code> (e.g. ignore white spaces at the beginning of the |
103 |
* lines, including the star inside javadoc or block comments): |
94 |
* lines, including the star inside javadoc or block comments): |
104 |
* <ul> |
95 |
* <ul> |
105 |
* <li>Eclipse 3.0 performance workspace (9951 units):<ul> |
96 |
* <li>JUnit 3.8.2 workspace (71 units): |
|
|
97 |
* <ul> |
106 |
* <li>0 error</li> |
98 |
* <li>0 error</li> |
107 |
* <li>0 failures</li> |
99 |
* <li>0 failures</li> |
108 |
* <li>8 failures due to old formatter</li> |
100 |
* <li>0 failures due to old formatter</li> |
109 |
* <li>723 files have different lines leading spaces</li> |
101 |
* <li>8 files have different lines leading spaces</li> |
110 |
* <li>9 files have different spaces</li> |
102 |
* <li>0 files have different spaces</li> |
111 |
* </ul></li> |
103 |
* </ul></li> |
112 |
* <li>Eclipse 3.4 workspace (16592 units):<ul> |
104 |
* <li>Eclipse 3.0 performance workspace (9951 units): |
|
|
105 |
* <ul> |
113 |
* <li>0 error</li> |
106 |
* <li>0 error</li> |
114 |
* <li>11 failures</li> |
107 |
* <li>1 failures</li> |
115 |
* <li>17 failures due to old formatter</li> |
108 |
* <li>8 failures due to old formatter</li> |
116 |
* <li>1244 files have different lines leading spaces</li> |
109 |
* <li>722 files have different lines leading spaces</li> |
117 |
* <li>11 files have different spaces</li> |
110 |
* <li>9 files have different spaces</li> |
118 |
* </ul></li> |
111 |
* </ul></li> |
119 |
* <li>ganymede M5 workspace (25819 units):<ul> |
112 |
* <li>Eclipse 3.4 workspace (17890 units): |
|
|
113 |
* <ul> |
120 |
* <li>0 error</li> |
114 |
* <li>0 error</li> |
121 |
* <li>12 failures due to different output while reformatting!</li> |
115 |
* <li>17 failures</li> |
122 |
* <li>15 failures due to old formatter</li> |
116 |
* <li>21 failures due to old formatter</li> |
123 |
* <li>1371 files have different line leading spaces when reformatting!</li> |
117 |
* <li>1372 files have different lines leading spaces</li> |
124 |
* <li>14 files have different spaces when reformatting!</li> |
118 |
* <li>12 files have different spaces</li> |
125 |
* </ul></li> |
119 |
* </ul></li> |
126 |
* <li>ganymede M6a workspace (26336 units):<ul> |
120 |
* <li>ganymede workspace (33190 units): |
127 |
* <li>0 error</li> |
121 |
* <ul> |
128 |
* <li>16 failures due to different output while reformatting!</li> |
122 |
* <li>1 error</li> |
129 |
* <li>17 failures due to old formatter</li> |
123 |
* <li>21 failures due to different output while reformatting!</li> |
130 |
* <li>1469 files have different line leading spaces when reformatting!</li> |
124 |
* <li>21 failures due to old formatter</li> |
131 |
* <li>14 files have different spaces when reformatting!</li> |
125 |
* <li>1780 files have different line leading spaces when reformatting!</li> |
|
|
126 |
* <li>20 files have different spaces when reformatting!</li> |
132 |
* </ul></li> |
127 |
* </ul></li> |
133 |
* </ul> |
128 |
* </ul> |
134 |
*/ |
129 |
*/ |
135 |
public class FormatterCommentsMassiveTests extends FormatterRegressionTests { |
130 |
public class FormatterCommentsMassiveTests extends FormatterRegressionTests { |
136 |
|
131 |
|
137 |
private static final String LINE_SEPARATOR = org.eclipse.jdt.internal.compiler.util.Util.LINE_SEPARATOR; |
|
|
138 |
final File file; |
132 |
final File file; |
139 |
final IPath path; |
133 |
final IPath path; |
140 |
List failures = new ArrayList(); |
|
|
141 |
List expectedFailures = new ArrayList(); |
142 |
List leadingWhitespacesFailures = new ArrayList(); |
143 |
List whitespacesFailures= new ArrayList(); |
144 |
boolean hasSpaceFailure; |
134 |
boolean hasSpaceFailure; |
145 |
private int changedHeaderFooter; |
135 |
private DefaultCodeFormatterOptions preferences; |
146 |
private int changedPreTags; |
136 |
private final static File INPUT_DIR = new File(System.getProperty("inputDir")); |
147 |
private int changedCodeTags; |
137 |
private final static File OUTPUT_DIR; |
148 |
private final static boolean DEBUG_TESTS = "true".equals(System.getProperty("debugTests")); |
138 |
private final static boolean COMPARE; |
149 |
private final static String DIR = System.getProperty("dir"); //$NON-NLS-1$ |
|
|
150 |
private final static String COMPARE = System.getProperty("compare"); //$NON-NLS-1$ |
151 |
private final static int IGNORE_SPACES; |
152 |
private final static int ALL_SPACES = 1; // ignore all spaces |
153 |
private final static int LINES_LEADING_SPACES = 2; // ignore all spaces at the beginning of all lines |
154 |
private final static int ALL_COMMENTS_SPACES = 3; // ignore all spaces inside all comments |
155 |
private final static int ALL_COMMENTS_LINES_LEADING_SPACES = 4; // ignore all spaces at the beginning of all comments lines |
156 |
static { |
139 |
static { |
157 |
String ignoreSpaces = System.getProperty("ignoreSpaces"); //$NON-NLS-1$ |
140 |
String dir = System.getProperty("outputDir"); //$NON-NLS-1$ |
158 |
int filterValue; |
141 |
File outputDir = null; |
159 |
if ("all".equals(ignoreSpaces)) { |
142 |
boolean compare = true; |
160 |
filterValue = ALL_SPACES; |
143 |
if (dir != null) { |
161 |
} else if ("linesLeading".equals(ignoreSpaces)) { |
144 |
outputDir = new File(dir); |
162 |
filterValue = LINES_LEADING_SPACES; |
145 |
if (!outputDir.exists()) { |
163 |
} else if ("comments".equals(ignoreSpaces)) { |
146 |
compare = false; |
164 |
filterValue = ALL_COMMENTS_SPACES; |
147 |
System.err.println("WARNING: The output directory "+dir+" does not exist..."); |
165 |
} else if ("commentsLinesLeading".equals(ignoreSpaces)) { |
148 |
System.err.println("=> NO comparison will be done! The formatted files will be written there instead."); |
166 |
filterValue = ALL_COMMENTS_LINES_LEADING_SPACES; |
149 |
try { |
167 |
} else { |
150 |
Thread.sleep(1000); |
168 |
filterValue = 0; // no filter |
151 |
} catch (InterruptedException e) { |
|
|
152 |
// skip |
153 |
} |
154 |
} |
169 |
} |
155 |
} |
170 |
IGNORE_SPACES = filterValue; |
156 |
OUTPUT_DIR = outputDir; |
|
|
157 |
COMPARE = compare; |
171 |
} |
158 |
} |
172 |
private final static int FORMAT_REPEAT = Integer.parseInt(System.getProperty("repeat", "2")); //$NON-NLS-1$ |
159 |
private final static int FORMAT_REPEAT = Integer.parseInt(System.getProperty("repeat", "2")); //$NON-NLS-1$ |
|
|
160 |
|
161 |
// Failures management |
162 |
final static int NO_OUTPUT_FAILURE = 0; |
163 |
final static int COMPARISON_FAILURE = 1; |
164 |
final static int REFORMATTING_FAILURE = 2; |
165 |
final static int REFORMATTING_LEADING_FAILURE = 4; |
166 |
final static int REFORMATTING_WHITESPACES_FAILURE = 5; |
167 |
final static int REFORMATTING_EXPECTED_FAILURE = 3; |
168 |
class FormattingFailure { |
169 |
String action; |
170 |
List failures = new ArrayList(); |
171 |
public FormattingFailure() { |
172 |
} |
173 |
public FormattingFailure(String action) { |
174 |
this.action = action; |
175 |
} |
176 |
public String toString() { |
177 |
if (action == null) { |
178 |
return "no output while formatting"; |
179 |
} |
180 |
return "different output while "+action; |
181 |
} |
182 |
|
183 |
} |
184 |
final static FormattingFailure[] FAILURES = new FormattingFailure[6]; |
185 |
{ |
186 |
FAILURES[NO_OUTPUT_FAILURE] = new FormattingFailure(); |
187 |
FAILURES[COMPARISON_FAILURE] = new FormattingFailure("comparing with previous version"); |
188 |
FAILURES[REFORMATTING_FAILURE] = new FormattingFailure("reformatting twice"); |
189 |
FAILURES[REFORMATTING_LEADING_FAILURE] = new FormattingFailure("reformatting twice but only by leading whitespaces"); |
190 |
FAILURES[REFORMATTING_WHITESPACES_FAILURE] = new FormattingFailure("reformatting twice but only by whitespaces"); |
191 |
FAILURES[REFORMATTING_EXPECTED_FAILURE] = new FormattingFailure("reformatting twice but was expected"); |
192 |
} |
173 |
private static final int MAX_FAILURES = Integer.parseInt(System.getProperty("maxFailures", "100")); // Max failures using string comparison |
193 |
private static final int MAX_FAILURES = Integer.parseInt(System.getProperty("maxFailures", "100")); // Max failures using string comparison |
174 |
private static boolean ASSERT_EQUALS_STRINGS = MAX_FAILURES > 0; |
194 |
private static boolean ASSERT_EQUALS_STRINGS = MAX_FAILURES > 0; |
175 |
private final static IPath[] EXPECTED_FAILURES = DIR.indexOf("v34") < 0 |
195 |
private final static IPath[] EXPECTED_FAILURES = INPUT_DIR.getPath().indexOf("v34") < 0 |
176 |
? new IPath[] { |
196 |
? new IPath[] { |
177 |
new Path("org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java"), |
197 |
new Path("org/eclipse/jdt/internal/compiler/ast/QualifiedNameReference.java"), |
178 |
new Path("org/eclipse/jdt/internal/eval/CodeSnippetSingleNameReference.java"), |
198 |
new Path("org/eclipse/jdt/internal/eval/CodeSnippetSingleNameReference.java"), |
Link Here
|
184 |
new Path("org/eclipse/team/internal/ccvs/ui/wizards/UpdateWizard.java"), |
204 |
new Path("org/eclipse/team/internal/ccvs/ui/wizards/UpdateWizard.java"), |
185 |
} |
205 |
} |
186 |
: new IPath[] { |
206 |
: new IPath[] { |
|
|
207 |
// Eclipse |
187 |
new Path("org/eclipse/equinox/internal/p2/director/NewDependencyExpander.java"), |
208 |
new Path("org/eclipse/equinox/internal/p2/director/NewDependencyExpander.java"), |
188 |
new Path("org/eclipse/jdt/core/JavaCore.java"), |
209 |
new Path("org/eclipse/jdt/core/JavaCore.java"), |
189 |
new Path("org/eclipse/jdt/internal/codeassist/CompletionEngine.java"), |
210 |
new Path("org/eclipse/jdt/internal/codeassist/CompletionEngine.java"), |
Link Here
|
203 |
new Path("org/eclipse/jdt/internal/core/search/JavaSearchScope.java"), |
224 |
new Path("org/eclipse/jdt/internal/core/search/JavaSearchScope.java"), |
204 |
new Path("org/eclipse/jdt/internal/eval/EvaluationContext.java"), |
225 |
new Path("org/eclipse/jdt/internal/eval/EvaluationContext.java"), |
205 |
new Path("org/eclipse/jdt/internal/ui/text/javadoc/JavadocContentAccess2.java"), |
226 |
new Path("org/eclipse/jdt/internal/ui/text/javadoc/JavadocContentAccess2.java"), |
|
|
227 |
new Path("org/eclipse/jdt/internal/apt/pluggable/core/filer/IdeJavaSourceOutputStream.java"), |
206 |
new Path("org/eclipse/team/internal/ccvs/ui/mappings/WorkspaceSubscriberContext.java"), |
228 |
new Path("org/eclipse/team/internal/ccvs/ui/mappings/WorkspaceSubscriberContext.java"), |
|
|
229 |
// Ganymede |
230 |
new Path("com/ibm/icu/text/Collator.java"), |
231 |
new Path("org/apache/lucene/analysis/ISOLatin1AccentFilter.java"), |
207 |
}; |
232 |
}; |
208 |
static { |
|
|
209 |
// Sort expected failures to allow binary search |
210 |
Arrays.sort(EXPECTED_FAILURES); |
211 |
} |
212 |
|
233 |
|
213 |
public static Test suite() { |
234 |
public static Test suite() { |
214 |
TestSuite suite = new Suite(FormatterCommentsMassiveTests.class.getName()); |
235 |
TestSuite suite = new Suite(FormatterCommentsMassiveTests.class.getName()); |
215 |
try { |
236 |
try { |
216 |
File testDir = ModelTestsUtil.getWorkspaceRoot().getLocation().toFile(); |
|
|
217 |
if (DIR != null) { |
218 |
File dir = new File(DIR); |
219 |
if (dir.exists()) testDir = dir; |
220 |
} |
221 |
FileFilter filter = new FileFilter() { |
237 |
FileFilter filter = new FileFilter() { |
222 |
public boolean accept(File pathname) { |
238 |
public boolean accept(File pathname) { |
223 |
return pathname.isDirectory() || pathname.getPath().endsWith(".java"); |
239 |
return pathname.isDirectory() || pathname.getPath().endsWith(".java"); |
Link Here
|
227 |
SimpleDateFormat format = new SimpleDateFormat(); |
243 |
SimpleDateFormat format = new SimpleDateFormat(); |
228 |
Date now = new Date(start); |
244 |
Date now = new Date(start); |
229 |
System.out.println("Date of test: "+format.format(now)); |
245 |
System.out.println("Date of test: "+format.format(now)); |
230 |
System.out.print("Get all Java files located in "+testDir+"..."); |
246 |
System.out.print("Get all Java files located in "+INPUT_DIR+"..."); |
231 |
File[] allFiles = ModelTestsUtil.getAllFiles(testDir, filter); |
247 |
File[] allFiles = ModelTestsUtil.getAllFiles(INPUT_DIR, filter); |
232 |
int length = allFiles.length; |
248 |
int length = allFiles.length; |
233 |
System.out.println(length+" found in " + (System.currentTimeMillis() - start) + "ms"); |
249 |
System.out.println(length+" found in " + (System.currentTimeMillis() - start) + "ms"); |
234 |
for (int i=0; i<length; i++) { |
250 |
for (int i=0; i<length; i++) { |
235 |
suite.addTest(new FormatterCommentsMassiveTests(allFiles[i])); |
251 |
suite.addTest(new FormatterCommentsMassiveTests(allFiles[i])); |
236 |
} |
252 |
} |
237 |
// ASSERT_EQUALS_STRINGS = length < 15000; |
|
|
238 |
} catch (Exception e) { |
253 |
} catch (Exception e) { |
239 |
// skip |
254 |
// skip |
240 |
} |
255 |
} |
Link Here
|
244 |
public FormatterCommentsMassiveTests(File file) { |
259 |
public FormatterCommentsMassiveTests(File file) { |
245 |
super("testCompare"); |
260 |
super("testCompare"); |
246 |
this.file = file; |
261 |
this.file = file; |
247 |
this.path = new Path(file.getPath().substring(DIR.length()+1)); |
262 |
this.path = new Path(file.getPath().substring(INPUT_DIR.getPath().length()+1)); |
248 |
} |
263 |
} |
249 |
|
264 |
|
250 |
/* (non-Javadoc) |
265 |
/* (non-Javadoc) |
Link Here
|
260 |
public void setUp() throws Exception { |
275 |
public void setUp() throws Exception { |
261 |
super.setUp(); |
276 |
super.setUp(); |
262 |
this.hasSpaceFailure = false; |
277 |
this.hasSpaceFailure = false; |
|
|
278 |
this.preferences = DefaultCodeFormatterOptions.getEclipseDefaultSettings(); |
263 |
} |
279 |
} |
264 |
|
280 |
|
265 |
/* (non-Javadoc) |
281 |
/* (non-Javadoc) |
Link Here
|
280 |
* @see org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#tearDownSuite() |
296 |
* @see org.eclipse.jdt.core.tests.formatter.FormatterRegressionTests#tearDownSuite() |
281 |
*/ |
297 |
*/ |
282 |
public void tearDownSuite() throws Exception { |
298 |
public void tearDownSuite() throws Exception { |
283 |
// skip standard model suite tear down |
299 |
if (OUTPUT_DIR != null) { |
284 |
int sFailures = this.failures.size(); |
300 |
if (COMPARE) { |
285 |
int seFailures = this.expectedFailures.size(); |
301 |
System.out.println("Comparison done with output files located in "+OUTPUT_DIR); |
286 |
int swFailures = this.whitespacesFailures.size(); |
|
|
287 |
int slwFailures = this.leadingWhitespacesFailures.size(); |
288 |
String failuresType = COMPARE != null ? "than old formatter" : "when reformatting"; |
289 |
System.out.println(); |
290 |
if (sFailures > 0) { |
291 |
System.out.println(sFailures+" files has still different output while reformatting!"); |
292 |
} |
293 |
if (seFailures > 0) { |
294 |
System.out.println(seFailures+" files has still different output while reformatting due to old formatter bugs!"); |
295 |
} |
296 |
if (slwFailures == 0) { |
297 |
System.out.println("No file has different line leading spaces "+failuresType+" :-)"); |
298 |
} else { |
299 |
System.out.println(slwFailures+" files have different line leading spaces "+failuresType+"!"); |
300 |
} |
301 |
if (swFailures > 0) { |
302 |
System.out.println(swFailures+" files have different spaces "+failuresType+"!"); |
303 |
} |
304 |
if (this.changedHeaderFooter >0) { |
305 |
System.out.println(this.changedHeaderFooter+" differences in header/footer have been found"); |
306 |
} |
307 |
if (this.changedPreTags >0) { |
308 |
System.out.println(this.changedPreTags+" differences in <pre> tags (blank lines) have been found"); |
309 |
} |
310 |
System.out.println(); |
311 |
if (sFailures > 0) { |
312 |
System.out.println("List of files with different output "+failuresType+":"); |
313 |
for (int i=0; i<sFailures; i++) { |
314 |
System.out.println(" - "+this.failures.get(i)); |
315 |
} |
302 |
} |
316 |
} |
303 |
} |
317 |
if (seFailures > 0) { |
304 |
// skip standard model suite tear down |
318 |
System.out.println("List of files with different output "+failuresType+" (due to old formatter bugs):"); |
305 |
System.out.println(); |
319 |
for (int i=0; i<seFailures; i++) { |
306 |
int max = FAILURES.length; |
320 |
System.out.println(" - "+this.expectedFailures.get(i)); |
307 |
for (int i=0; i<max; i++) { |
321 |
} |
308 |
List failures = FAILURES[i].failures; |
322 |
} |
309 |
int size = failures.size(); |
323 |
if (slwFailures > 0) { |
310 |
if (size > 0) { |
324 |
System.out.println("List of files with different line leading spaces "+failuresType+":"); |
311 |
System.out.print(size); |
325 |
for (int i=0; i<slwFailures; i++) { |
312 |
System.out.print(" file"); |
326 |
System.out.println(" - "+this.leadingWhitespacesFailures.get(i)); |
313 |
if (size == 1) { |
|
|
314 |
System.out.print(" has "); |
315 |
} else { |
316 |
System.out.print("s have "); |
317 |
} |
318 |
System.out.print(FAILURES[i]); |
319 |
System.out.println('!'); |
327 |
} |
320 |
} |
328 |
} |
321 |
} |
329 |
if (swFailures > 0) { |
322 |
System.out.println(); |
330 |
System.out.println("List of files with different spaces "+failuresType+":"); |
323 |
for (int i=0; i<max; i++) { |
331 |
for (int i=0; i<swFailures; i++) { |
324 |
List failures = FAILURES[i].failures; |
332 |
System.out.println(" - "+this.whitespacesFailures.get(i)); |
325 |
int size = failures.size(); |
|
|
326 |
if (size > 0) { |
327 |
System.out.println("List of file(s) with "+FAILURES[i]+":"); |
328 |
for (int j=0; j<size; j++) { |
329 |
System.out.println(" - "+failures.get(j)); |
330 |
} |
333 |
} |
331 |
} |
334 |
} |
332 |
} |
335 |
} |
333 |
} |
Link Here
|
340 |
* The line separators in 'actual' are converted to '\n' before the comparison. |
338 |
* The line separators in 'actual' are converted to '\n' before the comparison. |
341 |
*/ |
339 |
*/ |
342 |
protected void assertSourceEquals(String message, String expected, String actual) { |
340 |
protected void assertSourceEquals(String message, String expected, String actual) { |
|
|
341 |
if (expected == null) { |
342 |
assertNull(message, actual); |
343 |
return; |
344 |
} |
343 |
if (actual == null) { |
345 |
if (actual == null) { |
344 |
assertEquals(message, expected, null); |
346 |
assertEquals(message, expected, null); |
345 |
return; |
347 |
return; |
346 |
} |
348 |
} |
|
|
349 |
expected = Util.convertToIndependantLineDelimiter(expected); |
347 |
actual = Util.convertToIndependantLineDelimiter(actual); |
350 |
actual = Util.convertToIndependantLineDelimiter(actual); |
348 |
try { |
351 |
if (ASSERT_EQUALS_STRINGS) { |
349 |
if (ASSERT_EQUALS_STRINGS) { |
352 |
assertEquals(message, expected, actual); |
350 |
assertEquals(message, expected, actual); |
353 |
} else { |
351 |
} else { |
354 |
assertTrue(message, actual.equals(expected)); |
352 |
assertTrue(message, actual.equals(expected)); |
|
|
353 |
} |
354 |
} |
355 |
catch (ComparisonFailure cf) { |
356 |
if ("true".equals(COMPARE)) { |
357 |
String trimmedExpected = expected; |
358 |
String trimmedActual = actual; |
359 |
switch (IGNORE_SPACES) { |
360 |
case ALL_SPACES: |
361 |
trimmedExpected = ModelTestsUtil.removeWhiteSpace(expected); |
362 |
trimmedActual= ModelTestsUtil.removeWhiteSpace(actual); |
363 |
if (trimmedExpected.equals(trimmedActual)) { |
364 |
this.whitespacesFailures.add(this.path); |
365 |
return; |
366 |
} |
367 |
break; |
368 |
case LINES_LEADING_SPACES: |
369 |
trimmedExpected = ModelTestsUtil.trimLinesLeadingWhitespaces(expected); |
370 |
trimmedActual= ModelTestsUtil.trimLinesLeadingWhitespaces(actual); |
371 |
if (trimmedExpected.equals(trimmedActual)) { |
372 |
this.leadingWhitespacesFailures.add(this.path); |
373 |
return; |
374 |
} |
375 |
trimmedExpected = ModelTestsUtil.removeWhiteSpace(expected); |
376 |
trimmedActual= ModelTestsUtil.removeWhiteSpace(actual); |
377 |
if (trimmedExpected.equals(trimmedActual)) { |
378 |
this.whitespacesFailures.add(this.path); |
379 |
return; |
380 |
} |
381 |
break; |
382 |
} |
383 |
if (DEBUG_TESTS && ASSERT_EQUALS_STRINGS) { |
384 |
assertEquals(message, trimmedExpected, trimmedActual); |
385 |
} |
386 |
} |
387 |
this.failures.add(this.path); |
388 |
ASSERT_EQUALS_STRINGS = this.failures.size() < MAX_FAILURES; |
389 |
throw cf; |
390 |
} |
391 |
catch (AssertionFailedError afe) { |
392 |
this.failures.add(this.path); |
393 |
throw afe; |
394 |
} |
395 |
} |
396 |
|
397 |
private String cleanAllKnownDifferences(String comment) { |
398 |
int kind = comment.charAt(1) == '/' ? 1 : comment.charAt(2) == '*' ? 3 : 2; |
399 |
String cleanedComment = comment; |
400 |
switch (kind) { |
401 |
case 1: // line comment |
402 |
cleanedComment = cleanBlankLinesAfterLineComment(comment); |
403 |
break; |
404 |
case 3: // javadoc comment |
405 |
cleanedComment = cleanHeaderAndFooter(comment); |
406 |
String newComment = cleanPreTags(cleanedComment); |
407 |
if (cleanedComment == newComment) { |
408 |
cleanedComment = cleanCodeTags(cleanedComment); |
409 |
} else { |
410 |
cleanedComment = newComment; |
411 |
} |
412 |
break; |
413 |
} |
414 |
return cleanedComment; |
415 |
} |
416 |
private String cleanHeaderAndFooter(String comment) { |
417 |
int start = 1; // skip starting '/' |
418 |
int length = comment.length(); |
419 |
int end = length - 1; // skip ending '/' |
420 |
while (comment.charAt(start) == '*') { |
421 |
// remove all contiguous '*' in header |
422 |
start++; |
423 |
} |
424 |
while (comment.charAt(--end) == '*') { |
425 |
// remove all contiguous '*' in header |
426 |
} |
427 |
if (start > 3 || end < (length - 2)) { |
428 |
this.changedHeaderFooter++; |
429 |
return comment.substring(start, end); |
430 |
} |
431 |
return comment; |
432 |
} |
433 |
|
434 |
private String cleanBlankLinesAfterLineComment(String comment) { |
435 |
int length = comment.length(); |
436 |
if (comment.charAt(length-1) == '\n') { |
437 |
length--; |
438 |
if (comment.charAt(length-1) == '\r') { |
439 |
length--; |
440 |
} |
441 |
return comment.substring(0, length); |
442 |
} |
443 |
return comment; |
444 |
} |
445 |
|
446 |
private String cleanCodeTags(String comment) { |
447 |
if (comment.indexOf("<code>") < 0) return comment; |
448 |
StringTokenizer tokenizer = new StringTokenizer(comment, "\r\n\f"); |
449 |
StringBuffer buffer = new StringBuffer(); |
450 |
while (tokenizer.hasMoreTokens()) { |
451 |
String line = tokenizer.nextToken(); |
452 |
if (line.indexOf("<pre>") >= 0) { |
453 |
while (line.indexOf("</pre>") < 0) { |
454 |
line = tokenizer.nextToken(); |
455 |
} |
456 |
} else { |
457 |
buffer.append(line); |
458 |
buffer.append("\n"); |
459 |
} |
460 |
} |
461 |
this.changedCodeTags++; |
462 |
return buffer.toString(); |
463 |
} |
464 |
|
465 |
private String cleanPreTags(String comment) { |
466 |
if (comment.indexOf("<pre>") < 0) return comment; |
467 |
StringTokenizer tokenizer = new StringTokenizer(comment, "\r\n\f"); |
468 |
StringBuffer buffer = new StringBuffer(); |
469 |
StringBuffer emptyLines = new StringBuffer(); |
470 |
String previousLine = null; |
471 |
while (tokenizer.hasMoreTokens()) { |
472 |
String line = tokenizer.nextToken(); |
473 |
if (line.trim() == "*") { |
474 |
if (previousLine == null || previousLine.indexOf("<pre>") < 0) { |
475 |
buffer.append(line); |
476 |
buffer.append("\n"); |
477 |
continue; |
478 |
} else { |
479 |
emptyLines.append(line); |
480 |
emptyLines.append("\n"); |
481 |
} |
482 |
} else if (line.indexOf("<code>") >= 0) { |
483 |
while (line.indexOf("</code>") < 0) { |
484 |
line = tokenizer.nextToken(); |
485 |
} |
486 |
} else if (emptyLines.length() != 0) { |
487 |
if (line.indexOf("</pre>") < 0) { |
488 |
buffer.append(emptyLines); |
489 |
emptyLines.setLength(0); |
490 |
} |
491 |
buffer.append(line); |
492 |
buffer.append("\n"); |
493 |
} else { |
494 |
buffer.append(line); |
495 |
buffer.append("\n"); |
496 |
} |
497 |
previousLine = line; |
498 |
} |
355 |
} |
499 |
this.changedPreTags++; |
|
|
500 |
return buffer.toString(); |
501 |
} |
356 |
} |
502 |
|
357 |
|
503 |
DefaultCodeFormatter codeFormatter() { |
358 |
DefaultCodeFormatter codeFormatter() { |
504 |
DefaultCodeFormatterOptions preferences = DefaultCodeFormatterOptions.getEclipseDefaultSettings(); |
359 |
DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(this.preferences, getDefaultCompilerOptions()); |
505 |
DefaultCodeFormatter codeFormatter = new DefaultCodeFormatter(preferences); |
|
|
506 |
return codeFormatter; |
360 |
return codeFormatter; |
507 |
} |
361 |
} |
508 |
|
362 |
|
509 |
void compareFormattedSource() throws IOException, Exception { |
363 |
void compareFormattedSource() throws IOException, Exception { |
510 |
DefaultCodeFormatter codeFormatter = codeFormatter(); |
|
|
511 |
String source = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(this.file, null)); |
364 |
String source = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(this.file, null)); |
512 |
try { |
365 |
try { |
513 |
if ("comments".equals(COMPARE)) { |
366 |
// Format the source |
514 |
String[] oldFormattedComments = formattedComments(source, true); |
367 |
String actualResult = runFormatter(codeFormatter(), source, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, 0, source.length(), null); |
515 |
String[] newFormattedComments = formattedComments(source, false); |
368 |
|
516 |
int length = oldFormattedComments == null ? 0 : oldFormattedComments.length; |
369 |
// Look for output to compare with |
517 |
this.abortOnFailure = false; |
370 |
File outputFile = new Path(OUTPUT_DIR.getPath()).append(this.path).toFile(); |
518 |
assertEquals("Unexpected number of comments!", length, newFormattedComments == null ? 0 : newFormattedComments.length); |
371 |
if (COMPARE) { |
519 |
for (int i=0; i<length; i++) { |
372 |
String expectedResult = new String(org.eclipse.jdt.internal.compiler.util.Util.getFileCharContent(outputFile, null)); |
520 |
String oldComment = oldFormattedComments[i]; |
373 |
try { |
521 |
String newComment = newFormattedComments[i]; |
374 |
assertSourceEquals("Unexpected format output!", expectedResult, actualResult); |
522 |
if (oldComment == null) { |
375 |
} |
523 |
assertNull("Unexpected non-null new comment", newComment); |
376 |
catch (ComparisonFailure cf) { |
524 |
} else { |
377 |
FAILURES[COMPARISON_FAILURE].failures.add(this.path); |
525 |
String expected = cleanAllKnownDifferences(oldComment); |
378 |
throw cf; |
526 |
String actual = cleanAllKnownDifferences(newComment); |
379 |
} |
527 |
if (!expected.equals(actual)) { |
380 |
catch (AssertionFailedError afe) { |
528 |
String actualResult = runFormatter(codeFormatter, source, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, 0, source.length(), null); |
381 |
FAILURES[COMPARISON_FAILURE].failures.add(this.path); |
529 |
String expectedResult = expectedFormattedSource(source); |
382 |
throw afe; |
530 |
assertEquals("Unexpected difference with formatted comment "+(i+1), Util.convertToIndependantLineDelimiter(expectedResult), Util.convertToIndependantLineDelimiter(actualResult)); |
|
|
531 |
} |
532 |
} |
533 |
} |
383 |
} |
534 |
} else { |
384 |
} else { |
535 |
String actualResult = runFormatter(codeFormatter, source, CodeFormatter.K_COMPILATION_UNIT | CodeFormatter.F_INCLUDE_COMMENTS, 0, 0, source.length(), null); |
385 |
outputFile.getParentFile().mkdirs(); |
536 |
if (!this.hasSpaceFailure && "true".equals(COMPARE)) { |
386 |
Util.writeToFile(actualResult, outputFile.getAbsolutePath()); |
537 |
String expectedResult = expectedFormattedSource(source); |
|
|
538 |
assertLineEquals(actualResult, source, expectedResult, false); |
539 |
} |
540 |
} |
387 |
} |
541 |
} |
388 |
} |
542 |
catch (Exception e) { |
389 |
catch (Exception e) { |
Link Here
|
566 |
return buffer.toString(); |
413 |
return buffer.toString(); |
567 |
} |
414 |
} |
568 |
|
415 |
|
569 |
private String expectedFormattedSource(String source) { |
|
|
570 |
boolean enableNewCommentFormatter = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT; |
571 |
try { |
572 |
DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT = false; |
573 |
DefaultCodeFormatter codeFormatter = codeFormatter(); |
574 |
Scanner scanner = new Scanner(true, true, false/*nls*/, ClassFileConstants.JDK1_4/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
575 |
CodeSnippetParsingUtil codeSnippetParsingUtil = new CodeSnippetParsingUtil(); |
576 |
CompilationUnitDeclaration compilationUnitDeclaration = codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true); |
577 |
final TypeDeclaration[] types = compilationUnitDeclaration.types; |
578 |
int headerEndPosition = types == null ? compilationUnitDeclaration.sourceEnd : types[0].declarationSourceStart; |
579 |
scanner.setSource(source.toCharArray()); |
580 |
scanner.lineEnds = codeSnippetParsingUtil.recordedParsingInformation.lineEnds; |
581 |
int[][] commentsPositions = compilationUnitDeclaration.comments; |
582 |
int length = commentsPositions == null ? 0 : commentsPositions.length; |
583 |
String[] formattedComments = new String[length]; |
584 |
for (int i=0; i<length; i++) { |
585 |
int[] positions = commentsPositions[i]; |
586 |
int commentKind = CodeFormatter.K_JAVA_DOC; |
587 |
int commentStart = positions [0]; |
588 |
int commentEnd = positions [1]; |
589 |
if (commentEnd < 0) { // line or block comments have negative end position |
590 |
commentEnd = -commentEnd; |
591 |
if (commentStart > 0) { // block comments have positive start position |
592 |
commentKind = CodeFormatter.K_MULTI_LINE_COMMENT; |
593 |
} else { |
594 |
commentStart = -commentStart; |
595 |
commentKind = CodeFormatter.K_SINGLE_LINE_COMMENT; |
596 |
} |
597 |
} |
598 |
if (commentStart >= headerEndPosition) { |
599 |
int indentationLevel = getIndentationLevel(scanner, commentStart); |
600 |
formattedComments[i] = runFormatter(codeFormatter, source.substring(commentStart, commentEnd), commentKind, indentationLevel, 0, commentEnd - commentStart, LINE_SEPARATOR); |
601 |
} |
602 |
} |
603 |
SimpleDocument document = new SimpleDocument(source); |
604 |
for (int i=length-1; i>=0; i--) { |
605 |
if (formattedComments[i] != null) { |
606 |
int[] positions = commentsPositions[i]; |
607 |
int commentStart = positions [0]; |
608 |
int commentEnd = positions [1]; |
609 |
if (commentEnd < 0) { // line or block comments have negative end position |
610 |
commentEnd = -commentEnd; |
611 |
if (commentStart < 0) { // line comments have negative start position |
612 |
commentStart = -commentStart; |
613 |
} |
614 |
} |
615 |
document.replace(commentStart, commentEnd - commentStart, formattedComments[i]); |
616 |
} |
617 |
} |
618 |
String newSource = document.get(); |
619 |
String oldResult = runFormatter(codeFormatter, newSource, CodeFormatter.K_COMPILATION_UNIT, 0, 0, newSource.length(), null); |
620 |
return oldResult == null ? newSource : oldResult; |
621 |
} |
622 |
finally { |
623 |
DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT = enableNewCommentFormatter; |
624 |
} |
625 |
} |
626 |
private String[] formattedComments(String source, boolean old) { |
627 |
boolean enableNewCommentFormatter = DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT; |
628 |
try { |
629 |
DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT = !old; |
630 |
DefaultCodeFormatter codeFormatter = codeFormatter(); |
631 |
Scanner scanner = new Scanner(true, true, false/*nls*/, ClassFileConstants.JDK1_4/*sourceLevel*/, null/*taskTags*/, null/*taskPriorities*/, true/*taskCaseSensitive*/); |
632 |
CodeSnippetParsingUtil codeSnippetParsingUtil = new CodeSnippetParsingUtil(); |
633 |
CompilationUnitDeclaration compilationUnitDeclaration = codeSnippetParsingUtil.parseCompilationUnit(source.toCharArray(), getDefaultCompilerOptions(), true); |
634 |
final TypeDeclaration[] types = compilationUnitDeclaration.types; |
635 |
int headerEndPosition = types == null ? compilationUnitDeclaration.sourceEnd : types[0].declarationSourceStart; |
636 |
scanner.setSource(source.toCharArray()); |
637 |
scanner.lineEnds = codeSnippetParsingUtil.recordedParsingInformation.lineEnds; |
638 |
int[][] commentsPositions = compilationUnitDeclaration.comments; |
639 |
int length = commentsPositions == null ? 0 : commentsPositions.length; |
640 |
String[] formattedComments = new String[length]; |
641 |
for (int i=0; i<length; i++) { |
642 |
int[] positions = commentsPositions[i]; |
643 |
int commentKind = CodeFormatter.K_JAVA_DOC; |
644 |
int commentStart = positions [0]; |
645 |
int commentEnd = positions [1]; |
646 |
if (commentEnd < 0) { // line or block comments have negative end position |
647 |
commentEnd = -commentEnd; |
648 |
if (commentStart > 0) { // block comments have positive start position |
649 |
commentKind = CodeFormatter.K_MULTI_LINE_COMMENT; |
650 |
} else { |
651 |
commentStart = -commentStart; |
652 |
commentKind = CodeFormatter.K_SINGLE_LINE_COMMENT; |
653 |
} |
654 |
} |
655 |
if (commentStart >= headerEndPosition) { |
656 |
int indentationLevel = getIndentationLevel(scanner, commentStart); |
657 |
formattedComments[i] = runFormatter(codeFormatter, source.substring(commentStart, commentEnd), commentKind, indentationLevel, 0, commentEnd - commentStart, LINE_SEPARATOR); |
658 |
} |
659 |
} |
660 |
return formattedComments; |
661 |
} |
662 |
finally { |
663 |
DefaultCodeFormatter.ENABLE_NEW_COMMENTS_FORMAT = enableNewCommentFormatter; |
664 |
} |
665 |
} |
666 |
|
667 |
private int getIndentationLevel(Scanner scanner, int position) { |
668 |
int indentationLevel = 0; |
669 |
int numberOfIndentations = 0; |
670 |
int indentationSize; |
671 |
try { |
672 |
indentationSize = Integer.parseInt(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE); |
673 |
} catch (NumberFormatException nfe) { |
674 |
indentationSize = 4; |
675 |
} |
676 |
int lineNumber = scanner.getLineNumber(position); |
677 |
int lineStart = scanner.getLineStart(lineNumber); |
678 |
scanner.resetTo(lineStart, position-1); |
679 |
while (!scanner.atEnd()) { |
680 |
int ch = scanner.getNextChar(); |
681 |
switch (ch) { |
682 |
case '\n': |
683 |
indentationLevel = 0; |
684 |
numberOfIndentations = 0; |
685 |
break; |
686 |
case '\t': |
687 |
numberOfIndentations++; |
688 |
indentationLevel = numberOfIndentations * indentationSize; |
689 |
break; |
690 |
default: |
691 |
indentationLevel++; |
692 |
if ((indentationLevel%indentationSize) == 0) { |
693 |
numberOfIndentations++; |
694 |
} |
695 |
break; |
696 |
} |
697 |
} |
698 |
if ((indentationLevel%indentationSize) != 0) { |
699 |
numberOfIndentations++; |
700 |
indentationLevel = numberOfIndentations * indentationSize; |
701 |
} |
702 |
return numberOfIndentations; |
703 |
} |
704 |
|
705 |
private Map getDefaultCompilerOptions() { |
416 |
private Map getDefaultCompilerOptions() { |
706 |
Map optionsMap = new HashMap(30); |
417 |
Map optionsMap = new HashMap(30); |
707 |
optionsMap.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE); |
418 |
optionsMap.put(CompilerOptions.OPTION_LocalVariableAttribute, CompilerOptions.DO_NOT_GENERATE); |
Link Here
|
751 |
optionsMap.put(CompilerOptions.OPTION_ReportUnusedDeclaredThrownException, CompilerOptions.IGNORE); |
462 |
optionsMap.put(CompilerOptions.OPTION_ReportUnusedDeclaredThrownException, CompilerOptions.IGNORE); |
752 |
optionsMap.put(CompilerOptions.OPTION_ReportUnusedDeclaredThrownExceptionWhenOverriding, CompilerOptions.DISABLED); |
463 |
optionsMap.put(CompilerOptions.OPTION_ReportUnusedDeclaredThrownExceptionWhenOverriding, CompilerOptions.DISABLED); |
753 |
optionsMap.put(CompilerOptions.OPTION_ReportUnqualifiedFieldAccess, CompilerOptions.IGNORE); |
464 |
optionsMap.put(CompilerOptions.OPTION_ReportUnqualifiedFieldAccess, CompilerOptions.IGNORE); |
754 |
optionsMap.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_4); |
465 |
optionsMap.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6); |
755 |
optionsMap.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_2); |
466 |
optionsMap.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6); |
756 |
optionsMap.put(CompilerOptions.OPTION_TaskTags, ""); //$NON-NLS-1$ |
467 |
optionsMap.put(CompilerOptions.OPTION_TaskTags, ""); //$NON-NLS-1$ |
757 |
optionsMap.put(CompilerOptions.OPTION_TaskPriorities, ""); //$NON-NLS-1$ |
468 |
optionsMap.put(CompilerOptions.OPTION_TaskPriorities, ""); //$NON-NLS-1$ |
758 |
optionsMap.put(CompilerOptions.OPTION_TaskCaseSensitive, CompilerOptions.DISABLED); |
469 |
optionsMap.put(CompilerOptions.OPTION_TaskCaseSensitive, CompilerOptions.DISABLED); |
Link Here
|
761 |
optionsMap.put(CompilerOptions.OPTION_ReportSpecialParameterHidingField, CompilerOptions.DISABLED); |
472 |
optionsMap.put(CompilerOptions.OPTION_ReportSpecialParameterHidingField, CompilerOptions.DISABLED); |
762 |
optionsMap.put(CompilerOptions.OPTION_MaxProblemPerUnit, String.valueOf(100)); |
473 |
optionsMap.put(CompilerOptions.OPTION_MaxProblemPerUnit, String.valueOf(100)); |
763 |
optionsMap.put(CompilerOptions.OPTION_InlineJsr, CompilerOptions.DISABLED); |
474 |
optionsMap.put(CompilerOptions.OPTION_InlineJsr, CompilerOptions.DISABLED); |
764 |
optionsMap.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5); |
475 |
optionsMap.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6); |
765 |
return optionsMap; |
476 |
return optionsMap; |
766 |
} |
477 |
} |
767 |
|
478 |
|
Link Here
|
769 |
int length = EXPECTED_FAILURES.length; |
480 |
int length = EXPECTED_FAILURES.length; |
770 |
for (int i=0; i<length; i++) { |
481 |
for (int i=0; i<length; i++) { |
771 |
IPath expectedFailure= EXPECTED_FAILURES[i]; |
482 |
IPath expectedFailure= EXPECTED_FAILURES[i]; |
772 |
if (this.path.matchingFirstSegments(expectedFailure) == expectedFailure.segmentCount()) { |
483 |
if (this.path.toString().indexOf(expectedFailure.toString()) >= 0) { |
773 |
this.expectedFailures.add(this.path); |
484 |
FAILURES[REFORMATTING_EXPECTED_FAILURE].failures.add(this.path); |
774 |
return true; |
485 |
return true; |
775 |
} |
486 |
} |
776 |
} |
487 |
} |
777 |
return false; |
488 |
return false; |
778 |
} |
489 |
} |
779 |
|
490 |
|
|
|
491 |
/* |
492 |
private boolean runFormatterWithoutComments(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator) { |
493 |
DefaultCodeFormatterOptions preferencesWithoutComment = DefaultCodeFormatterOptions.getEclipseDefaultSettings(); |
494 |
preferencesWithoutComment.comment_format_line_comment = false; |
495 |
preferencesWithoutComment.comment_format_block_comment = false; |
496 |
preferencesWithoutComment.comment_format_javadoc_comment = false; |
497 |
DefaultCodeFormatter codeFormatterWithoutComment = new DefaultCodeFormatter(preferencesWithoutComment); |
498 |
|
499 |
TextEdit edit = codeFormatterWithoutComment.format(kind, source, offset, length, indentationLevel, lineSeparator);//$NON-NLS-1$ |
500 |
if (edit == null) return false; |
501 |
String initialResult = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit); |
502 |
|
503 |
int count = 1; |
504 |
String result = initialResult; |
505 |
String previousResult = result; |
506 |
while (count++ < FORMAT_REPEAT) { |
507 |
edit = codeFormatterWithoutComment.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);//$NON-NLS-1$ |
508 |
if (edit == null) return false; |
509 |
previousResult = result; |
510 |
result = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit); |
511 |
} |
512 |
return previousResult.equals(result); |
513 |
} |
514 |
*/ |
515 |
|
780 |
String runFormatter(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator) { |
516 |
String runFormatter(CodeFormatter codeFormatter, String source, int kind, int indentationLevel, int offset, int length, String lineSeparator) { |
781 |
TextEdit edit = codeFormatter.format(kind, source, offset, length, indentationLevel, lineSeparator);//$NON-NLS-1$ |
517 |
TextEdit edit = codeFormatter.format(kind, source, offset, length, indentationLevel, lineSeparator);//$NON-NLS-1$ |
782 |
if (edit == null) return null; |
518 |
try { |
783 |
String result = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit); |
519 |
assertNotNull("Formatted source should not be null!", edit); |
|
|
520 |
} |
521 |
catch (ComparisonFailure cf) { |
522 |
FAILURES[NO_OUTPUT_FAILURE].failures.add(this.path); |
523 |
throw cf; |
524 |
} |
525 |
catch (AssertionFailedError afe) { |
526 |
FAILURES[NO_OUTPUT_FAILURE].failures.add(this.path); |
527 |
throw afe; |
528 |
} |
529 |
String initialResult = org.eclipse.jdt.internal.core.util.Util.editedString(source, edit); |
784 |
|
530 |
|
785 |
int count = 1; |
531 |
int count = 1; |
786 |
if (COMPARE == null && length == source.length()) { |
532 |
String result = initialResult; |
787 |
String previousResult = result; |
533 |
String previousResult = result; |
788 |
while (count++ < FORMAT_REPEAT) { |
534 |
while (count++ < FORMAT_REPEAT) { |
789 |
edit = codeFormatter.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);//$NON-NLS-1$ |
535 |
edit = codeFormatter.format(kind, result, 0, result.length(), indentationLevel, lineSeparator);//$NON-NLS-1$ |
790 |
if (edit == null) return null; |
536 |
if (edit == null) return null; |
791 |
previousResult = result; |
537 |
previousResult = result; |
792 |
result = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit); |
538 |
result = org.eclipse.jdt.internal.core.util.Util.editedString(result, edit); |
793 |
} |
539 |
} |
794 |
if (!previousResult.equals(result)) { |
540 |
if (!previousResult.equals(result)) { |
795 |
switch (IGNORE_SPACES) { |
541 |
|
796 |
case ALL_SPACES: |
542 |
// Try to compare without leading spaces |
797 |
String trimmedExpected = ModelTestsUtil.removeWhiteSpace(previousResult); |
543 |
String trimmedExpected = ModelTestsUtil.trimLinesLeadingWhitespaces(previousResult); |
798 |
String trimmedActual= ModelTestsUtil.removeWhiteSpace(result); |
544 |
String trimmedActual= ModelTestsUtil.trimLinesLeadingWhitespaces(result); |
799 |
if (trimmedExpected.equals(trimmedActual)) { |
545 |
if (trimmedExpected.equals(trimmedActual)) { |
800 |
this.whitespacesFailures.add(this.path); |
546 |
FAILURES[REFORMATTING_LEADING_FAILURE].failures.add(this.path); |
801 |
this.hasSpaceFailure = true; |
547 |
this.hasSpaceFailure = true; |
802 |
return previousResult; |
548 |
return initialResult; |
803 |
} |
549 |
} |
804 |
break; |
550 |
|
805 |
case LINES_LEADING_SPACES: |
551 |
// Try to compare without spaces at all |
806 |
trimmedExpected = ModelTestsUtil.trimLinesLeadingWhitespaces(previousResult); |
552 |
if (ModelTestsUtil.removeWhiteSpace(previousResult).equals(ModelTestsUtil.removeWhiteSpace(result))) { |
807 |
trimmedActual= ModelTestsUtil.trimLinesLeadingWhitespaces(result); |
553 |
FAILURES[REFORMATTING_WHITESPACES_FAILURE].failures.add(this.path); |
808 |
if (trimmedExpected.equals(trimmedActual)) { |
554 |
this.hasSpaceFailure = true; |
809 |
this.leadingWhitespacesFailures.add(this.path); |
555 |
return initialResult; |
810 |
this.hasSpaceFailure = true; |
556 |
} |
811 |
return previousResult; |
557 |
|
812 |
} |
558 |
/* |
813 |
if (ModelTestsUtil.removeWhiteSpace(previousResult).equals(ModelTestsUtil.removeWhiteSpace(result))) { |
559 |
// Try to see if the formatting also fails without comments |
814 |
this.whitespacesFailures.add(this.path); |
560 |
if (!runFormatterWithoutComments(null, source, kind, indentationLevel, offset, length, lineSeparator)) { |
815 |
this.hasSpaceFailure = true; |
561 |
return initialResult; |
816 |
return previousResult; |
562 |
} |
817 |
} |
563 |
|
818 |
break; |
564 |
// format without comments is OK => there's a problem with comment formatting |
819 |
} |
565 |
String counterString = counterToString(count-1); |
820 |
if (!isExpectedFailure()) { |
566 |
assertSourceEquals(counterString+" formatting is different from first one!", previousResult, result); |
821 |
String counterString = counterToString(count-1); |
567 |
*/ |
822 |
assertSourceEquals(counterString+" formatting is different from first one!", Util.convertToIndependantLineDelimiter(previousResult), Util.convertToIndependantLineDelimiter(result)); |
568 |
if (!isExpectedFailure()) { |
|
|
569 |
String counterString = counterToString(count-1); |
570 |
try { |
571 |
assertSourceEquals(counterString+" formatting is different from first one!", previousResult, result); |
572 |
} |
573 |
catch (ComparisonFailure cf) { |
574 |
FAILURES[REFORMATTING_FAILURE].failures.add(this.path); |
575 |
throw cf; |
576 |
} |
577 |
catch (AssertionFailedError afe) { |
578 |
FAILURES[REFORMATTING_FAILURE].failures.add(this.path); |
579 |
throw afe; |
823 |
} |
580 |
} |
824 |
result = previousResult; |
|
|
825 |
} |
581 |
} |
826 |
} |
582 |
} |
827 |
return result; |
583 |
return initialResult; |
828 |
} |
584 |
} |
829 |
|
585 |
|
830 |
public void testCompare() throws IOException, Exception { |
586 |
public void testCompare() throws IOException, Exception { |