Skip to content

Commit

Permalink
fix(serializer-json): Update tests for format changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Dec 17, 2023
1 parent 78dd046 commit 540653a
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import java.util.function.UnaryOperator;
import net.kyori.adventure.builder.AbstractBuilder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer;
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.adventure.util.Buildable;
import net.kyori.adventure.util.PlatformAPI;
import net.kyori.adventure.util.flag.FeatureSet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {
} else if (KEY_TYPE.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) KeySerializer.INSTANCE;
} else if (STYLE_TYPE.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) StyleSerializer.create(this.legacyHoverSerializer, this.features.value(JSONFlags.EMIT_LEGACY_HOVER_EVENT), gson);
return (TypeAdapter<T>) StyleSerializer.create(this.legacyHoverSerializer, this.features, gson);
} else if (CLICK_ACTION_TYPE.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) ClickEventActionSerializer.INSTANCE;
} else if (HOVER_ACTION_TYPE.isAssignableFrom(rawType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.adventure.util.Codec;
import net.kyori.adventure.util.flag.FeatureSet;
import org.jetbrains.annotations.Nullable;

import static net.kyori.adventure.text.serializer.json.JSONComponentConstants.CLICK_EVENT;
Expand Down Expand Up @@ -80,17 +82,24 @@ final class StyleSerializer extends TypeAdapter<Style> {
}
}

static TypeAdapter<Style> create(final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHover, final boolean emitLegacyHover, final Gson gson) {
return new StyleSerializer(legacyHover, emitLegacyHover, gson).nullSafe();
static TypeAdapter<Style> create(final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHover, final FeatureSet features, final Gson gson) {
return new StyleSerializer(
legacyHover,
features.value(JSONFlags.EMIT_LEGACY_HOVER_EVENT),
features.value(JSONFlags.EMIT_MODERN_HOVER_EVENT),
gson
).nullSafe();
}

private final net.kyori.adventure.text.serializer.json.LegacyHoverEventSerializer legacyHover;
private final boolean emitLegacyHover;
private final boolean emitModernHover;
private final Gson gson;

private StyleSerializer(final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHover, final boolean emitLegacyHover, final Gson gson) {
private StyleSerializer(final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHover, final boolean emitLegacyHover, final boolean emitModernHover, final Gson gson) {
this.legacyHover = legacyHover;
this.emitLegacyHover = emitLegacyHover;
this.emitModernHover = emitModernHover;
this.gson = gson;
}

Expand Down Expand Up @@ -253,13 +262,13 @@ public void write(final JsonWriter out, final Style value) throws IOException {
}

final @Nullable HoverEvent<?> hoverEvent = value.hoverEvent();
if (hoverEvent != null && (hoverEvent.action() != HoverEvent.Action.SHOW_ACHIEVEMENT || this.emitLegacyHover)) {
if (hoverEvent != null && ((this.emitModernHover && hoverEvent.action() != HoverEvent.Action.SHOW_ACHIEVEMENT) || this.emitLegacyHover)) {
out.name(HOVER_EVENT);
out.beginObject();
out.name(HOVER_EVENT_ACTION);
final HoverEvent.Action<?> action = hoverEvent.action();
this.gson.toJson(action, SerializerFactory.HOVER_ACTION_TYPE, out);
if (action != HoverEvent.Action.SHOW_ACHIEVEMENT) { // legacy action has no modern contents value
if (this.emitModernHover && action != HoverEvent.Action.SHOW_ACHIEVEMENT) { // legacy action has no modern contents value
out.name(HOVER_EVENT_CONTENTS);
if (action == HoverEvent.Action.SHOW_ITEM) {
this.gson.toJson(hoverEvent.value(), SerializerFactory.SHOW_ITEM_TYPE, out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
*/
package net.kyori.adventure.text.serializer.json;

import java.util.function.Consumer;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.flag.FeatureSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -47,6 +49,16 @@ final class DummyJSONComponentSerializer implements JSONComponentSerializer {

// A no-op builder that just returns the unsupported instance.
static final class BuilderImpl implements Builder {
@Override
public @NotNull Builder features(final @NotNull FeatureSet flags) {
return this;
}

@Override
public @NotNull Builder editFeatures(final @NotNull Consumer<FeatureSet.Builder> flagEditor) {
return this;
}

@Override
public @NotNull Builder downsampleColors() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void test() {
object.addProperty(JSONComponentConstants.COLOR, "#FF00FF");
object.addProperty(JSONComponentConstants.TEXT, "PREPEND>");
}));
extra.add(object(object -> object.addProperty(JSONComponentConstants.TEXT, "/sign test")));
extra.add("/sign test");
extra.add(object(object -> {
object.addProperty(JSONComponentConstants.COLOR, "#FF00FF");
object.addProperty(JSONComponentConstants.TEXT, "<APPEND");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,46 @@
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import org.junit.jupiter.api.BeforeEach;

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

public abstract class SerializerTest {
private final Gson gson = new Gson();
private final JSONComponentSerializer serializer = JSONComponentSerializer.json();
private JSONComponentSerializer serializer;

@BeforeEach
@SuppressWarnings("checkstyle:MethodName")
void setUpSerializer() {
this.serializer = this.createSerializer();
}

protected JSONComponentSerializer createSerializer() {
return JSONComponentSerializer.json();
}

final JsonElement serialize(final Component component) {
return this.gson.fromJson(this.serializer.serialize(component), JsonElement.class);
return this.serialize(this.serializer, component);
}

final JsonElement serialize(final JSONComponentSerializer cereal, final Component component) {
return this.gson.fromJson(cereal.serialize(component), JsonElement.class);
}

final Component deserialize(final JsonElement json) {
return this.serializer.deserialize(json.toString());
return this.deserialize(this.serializer, json);
}

final Component deserialize(final JSONComponentSerializer cereal, final JsonElement json) {
return cereal.deserialize(json.toString());
}

final void testStyle(final Style style, final Consumer<? super JsonObject> consumer) {
this.testObject(Component.text("", style), object -> {
this.testStyle(this.serializer, style, consumer);
}

final void testStyle(final JSONComponentSerializer serial, final Style style, final Consumer<? super JsonObject> consumer) {
this.testObject(serial, Component.text("", style), object -> {
object.addProperty(JSONComponentConstants.TEXT, "");
consumer.accept(object);
});
Expand All @@ -64,10 +87,14 @@ final void testArray(final Component component, final Consumer<? super JsonArray
}

final void testObject(final Component component, final Consumer<? super JsonObject> consumer) {
this.testObject(this.serializer, component, consumer);
}

final void testObject(final JSONComponentSerializer serializer, final Component component, final Consumer<? super JsonObject> consumer) {
final JsonObject json = object(consumer);

assertEquals(json, this.serialize(component));
assertEquals(component, this.deserialize(json));
assertEquals(json, this.serialize(serializer, component));
assertEquals(component, this.deserialize(serializer, json));
}

static JsonArray array(final Consumer<? super JsonArray> consumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@

import java.util.UUID;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.Style;
import org.junit.jupiter.api.Test;

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

final class ShowEntitySerializerTest extends SerializerTest {
@Test
void testWithStringUuid() {
final UUID id = UUID.randomUUID();
this.testStyle(Style.style().hoverEvent(HoverEvent.showEntity(Key.key("zombie"), id)).build(), json -> {
final JSONComponentSerializer cereal = JSONComponentSerializer.builder()
.editFeatures(b -> b.value(JSONFlags.EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY, false))
.build();

this.testStyle(cereal, Style.style().hoverEvent(HoverEvent.showEntity(Key.key("zombie"), id)).build(), json -> {
json.add(JSONComponentConstants.HOVER_EVENT, object(hover -> {
hover.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, "show_entity");
hover.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(contents -> {
Expand All @@ -50,10 +51,9 @@ void testWithStringUuid() {
@Test
void testWithIntArrayUuid() {
final UUID id = UUID.randomUUID();
assertEquals(
Component.text("", Style.style().hoverEvent(HoverEvent.showEntity(Key.key("zombie"), id)).build()),
this.deserialize(object(comp -> {
comp.addProperty(JSONComponentConstants.TEXT, "");
this.testStyle(
Style.style().hoverEvent(HoverEvent.showEntity(Key.key("zombie"), id)).build(),
comp -> {
comp.add(JSONComponentConstants.HOVER_EVENT, object(hover -> {
hover.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, "show_entity");
hover.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(contents -> {
Expand All @@ -66,8 +66,7 @@ void testWithIntArrayUuid() {
}));
}));
}));
})
)
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

class StyleTest extends SerializerTest {

@Override
protected JSONComponentSerializer createSerializer() {
return JSONComponentSerializer.builder()
.editFeatures(b -> b.value(JSONFlags.EMIT_COMPACT_TEXT_COMPONENT, false))
.build();
}

@Test
void testWithDecorationAsColor() {
final Style s0 = deserialize(object(object -> {
Expand Down Expand Up @@ -139,7 +147,12 @@ void testShowEntityHoverEvent() {
hoverEvent.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, name(HoverEvent.Action.SHOW_ENTITY));
hoverEvent.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(contents -> {
contents.addProperty(JSONComponentConstants.SHOW_ENTITY_TYPE, "minecraft:pig");
contents.addProperty(JSONComponentConstants.SHOW_ENTITY_ID, dolores.toString());
contents.add(JSONComponentConstants.SHOW_ENTITY_ID, array(arr -> {
arr.add(dolores.getMostSignificantBits() >> 32);
arr.add((int) (dolores.getMostSignificantBits() & 0xffffffffl));
arr.add(dolores.getLeastSignificantBits() >> 32);
arr.add((int) (dolores.getLeastSignificantBits() & 0xffffffffl));
}));
contents.add(JSONComponentConstants.SHOW_ENTITY_NAME, object(name -> {
name.addProperty(JSONComponentConstants.TEXT, "Dolores");
name.addProperty(JSONComponentConstants.COLOR, "#0A1AB9");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@
*/
package net.kyori.adventure.text.serializer.json;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import org.junit.jupiter.api.Test;

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

final class TextComponentTest extends SerializerTest {
@Test
void testSimple() {
this.testObject(
Component.text("Hello, world."),
json -> json.addProperty(JSONComponentConstants.TEXT, "Hello, world.")
);
final Component input = Component.text("Hello, world.");
final JsonElement output = new JsonPrimitive("Hello, world.");

assertEquals(output, this.serialize(input));
assertEquals(input, this.deserialize(output));
}

@Test
Expand Down Expand Up @@ -96,10 +101,10 @@ void testComplex2() {
json.addProperty(JSONComponentConstants.COLOR, name(NamedTextColor.DARK_PURPLE));
json.add(JSONComponentConstants.HOVER_EVENT, object(event -> {
event.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, name(HoverEvent.Action.SHOW_TEXT));
event.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(value -> value.addProperty(JSONComponentConstants.TEXT, "A test.")));
event.addProperty(JSONComponentConstants.HOVER_EVENT_CONTENTS, "A test.");
}));
json.add(JSONComponentConstants.EXTRA, array(extra -> {
extra.add(object(item -> item.addProperty(JSONComponentConstants.TEXT, " ")));
extra.add(" ");
extra.add(object(item -> {
item.addProperty(JSONComponentConstants.TEXT, "A what?");
item.addProperty(JSONComponentConstants.COLOR, name(NamedTextColor.DARK_AQUA));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@
final class TranslatableComponentTest extends SerializerTest {
private static final String KEY = "multiplayer.player.left";

@Override
protected JSONComponentSerializer createSerializer() {
return JSONComponentSerializer.builder()
.editFeatures(b -> b.value(JSONFlags.EMIT_COMPACT_TEXT_COMPONENT, false))
.build();
}

@Test
void testNoArgs() {
this.testObject(
Expand Down Expand Up @@ -87,7 +94,12 @@ void testSingleArgWithEvents() {
event.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, name(HoverEvent.Action.SHOW_ENTITY));
event.add(JSONComponentConstants.HOVER_EVENT_CONTENTS, object(value -> {
value.addProperty(JSONComponentConstants.SHOW_ENTITY_TYPE, "minecraft:player");
value.addProperty(JSONComponentConstants.SHOW_ENTITY_ID, id.toString());
value.add(JSONComponentConstants.SHOW_ENTITY_ID, array(arr -> {
arr.add(-351136121);
arr.add(-1961211580);
arr.add(-1118969688);
arr.add(416931810);
}));
value.add(JSONComponentConstants.SHOW_ENTITY_NAME, object(namej -> {
namej.addProperty(JSONComponentConstants.TEXT, name);
}));
Expand Down

0 comments on commit 540653a

Please sign in to comment.