Skip to content

Commit

Permalink
[PlaybackSerialiser] Added tests for failing to extract container
Browse files Browse the repository at this point in the history
  • Loading branch information
ScribbleTAS committed Jun 15, 2024
1 parent ccfd759 commit 82ed56d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,34 +413,36 @@ public BigArrayList<TickContainer> deserialise(BigArrayList<String> lines, long
protected enum ExtractPhases {
/**
* InlineComment phase.
*
* <pre>
* ---
* // This is a comment
* // $fileCommand();
*
* ---
* </pre>
*
* Empty lines also count as comments
*/
COMMENTS,
/**
* Tick phase. Start with a number, then a | character
*
* <pre>
* ---
* 57|W,LCONTROL;w|;0,887,626|17.85;-202.74799
* ---
* </pre>
*
* Only one line should be in this phase
*/
TICK,
/**
* Subtick phase. Start with a tabulator, then a number, then a | character
*
* <pre>
* ---
* 1||RC;0,1580,658|17.85;-202.74799\t\t// This is an endline comment
* 2||;0,1580,658|17.85;-202.74799
* ---
* Can have multiple subticks
* --- 1||RC;0,1580,658|17.85;-202.74799\t\t// This is an endline comment
* 2||;0,1580,658|17.85;-202.74799 --- Can have multiple subticks
*/
SUBTICK,
/**
Expand Down Expand Up @@ -470,31 +472,39 @@ protected enum ExtractPhases {
* 2||;0,1580,658|17.85;-202.74799
* ---------------------
* </pre>
*
* <h2>Logic</h2>
* <ol>
* <li>Phase: None
* <ol>
* <li>If a comment is found, set the phase to comment</li>
* <li>If a tick is found, set the phase to tick</li>
* <li>If a subtick is found, throw an error. Subticks always come after ticks</li>
* </ol></li>
* <li>If a subtick is found, throw an error. Subticks always come after
* ticks</li>
* </ol>
* </li>
* <li>Phase: Comment
* <ol>
* <li>If a tick is found, set the phase to tick</li>
* <li>If a subtick is found, throw an error. Subticks always come after ticks</li>
* </ol></li>
* <li>If a subtick is found, throw an error. Subticks always come after
* ticks</li>
* </ol>
* </li>
* <li>Phase: Tick
* <ol>
* <li>If a subtick is found, set the phase to subticks</li>
* <li>If a tick is found, end the extraction</li>
* <li>If a comment is found, end the extraction</li>
* </ol></li>
* </ol>
* </li>
* <li>Phase: Subtick
* <ol>
* <li>If a tick is found, end the extraction</li>
* <li>If a comment is found, end the extraction</li>
* </ol></li>
* </ol>
* </ol>
* </li>
* </ol>
*
* @param extracted The extracted lines, passed in by reference
* @param lines The line list
* @param startPos The start position of this tick
Expand All @@ -513,40 +523,39 @@ protected long extractContainer(List<String> extracted, BigArrayList<String> lin

switch (phase) {
case NONE:
if (contains(subtickRegex, line)) { // Subtick
throw new PlaybackLoadException(currentTick, currentSubtick, "Error while trying to parse line %s in line %s. This should not be a subtick at this position", line, startPos + counter);
if (contains(subtickRegex, line)) { // Subtick
throw new PlaybackLoadException(currentTick, currentSubtick, "Error while trying to parse the file in line %s. This should not be a subtick at this position", startPos + counter + 1);
}

if (contains(commentRegex, line)||line.isEmpty()) { // Comment
if (contains(commentRegex, line) || line.isEmpty()) { // Comment
phase = ExtractPhases.COMMENTS;
}
else if (contains(tickRegex, line)) { // Tick
} else if (contains(tickRegex, line)) { // Tick
phase = ExtractPhases.TICK;
}

break;
case COMMENTS:
if (contains(subtickRegex, line)) { // Subtick
throw new PlaybackLoadException(currentTick, currentSubtick, "Error while trying to parse line %s in line %s. This should not be a subtick at this position", line, startPos + counter);
if (contains(subtickRegex, line)) { // Subtick
throw new PlaybackLoadException(currentTick, currentSubtick, "Error while trying to parse the file in line %s. This should not be a subtick at this position", startPos + counter + 1);
}

if (contains(tickRegex, line)) {
if (contains(tickRegex, line)) { // Tick
phase = ExtractPhases.TICK;
}

break;
case TICK:
if (contains(subtickRegex, line)) {
if (contains(subtickRegex, line)) { // Subtick
phase = ExtractPhases.SUBTICK;
}

if (contains(commentRegex, line) || contains(tickRegex, line) || line.isEmpty()) {
if (contains(commentRegex, line) || contains(tickRegex, line) || line.isEmpty()) { // Comment
return startPos + counter - 1;
}

break;
case SUBTICK:
if (contains(commentRegex, line) || contains(tickRegex, line) || line.isEmpty()) {
if (contains(commentRegex, line) || contains(tickRegex, line) || line.isEmpty()) { // Comment
return startPos + counter - 1;
}
break;
Expand Down Expand Up @@ -804,8 +813,6 @@ protected void splitInputs(List<String> lines, List<String> serialisedKeyboard,
if (!tickMatcher.group(3).isEmpty()) {
serialisedCameraAngle.add(tickMatcher.group(3));
}
} else {
throw new PlaybackLoadException("Cannot find inputs in line %s", line);
}

List<PlaybackFileCommand> deserialisedFileCommands = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,59 @@ void testExtractTick() {
assertIterableEquals(expected, actual);
assertIterableEquals(expectedIndex, actualIndex);
}

@Test
void testExtractExceptions() {
// Create lines to be extracted from
BigArrayList<String> lines = new BigArrayList<>();
lines.add("\t1||RC;0,1580,658|17.85;-202.74799");
lines.add("55|W,LCONTROL;w|;0,887,626|17.85;-202.74799");
lines.add("\t2||;0,1580,658|17.85;-202.74799");

Throwable t = assertThrows(PlaybackLoadException.class, ()->{
extractContainer(new ArrayList<>(), lines, 0);
});

// C o m p a r e
assertEquals("Tick 0, Subtick 0: Error while trying to parse the file in line 1. This should not be a subtick at this position", t.getMessage());
}

@Test
void testExtractExceptions2() {
// Create lines to be extracted from
BigArrayList<String> lines = new BigArrayList<>();
lines.add("// Comment");
lines.add("\t1||RC;0,1580,658|17.85;-202.74799\t\t// This is an endline comment");
lines.add("57|W,LCONTROL;w|;0,887,626|17.85;-202.74799");
lines.add("\t2||;0,1580,658|17.85;-202.74799");

Throwable t = assertThrows(PlaybackLoadException.class, ()->{
extractContainer(new ArrayList<>(), lines, 0);
});

// C o m p a r e
assertEquals("Tick 0, Subtick 0: Error while trying to parse the file in line 2. This should not be a subtick at this position", t.getMessage());
}

@Test
void testExtractExceptions3() {
// Create lines to be extracted from
BigArrayList<String> lines = new BigArrayList<>();
lines.add("57|W,LCONTROL;w|;0,887,626|17.85;-202.74799");
lines.add("// Comment");
lines.add("\t1||RC;0,1580,658|17.85;-202.74799\t\t// This is an endline comment");
lines.add("\t2||;0,1580,658|17.85;-202.74799");

extractContainer(new ArrayList<>(), lines, 0); // First extraction passes as it parses up to the comment.

Throwable t = assertThrows(PlaybackLoadException.class, ()->{
extractContainer(new ArrayList<>(), lines, 1); // Second extraction fails as it starts with the comment then, a subtick which is disallowed
});

// C o m p a r e
assertEquals("Tick 0, Subtick 0: Error while trying to parse the file in line 3. This should not be a subtick at this position", t.getMessage());
}

/**
* Test deserialising a container a.k.a a tick
*/
Expand Down

0 comments on commit 82ed56d

Please sign in to comment.