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 static org.junit.Assert.assertEquals; 25 26 import org.junit.Test; 27 28 import com.github.javaparser.JavaParser; 29 import com.github.javaparser.ast.expr.Expression; 30 31 public class YamlPrinterTest { 32 33 @Test 34 public void testWithType() { 35 String expectedOutput = "---" + System.lineSeparator(); 36 expectedOutput += "root(Type=MethodCallExpr): " + System.lineSeparator(); 37 expectedOutput += " name(Type=SimpleName): " + System.lineSeparator(); 38 expectedOutput += " identifier: \"x\"" + System.lineSeparator(); 39 expectedOutput += " arguments: " + System.lineSeparator(); 40 expectedOutput += " - argument(Type=IntegerLiteralExpr): " + System.lineSeparator(); 41 expectedOutput += " value: \"1\"" + System.lineSeparator(); 42 expectedOutput += " - argument(Type=IntegerLiteralExpr): " + System.lineSeparator(); 43 expectedOutput += " value: \"1\"" + System.lineSeparator(); 44 expectedOutput += "..."; 45 46 YamlPrinter yamlPrinter = new YamlPrinter(true); 47 Expression expression = JavaParser.parseExpression("x(1,1)"); 48 String output = yamlPrinter.output(expression); 49 assertEquals(expectedOutput, output); 50 } 51 52 @Test 53 public void testWithoutType() { 54 String expectedOutput = "---" + System.lineSeparator(); 55 expectedOutput += "root: " + System.lineSeparator(); 56 expectedOutput += " operator: \"PLUS\"" + System.lineSeparator(); 57 expectedOutput += " left: " + System.lineSeparator(); 58 expectedOutput += " value: \"1\"" + System.lineSeparator(); 59 expectedOutput += " right: " + System.lineSeparator(); 60 expectedOutput += " value: \"1\"" + System.lineSeparator(); 61 expectedOutput += "..."; 62 63 YamlPrinter yamlPrinter = new YamlPrinter(false); 64 Expression expression = JavaParser.parseExpression("1+1"); 65 String output = yamlPrinter.output(expression); 66 assertEquals(expectedOutput, output); 67 } 68 69 @Test 70 public void testWithColonFollowedBySpaceInValue() { 71 String expectedOutput = "---" + System.lineSeparator(); 72 expectedOutput += "root(Type=StringLiteralExpr): " + System.lineSeparator(); 73 expectedOutput += " value: \"a\\\\: b\"" + System.lineSeparator(); 74 expectedOutput += "..."; 75 76 YamlPrinter yamlPrinter = new YamlPrinter(true); 77 Expression expression = JavaParser.parseExpression("\"a\\\\: b\""); 78 String output = yamlPrinter.output(expression); 79 assertEquals(expectedOutput, output); 80 } 81 82 @Test 83 public void testWithColonFollowedByLineSeparatorInValue() { 84 String expectedOutput = "---" + System.lineSeparator(); 85 expectedOutput += "root(Type=StringLiteralExpr): " + System.lineSeparator(); 86 expectedOutput += " value: \"a\\\\:\\\\nb\"" + System.lineSeparator(); 87 expectedOutput += "..."; 88 89 YamlPrinter yamlPrinter = new YamlPrinter(true); 90 Expression expression = JavaParser.parseExpression("\"a\\\\:\\\\nb\""); 91 String output = yamlPrinter.output(expression); 92 assertEquals(expectedOutput, output); 93 } 94 } 95