Skip to content

Commit

Permalink
[PlaybackSerialiser] Added PlaybackSerialiser tests
Browse files Browse the repository at this point in the history
- [VirtualInput] Added documentation for createSubtick
  • Loading branch information
ScribbleTAS committed Jun 19, 2024
1 parent 82ed56d commit 144b45f
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void onEnable() {
};

public void onDisable() {

};

public void onRecord(long tick, TickContainer tickContainer) {
Expand Down Expand Up @@ -129,6 +130,15 @@ public PlaybackFileCommandContainer(List<List<PlaybackFileCommand>> list) {
}
}
}

public void add(String key, PlaybackFileCommand fileCommand) {
List<PlaybackFileCommand> toAdd = getOrDefault(key, new ArrayList<>());
if(toAdd.isEmpty()) {
put(key, toAdd);
}

toAdd.add(fileCommand);
}

public PlaybackFileCommandContainer split(String... keys) {
return split(Arrays.asList(keys));
Expand Down Expand Up @@ -167,5 +177,10 @@ public List<List<PlaybackFileCommand>> valuesBySubtick() {

return out;
}

@Override
public boolean equals(Object o) {
return super.equals(o);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ public void unregister(PlaybackFileCommandExtension extension) {
}
}

public void setEnabled(String extensionName, boolean enabled) {
public boolean setEnabled(String extensionName, boolean enabled) {
PlaybackFileCommandExtension extension = REGISTRY.get(extensionName);
if(extension != null) {
extension.setEnabled(enabled);
enabledExtensions = getEnabled();
if(extension == null) {
return false;
}
extension.setEnabled(enabled);
enabledExtensions = getEnabled();
return true;
}

private void disableAll() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,12 @@ public BigArrayList<TickContainer> deserialise(BigArrayList<String> lines, long
BigArrayList<TickContainer> out = new BigArrayList<>();

for (long i = startPos; i < lines.size(); i++) {
List<String> tick = new ArrayList<>();
List<String> container = new ArrayList<>();
// Extract the tick and set the index
i = extractContainer(tick, lines, i);
i = extractContainer(container, lines, i);
currentTick = i;
// Extract container
deserialiseContainer(out, tick);
deserialiseContainer(out, container);
}
previousTickContainer = null;
return out;
Expand Down Expand Up @@ -802,6 +802,8 @@ protected float deserialiseRelativeFloat(String name, String floatstring, Float
protected void splitInputs(List<String> lines, List<String> serialisedKeyboard, List<String> serialisedMouse, List<String> serialisedCameraAngle, List<String> commentsAtEnd, List<List<PlaybackFileCommand>> endlineFileCommands) {

for (String line : lines) {
List<PlaybackFileCommand> deserialisedFileCommands = new ArrayList<>();

Matcher tickMatcher = extract("^\\t?\\d+\\|(.*?)\\|(.*?)\\|(\\S*)\\s?", line);
if (tickMatcher.find()) {
if (!tickMatcher.group(1).isEmpty()) {
Expand All @@ -813,11 +815,10 @@ protected void splitInputs(List<String> lines, List<String> serialisedKeyboard,
if (!tickMatcher.group(3).isEmpty()) {
serialisedCameraAngle.add(tickMatcher.group(3));
}
String endlineComment = line.substring(tickMatcher.group(0).length());
commentsAtEnd.add(deserialiseEndlineComment(endlineComment, deserialisedFileCommands));
}

List<PlaybackFileCommand> deserialisedFileCommands = new ArrayList<>();
String endlineComment = line.substring(tickMatcher.group(0).length());
commentsAtEnd.add(deserialiseEndlineComment(endlineComment, deserialisedFileCommands));

if (deserialisedFileCommands.isEmpty())
deserialisedFileCommands = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.minecrafttas.tasmod.playback.tasfile.flavor;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -57,6 +58,6 @@ public SerialiserFlavorBase getFlavor(String name) {
}

public List<SerialiserFlavorBase> getFlavors() {
return (List<SerialiserFlavorBase>) REGISTRY.values();
return new ArrayList<>(REGISTRY.values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,7 @@ public void updateFromState(int[] keycodes, char[] chars) {
}
}

/**
* Creates a new subtick by {@link #shallowClone()}ing this VirtualKeyboard.<br>
* If {@link Subtickable#ignoreFirstUpdate} is true, no new subtick will be created.<br>
*/
@Override
public void createSubtick() {
if(isParent() && !ignoreFirstUpdate()) {
addSubtick(shallowClone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void updateFromState(int[] keycodes, int scrollwheel, int cursorX, int cu
this.cursorY = cursorY;
}


@Override
public void createSubtick() {
if (isParent() && !ignoreFirstUpdate()) {
addSubtick(shallowClone());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public List<String> getCurrentPresses() {
return out;
}

/**
* Creates a new subtick by {@link #shallowClone() shallowCloning} this VirtualPeripheral.<br>
* If {@link Subtickable#ignoreFirstUpdate} is true, no new subtick will be created.<br>
*/
public void createSubtick() {}

@Override
public String toString() {
return String.join(",", getCurrentPresses());
Expand Down
83 changes: 75 additions & 8 deletions src/test/java/tasmod/playback/tasfile/PlaybackSerialiserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
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.filecommands.PlaybackFileCommand.PlaybackFileCommandContainer;
import com.minecrafttas.tasmod.playback.filecommands.PlaybackFileCommand.PlaybackFileCommandExtension;
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadata;
import com.minecrafttas.tasmod.playback.metadata.PlaybackMetadataRegistry.PlaybackMetadataExtension;
Expand Down Expand Up @@ -73,47 +79,61 @@ public void onLoad(PlaybackMetadata metadata) {

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

}

}

private static class TestExtension extends PlaybackFileCommandExtension {
private static class TestFileCommand extends PlaybackFileCommandExtension {

List<PlaybackFileCommandContainer> inline = new ArrayList<>();

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

@Override
public void onDeserialiseInlineComment(long tick, TickContainer container, PlaybackFileCommandContainer fileCommandContainer) {
inline.add(fileCommandContainer);
}

@Override
public String[] getFileCommandNames() {
return new String[]{"testKey"};
}
}

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();
private static TestFileCommand testFileCommand = new TestFileCommand();

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

@AfterEach
void afterEach() {
testFileCommand.inline.clear();
}

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

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

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

// Keyboard
Expand Down Expand Up @@ -171,6 +191,53 @@ void testSerialiser() {
}
}

@Test
void testDeserialiser() throws PlaybackLoadException, IOException {
List<String> lines = new ArrayList<>();
lines.add("TASFile");
lines.add("FileCommand-Extensions: tasmod_testFileExtension");
lines.add("Flavor: Test");
lines.add("### Test");
lines.add("TestKey: Wat");
lines.add("##################################################");
lines.add("// $testKey(test);");
lines.add("1|W;w|| // test");
lines.add("\t1|W,T;t||");

File file = new File("src/test/resources/serialiser/PlaybackSerialiserTest2.mctas");
try {
FileUtils.writeLines(file, lines);
} catch (IOException e) {
e.printStackTrace();
}

BigArrayList<TickContainer> actual = PlaybackSerialiser2.loadFromFile(file);

BigArrayList<TickContainer> expected = new BigArrayList<>();

VirtualKeyboard keyboard = new VirtualKeyboard();
keyboard.updateFromEvent(VirtualKey.W, true, 'w');
keyboard.updateFromEvent(VirtualKey.T, true, 't');

CommentContainer container = new CommentContainer();
container.addEndlineComment("test");
expected.add(new TickContainer(keyboard, new VirtualMouse(), new VirtualCameraAngle(), container));

assertBigArrayList(expected, actual);

assertEquals("Wat", testMetadata.actual);

List<PlaybackFileCommandContainer> fclist = new ArrayList<>();
PlaybackFileCommandContainer fccontainer = new PlaybackFileCommandContainer();
fccontainer.add("testKey", new PlaybackFileCommand("testKey", "test"));

fclist.add(fccontainer);

assertIterableEquals(fclist, testFileCommand.inline);

file.delete();
}

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

0 comments on commit 144b45f

Please sign in to comment.