Skip to content

Commit

Permalink
Make test more strict
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass committed Mar 27, 2024
1 parent 75887c8 commit 6fd6615
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
package io.github.syst3ms.skriptparser.variables;

import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;

import io.github.syst3ms.skriptparser.file.FileSection;
import io.github.syst3ms.skriptparser.log.SkriptLogger;
import io.github.syst3ms.skriptparser.types.Type;
import io.github.syst3ms.skriptparser.types.TypeManager;
import io.github.syst3ms.skriptparser.types.changers.TypeSerializer;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class RamStorage extends VariableStorage {

public static final Map<String, SerializedVariable> VARIABLES = new HashMap<>();
public static RamStorage SELF;

public RamStorage(SkriptLogger logger) {
super(logger, "ram");
SELF = this;
}

@Override
Expand All @@ -42,6 +53,21 @@ protected File getFile(String fileName) {
return null;
}

@Override
public Optional<?> deserialize(@NotNull String typeName, @NotNull byte[] value) {
if (value == null || typeName == null)
throw new IllegalArgumentException("value and/or typeName cannot be null");
Type<?> type = TypeManager.getByExactName(typeName).orElse(null);
if (type == null)
throw new UnsupportedOperationException("Class '" + value.getClass().getName() + "' cannot be deserialized. No type registered.");
TypeSerializer<?> serializer = type.getSerializer().orElse(null);
if (serializer == null)
throw new UnsupportedOperationException("Class '" + value.getClass().getName() + "' cannot be deserialized. No type serializer.");
String json = new String(value);
JsonReader reader = gson.newJsonReader(new StringReader(json));
return Optional.ofNullable(serializer.deserialize(gson, JsonParser.parseReader(reader)));
}

@Override
protected boolean save(String name, @Nullable String type, @Nullable byte[] value) {
if (type == null || value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ public void setupVariables() {
@Test
public void testVariables() throws InterruptedException {
Thread.sleep(1);
assert RamStorage.VARIABLES.containsKey("test") : Arrays.toString(RamStorage.VARIABLES.keySet().toArray(String[]::new));
assert RamStorage.VARIABLES.containsKey("test");
Optional<Object> object = Variables.getVariable("test", null, false);
assert object.isPresent();
assert object.get().equals("Hello World!");
Variables.setVariable("test", "Hello New World!", null, false);
Thread.sleep(1);
Optional<Object> newObject = Variables.getVariable("test", null, false);
assert newObject.isPresent();
assert newObject.get().equals("Hello New World!");
assert RamStorage.VARIABLES.containsKey("test");
SerializedVariable variable = RamStorage.VARIABLES.get("test");
Optional<?> value = RamStorage.SELF.deserialize(variable.value.type, variable.value.data);
assert value.isPresent();
assert value.get().equals("Hello New World!") : value.get();
}

}

0 comments on commit 6fd6615

Please sign in to comment.