1 /* 2 * Copyright (C) 2007-2010 Jlio 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; 23 24 import com.github.javaparser.ast.CompilationUnit; 25 import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; 26 import com.github.javaparser.ast.body.FieldDeclaration; 27 import com.github.javaparser.ast.expr.VariableDeclarationExpr; 28 import com.github.javaparser.ast.type.PrimitiveType; 29 import org.junit.Test; 30 31 import static com.github.javaparser.JavaParser.*; 32 import static com.github.javaparser.utils.TestUtils.assertEqualsNoEol; 33 import static org.junit.Assert.assertEquals; 34 35 public class PrettyPrinterTest { 36 37 private String prettyPrintField(String code) { 38 CompilationUnit cu = parse(code); 39 return new PrettyPrinter().print(cu.findFirst(FieldDeclaration.class).get()); 40 } 41 42 private String prettyPrintVar(String code) { 43 CompilationUnit cu = parse(code); 44 return new PrettyPrinter().print(cu.findAll(VariableDeclarationExpr.class).get(0)); 45 } 46 47 @Test 48 public void printingArrayFields() { 49 String code; 50 code = "class A { int a, b[]; }"; 51 assertEquals("int a, b[];", prettyPrintField(code)); 52 53 code = "class A { int[] a[], b[]; }"; 54 assertEquals("int[][] a, b;", prettyPrintField(code)); 55 56 code = "class A { int[] a[][], b; }"; 57 assertEquals("int[] a[][], b;", prettyPrintField(code)); 58 59 code = "class A { int[] a, b; }"; 60 assertEquals("int[] a, b;", prettyPrintField(code)); 61 62 code = "class A { int a[], b[]; }"; 63 assertEquals("int[] a, b;", prettyPrintField(code)); 64 } 65 66 @Test 67 public void printingArrayVariables() { 68 String code; 69 code = "class A { void foo(){ int a, b[]; }}"; 70 assertEquals("int a, b[]", prettyPrintVar(code)); 71 72 code = "class A { void foo(){ int[] a[], b[]; }}"; 73 assertEquals("int[][] a, b", prettyPrintVar(code)); 74 75 code = "class A { void foo(){ int[] a[][], b; }}"; 76 assertEquals("int[] a[][], b", prettyPrintVar(code)); 77 78 code = "class A { void foo(){ int[] a, b; }}"; 79 assertEquals("int[] a, b", prettyPrintVar(code)); 80 81 code = "class A { void foo(){ int a[], b[]; }}"; 82 assertEquals("int[] a, b", prettyPrintVar(code)); 83 } 84 85 private String prettyPrintConfigurable(String code) { 86 CompilationUnit cu = parse(code); 87 PrettyPrinter printer = new PrettyPrinter(new PrettyPrinterConfiguration().setVisitorFactory(TestVisitor::new)); 88 return printer.print(cu.findFirst(ClassOrInterfaceDeclaration.class).get()); 89 } 90 91 @Test 92 public void printUseTestVisitor() { 93 String code; 94 code = "class A { void foo(){ int a, b[]; }}"; 95 assertEquals("test", prettyPrintConfigurable(code)); 96 } 97 98 @Test 99 public void prettyColumnAlignParameters_enabled() { 100 PrettyPrinterConfiguration config = new PrettyPrinterConfiguration() 101 .setIndent("\t") 102 .setColumnAlignParameters(true); 103 104 final String EOL = config.getEndOfLineCharacter(); 105 106 String code = "class Example { void foo(Object arg0,Object arg1){ myMethod(1, 2, 3, 5, Object.class); } }"; 107 String expected = "class Example {" + EOL + 108 "" + EOL + 109 "\tvoid foo(Object arg0, Object arg1) {" + EOL + 110 "\t\tmyMethod(1," + EOL + 111 "\t\t 2," + EOL + 112 "\t\t 3," + EOL + 113 "\t\t 5," + EOL + 114 "\t\t Object.class);" + EOL + 115 "\t}" + EOL + 116 "}" + EOL + 117 ""; 118 119 assertEquals(expected, new PrettyPrinter(config).print(parse(code))); 120 } 121 122 @Test 123 public void prettyColumnAlignParameters_disabled() { 124 PrettyPrinterConfiguration config = new PrettyPrinterConfiguration(); 125 final String EOL = config.getEndOfLineCharacter(); 126 127 String code = "class Example { void foo(Object arg0,Object arg1){ myMethod(1, 2, 3, 5, Object.class); } }"; 128 String expected = "class Example {" + EOL + 129 "" + EOL + 130 " void foo(Object arg0, Object arg1) {" + EOL + 131 " myMethod(1, 2, 3, 5, Object.class);" + EOL + 132 " }" + EOL + 133 "}" + EOL + 134 ""; 135 136 assertEquals(expected, new PrettyPrinter(config).print(parse(code))); 137 } 138 139 @Test 140 public void prettyAlignMethodCallChains_enabled() { 141 PrettyPrinterConfiguration config = new PrettyPrinterConfiguration() 142 .setIndent("\t") 143 .setColumnAlignFirstMethodChain(true); 144 145 final String EOL = config.getEndOfLineCharacter(); 146 147 String code = "class Example { void foo() { IntStream.range(0, 10).filter(x -> x % 2 == 0).map(x -> x * IntStream.of(1,3,5,1).sum()).forEach(System.out::println); } }"; 148 String expected = "class Example {" + EOL + 149 "" + EOL + 150 "\tvoid foo() {" + EOL + 151 "\t\tIntStream.range(0, 10)" + EOL + 152 "\t\t .filter(x -> x % 2 == 0)" + EOL + 153 "\t\t .map(x -> x * IntStream.of(1, 3, 5, 1)" + EOL + 154 "\t\t .sum())" + EOL + 155 "\t\t .forEach(System.out::println);" + EOL + 156 "\t}" + EOL + 157 "}" + EOL + 158 ""; 159 160 assertEquals(expected, new PrettyPrinter(config).print(parse(code))); 161 } 162 163 @Test 164 public void prettyAlignMethodCallChains_disabled() { 165 PrettyPrinterConfiguration config = new PrettyPrinterConfiguration(); 166 final String EOL = config.getEndOfLineCharacter(); 167 168 String code = "class Example { void foo() { IntStream.range(0, 10).filter(x -> x % 2 == 0).map(x -> x * IntStream.of(1,3,5,1).sum()).forEach(System.out::println); } }"; 169 String expected = "class Example {" + EOL + 170 "" + EOL + 171 " void foo() {" + EOL + 172 " IntStream.range(0, 10).filter(x -> x % 2 == 0).map(x -> x * IntStream.of(1, 3, 5, 1).sum()).forEach(System.out::println);" + EOL + 173 " }" + EOL + 174 "}" + EOL + 175 ""; 176 177 assertEquals(expected, new PrettyPrinter(config).print(parse(code))); 178 } 179 180 @Test 181 public void enumConstantsHorizontally() { 182 CompilationUnit cu = parse("enum X{A, B, C, D, E}"); 183 assertEqualsNoEol("enum X {\n\n A, B, C, D, E\n}\n", new PrettyPrinter().print(cu)); 184 } 185 186 @Test 187 public void enumConstantsVertically() { 188 CompilationUnit cu = parse("enum X{A, B, C, D, E, F}"); 189 assertEqualsNoEol("enum X {\n\n A,\n B,\n C,\n D,\n E,\n F\n}\n", new PrettyPrinter().print(cu)); 190 } 191 192 @Test 193 public void printingInconsistentVariables() { 194 FieldDeclaration fieldDeclaration = parseBodyDeclaration("int a, b;").asFieldDeclaration(); 195 196 assertEquals("int a, b;", fieldDeclaration.toString()); 197 198 fieldDeclaration.getVariable(0).setType(PrimitiveType.doubleType()); 199 200 assertEquals("??? a, b;", fieldDeclaration.toString()); 201 202 fieldDeclaration.getVariable(1).setType(PrimitiveType.doubleType()); 203 204 assertEquals("double a, b;", fieldDeclaration.toString()); 205 } 206 } 207