Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync with underscore-java #94

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions src/main/java/com/github/underscore/Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -1521,30 +1521,26 @@ private static Object addElement(
}

static Map<String, String> parseAttributes(final String source) {
final Map<String, String> result = new LinkedHashMap<>();
final StringBuilder key = new StringBuilder();
final StringBuilder value = new StringBuilder();
boolean quoteFound = false;
boolean equalFound = false;
for (int index = 0; index < source.length(); index += 1) {
if (source.charAt(index) == '=') {
equalFound = !equalFound;
continue;
}
if (source.charAt(index) == '"') {
if (quoteFound && equalFound) {
Map<String, String> result = new LinkedHashMap<>();
StringBuilder key = new StringBuilder();
StringBuilder value = new StringBuilder();
boolean inQuotes = false;
boolean expectingValue = false;
for (char c : source.toCharArray()) {
if (c == '"') {
inQuotes = !inQuotes;
if (!inQuotes && expectingValue) {
result.put(key.toString(), value.toString());
key.setLength(0);
value.setLength(0);
equalFound = false;
}
quoteFound = !quoteFound;
} else if (quoteFound || SKIPPED_CHARS.contains(source.charAt(index))) {
if (quoteFound) {
value.append(source.charAt(index));
expectingValue = false;
}
} else {
key.append(source.charAt(index));
} else if (c == '=' && !inQuotes) {
expectingValue = true;
} else if (inQuotes) {
value.append(c);
} else if (!SKIPPED_CHARS.contains(c)) {
key.append(c);
}
}
return result;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/github/underscore/FunctionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ void defer() {
return null;
});
assertEquals(0, counter[0].intValue(), "incr was debounced");
await().atMost(120, TimeUnit.MILLISECONDS)
await().atLeast(90, TimeUnit.MILLISECONDS)
.until(
() -> {
assertEquals(1, counter[0].intValue(), "incr was debounced");
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/com/github/underscore/LodashTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,19 @@ void xmpToJson4() {
+ "</z:catalog>"));
}

@Test
void xmpToJson5() {
assertEquals("{\n"
+ " \"Comment\": {\n"
+ " \"-stringValue\": \"============================\",\n"
+ " \"-self-closing\": \"true\"\n"
+ " },\n"
+ " \"#omit-xml-declaration\": \"yes\"\n"
+ "}",
U.xmlToJson(
"<Comment stringValue=\"============================\"/>"));
}

@Test
void xmlToJsonMinimum() {
assertEquals(
Expand Down
66 changes: 64 additions & 2 deletions src/test/java/com/github/underscore/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2358,9 +2358,71 @@ void parseAttributes() {
"{version=1.0}",
Xml.parseAttributes(" version = \"1.0\" encoding= \"UTF-8 ").toString());
assertEquals(
"{}", Xml.parseAttributes(" version = \"1.0 encoding= \"UTF-8\" ").toString());
"{version=1.0 encoding= }",
Xml.parseAttributes(" version = \"1.0 encoding= \"UTF-8\" ").toString());
assertEquals(
"{}", Xml.parseAttributes(" version = 1.0\" encoding= \"UTF-8\" ").toString());
"{version1.0= encoding= }",
Xml.parseAttributes(" version = 1.0\" encoding= \"UTF-8\" ").toString());
}

@Test
void testSingleAttribute() {
Map<String, String> result = Xml.parseAttributes("key1=\"value1\"");
assertEquals(Map.of("key1", "value1"), result);
}

@Test
void testMultipleAttributes() {
Map<String, String> result = Xml.parseAttributes("key1=\"value1\" key2=\"value2\"");
assertEquals(Map.of("key1", "value1", "key2", "value2"), result);
}

@Test
void testAttributeWithSpaces() {
Map<String, String> result = Xml.parseAttributes("key1=\"value with spaces\" key2=\"another value\"");
assertEquals(Map.of("key1", "value with spaces", "key2", "another value"), result);
}

@Test
void testEmptyValue() {
Map<String, String> result = Xml.parseAttributes("key1=\"value1\" key2=\"\"");
assertEquals(Map.of("key1", "value1", "key2", ""), result);
}

@Test
void testAttributesWithoutSpaceSeparation() {
Map<String, String> result = Xml.parseAttributes("key1=\"value1\"key2=\"value2\"");
assertEquals(Map.of("key1", "value1", "key2", "value2"), result);
}

@Test
void testUnclosedQuotes() {
Map<String, String> result = Xml.parseAttributes("key1=\"value1 key2=\"value2\"");
assertEquals(Map.of("key1", "value1 key2="), result);
}

@Test
void testEqualsSignInValue() {
Map<String, String> result = Xml.parseAttributes("key1=\"value=1\" key2=\"value=2\"");
assertEquals(Map.of("key1", "value=1", "key2", "value=2"), result);
}

@Test
void testTrailingWhitespace() {
Map<String, String> result = Xml.parseAttributes("key1=\"value1\" key2=\"value2\" ");
assertEquals(Map.of("key1", "value1", "key2", "value2"), result);
}

@Test
void testLeadingWhitespace() {
Map<String, String> result = Xml.parseAttributes(" key1=\"value1\" key2=\"value2\"");
assertEquals(Map.of("key1", "value1", "key2", "value2"), result);
}

@Test
void testNoEqualsSign() {
Map<String, String> result = Xml.parseAttributes("key1\"value1\" key2=\"value2\"");
assertEquals(Map.of("key1key2", "value1value2"), result);
}

@SuppressWarnings("unchecked")
Expand Down
Loading