Skip to content

Commit

Permalink
chore: update parsing test cases and fix printing
Browse files Browse the repository at this point in the history
  • Loading branch information
alightgoesout committed Mar 16, 2024
1 parent 26c0783 commit c4af17c
Show file tree
Hide file tree
Showing 55 changed files with 80 additions and 75 deletions.
58 changes: 27 additions & 31 deletions src/main/java/kdl/objects/KDLNode.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package kdl.objects;

import kdl.print.PrintConfig;
import kdl.print.PrintUtil;

import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import kdl.print.PrintConfig;

import static kdl.print.PrintUtil.writeStringQuotedAppropriately;

public class KDLNode implements KDLObject {
private final String identifier;
Expand Down Expand Up @@ -69,41 +75,31 @@ public void writeKDL(Writer writer, PrintConfig printConfig) throws IOException
void writeKDLPretty(Writer writer, int depth, PrintConfig printConfig) throws IOException {
if (type.isPresent()) {
writer.write('(');
PrintUtil.writeStringQuotedAppropriately(writer, type.get(), true, printConfig);
writeStringQuotedAppropriately(writer, type.get(), true, printConfig);
writer.write(')');
}

PrintUtil.writeStringQuotedAppropriately(writer, identifier, true, printConfig);
if (!args.isEmpty() || !props.isEmpty() || child.isPresent()) {
writer.write(' ');
}
writeStringQuotedAppropriately(writer, identifier, true, printConfig);

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

final ArrayList<String> keys = new ArrayList<>(props.keySet());
for (int i = 0; i < keys.size(); i++) {
final KDLValue<?> value = props.get(keys.get(i));
if (!(value instanceof KDLNull) || printConfig.shouldPrintNullProps()) {
PrintUtil.writeStringQuotedAppropriately(writer, keys.get(i), true, printConfig);
writer.write('=');
value.writeKDL(writer, printConfig);
if (i < keys.size() - 1 || child.isPresent()) {
writer.write(' ');
}
}
}
for (var entry : props.entrySet()) {
if (!(entry.getValue() instanceof KDLNull) || printConfig.shouldPrintNullProps()) {
writer.write(' ');
writeStringQuotedAppropriately(writer, entry.getKey(), true, printConfig);
writer.write('=');
entry.getValue().writeKDL(writer, printConfig);
}
}

if (child.isPresent()) {
if (!child.get().getNodes().isEmpty() || printConfig.shouldPrintEmptyChildren()) {
writer.write('{');
writer.write(" {");
writer.write(printConfig.getNewline());
child.get().writeKDL(writer, depth + 1, printConfig);
for (int i = 0; i < printConfig.getIndent() * depth; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/kdl/RoundTripTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ static List<Arguments> inputs() throws IOException {
private static final Path EXPECTED_FOLDER = Paths.get("src/test/resources/test_cases/expected_kdl");
private static final PrintConfig PRINT_CONFIG = PrintConfig.builder()
.setEscapeLinespace(true)
.setRespectRadix(false)
.setPrintEmptyChildren(false)
.build();

}
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/expected_kdl/all_escapes.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node "\"\\\b\f\n\r\t"
node "\"\\/\b\f\n\r\t"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node (type)0x10
node (type)16
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/expected_kdl/binary.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0b10
node 2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0b10
node 2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0b10
node 2
3 changes: 1 addition & 2 deletions src/test/resources/test_cases/expected_kdl/empty_child.kdl
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node {
}
node
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node {
}
node
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node {
}
node
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node {
}
node

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node false_id=1
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/expected_kdl/hex.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0xabcdef1234567890
node 12379813812177893520
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/expected_kdl/hex_int.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0xabcdef0123456789abcdef
node 207698809136909011942886895
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0xabcdef0123
node 737894400291
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0x1
node 1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0b1
node 1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0o1
node 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node null_id=1
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/expected_kdl/octal.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0o76543210
node 16434824
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 1 1.0 1.0E+10 1.0E-10 0x1 0o7 0b10 "arg" "arg\\\\" true false null
node 1 1.0 1.0E+10 1.0E-10 1 7 2 "arg" "arg\\\\" true false null
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node key=(type)0x10
node key=(type)16
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
node1 {
}
node1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node arg="correct"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0x123abc
node 1194684
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0o123
node 83
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node true_id=1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node 0o1234567
node 342391
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/input/all_escapes.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node "\"\\\b\f\n\r\t"
node "\"\\\/\b\f\n\r\t"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo123\bar "weeee"
foo123\bar "weeee"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo123{bar}foo "weeee"
foo123{bar}foo "weeee"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo123<bar>foo "weeee"
foo123<bar>foo "weeee"
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/input/comma_in_bare_id.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo123,bar "weeee"
foo123,bar "weeee"
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/input/crlf_between_nodes.kdl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node1
node1
node2
3 changes: 0 additions & 3 deletions src/test/resources/test_cases/input/escline_comment_node.kdl

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
false_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node false_id=1
1 change: 1 addition & 0 deletions src/test/resources/test_cases/input/false_prop_key.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node false=1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node null_id=1
1 change: 1 addition & 0 deletions src/test/resources/test_cases/input/null_prop_key.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node null=1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
// comment
// comment
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/input/parens_in_bare_id.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo123(bar)foo "weeee"
foo123(bar)foo "weeee"
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/input/quote_in_bare_id.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo123"bar "weeee"
foo123"bar "weeee"
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/input/slash_in_bare_id.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo123/bar "weeee"
foo123/bar "weeee"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node arg="correct" /- arg="wrong"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
foo123[bar]foo "weeee"
foo123[bar]foo "weeee"
2 changes: 1 addition & 1 deletion src/test/resources/test_cases/input/trailing_crlf.kdl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node
node
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true_id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node true_id=1
1 change: 1 addition & 0 deletions src/test/resources/test_cases/input/true_prop_key.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node true=1

0 comments on commit c4af17c

Please sign in to comment.