Skip to content

Commit

Permalink
[PlaybackSerialiser] Added tests for serialisingComments
Browse files Browse the repository at this point in the history
- Rewrote inlineComment deserialisation to better merge filecommands and inline comments
  • Loading branch information
ScribbleTAS committed Jun 13, 2024
1 parent 1643d51 commit df287a5
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ public class PlaybackFileCommand{

private String[] args;

public PlaybackFileCommand(String name) {
this(name,(String[]) null);
}

public PlaybackFileCommand(String name, String... args) {
if(args == null) {
args = new String[] {};
}
this.name = name;
this.args = args;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -140,8 +140,8 @@ protected String serialiseFileCommand(PlaybackFileCommand fileCommand) {
return String.format("$%s(%s);", fileCommand.getName(), String.join(", ", fileCommand.getArgs()));
}

protected String serialiseMultipleFileCommands(List<PlaybackFileCommand> fileCommands) {
if(fileCommands == null) {
protected String serialiseFileCommandsInLine(List<PlaybackFileCommand> fileCommands) {
if (fileCommands == null) {
return null;
}
List<String> serialisedCommands = new ArrayList<>();
Expand Down Expand Up @@ -178,31 +178,48 @@ protected List<String> serialiseCameraAngle(VirtualCameraAngle cameraAngle) {

protected List<String> serialiseInlineComments(List<String> inlineComments, List<List<PlaybackFileCommand>> fileCommandsInline) {
List<String> out = new ArrayList<>();
if (inlineComments == null) {
return out;

Queue<List<PlaybackFileCommand>> fileCommandQueue = null;
if (fileCommandsInline != null) {
fileCommandQueue = new LinkedList<>(fileCommandsInline);
}

Queue<List<PlaybackFileCommand>> fcListQueue = new ConcurrentLinkedQueue<>(fileCommandsInline);
// Serialise comments and merge them with file commands
if (inlineComments != null) {

for (String inlineComment : inlineComments) {
String serialisedFileCommand = serialiseMultipleFileCommands(fcListQueue.poll());
Queue<String> commentQueue = new LinkedList<>(inlineComments);

if(inlineComment == null && serialisedFileCommand == null) {
out.add("");
continue;
// Iterate through comments
while (!commentQueue.isEmpty()) {
String comment = commentQueue.poll(); // Due to commentQueue being a LinkedList, comment can be null at this point!

String command = null;
if (fileCommandQueue != null) {
command = serialiseFileCommandsInLine(fileCommandQueue.poll()); // Trying to poll a fileCommand. Command can be null at this point
}

// Add an empty line if comment and command is null
if (comment == null && command == null) {
out.add("");
continue;
}

out.add(String.format("// %s", joinNotEmpty(" ", command, comment)));
}

inlineComment = inlineComment != null ? inlineComment : "";
serialisedFileCommand = serialisedFileCommand != null ? serialisedFileCommand+" " : "";


out.add(String.format("// %s%s", serialisedFileCommand, inlineComment));
}

while (!fcListQueue.isEmpty()) {
String serialisedFileCommand = serialiseMultipleFileCommands(fcListQueue.poll());
if (serialisedFileCommand != null) {
out.add(String.format("// %s", serialisedFileCommand));
}

if (fileCommandQueue != null) {

// If the fileCommandQueue is not empty or longer than the commentQueue,
// add the rest of the fileCommands to the end
while (!fileCommandQueue.isEmpty()) {

String command = serialiseFileCommandsInLine(fileCommandQueue.poll());
if (command != null) {
out.add(String.format("// %s", command));
} else {
out.add(""); // Add an empty line if command is null
}
}
}

Expand Down Expand Up @@ -246,6 +263,33 @@ protected String getOrEmpty(String string) {
return string == null ? "" : string;
}

/**
* Joins strings together but ignores empty strings
*
* @param delimiter The delimiter of the joined string
* @param args The strings to join
* @return Joined string
*/
protected String joinNotEmpty(String delimiter, Iterable<String> args) {
String out = "";

List<String> copy = new ArrayList<>();

args.forEach((arg) -> {
if (arg != null && !arg.isEmpty()) {
copy.add(arg);
}
});

out = String.join(delimiter, copy);

return out;
}

protected String joinNotEmpty(String delimiter, String... args) {
return joinNotEmpty(delimiter, Arrays.asList(args));
}

/*========================================================
_____ _ _ _
| __ \ (_) | (_)
Expand Down Expand Up @@ -425,7 +469,7 @@ protected void splitContainer(List<String> lines, List<String> comments, List<St
if (contains(singleComment(), line)) {
List<PlaybackFileCommand> deserialisedFileCommand = new ArrayList<>();
comments.add(deserialiseInlineComment(line, deserialisedFileCommand));
if(deserialisedFileCommand.isEmpty()) {
if (deserialisedFileCommand.isEmpty()) {
deserialisedFileCommand = null;
}
inlineFileCommands.add(deserialisedFileCommand);
Expand All @@ -438,9 +482,9 @@ protected void splitContainer(List<String> lines, List<String> comments, List<St
protected String deserialiseInlineComment(String comment, List<PlaybackFileCommand> deserialisedFileCommands) {
comment = deserialiseFileCommands(comment, deserialisedFileCommands);
comment = extract("^// ?(.+)", comment, 1);
if(comment!=null) {
if (comment != null) {
comment = comment.trim();
if(comment.isEmpty()) {
if (comment.isEmpty()) {
comment = null;
}
}
Expand All @@ -459,6 +503,7 @@ protected String deserialiseFileCommands(String comment, List<PlaybackFileComman
deserialisedFileCommands.add(new PlaybackFileCommand(name, args));
comment = matcher.replaceFirst("");
}

return comment;
}

Expand Down
123 changes: 110 additions & 13 deletions src/test/java/tasmod/playback/tasfile/SerialiserFlavorBaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.junit.jupiter.api.Test;

import com.dselent.bigarraylist.BigArrayList;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.CommentContainer;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TickContainer;
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand;
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadata;
Expand Down Expand Up @@ -182,22 +181,103 @@ void testSerialiseContainer() {
// C o m p a r e
assertBigArrayList(expected, actual);
}


/**
* Test serialising inline and endline comments.
*/
@Test
void testSerialiseComments() {
List<String> inlineComments = new ArrayList<>();

inlineComments.add("Test");
inlineComments.add(null);
inlineComments.add(null); // Should result in an empty line
inlineComments.add("Test2");

inlineComments.add(""); // Should result in "// "

List<String> actual = serialiseInlineComments(inlineComments, new ArrayList<>());

List<String> expected = new ArrayList<>();
expected.add("// Test");
expected.add("");
expected.add("// Test2");

expected.add("// ");

assertIterableEquals(expected, actual);

actual = serialiseEndlineComments(inlineComments, null);

assertIterableEquals(expected, actual);

}

@Test
void testSerialiseFileCommands() {
List<List<PlaybackFileCommand>> fileCommands = new ArrayList<>();
List<PlaybackFileCommand> fcInLine = new ArrayList<>();
fcInLine.add(new PlaybackFileCommand("test"));
fcInLine.add(new PlaybackFileCommand("testing2", "true", "false"));

List<PlaybackFileCommand> fcInLine2 = new ArrayList<>();
fcInLine2.add(new PlaybackFileCommand("interpolation", "true"));

fileCommands.add(fcInLine);
fileCommands.add(null);
fileCommands.add(fcInLine2);
fileCommands.add(new ArrayList<>());

List<String> actual = serialiseInlineComments(null, fileCommands);

List<String> expected = new ArrayList<>();
expected.add("// $test(); $testing2(true, false);");
expected.add("");
expected.add("// $interpolation(true);");
expected.add("// ");

assertIterableEquals(expected, actual);
}

@Test
void testMergingCommentsAndCommands() {
List<List<PlaybackFileCommand>> fileCommands = new ArrayList<>();
List<PlaybackFileCommand> fcInLine = new ArrayList<>();
fcInLine.add(new PlaybackFileCommand("test"));
fcInLine.add(new PlaybackFileCommand("testing2", "true", "false"));

fileCommands.add(fcInLine);
fileCommands.add(null);
fileCommands.add(null);
fileCommands.add(new ArrayList<>());

List<PlaybackFileCommand> fcInLine2 = new ArrayList<>();
fcInLine2.add(new PlaybackFileCommand("interpolation", "true"));

fileCommands.add(fcInLine2);

List<PlaybackFileCommand> fcInLine3 = new ArrayList<>();
fcInLine3.add(new PlaybackFileCommand("info", "Scribble"));
fcInLine3.add(new PlaybackFileCommand("info", "Dribble"));

fileCommands.add(fcInLine3);

List<String> inlineComments = new ArrayList<>();

inlineComments.add("Test");
inlineComments.add(null);
inlineComments.add("Test2");
inlineComments.add("");
inlineComments.add(null);

List<String> actual = serialiseInlineComments(inlineComments, fileCommands);

List<String> expected = new ArrayList<>();

expected.add("// $test(); $testing2(true, false); Test"); // Test both filecommand and comment
expected.add(""); // Test null from both
expected.add("// Test2"); // Test comment only
expected.add("// "); // Test empty from both
expected.add("// $interpolation(true);"); // Test command only
expected.add("// $info(Scribble); $info(Dribble);"); // Test command can't be merged with comments and is added at the end instead

assertIterableEquals(expected, actual);
}

Expand Down Expand Up @@ -410,14 +490,12 @@ void testSplitInputs() {
expectedComment.add("Test");
expectedComment.add(null);



expectedFileCommand.add(new ArrayList<>());
expectedFileCommand.add(new ArrayList<>());

List<PlaybackFileCommand> lineCommand = new ArrayList<>();
lineCommand.add(new PlaybackFileCommand("test","true"));
lineCommand.add(new PlaybackFileCommand("test", "true"));

expectedFileCommand.add(lineCommand);

assertIterableEquals(actualKeyboard, expectedKeyboard);
Expand Down Expand Up @@ -453,7 +531,7 @@ void testSplitContainer() {
expectedTicks.add("55|W,LCONTROL;w|;0,887,626|17.85;-202.74799");
expectedTicks.add("\t1||RC;-15,1580,658|11.85;-2.74799");
expectedTicks.add("\t2||;0,1580,658|45;-22.799");

List<List<PlaybackFileCommand>> expectedInlineFileCommands = new ArrayList<>();
List<PlaybackFileCommand> commands = new ArrayList<>();
commands.add(new PlaybackFileCommand("interpolation", "on"));
Expand Down Expand Up @@ -592,6 +670,25 @@ void testCenterHeadingEvenText2() {
assertEquals(expected, actual);
}

@Test
void testJoinNotEmpty() {
String actual = joinNotEmpty(" ", "Test", "", "Weee", "", "Wow");

String expected = "Test Weee Wow";

assertEquals(expected, actual);

List<String> actual2 = new ArrayList<>();
actual2.add("Test");
actual2.add("");
actual2.add("Weee");
actual2.add(null);
actual2.add("Wow");

actual = joinNotEmpty(" ", actual2);
assertEquals(expected, actual);
}

private <T extends Serializable> void assertBigArrayList(BigArrayList<T> expected, BigArrayList<T> actual) {
assertIterableEquals(convertBigArrayListToArrayList(expected), convertBigArrayListToArrayList(actual));
}
Expand Down

0 comments on commit df287a5

Please sign in to comment.