1 /*
2  * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3  * Copyright (C) 2011, 2013-2016 The JavaParser Team.
4  *
5  * This file is part of JavaParser.
6  *
7  * JavaParser can be used either under the terms of
8  * a) the GNU Lesser General Public License as published by
9  *     the Free Software Foundation, either version 3 of the License, or
10  *     (at your option) any later version.
11  * b) the terms of the Apache License
12  *
13  * You should have received a copy of both licenses in LICENCE.LGPL and
14  * LICENCE.APACHE. Please refer to those files for details.
15  *
16  * JavaParser is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  */
21 
22 package com.github.javaparser.printer.lexicalpreservation;
23 
24 import com.github.javaparser.ast.CompilationUnit;
25 import com.github.javaparser.ast.Node;
26 import com.github.javaparser.ast.expr.Expression;
27 
28 import java.io.IOException;
29 
30 import static com.github.javaparser.StaticJavaParser.parse;
31 import static com.github.javaparser.StaticJavaParser.parseExpression;
32 import static com.github.javaparser.utils.TestUtils.readResource;
33 import static org.junit.jupiter.api.Assertions.assertEquals;
34 
35 public abstract class AbstractLexicalPreservingTest {
36 
37     protected CompilationUnit cu;
38     protected Expression expression;
39 
considerCode(String code)40     protected void considerCode(String code) {
41         cu = LexicalPreservingPrinter.setup(parse(code));
42     }
43 
considerExpression(String code)44     protected void considerExpression(String code) {
45         expression = LexicalPreservingPrinter.setup(parseExpression(code));
46     }
47 
considerExample(String resourceName)48     protected String considerExample(String resourceName) throws IOException {
49         String code = readExample(resourceName);
50         considerCode(code);
51         return code;
52     }
53 
readExample(String resourceName)54     protected String readExample(String resourceName) throws IOException {
55         return readResource("com/github/javaparser/lexical_preservation_samples/" + resourceName + ".java.txt");
56     }
57 
assertTransformed(String exampleName, Node node)58     protected void assertTransformed(String exampleName, Node node) throws IOException {
59         String expectedCode = readExample(exampleName + "_expected");
60         String actualCode = LexicalPreservingPrinter.print(node);
61         assertEquals(expectedCode, actualCode);
62     }
63 
assertUnchanged(String exampleName)64     protected void assertUnchanged(String exampleName) throws IOException {
65         String code = considerExample(exampleName + "_original");
66         assertEquals(code, LexicalPreservingPrinter.print(cu != null ? cu : expression));
67     }
68 
assertTransformedToString(String expectedPartialCode, Node node)69     protected void assertTransformedToString(String expectedPartialCode, Node node) {
70         String actualCode = LexicalPreservingPrinter.print(node);
71         assertEquals(expectedPartialCode, actualCode);
72     }
73 
74 }
75