Skip to content

Commit 52844f6

Browse files
committed
feat: add test code
1 parent 3cfd3c1 commit 52844f6

12 files changed

Lines changed: 592 additions & 0 deletions
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.theevilreaper.manis.dialog;
2+
3+
import net.kyori.adventure.key.Key;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class DialogRegistryTest extends DialogTemplateBase {
10+
11+
private DialogRegistry registry;
12+
13+
@BeforeEach
14+
void initRegistry() {
15+
registry = DialogRegistry.of();
16+
assertNotNull(registry, "The registry can not be null");
17+
}
18+
19+
@Test
20+
void testTemplateRegistration() {
21+
this.registry.add(testTemplate);
22+
assertTrue(registry.contains(testTemplate.key()));
23+
assertEquals(testTemplate, registry.get(testTemplate.key()));
24+
assertTrue(registry.contains(Key.key("test:test")));
25+
}
26+
27+
@Test
28+
void testTemplateRemoval() {
29+
assertNull(this.registry.remove(Key.key("test:test")));
30+
this.registry.add(testTemplate);
31+
assertNotNull(this.registry.remove(testTemplate.key()));
32+
}
33+
34+
@Test
35+
void testTemplateGetOrDefault() {
36+
assertNotNull(this.registry.get(Key.key("test:test2"), testTemplate));
37+
assertNull(this.registry.get(Key.key("test:test2"), null));
38+
39+
}
40+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.theevilreaper.manis.dialog;
2+
3+
import net.kyori.adventure.key.Key;
4+
import net.kyori.adventure.nbt.CompoundBinaryTag;
5+
import net.kyori.adventure.text.Component;
6+
import net.minestom.server.dialog.DialogAction;
7+
import net.minestom.server.dialog.DialogAfterAction;
8+
import net.minestom.server.item.ItemStack;
9+
import net.minestom.server.item.Material;
10+
import net.theevilreaper.manis.dialog.type.DialogType;
11+
import org.junit.jupiter.api.BeforeAll;
12+
13+
public abstract class DialogTemplateBase {
14+
15+
protected static DialogTemplate testTemplate;
16+
17+
@BeforeAll
18+
static void init() {
19+
testTemplate = DialogType.confirm(Key.key("test:test"))
20+
.meta(metaBuilder ->
21+
metaBuilder.title(Component.text("Test title"))
22+
.afterAction(DialogAfterAction.CLOSE)
23+
.pause(true)
24+
.itemBody(itemTemplate -> itemTemplate.item(ItemStack.of(Material.ACACIA_LEAVES)))
25+
)
26+
.yesButton(actionButton ->
27+
actionButton.label(Component.text("Yes"))
28+
.action(new DialogAction.Custom(Key.key("test:test"), CompoundBinaryTag.empty())))
29+
.noButton(actionButton ->
30+
actionButton.label(Component.text("No"))
31+
.action(new DialogAction.Custom(Key.key("test:test"), CompoundBinaryTag.empty()))
32+
)
33+
.build();
34+
}
35+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.theevilreaper.manis.dialog.action;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.minestom.server.dialog.DialogAction;
5+
import net.minestom.server.dialog.DialogActionButton;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.ValueSource;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
class ActionButtonTest {
13+
14+
@ParameterizedTest(name = "Test invalid width {0}")
15+
@ValueSource(ints = {-1, 0, 1025})
16+
void testInvalidWidthUsage(int value) {
17+
assertThrowsExactly(
18+
IllegalArgumentException.class,
19+
() -> ActionButton.builder().width(value).build(),
20+
"Width must be between 1 and 1024"
21+
);
22+
}
23+
24+
@Test
25+
void testActionButtonCreation() {
26+
DialogActionButton actionButton = ActionButton.builder()
27+
.width(239)
28+
.label(Component.text("Beautiful label"))
29+
.tooltip(Component.text("Nice Tooltip :)"))
30+
.action(new DialogAction.OpenUrl(""))
31+
.build();
32+
33+
assertNotNull(actionButton);
34+
assertEquals(239, actionButton.width());
35+
assertEquals(Component.text("Beautiful label"), actionButton.label());
36+
assertEquals(Component.text("Nice Tooltip :)"), actionButton.tooltip());
37+
assertInstanceOf(DialogAction.OpenUrl.class, actionButton.action());
38+
assertEquals(new DialogAction.OpenUrl(""), actionButton.action());
39+
}
40+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.theevilreaper.manis.dialog.display.component;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.minestom.server.dialog.DialogBody;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.ValueSource;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
11+
class ComponentTemplateTest {
12+
13+
@ParameterizedTest(name = "Test invalid width {0}")
14+
@ValueSource(ints = {-1, 0, 1025})
15+
void testInvalidWidthUsage(int width) {
16+
assertThrowsExactly(
17+
IllegalArgumentException.class,
18+
() -> ComponentTemplate.builder().width(width).build(),
19+
"Width must be between 1 and 1024"
20+
);
21+
}
22+
23+
@Test
24+
void testComponentCreation() {
25+
DialogBody body = ComponentTemplate
26+
.builder()
27+
.width(100)
28+
.contents(Component.text("Test Component"))
29+
.build();
30+
assertNotNull(body, "The body can't be null");
31+
assertInstanceOf(DialogBody.PlainMessage.class, body, "The body must be of type PlainMessage");
32+
33+
DialogBody.PlainMessage message = (DialogBody.PlainMessage) body;
34+
assertEquals(100, message.width());
35+
assertEquals(Component.text("Test Component"), message.contents());
36+
}
37+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package net.theevilreaper.manis.dialog.display.item;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.minestom.server.dialog.DialogBody;
5+
import net.minestom.server.item.ItemStack;
6+
import net.minestom.server.item.Material;
7+
import net.minestom.testing.Env;
8+
import net.minestom.testing.extension.MicrotusExtension;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.extension.ExtendWith;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
@ExtendWith(MicrotusExtension.class)
15+
class ItemTemplateTest {
16+
17+
@Test
18+
void testInvalidWidthAndHeightUsage() {
19+
assertThrowsExactly(
20+
IllegalArgumentException.class,
21+
() -> ItemTemplate.builder().width(-1).build(),
22+
"Width and Height must be greater than 0"
23+
);
24+
assertThrowsExactly(
25+
IllegalArgumentException.class,
26+
() -> ItemTemplate.builder().height(0).build(),
27+
"Width and Height must be greater than 0"
28+
);
29+
}
30+
31+
@Test
32+
void testItemTemplateWithoutAItem() {
33+
assertThrowsExactly(
34+
NullPointerException.class,
35+
() -> ItemTemplate.builder().build(),
36+
"Item must not be null"
37+
);
38+
}
39+
40+
@Test
41+
void testItemTemplateCreation(Env env) {
42+
DialogBody body = ItemTemplate.builder()
43+
.width(10)
44+
.height(20)
45+
.showToolTip(false)
46+
.showDecoration(true)
47+
.description(componentTemplate -> {
48+
componentTemplate.contents(Component.text("Test Item"));
49+
componentTemplate.width(100);
50+
})
51+
.item(ItemStack.builder(Material.ACACIA_BUTTON).build())
52+
.build();
53+
54+
55+
assertNotNull(body, "The body must not be null");
56+
assertInstanceOf(DialogBody.Item.class, body, "The body must be of type Item");
57+
DialogBody.Item template = (DialogBody.Item) body;
58+
59+
assertEquals(10, template.width());
60+
assertEquals(20, template.height());
61+
assertTrue(template.showDecoration());
62+
assertFalse(template.showTooltip());
63+
assertEquals(Material.ACACIA_BUTTON, template.itemStack().material());
64+
65+
assertNotNull(template.description(), "The description must not be null");
66+
67+
DialogBody.PlainMessage message = template.description();
68+
assertEquals(100, message.width());
69+
assertEquals(Component.text("Test Item"), message.contents());
70+
71+
}
72+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package net.theevilreaper.manis.dialog.event;
2+
3+
import net.kyori.adventure.key.Key;
4+
import net.minestom.server.entity.Player;
5+
import net.minestom.server.event.EventFilter;
6+
import net.minestom.server.instance.Instance;
7+
import net.minestom.testing.Collector;
8+
import net.minestom.testing.Env;
9+
import net.minestom.testing.TestConnection;
10+
import net.minestom.testing.extension.MicrotusExtension;
11+
import net.theevilreaper.manis.dialog.DialogTemplateBase;
12+
import org.junit.jupiter.api.Test;
13+
import org.junit.jupiter.api.extension.ExtendWith;
14+
15+
import static org.junit.jupiter.api.Assertions.*;
16+
17+
@ExtendWith(MicrotusExtension.class)
18+
class PlayerOpenDialogEventIntegrationTest extends DialogTemplateBase {
19+
20+
@Test
21+
void testEventLifecycle(Env env) {
22+
Instance instance = env.createFlatInstance();
23+
TestConnection connection = env.createConnection();
24+
Player player = connection.connect(instance);
25+
26+
env.listen(PlayerOpenDialogEvent.class);
27+
Collector<PlayerOpenDialogEvent> eventTracker = env.trackEvent(PlayerOpenDialogEvent.class, EventFilter.PLAYER, player);
28+
29+
testTemplate.open(player);
30+
31+
eventTracker.assertSingle();
32+
eventTracker.assertSingle(event -> {
33+
assertEquals(player, event.getPlayer(), "Player mismatch in event");
34+
assertEquals(Key.key("test:test"), event.getKey(), "Dialog key mismatch in event");
35+
});
36+
env.destroyInstance(instance, true);
37+
}
38+
39+
@Test
40+
void testCancelledEventLifecycle(Env env) {
41+
Instance instance = env.createFlatInstance();
42+
TestConnection connection = env.createConnection();
43+
Player player = connection.connect(instance);
44+
45+
env.process().eventHandler().addListener(PlayerOpenDialogEvent.class, event -> event.setCancelled(true));
46+
47+
env.listen(PlayerOpenDialogEvent.class);
48+
Collector<PlayerOpenDialogEvent> eventTracker = env.trackEvent(PlayerOpenDialogEvent.class, EventFilter.PLAYER, player);
49+
50+
testTemplate.open(player);
51+
52+
eventTracker.assertEmpty();
53+
env.destroyInstance(instance, true);
54+
}
55+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package net.theevilreaper.manis.dialog.input.bool;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.minestom.server.dialog.DialogInput;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class BooleanTemplateTest {
10+
11+
private static final String BOOLEAN_KEY = "boolean_supplier";
12+
13+
@Test
14+
void testBooleanCreationWithDefaults() {
15+
DialogInput booleanInput = BooleanTemplate.builder(BOOLEAN_KEY)
16+
.build();
17+
18+
19+
assertNotNull(booleanInput, "The boolean input can't be null");
20+
assertInstanceOf(DialogInput.Boolean.class, booleanInput, "The boolean input must be of type Boolean");
21+
DialogInput.Boolean booleanInputInstance = (DialogInput.Boolean) booleanInput;
22+
23+
assertFalse(booleanInputInstance.initial());
24+
assertEquals(BOOLEAN_KEY, booleanInputInstance.key());
25+
assertEquals("true", booleanInputInstance.onTrue());
26+
assertEquals("false", booleanInputInstance.onFalse());
27+
}
28+
29+
@Test
30+
void testBooleanCreation() {
31+
DialogInput booleanInput = BooleanTemplate.builder(BOOLEAN_KEY)
32+
.initialValue(true)
33+
.onTrue("true_value")
34+
.onFalse("false_value")
35+
.label(Component.text("Test Label"))
36+
.build();
37+
38+
assertNotNull(booleanInput, "The boolean input can't be null");
39+
assertInstanceOf(DialogInput.Boolean.class, booleanInput, "The boolean input must be of type Boolean");
40+
DialogInput.Boolean booleanInputInstance = (DialogInput.Boolean) booleanInput;
41+
42+
assertTrue(booleanInputInstance.initial());
43+
assertEquals(Component.text("Test Label"), booleanInputInstance.label());
44+
assertEquals(BOOLEAN_KEY, booleanInputInstance.key());
45+
assertEquals("true_value", booleanInputInstance.onTrue());
46+
assertEquals("false_value", booleanInputInstance.onFalse());
47+
}
48+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package net.theevilreaper.manis.dialog.input.option;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.minestom.server.dialog.DialogInput;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class SingleOptionTemplateTest {
10+
11+
private static final String OPTION_KEY = "test";
12+
13+
@Test
14+
void testMissingOptionAtCreation() {
15+
assertThrowsExactly(
16+
IllegalStateException.class,
17+
() -> SingleOptionTemplate.builder(OPTION_KEY).build(),
18+
"No options have been added. Required at least one option."
19+
);
20+
}
21+
22+
@Test
23+
void testSingleOptionCreation() {
24+
DialogInput input = SingleOptionTemplate.builder(OPTION_KEY)
25+
.label(Component.text("Label"))
26+
.labelVisibility(true)
27+
.with(100)
28+
.option(new DialogInput.SingleOption.Option("Option 1", Component.text("Option 1"), true))
29+
.option(new DialogInput.SingleOption.Option("Option 2", Component.text("Option 2"), false))
30+
.build();
31+
32+
assertNotNull(input, "The created input must not be null");
33+
assertInstanceOf(DialogInput.SingleOption.class, input, "The created input must be of type SingleOption");
34+
35+
DialogInput.SingleOption singleOption = (DialogInput.SingleOption) input;
36+
37+
assertEquals("test", singleOption.key());
38+
assertEquals(100, singleOption.width());
39+
assertTrue(singleOption.labelVisible());
40+
assertEquals(Component.text("Label"), singleOption.label());
41+
assertEquals(2, singleOption.options().size());
42+
}
43+
}

0 commit comments

Comments
 (0)