Skip to content

Commit

Permalink
Added Test and moved raw string sanitation to a new method
Browse files Browse the repository at this point in the history
  • Loading branch information
immortalsoftware committed Sep 6, 2018
1 parent 0619a84 commit 2bac28a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Expand Up @@ -26,6 +26,7 @@
import org.junit.Test;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.Expression;

public class YamlPrinterTest {
Expand Down Expand Up @@ -91,4 +92,29 @@ public void testWithColonFollowedByLineSeparatorInValue() {
String output = yamlPrinter.output(expression);
assertEquals(expectedOutput, output);
}

@Test
public void testParsingJavadocWithQuoteAndNewline() {
String code = "/**" + System.lineSeparator() +
" * \" this comment contains a quote and newlines" + System.lineSeparator() +
" */" + System.lineSeparator() +
"public class Dog {" + System.lineSeparator() +
"" + System.lineSeparator() +
"}";
String expectedOutput = "---" + System.lineSeparator();
expectedOutput += "root(Type=CompilationUnit): " + System.lineSeparator();
expectedOutput += " types: " + System.lineSeparator();
expectedOutput += " - type(Type=ClassOrInterfaceDeclaration): " + System.lineSeparator();
expectedOutput += " isInterface: \"false\"" + System.lineSeparator();
expectedOutput += " name(Type=SimpleName): " + System.lineSeparator();
expectedOutput += " identifier: \"Dog\"" + System.lineSeparator();
expectedOutput += " comment(Type=JavadocComment): " + System.lineSeparator();
expectedOutput += " content: \"\\n * \\\" this comment contains a quote\\n \"" + System.lineSeparator();
expectedOutput += "...";

YamlPrinter yamlPrinter = new YamlPrinter(true);
CompilationUnit computationUnit = JavaParser.parse(code);
String output = yamlPrinter.output(computationUnit);
assertEquals(expectedOutput, output);
}
}
Expand Up @@ -69,11 +69,7 @@ public void output(Node node, String name, int level, StringBuilder builder) {

level++;
for (PropertyMetaModel a : attributes) {
String text = "\"" + a.getValue(node).toString().replace("\\", "\\\\").
replaceAll("\"", "\\\\\"").replace("\n", "\\n").replace("\t", "\\t")
+ "\"";
builder.append(System.lineSeparator() + indent(level) + a.getName() + ": \""
+ text + "\"");
builder.append(System.lineSeparator() + indent(level) + a.getName() + ": " + escapeValue(a.getValue(node).toString()));
}

for (PropertyMetaModel sn : subNodes) {
Expand Down Expand Up @@ -101,4 +97,9 @@ private String indent(int level) {
sb.append(" ");
return sb.toString();
}

private String escapeValue(String value) {
return "\"" + value.replace("\\", "\\\\").replaceAll("\"", "\\\\\"").replace("\n", "\\n").
replace("\t", "\\t") + "\"";
}
}

0 comments on commit 2bac28a

Please sign in to comment.