|
18 | 18 |
|
19 | 19 | import org.junit.Test;
|
20 | 20 | import spoon.Launcher;
|
| 21 | +import spoon.SpoonException; |
| 22 | +import spoon.compiler.Environment; |
| 23 | +import spoon.processing.AbstractProcessor; |
21 | 24 | import spoon.processing.Processor;
|
| 25 | +import spoon.processing.ProcessorProperties; |
| 26 | +import spoon.processing.TraversalStrategy; |
| 27 | +import spoon.reflect.CtModel; |
22 | 28 | import spoon.reflect.code.CtStatement;
|
23 | 29 | import spoon.reflect.declaration.CtClass;
|
24 |
| -import spoon.reflect.declaration.CtCompilationUnit; |
25 | 30 | import spoon.reflect.declaration.CtElement;
|
26 | 31 | import spoon.reflect.declaration.CtField;
|
27 | 32 | import spoon.reflect.declaration.CtMethod;
|
| 33 | +import spoon.reflect.declaration.CtModule; |
| 34 | +import spoon.reflect.declaration.CtPackage; |
28 | 35 | import spoon.reflect.declaration.CtType;
|
29 | 36 | import spoon.reflect.declaration.ModifierKind;
|
30 | 37 | import spoon.reflect.factory.Factory;
|
| 38 | +import spoon.reflect.reference.CtPackageReference; |
31 | 39 | import spoon.reflect.reference.CtTypeReference;
|
| 40 | +import spoon.reflect.visitor.CtScanner; |
32 | 41 | import spoon.reflect.visitor.ImportCleaner;
|
33 | 42 | import spoon.reflect.visitor.ImportConflictDetector;
|
| 43 | +import spoon.reflect.visitor.filter.TypeFilter; |
34 | 44 | import spoon.support.modelobs.ChangeCollector;
|
| 45 | +import spoon.support.modelobs.SourceFragmentCreator; |
35 | 46 | import spoon.support.sniper.SniperJavaPrettyPrinter;
|
36 | 47 | import spoon.test.prettyprinter.testclasses.ToBeChanged;
|
37 | 48 |
|
|
40 | 51 | import java.io.IOException;
|
41 | 52 | import java.io.InputStream;
|
42 | 53 | import java.io.UnsupportedEncodingException;
|
| 54 | +import java.nio.charset.Charset; |
| 55 | +import java.nio.charset.StandardCharsets; |
| 56 | +import java.nio.file.Files; |
| 57 | +import java.nio.file.Paths; |
43 | 58 | import java.util.Arrays;
|
44 | 59 | import java.util.Collections;
|
| 60 | +import java.util.LinkedList; |
| 61 | +import java.util.List; |
| 62 | +import java.util.Set; |
45 | 63 | import java.util.function.BiConsumer;
|
46 | 64 | import java.util.function.Consumer;
|
47 | 65 | import java.util.regex.Matcher;
|
48 | 66 | import java.util.regex.Pattern;
|
49 | 67 |
|
50 | 68 | import static org.junit.Assert.assertEquals;
|
| 69 | +import static org.junit.Assert.assertTrue; |
| 70 | +import static org.junit.Assert.fail; |
51 | 71 |
|
52 | 72 | public class TestSniperPrinter {
|
53 | 73 |
|
@@ -263,4 +283,51 @@ private String sourceWithoutImports(String source) {
|
263 | 283 | }
|
264 | 284 | return source.substring(lastImportEnd).trim();
|
265 | 285 | }
|
| 286 | + |
| 287 | + private static String fileAsString(String path, Charset encoding) |
| 288 | + throws IOException |
| 289 | + { |
| 290 | + byte[] encoded = Files.readAllBytes(Paths.get(path)); |
| 291 | + return new String(encoded, encoding); |
| 292 | + } |
| 293 | + |
| 294 | + public void testToStringWithSniperPrinter(String inputSourcePath) throws Exception { |
| 295 | + |
| 296 | + final Launcher launcher = new Launcher(); |
| 297 | + launcher.addInputResource(inputSourcePath); |
| 298 | + String originalContent = fileAsString(inputSourcePath, StandardCharsets.UTF_8).replace("\t",""); |
| 299 | + CtModel model = launcher.buildModel(); |
| 300 | + |
| 301 | + new SourceFragmentCreator().attachTo(launcher.getFactory().getEnvironment()); |
| 302 | + |
| 303 | + launcher.getEnvironment().setPrettyPrinterCreator( |
| 304 | + () -> { |
| 305 | + SniperJavaPrettyPrinter sp = new SniperJavaPrettyPrinter(launcher.getEnvironment()); |
| 306 | + sp.setIgnoreImplicit(true); |
| 307 | + return sp; |
| 308 | + } |
| 309 | + ); |
| 310 | + List<CtElement> ops = model.getElements(new TypeFilter<>(CtElement.class)); |
| 311 | + |
| 312 | + |
| 313 | + ops.stream() |
| 314 | + .filter(el -> !(el instanceof spoon.reflect.CtModelImpl.CtRootPackage) && |
| 315 | + !(el instanceof spoon.reflect.factory.ModuleFactory.CtUnnamedModule) |
| 316 | + ).forEach(el -> { |
| 317 | + try { |
| 318 | + //Contract, calling toString on unmodified AST elements should draw only from original. |
| 319 | + assertTrue("ToString() on element (" + el.getClass().getName() + ") = \"" + el + "\" is not in original content", |
| 320 | + originalContent.contains(el.toString().replace("\t",""))); |
| 321 | + } catch (UnsupportedOperationException | SpoonException e) { |
| 322 | + //Printer should not throw exception on printable element. (Unless there is a bug in the printer...) |
| 323 | + fail("ToString() on Element (" + el.getClass().getName() + "): at " + el.getPath() + " lead to an exception: " + e); |
| 324 | + } |
| 325 | + }); |
| 326 | + } |
| 327 | + |
| 328 | + @Test |
| 329 | + public void testToStringWithSniperOnElementScan() throws Exception { |
| 330 | + testToStringWithSniperPrinter("src/test/java/spoon/test/prettyprinter/testclasses/ElementScan.java"); |
| 331 | + } |
| 332 | + |
266 | 333 | }
|
0 commit comments