Skip to content

Commit

Permalink
[PlaybackSerialiser] Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ScribbleTAS committed May 29, 2024
1 parent 4a46f2a commit 2c3d99e
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.dselent.bigarraylist.BigArrayList;
import com.minecrafttas.tasmod.TASmod;
import com.minecrafttas.tasmod.TASmodClient;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TickInputContainer;
import com.minecrafttas.tasmod.playback.extensions.PlaybackExtension;
Expand Down Expand Up @@ -157,9 +154,13 @@ public static BigArrayList<TickInputContainer> loadFromFile(File file, Serialise

reader.close();

// Deserialise Metadata
List<String> headerLines = flavor.extractHeader(lines);
List<PlaybackMetadata> deserialisedMetadata = flavor.deserialiseMetadata(headerLines);
// Deserialise Header
List<String> headerLines = new ArrayList<>();
List<PlaybackMetadata> deserialisedMetadata = new ArrayList<>();
List<String> deserialisedExtensionNames = new ArrayList<>();

flavor.deserialiseHeader(headerLines, deserialisedMetadata, deserialisedExtensionNames);

TASmodRegistry.PLAYBACK_METADATA.handleOnLoad(deserialisedMetadata);

// Deserialise main data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.tuple.Pair;

import com.dselent.bigarraylist.BigArrayList;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TickInputContainer;
import com.minecrafttas.tasmod.playback.extensions.PlaybackExtension;
Expand Down Expand Up @@ -196,6 +194,10 @@ public boolean deserialiseFlavorName(List<String> headerLines) {
return false;
}

public void deserialiseHeader(List<String> headerLines, List<PlaybackMetadata> metadataList, List<String> activeExtensionList) {
metadataList.addAll(deserialiseMetadata(headerLines));
}

public List<String> extractHeader(BigArrayList<String> lines) {
List<String> extracted = new ArrayList<>();

Expand All @@ -213,51 +215,6 @@ public List<String> extractHeader(BigArrayList<String> lines) {
throw new PlaybackLoadException("Cannot find the end of the header");
}

/**
* Deserialises {@link PlaybackMetadata} in the header of the file.<br>
* <br>
* First extracts the metadata specific lines, then reads the section names and
* key value pairs.
*
* @param headerLines All lines in the header. Can be easily extracted with
* {@link #extractHeader(List)}
* @return A list of {@link PlaybackMetadata}
*/
public List<PlaybackMetadata> deserialiseMetadata(List<String> headerLines) {
List<String> metadataLines = extractMetadata(headerLines);
List<PlaybackMetadata> out = new ArrayList<>();

String metadataName = null;
Pair<String, String> pair = null;
LinkedHashMap<String, String> values = new LinkedHashMap<>();

if (metadataLines.isEmpty())
return new ArrayList<>();

for (String metadataLine : metadataLines) {

String newMetadataName = deserialiseMetadataName(metadataLine);

if (newMetadataName != null) { // Means a new metadata section is beginning... In this case, the metadataLine
// is "### Name" and the newMetadataName is "Name"

if (metadataName != null && !metadataName.equals(newMetadataName)) { // If metadataName is null, then the first section begins
// If metadataName is different than the newMetadataName,
// then a new section begins and we first need to store the old.
out.add(PlaybackMetadata.fromHashMap(metadataName, values));
values = new LinkedHashMap<>();
}
metadataName = newMetadataName;
continue;

} else if ((pair = deseraialiseMetadataValue(metadataLine)) != null) {
values.put(pair.getLeft(), pair.getRight());
}
}
out.add(PlaybackMetadata.fromHashMap(metadataName, values));
return out;
}

public List<String> deserialiseExtensions(List<String> headerLines) {
for (String line : headerLines) {
Matcher matcher = extract("Extensions: ?(.*)", line);
Expand All @@ -271,35 +228,38 @@ public List<String> deserialiseExtensions(List<String> headerLines) {
}
throw new PlaybackLoadException("Extensions value was not found in the header");
}

public List<PlaybackMetadata> deserialiseMetadata(List<String> headerLines) {
List<PlaybackMetadata> out = new ArrayList<>();

protected List<String> extractMetadata(List<String> lines) {
List<String> extracted = new ArrayList<>();
String metadataName = null;
LinkedHashMap<String, String> values = new LinkedHashMap<>();

boolean start = false;
for (String headerLine : headerLines) {

for (String line : lines) {
if (deserialiseMetadataName(line) != null)
start = true;
Matcher nameMatcher = extract("^#{3} (.+)", headerLine); // If the line starts with ###, an optional space char after and then capture the name
Matcher valueMatcher = extract("^([^#].*?):\\s*(.+)", headerLine); // If the line doesn't start with a #, then the key of the metadata, then a : then any or no number of whitespace chars, then the value of the metadata

if (nameMatcher.find()) {

if (line.equals(headerEnd()))
break;
if (metadataName != null && !metadataName.equals(nameMatcher.group(1))) { // If metadataName is null, then the first section begins
// If metadataName is different than the newMetadataName,
// then a new section begins and we first need to store the old.
out.add(PlaybackMetadata.fromHashMap(metadataName, values));
values.clear();
}
metadataName = nameMatcher.group(1);
continue;

if (start)
extracted.add(line);
} else if (metadataName != null && valueMatcher.find()) {
values.put(valueMatcher.group(1), valueMatcher.group(2));
}
}

return extracted;
}

protected String deserialiseMetadataName(String line) {
return extract("^### (.+)", line, 1);
}

protected Pair<String, String> deseraialiseMetadataValue(String metadataLine) {
Matcher matcher = extract("^(.+?):(.+)", metadataLine);
if (matcher.find())
return Pair.of(matcher.group(1).trim(), matcher.group(2).trim());
return null;

if(metadataName!=null)
out.add(PlaybackMetadata.fromHashMap(metadataName, values));

return out;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class TASmodRegistry {
public static final PlaybackExtensionsRegistry PLAYBACK_EXTENSION = new PlaybackExtensionsRegistry();

/**
* Registry for registering custom seialiser flavors that dictate the syntax of the inputs stored in the TASfile.<br>
* Registry for registering custom serialiser flavors that dictate the syntax of the inputs stored in the TASfile.<br>
* <br>
* Either create a new flavor by extending {@link SerialiserFlavorBase}<br>
* or extend an existing flavor (like {@link Beta1Flavor}) and overwrite parts of the methods.<br>
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/tasmod/playback/tasfile/PlaybackSerialiserTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tasmod.playback.tasfile;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;

import java.io.File;
Expand All @@ -10,10 +11,14 @@

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import com.dselent.bigarraylist.BigArrayList;
import com.minecrafttas.tasmod.playback.PlaybackControllerClient.TickInputContainer;
import com.minecrafttas.tasmod.playback.extensions.PlaybackExtension;
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadata;
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadataRegistry.PlaybackMetadataExtension;
import com.minecrafttas.tasmod.playback.tasfile.PlaybackSerialiser2;
import com.minecrafttas.tasmod.playback.tasfile.exception.PlaybackLoadException;
import com.minecrafttas.tasmod.playback.tasfile.flavor.SerialiserFlavorBase;
Expand All @@ -34,24 +39,77 @@ public String flavorName() {

}

private static class TestMetadatada implements PlaybackMetadataExtension {

String testValue = "";
String actual = "e";

@Override
public String getExtensionName() {
return "Test";
}

@Override
public void onCreate() {

}

@Override
public PlaybackMetadata onStore() {
PlaybackMetadata metadata =new PlaybackMetadata(this);
metadata.setValue("TestKey", testValue);
return metadata;
}

@Override
public void onLoad(PlaybackMetadata metadata) {
actual = metadata.getValue("TestKey");
}

@Override
public void onClear() {
// TODO Auto-generated method stub

}

}

private static class TestExtension extends PlaybackExtension {

@Override
public String extensionName() {
return "tasmod_testExtension";
}

}

File file = new File("src/test/resources/serialiser/PlaybackSerialiserTest.mctas");

private static TestFlavor testFlavor = new TestFlavor();
private static TestMetadatada testMetadata = new TestMetadatada();
private static TestExtension testExtension = new TestExtension();

@BeforeAll
static void register() {
TASmodRegistry.SERIALISER_FLAVOR.register(testFlavor);
TASmodRegistry.PLAYBACK_METADATA.register(testMetadata);
TASmodRegistry.PLAYBACK_EXTENSION.register(testExtension);
}

@AfterAll
static void unregister() {
TASmodRegistry.SERIALISER_FLAVOR.unregister(testFlavor);
TASmodRegistry.PLAYBACK_METADATA.unregister(testMetadata);
TASmodRegistry.PLAYBACK_EXTENSION.unregister(testExtension);
}

@Test
@Disabled
void testSerialiser() {
BigArrayList<TickInputContainer> expected = new BigArrayList<>();

testMetadata.testValue = "testing";
TASmodRegistry.PLAYBACK_EXTENSION.setEnabled("tasmod_testExtension", true);
// Tick 1

// Keyboard
Expand Down Expand Up @@ -101,6 +159,7 @@ void testSerialiser() {
try {
BigArrayList<TickInputContainer> actual = PlaybackSerialiser2.loadFromFile(file, testFlavor);
assertBigArrayList(expected, actual);
assertEquals("testing", testMetadata.actual);
} catch (PlaybackLoadException | IOException e) {
e.printStackTrace();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,47 +222,6 @@ void testExtractHeaderFail() {
assertEquals("Cannot find the end of the header", exception.getMessage());
}

/**
* Test extracting only the metadata (### General and below)
*/
@Test
void testExtractMetadata() {
List<String> lines = new ArrayList<>();
lines.add("###### TASfile ######");
lines.add("Flavor: beta");
lines.add("Extensions: desync_monitor, control_bytes, vanilla_commands");
lines.add("### General");
lines.add("Author: Scribble");
lines.add("Title: 77 Buttons");
lines.add("##################################################");

List<String> actual = extractMetadata(lines);

List<String> expected = new ArrayList<>();
expected.add("### General");
expected.add("Author: Scribble");
expected.add("Title: 77 Buttons");

assertIterableEquals(expected, actual);
}

/**
* Test extracting metadata, but no metadata was encoded
*/
@Test
void testExtractEmptyMetadata() {
List<String> lines = new ArrayList<>();
lines.add("###### TASfile ######");
lines.add("Flavor: beta");
lines.add("Extensions: desync_monitor, control_bytes, vanilla_commands");
lines.add("##################################################");

List<String> actual = extractMetadata(lines);

List<String> expected = new ArrayList<>();
assertIterableEquals(expected, actual);
}

/**
* Test deserialising metadata
*/
Expand Down

0 comments on commit 2c3d99e

Please sign in to comment.