Skip to content

Commit

Permalink
Add tests for grammarAwareFormatting setting
Browse files Browse the repository at this point in the history
Signed-off-by: Jessica He <jhe@redhat.com>
  • Loading branch information
JessicaJHee authored and angelozerr committed Nov 11, 2022
1 parent e4292ef commit 09b3f49
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 128 deletions.
Expand Up @@ -40,6 +40,7 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
String text = formatterDocument.getText();
int availableLineWidth = parentConstraints.getAvailableLineWidth();
int indentLevel = parentConstraints.getIndentLevel();
boolean isMixedContent = formatElementCategory == FormatElementCategory.MixedContent;

int spaceStart = -1;
int spaceEnd = -1;
Expand Down Expand Up @@ -73,7 +74,7 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
int maxLineWidth = getMaxLineWidth();
availableLineWidth -= contentEnd - contentStart;
if (textNode.getStart() != contentStart && availableLineWidth >= 0
&& (isJoinContentLines() || !containsNewLine)) {
&& (isJoinContentLines() || !containsNewLine || isMixedContent)) {
// Decrement width for normalized space between text content (not done at
// beginning)
availableLineWidth--;
Expand All @@ -89,21 +90,18 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
spaceStart = -1;
spaceEnd = -1;
continue;
} else if (containsNewLine && !isJoinContentLines()) {
} else if (containsNewLine && !isJoinContentLines() && !isMixedContent) {
availableLineWidth = maxLineWidth - (contentEnd - contentStart)
- indentLevel * getTabSize();
}
}
if (isJoinContentLines() || !containsNewLine) {
// Case of isJoinContent == true: join all text content with single space
// Case of isJoinContent == false: normalize space only between element start
// tag and start of text content or doesn't contain a new line
replaceSpacesWithOneSpace(spaceStart, spaceEnd - 1, edits);
containsNewLine = false;
} else if (containsNewLine) {
if (containsNewLine && !isJoinContentLines() && !isMixedContent) {
replaceLeftSpacesWithIndentationPreservedNewLines(spaceStart, spaceEnd,
indentLevel, edits);
containsNewLine = false;
} else if (isJoinContentLines() || !containsNewLine || isMixedContent) {
replaceSpacesWithOneSpace(spaceStart, spaceEnd - 1, edits);
containsNewLine = false;
}
spaceStart = -1;
spaceEnd = -1;
Expand All @@ -112,7 +110,7 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
if (formatElementCategory != FormatElementCategory.IgnoreSpace && spaceEnd + 1 != text.length()) {
DOMElement parentElement = textNode.getParentElement();
// Don't format final spaces if text is at the end of the file
if ((!containsNewLine || isJoinContentLines()) && (!isMaxLineWidthSupported() || availableLineWidth >= 0)) {
if ((!containsNewLine || isJoinContentLines() || isMixedContent) && (!isMaxLineWidthSupported() || availableLineWidth >= 0)) {
// Replace spaces with single space in the case of:
// 1. there is no new line
// 2. isJoinContentLines
Expand Down
Expand Up @@ -701,13 +701,13 @@ public void testCommentLongWrap() throws BadLocationException {
" Content <!-- comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment -->"
+ //
"</a>";
String expected = "<a>\n" + //
" Content <!-- comment comment comment comment comment comment comment comment\n" + //
String expected = "<a> Content <!-- comment comment comment comment comment comment comment comment\n" + //
" comment comment comment comment comment comment comment comment comment\n" + //
" comment comment comment comment comment comment comment comment --></a>";
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setMaxLineWidth(80);
assertFormat(content, expected, settings, //
te(0, 3, 1, 2, " "), //
te(1, 78, 1, 79, "\n "), //
te(1, 150, 1, 151, "\n "));
assertFormat(expected, expected, settings);
Expand Down Expand Up @@ -759,8 +759,7 @@ public void testCommentLongTextContentWrapNewLine() throws BadLocationException
" content content content content content content content content content content content content content content content content content content <!-- comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment -->\n"
+ //
"</a>";
String expected = "<a>\n" + //
" content content content content content content content content content\n" + //
String expected = "<a> content content content content content content content content content\n" + //
" content content content content content content content content content <!--\n" + //
" comment comment comment comment comment comment comment comment comment\n" + //
" comment comment comment comment comment comment comment comment comment\n" + //
Expand All @@ -769,6 +768,7 @@ public void testCommentLongTextContentWrapNewLine() throws BadLocationException
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setMaxLineWidth(80);
assertFormat(content, expected, settings, //
te(0, 3, 1, 2, " "), //
te(1, 73, 1, 74, "\n "), //
te(1, 150, 1, 151, "\n "),
te(1, 222, 1, 223, "\n "), //
Expand Down Expand Up @@ -836,17 +836,15 @@ public void testContentFormatting2() throws BadLocationException {
" Content3\r" + //
" </b>\r" + //
"</a>";
String expected = "<a>\r" + //
" Content\r" + //
" <b>\r" + //
String expected = "<a> Content <b>\r" + //
" Content2\r" + //
" Content3\r" + //
" </b>\r" + //
"</a>";

assertFormat(content, expected, //
te(0, 3, 1, 1, "\r "), //
te(1, 8, 2, 1, "\r "), //
te(0, 3, 1, 1, " "), //
te(1, 8, 2, 1, " "), //
te(2, 4, 3, 3, "\r "),
te(4, 12, 5, 1, "\r "));
assertFormat(expected, expected);
Expand All @@ -860,16 +858,14 @@ public void testContentFormattingDontMoveEndTag() throws BadLocationException {
" Content2\r" + //
" Content3 </b>\r" + //
"</a>";
String expected = "<a>\r" + //
" Content\r" + //
" <b>\r" + //
String expected = "<a> Content <b>\r" + //
" Content2\r" + //
" Content3 </b>\r" + //
"</a>";

assertFormat(content, expected, //
te(0, 3, 1, 1, "\r "), //
te(1, 8, 2, 1, "\r "), //
te(0, 3, 1, 1, " "), //
te(1, 8, 2, 1, " "), //
te(2, 4, 3, 3, "\r "),
te(4, 12, 4, 17, " "));
assertFormat(expected, expected);
Expand Down
Expand Up @@ -128,14 +128,14 @@ public void testJoinCommentLinesLongWrap() throws BadLocationException {
" Content <!-- comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment -->"
+ //
"</a>";
String expected = "<a>\n" + //
" Content <!-- comment comment comment comment comment comment comment comment\n" + //
String expected = "<a> Content <!-- comment comment comment comment comment comment comment comment\n" + //
" comment comment comment comment comment comment comment comment comment\n" + //
" comment comment comment comment comment comment comment comment --></a>";
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setJoinCommentLines(true);
settings.getFormattingSettings().setMaxLineWidth(80);
assertFormat(content, expected, settings, //
te(0, 3, 1, 2, " "), //
te(1, 78, 1, 79, "\n "), //
te(1, 150, 1, 151, "\n "));
assertFormat(expected, expected, settings);
Expand Down
Expand Up @@ -139,15 +139,12 @@ public void testJoinContentLinesWithSiblingElementFalse() throws BadLocationExce
" zz \n" + //
" <a> </a> \n" + //
"</a>";
String expected = "<a>\n" + //
" zz\n" + //
" zz\n" + //
" <a> </a>\n" + //
String expected = "<a> zz zz <a> </a>\n" + //
"</a>";
assertFormat(content, expected, settings, //
te(0, 3, 1, 3, "\n "),
te(1, 5, 2, 3, "\n "),
te(2, 5, 3, 3, "\n "),
te(0, 3, 1, 3, " "),
te(1, 5, 2, 3, " "),
te(2, 5, 3, 3, " "),
te(3, 6, 3, 8, " "),
te(3, 12, 4, 0, "\n"));
assertFormat(expected, expected, settings);
Expand Down
Expand Up @@ -543,20 +543,18 @@ public void mixedTextNoJoinContentLines() throws BadLocationException {
+ //
" Language</alt></acronym> namespaces are used to distinguish between different element sets. In the last few years, almost all\r\n"
+ //
" new\r\n" + //
" XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n"
" new XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n"
+ //
" vocabularies. DocBook V5.0 is <emphasis>following</emphasis> this <emphasis>design</emphasis>/<emphasis>rule</emphasis>. Using\r\n"
+ //
" vocabularies. DocBook V5.0 is\r\n" + //
"\r\n" + //
"\r\n" + //
" <emphasis>following</emphasis> this\r\n" + //
" <emphasis>design</emphasis>/<emphasis>rule</emphasis>.\r\n" + //
"\r\n" + //
" Using\r\n" + //
" namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:</para>";

assertFormat(content, expected, settings, //
te(1, 127, 1, 128, "\r\n "));
te(1, 127, 1, 128, "\r\n "), //
te(1, 131, 2, 2, " "), //
te(3, 31, 6, 2, " "), //
te(6, 37, 7, 2, " "), //
te(7, 56, 9, 2, " "));
assertFormat(expected, expected, settings);
}

Expand Down
Expand Up @@ -49,7 +49,7 @@ public void testDontPreserveEmptyContentTag() throws BadLocationException {
" " + //
"</a>";
String expected = "<a>\n" + //
"</a>";
"</a>";
assertFormat(content, expected, settings, //
te(0, 3, 1, 5, "\n"));
assertFormat(expected, expected, settings);
Expand Down Expand Up @@ -122,10 +122,9 @@ public void testDontPreserveEmptyContentTagWithSiblingContent() throws BadLocati

String content = "<a>\n" + //
" zz <b> </b>tt </a>";
String expected = "<a>\n" + //
" zz <b> </b>tt </a>";
String expected = "<a> zz <b> </b>tt </a>";
assertFormat(content, expected, settings, //
te(0, 3, 1, 3, "\n "), //
te(0, 3, 1, 3, " "), //
te(1, 5, 1, 10, " "), //
te(1, 13, 1, 15, " "), //
te(1, 21, 1, 26, " "));
Expand Down Expand Up @@ -167,11 +166,10 @@ public void testDontPreserveEmptyContentTagWithSiblingWithComment() throws BadLo

String content = "<a>\n" + //
" zz <b> </b>tt <!-- Comment --> </a>";
String expected = "<a>\n" + //
" zz <b> </b>tt <!-- Comment -->\n" + //
String expected = "<a> zz <b> </b>tt <!-- Comment -->\n" + //
"</a>";
assertFormat(content, expected, settings, //
te(0, 3, 1, 3, "\n "), //
te(0, 3, 1, 3, " "), //
te(1, 5, 1, 9, " "), //
te(1, 12, 1, 14, " "), //
te(1, 37, 1, 42, "\n"));
Expand Down
Expand Up @@ -133,17 +133,11 @@ public void testPreserveNewlinesMixedContent() throws BadLocationException {
String expected = "<xml>\r\n" + //
"\r\n" + //
"\r\n" + //
" <a></a>\r\n" + //
"\r\n" + //
"\r\n" + //
" test\r\n" + //
"\r\n" + //
"\r\n" + //
"</xml>";
" <a></a> test </xml>";
assertFormat(content, expected, settings, //
te(0, 5, 5, 2, "\r\n\r\n\r\n "), //
te(5, 9, 8, 0, "\r\n\r\n\r\n "), //
te(8, 4, 13, 0, "\r\n\r\n\r\n"));
te(5, 9, 8, 0, " "), //
te(8, 4, 13, 0, " "));
assertFormat(expected, expected, settings);
}

Expand Down
Expand Up @@ -54,17 +54,17 @@ public void preserveSpacesWithXmlSpace2() throws BadLocationException {
" </b>\r\n" + //
"</a>";
String expected = "<a>\r\n" + //
" <b>\r\n" + //
" c <d></d> e\r\n" + //
" </b>\r\n" + //
" <b> c <d></d> e </b>\r\n" + //
" <b xml:space=\"preserve\">\r\n" + //
" c <d></d> e\r\n" + //
" </b>\r\n" + //
"</a>";

assertFormat(content, expected, //
te(1, 5, 2, 4, " "), //
te(2, 5, 2, 7, " "), //
te(2, 14, 2, 16, " "));
te(2, 14, 2, 16, " "), //
te(2, 17, 3, 2, " "));
assertFormat(expected, expected);
}

Expand All @@ -86,60 +86,6 @@ public void preserveSpacesWithDefaultSettings() throws BadLocationException {
assertFormat(content, expected);
}

@Test
public void preserveSpacesWithXsdStringWithNoMaxLineWidth() throws Exception {
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 src/test/resources/xsd/maven-4.0.0.xsd\">\r\n"
+ //
" <description>a b c</description>\r\n" + //
" <description2>a b c</description2>\r\n" + //
"</project>";

String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 src/test/resources/xsd/maven-4.0.0.xsd\">\r\n"
+ //
" <description>a b c</description>\r\n" + // <-- preserve space because description is xs:string
" <description2>a b c</description2>\r\n" + // <-- no preserve space because description2 is not a
// xs:string
"</project>";

SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setMaxLineWidth(0);
settings.getFormattingSettings().setGrammarAwareFormatting(true);
assertFormat(content, expected, settings, //
te(3, 17, 3, 21, " "), //
te(3, 22, 3, 27, " "));
}

@Test
public void preserveSpacesWithXsdStringWithMaxLineWidth() throws Exception {
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 src/test/resources/xsd/maven-4.0.0.xsd\">\r\n"
+ //
" <description>a b c</description>\r\n" + //
" <description2>a b c</description2>\r\n" + //
"</project>";

String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
"<project xmlns=\"http://maven.apache.org/POM/4.0.0\"\r\n" + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 src/test/resources/xsd/maven-4.0.0.xsd\">\r\n"
+ //
" <description>a b c</description>\r\n" + // <-- preserve space because description is xs:string
" <description2>a b c</description2>\r\n" + // <-- no preserve space because description2 is not a
// xs:string
"</project>";

SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setGrammarAwareFormatting(true);
settings.getFormattingSettings().setMaxLineWidth(80);
assertFormat(content, expected, settings, //
te(1, 50, 1, 51, "\r\n "), //
te(1, 104, 1, 105, "\r\n "), //
te(3, 17, 3, 21, " "), //
te(3, 22, 3, 27, " "));
}

private static void assertFormat(String unformatted, String actual, TextEdit... expectedEdits)
throws BadLocationException {
assertFormat(unformatted, actual, new SharedSettings(), expectedEdits);
Expand Down

0 comments on commit 09b3f49

Please sign in to comment.