Skip to content

Commit 042e664

Browse files
committed
test: Add tests
1 parent 8497cf0 commit 042e664

12 files changed

+333
-28
lines changed

dfu/src/test/java/net/kyori/adventure/nbt/dfu/AdventureCodecTest.java

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package net.kyori.adventure.nbt.dfu;
2525

2626
import com.google.gson.JsonElement;
27+
import com.mojang.serialization.Codec;
2728
import com.mojang.serialization.DataResult;
2829
import com.mojang.serialization.JsonOps;
2930
import java.util.HashMap;
@@ -45,7 +46,7 @@
4546
import static org.junit.jupiter.api.Assertions.assertEquals;
4647

4748
public class AdventureCodecTest {
48-
private <T> T valueOrThrow(final DataResult<T> result) {
49+
protected static <T> T valueOrThrow(final DataResult<T> result) {
4950
result.error().ifPresent(err -> {
5051
throw new RuntimeException("Error with data result: " + err.message());
5152
});
@@ -55,70 +56,58 @@ private <T> T valueOrThrow(final DataResult<T> result) {
5556

5657
@Test
5758
void testComponent() {
58-
this.assertComponentCodec(
59+
assertComponentCodec(
5960
Component.text("hello", NamedTextColor.RED)
6061
.clickEvent(ClickEvent.suggestCommand("/dfu"))
6162
.hoverEvent(Component.text("hover"))
6263
.decorate(TextDecoration.BOLD)
6364
);
6465

65-
this.assertComponentCodec(
66+
assertComponentCodec(
6667
Component.text("test", NamedTextColor.RED)
6768
.hoverEvent(HoverEvent.showItem(HoverEvent.ShowItem.showItem(Key.key("diamond"), 1, BinaryTagHolder.binaryTagHolder("{display: {Lore: ['Test']}}"))))
6869
.shadowColor(ShadowColor.none())
6970
);
7071

71-
this.assertComponentCodec(
72+
assertComponentCodec(
7273
Component.translatable("trans.key", TranslationArgument.numeric(1), TranslationArgument.component(Component.text("test")))
7374
.clickEvent(ClickEvent.openUrl("https://example.com"))
7475
);
7576

76-
this.assertComponentCodec(
77+
assertComponentCodec(
7778
Component.blockNBT("pos", BlockNBTComponent.Pos.fromString("1 2 3"))
7879
);
79-
this.assertComponentCodec(
80+
assertComponentCodec(
8081
Component.blockNBT("pos", BlockNBTComponent.Pos.fromString("^1 ^2 ^3"))
8182
);
8283

83-
this.assertComponentCodec(
84+
assertComponentCodec(
8485
Component.entityNBT("entities", "@e")
8586
);
8687

87-
this.assertComponentCodec(
88+
assertComponentCodec(
8889
Component.text("Press ", NamedTextColor.GRAY)
8990
.append(Component.keybind("key.jump", NamedTextColor.RED))
9091
.append(Component.text(" to jump", NamedTextColor.GRAY))
9192
);
9293

9394
final Map<Key, DataComponentValue> dataComponentValueMap = new HashMap<>();
9495
dataComponentValueMap.put(Key.key("name"), BinaryTagHolder.binaryTagHolder("{'text':'Example'}"));
95-
this.assertComponentCodec(
96+
assertComponentCodec(
9697
Component.text("Example")
9798
.hoverEvent(HoverEvent.showItem(Key.key("diamond"), 1, dataComponentValueMap))
9899
);
99100
}
100101

101-
private void assertComponentCodec(final Component component) {
102-
final JsonElement jsonElement = this.valueOrThrow(AdventureCodecs.COMPONENT.encodeStart(JsonOps.INSTANCE, component));
103-
final Component decode = this.valueOrThrow(AdventureCodecs.COMPONENT.decode(JsonOps.INSTANCE, jsonElement)).getFirst();
102+
protected static void assertComponentCodec(final Component component) {
103+
final JsonElement jsonElement = valueOrThrow(AdventureCodecs.COMPONENT.encodeStart(JsonOps.INSTANCE, component));
104+
final Component decode = valueOrThrow(AdventureCodecs.COMPONENT.decode(JsonOps.INSTANCE, jsonElement)).getFirst();
104105
assertEquals(component, decode);
105106
}
106107

107-
@Test
108-
void testTranslationArgument() {
109-
final TranslationArgument bool = TranslationArgument.bool(true);
110-
JsonElement jsonElement = this.valueOrThrow(AdventureCodecs.TRANSLATION_ARGUMENT.encodeStart(JsonOps.INSTANCE, bool));
111-
TranslationArgument valued = this.valueOrThrow(AdventureCodecs.TRANSLATION_ARGUMENT.decode(JsonOps.INSTANCE, jsonElement)).getFirst();
112-
assertEquals(bool, valued);
113-
114-
final TranslationArgument numeric = TranslationArgument.numeric(1);
115-
jsonElement = this.valueOrThrow(AdventureCodecs.TRANSLATION_ARGUMENT.encodeStart(JsonOps.INSTANCE, numeric));
116-
valued = this.valueOrThrow(AdventureCodecs.TRANSLATION_ARGUMENT.decode(JsonOps.INSTANCE, jsonElement)).getFirst();
117-
assertEquals(numeric, valued);
118-
119-
final TranslationArgument component = TranslationArgument.component(Component.text("hello"));
120-
jsonElement = this.valueOrThrow(AdventureCodecs.TRANSLATION_ARGUMENT.encodeStart(JsonOps.INSTANCE, component));
121-
valued = this.valueOrThrow(AdventureCodecs.TRANSLATION_ARGUMENT.decode(JsonOps.INSTANCE, jsonElement)).getFirst();
122-
assertEquals(component, valued);
108+
protected static <A> void assertCodec(final Codec<A> codec, final A value) {
109+
final JsonElement jsonElement = valueOrThrow(codec.encodeStart(JsonOps.INSTANCE, value));
110+
final A decode = valueOrThrow(codec.decode(JsonOps.INSTANCE, jsonElement)).getFirst();
111+
assertEquals(value, decode);
123112
}
124113
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class BlockNBTComponentCodecTest extends AdventureCodecTest {
7+
@Test
8+
void test() {
9+
assertComponentCodec(
10+
Component.blockNBT()
11+
.nbtPath("test")
12+
.absoluteWorldPos(1, 2, 3)
13+
.build()
14+
);
15+
assertComponentCodec(
16+
Component.blockNBT()
17+
.nbtPath("test")
18+
.localPos(1, 2, 3)
19+
.build()
20+
);
21+
assertComponentCodec(
22+
Component.blockNBT()
23+
.nbtPath("test")
24+
.relativeWorldPos(1, 2, 3)
25+
.build()
26+
);
27+
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class EntityNBTComponentCodecTest extends AdventureCodecTest {
7+
@Test
8+
void test() {
9+
assertComponentCodec(
10+
Component.entityNBT().nbtPath("abc").selector("test").build()
11+
);
12+
assertComponentCodec(
13+
Component.entityNBT().nbtPath("abc").selector("test").interpret(true).build()
14+
);
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class KeybindComponentCodecTest extends AdventureCodecTest {
7+
@Test
8+
void test() {
9+
assertComponentCodec(
10+
Component.keybind("key.jump")
11+
);
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class ScoreComponentCodecTest extends AdventureCodecTest {
7+
@Test
8+
void test() {
9+
assertComponentCodec(
10+
Component.score("test", "obj")
11+
);
12+
assertComponentCodec(
13+
Component.score("test", "obj", "value")
14+
);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import net.kyori.adventure.text.Component;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class SelectorComponentCodecTest extends AdventureCodecTest {
7+
@Test
8+
void test() {
9+
assertComponentCodec(
10+
Component.selector("selector")
11+
);
12+
assertComponentCodec(
13+
Component.selector("selector", Component.text(","))
14+
);
15+
}
16+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import java.util.UUID;
4+
import net.kyori.adventure.dfu.AdventureCodecs;
5+
import net.kyori.adventure.key.Key;
6+
import net.kyori.adventure.text.Component;
7+
import net.kyori.adventure.text.event.HoverEvent;
8+
import org.junit.jupiter.api.Test;
9+
10+
public class ShowEntityCodecTest extends AdventureCodecTest {
11+
@Test
12+
void test() {
13+
assertCodec(
14+
AdventureCodecs.SHOW_ENTITY,
15+
HoverEvent.ShowEntity.showEntity(Key.key("minecraft:player"), UUID.randomUUID())
16+
);
17+
assertCodec(
18+
AdventureCodecs.SHOW_ENTITY,
19+
HoverEvent.ShowEntity.showEntity(Key.key("minecraft:player"), UUID.randomUUID(), Component.text("name"))
20+
);
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import net.kyori.adventure.dfu.AdventureCodecs;
6+
import net.kyori.adventure.key.Key;
7+
import net.kyori.adventure.nbt.api.BinaryTagHolder;
8+
import net.kyori.adventure.text.event.DataComponentValue;
9+
import net.kyori.adventure.text.event.HoverEvent;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class ShowItemCodecTest extends AdventureCodecTest {
13+
@Test
14+
void test() {
15+
assertCodec(
16+
AdventureCodecs.SHOW_ITEM,
17+
HoverEvent.ShowItem.showItem(
18+
Key.key("stone"), 1
19+
)
20+
);
21+
assertCodec(
22+
AdventureCodecs.SHOW_ITEM,
23+
HoverEvent.ShowItem.showItem(
24+
Key.key("stone"), 1,
25+
BinaryTagHolder.binaryTagHolder("{inValidPath: '123'}")
26+
)
27+
);
28+
Map<Key, DataComponentValue> componentValueMap = new HashMap<>();
29+
componentValueMap.put(Key.key("in_valid_path"), BinaryTagHolder.binaryTagHolder("123"));
30+
assertCodec(
31+
AdventureCodecs.SHOW_ITEM,
32+
HoverEvent.ShowItem.showItem(
33+
Key.key("stone"), 1,
34+
componentValueMap
35+
)
36+
);
37+
}
38+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import net.kyori.adventure.key.Key;
4+
import net.kyori.adventure.text.Component;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class StorgeNBTComponentCodecTest extends AdventureCodecTest {
8+
@Test
9+
void test() {
10+
assertComponentCodec(
11+
Component.storageNBT().nbtPath("abc").storage(Key.key("doom:apple")).build()
12+
);
13+
assertComponentCodec(
14+
Component.storageNBT().nbtPath("abc").storage(Key.key("doom:apple")).interpret(true).build()
15+
);
16+
}
17+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package net.kyori.adventure.nbt.dfu;
2+
3+
import com.google.gson.JsonElement;
4+
import com.mojang.serialization.JsonOps;
5+
import net.kyori.adventure.dfu.AdventureCodecs;
6+
import net.kyori.adventure.key.Key;
7+
import net.kyori.adventure.text.Component;
8+
import net.kyori.adventure.text.event.ClickEvent;
9+
import net.kyori.adventure.text.format.NamedTextColor;
10+
import net.kyori.adventure.text.format.ShadowColor;
11+
import net.kyori.adventure.text.format.Style;
12+
import net.kyori.adventure.text.format.TextDecoration;
13+
import org.junit.jupiter.api.Test;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
17+
public class StyleCodecTest extends AdventureCodecTest {
18+
private static void assertStyleCodec(final Style style) {
19+
final JsonElement jsonElement = valueOrThrow(AdventureCodecs.STYLE.encodeStart(JsonOps.INSTANCE, style));
20+
final Style decode = valueOrThrow(AdventureCodecs.STYLE.decode(JsonOps.INSTANCE, jsonElement)).getFirst();
21+
assertEquals(style, decode);
22+
}
23+
24+
@Test
25+
void test() {
26+
assertStyleCodec(
27+
Style.style()
28+
.hoverEvent(Component.text("Hover Text"))
29+
.clickEvent(ClickEvent.changePage(10))
30+
.insertion("abc")
31+
.color(NamedTextColor.GREEN)
32+
.shadowColor(ShadowColor.none())
33+
.decoration(TextDecoration.BOLD, true)
34+
.font(Key.key("default"))
35+
.build()
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)