Skip to content

Commit

Permalink
Add tests for DTD quote enforced formatting
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <[email protected]>
  • Loading branch information
dkwon17 authored and angelozerr committed Jun 2, 2020
1 parent 94b595f commit 84d2689
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private void sendLimitExceededWarning(String uri, int resultLimit, String featur
Collections.singletonList("xml.symbols.maxItemsComputed"));
ActionableNotification notification = new ActionableNotification().withSeverity(MessageType.Info)
.withMessage(message).withCommands(Collections.singletonList(command));
client.actionableNotification(notification);
client.actionableNotification(notification);
} else {
// the open settings command is not supported by the client, display a simple
// message with LSP
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,46 +455,39 @@ public static int findEndWord(String text, int offset, Predicate<Character> isVa
}

/**
* Checks if 'value' has matching surrounding quotations.
* Returns <code>value</code> without surrounding quotes.
*
* If <code>value</code> does not have matching surrounding quotes,
* returns <code>value</code>.
*
* @param value
* @return
* @return <code>value</code> without surrounding quotes.
*/
public static boolean isQuoted(String value) {
if (value == null) {
return false;
}
if (value.isEmpty()) {
return false;
}
char quoteValueStart = value.charAt(0);
boolean start = quoteValueStart == '\"' || quoteValueStart == '\'' ? true : false;
if (start == false) {
return false;
public static String convertToQuotelessValue(String value) {
if (value == null || !isQuoted(value)) {
return value;
}
char quoteValueEnd = value.charAt(value.length() - 1);
boolean end = (quoteValueEnd == '\"' || quoteValueEnd == '\'') && quoteValueEnd == quoteValueStart ? true
: false;
return end;

return value.substring(1, value.length() - 1);
}

/**
* Returns a String of 'value' without surrounding quotes if it had them.
* Returns true if <code>value</code> has matching surrounding quotes
* and false otherwise.
*
* @param value
* @return
* @return true if <code>value</code> has matching surrounding quotes.
*/
public static String convertToQuotelessValue(String value) {
if (value == null) {
return null;
public static boolean isQuoted(String value) {
if (value == null || value.length() < 2) {
return false;
}
if (value.isEmpty()) {
return value;

char quoteValueStart = value.charAt(0);
if (quoteValueStart != '\"' && quoteValueStart != '\'') {
return false;
}
char quoteValue = value.charAt(0);
int start = quoteValue == '\"' || quoteValue == '\'' ? 1 : 0;
quoteValue = value.charAt(value.length() - 1);
int end = quoteValue == '\"' || quoteValue == '\'' ? value.length() - 1 : value.length();
return value.substring(start, end);
char quoteValueEnd = value.charAt(value.length() - 1);
return quoteValueEnd == quoteValueStart;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,60 @@ public void testUseSingleQuotesMultipleAttributesSplit() throws BadLocationExcep
format(content, expected, settings);
}

@Test
public void testUseSingleQuotesLocalDTD() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getPreferences().setQuoteStyle(QuoteStyle.singleQuotes);
settings.getFormattingSettings().setEnforceQuoteStyle(EnforceQuoteStyle.preferred);
String content = "<!DOCTYPE note SYSTEM \"note.dtd\">";
String expected = "<!DOCTYPE note SYSTEM \'note.dtd\'>";
format(content, expected, settings);
}

@Test
public void testUseSingleQuotesLocalDTDWithSubset() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getPreferences().setQuoteStyle(QuoteStyle.singleQuotes);
settings.getFormattingSettings().setEnforceQuoteStyle(EnforceQuoteStyle.preferred);
String content = "<!DOCTYPE article [\n" + //
" <!ENTITY AUTHOR \"John Doe\">\n" + //
" <!ENTITY COMPANY \"JD Power Tools, Inc.\">\n" + //
" <!ENTITY EMAIL \"[email protected]\">\n" + //
" <!ELEMENT E EMPTY>\n" + //
" <!ATTLIST E WIDTH CDATA \"0\">\n" + //
"]>\n" + //
"\n" + //
"<root attr=\"hello\"></root>";
String expected = "<!DOCTYPE article [\n" + //
" <!ENTITY AUTHOR \'John Doe\'>\n" + //
" <!ENTITY COMPANY \'JD Power Tools, Inc.\'>\n" + //
" <!ENTITY EMAIL \'[email protected]\'>\n" + //
" <!ELEMENT E EMPTY>\n" + //
" <!ATTLIST E WIDTH CDATA \'0\'>\n" + //
"]>\n" + //
"\n" + //
"<root attr=\'hello\'></root>";
format(content, expected, settings);
}

@Test
public void testUseSingleQuotesDTDFile() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getPreferences().setQuoteStyle(QuoteStyle.singleQuotes);
settings.getFormattingSettings().setEnforceQuoteStyle(EnforceQuoteStyle.preferred);
String content = "<!ENTITY AUTHOR \"John Doe\">\n" + //
"<!ENTITY COMPANY \"JD Power Tools, Inc.\">\n" + //
"<!ENTITY EMAIL \"[email protected]\">\n" + //
"<!ELEMENT E EMPTY>\n" + //
"<!ATTLIST E WIDTH CDATA \"0\">";
String expected = "<!ENTITY AUTHOR \'John Doe\'>\n" + //
"<!ENTITY COMPANY \'JD Power Tools, Inc.\'>\n" + //
"<!ENTITY EMAIL \'[email protected]\'>\n" + //
"<!ELEMENT E EMPTY>\n" + //
"<!ATTLIST E WIDTH CDATA \'0\'>";
format(content, expected, settings, "test.dtd");
}

@Test
public void testDontFormatQuotesByDefault() throws BadLocationException {
SharedSettings settings = new SharedSettings();
Expand Down

0 comments on commit 84d2689

Please sign in to comment.