Skip to content

Commit c4af17c

Browse files
committed
chore: update parsing test cases and fix printing
1 parent 26c0783 commit c4af17c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+80
-75
lines changed

src/main/java/kdl/objects/KDLNode.java

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package kdl.objects;
22

3-
import kdl.print.PrintConfig;
4-
import kdl.print.PrintUtil;
5-
63
import java.io.IOException;
74
import java.io.Writer;
85
import java.math.BigDecimal;
9-
import java.util.*;
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Objects;
12+
import java.util.Optional;
1013
import java.util.concurrent.ConcurrentHashMap;
1114
import java.util.function.Predicate;
15+
import kdl.print.PrintConfig;
16+
17+
import static kdl.print.PrintUtil.writeStringQuotedAppropriately;
1218

1319
public class KDLNode implements KDLObject {
1420
private final String identifier;
@@ -69,41 +75,31 @@ public void writeKDL(Writer writer, PrintConfig printConfig) throws IOException
6975
void writeKDLPretty(Writer writer, int depth, PrintConfig printConfig) throws IOException {
7076
if (type.isPresent()) {
7177
writer.write('(');
72-
PrintUtil.writeStringQuotedAppropriately(writer, type.get(), true, printConfig);
78+
writeStringQuotedAppropriately(writer, type.get(), true, printConfig);
7379
writer.write(')');
7480
}
7581

76-
PrintUtil.writeStringQuotedAppropriately(writer, identifier, true, printConfig);
77-
if (!args.isEmpty() || !props.isEmpty() || child.isPresent()) {
78-
writer.write(' ');
79-
}
82+
writeStringQuotedAppropriately(writer, identifier, true, printConfig);
8083

81-
for (int i = 0; i < this.args.size(); i++) {
82-
final KDLValue<?> value = this.args.get(i);
83-
if (!(value instanceof KDLNull) || printConfig.shouldPrintNullArgs()) {
84-
value.writeKDL(writer, printConfig);
85-
if (i < this.args.size() - 1 || !props.isEmpty() || child.isPresent()) {
86-
writer.write(' ');
87-
}
88-
}
89-
}
84+
for (var value : this.args) {
85+
if (!(value instanceof KDLNull) || printConfig.shouldPrintNullArgs()) {
86+
writer.write(' ');
87+
value.writeKDL(writer, printConfig);
88+
}
89+
}
9090

91-
final ArrayList<String> keys = new ArrayList<>(props.keySet());
92-
for (int i = 0; i < keys.size(); i++) {
93-
final KDLValue<?> value = props.get(keys.get(i));
94-
if (!(value instanceof KDLNull) || printConfig.shouldPrintNullProps()) {
95-
PrintUtil.writeStringQuotedAppropriately(writer, keys.get(i), true, printConfig);
96-
writer.write('=');
97-
value.writeKDL(writer, printConfig);
98-
if (i < keys.size() - 1 || child.isPresent()) {
99-
writer.write(' ');
100-
}
101-
}
102-
}
91+
for (var entry : props.entrySet()) {
92+
if (!(entry.getValue() instanceof KDLNull) || printConfig.shouldPrintNullProps()) {
93+
writer.write(' ');
94+
writeStringQuotedAppropriately(writer, entry.getKey(), true, printConfig);
95+
writer.write('=');
96+
entry.getValue().writeKDL(writer, printConfig);
97+
}
98+
}
10399

104100
if (child.isPresent()) {
105101
if (!child.get().getNodes().isEmpty() || printConfig.shouldPrintEmptyChildren()) {
106-
writer.write('{');
102+
writer.write(" {");
107103
writer.write(printConfig.getNewline());
108104
child.get().writeKDL(writer, depth + 1, printConfig);
109105
for (int i = 0; i < printConfig.getIndent() * depth; i++) {

src/test/java/kdl/RoundTripTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ static List<Arguments> inputs() throws IOException {
6464
private static final Path EXPECTED_FOLDER = Paths.get("src/test/resources/test_cases/expected_kdl");
6565
private static final PrintConfig PRINT_CONFIG = PrintConfig.builder()
6666
.setEscapeLinespace(true)
67+
.setRespectRadix(false)
68+
.setPrintEmptyChildren(false)
6769
.build();
6870

6971
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node "\"\\\b\f\n\r\t"
1+
node "\"\\/\b\f\n\r\t"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node (type)0x10
1+
node (type)16
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node 0b10
1+
node 2
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node 0b10
1+
node 2
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node 0b10
1+
node 2
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
node {
2-
}
1+
node
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
node {
2-
}
1+
node
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
node {
2-
}
1+
node

0 commit comments

Comments
 (0)