Skip to content

Commit 2f8ae1e

Browse files
committed
test: add buildOPPermissions unit tests
1 parent 37904d4 commit 2f8ae1e

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

velocity/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies {
3939
compileOnly("com.github.retrooper:packetevents-velocity:2.11.2")
4040

4141
testImplementation(libs.junit.jupiter)
42+
testImplementation("com.velocitypowered:velocity-api:3.4.0-SNAPSHOT")
4243
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
4344
}
4445

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package dev.objz.commandbridge.velocity.dispatch;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.lang.reflect.Method;
7+
import java.time.Duration;
8+
import java.util.List;
9+
import java.util.Set;
10+
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.Test;
13+
14+
import dev.objz.commandbridge.api.channel.command.RunAs;
15+
import dev.objz.commandbridge.logging.Log;
16+
import dev.objz.commandbridge.scripting.model.Defaults;
17+
import dev.objz.commandbridge.scripting.model.Permissions;
18+
import dev.objz.commandbridge.scripting.model.Script;
19+
import dev.objz.commandbridge.scripting.model.records.mapping.CmdMapping;
20+
21+
class BuildOPPermissionsTest {
22+
23+
@BeforeAll
24+
static void installLog() {
25+
try {
26+
Log.install(java.util.logging.Logger.getLogger("test"));
27+
} catch (IllegalStateException ignored) {
28+
}
29+
}
30+
31+
@Test
32+
void normalCommand() throws Exception {
33+
CommandDispatcher dispatcher = new CommandDispatcher(null, null, null, null);
34+
Script script = new Script(4, "test-script", true, null, null,
35+
new Permissions(true, false), List.of(),
36+
new Defaults(RunAs.CONSOLE, null, null, Duration.ZERO, Duration.ZERO),
37+
List.of(), List.of());
38+
CmdMapping mapping = new CmdMapping("gamemode creative", null, null, null, null, null);
39+
40+
Set<String> result = invokeMethod(dispatcher, script, mapping);
41+
42+
assertEquals(3, result.size());
43+
assertTrue(result.contains("commandbridge.command.test-script"));
44+
assertTrue(result.contains("gamemode"));
45+
assertTrue(result.contains("gamemode.*"));
46+
}
47+
48+
@Test
49+
void commandWithSlash() throws Exception {
50+
CommandDispatcher dispatcher = new CommandDispatcher(null, null, null, null);
51+
Script script = new Script(4, "test-script", true, null, null,
52+
new Permissions(true, false), List.of(),
53+
new Defaults(RunAs.CONSOLE, null, null, Duration.ZERO, Duration.ZERO),
54+
List.of(), List.of());
55+
CmdMapping mapping = new CmdMapping("/gamemode creative", null, null, null, null, null);
56+
57+
Set<String> result = invokeMethod(dispatcher, script, mapping);
58+
59+
assertEquals(3, result.size());
60+
assertTrue(result.contains("commandbridge.command.test-script"));
61+
assertTrue(result.contains("gamemode"));
62+
assertTrue(result.contains("gamemode.*"));
63+
}
64+
65+
@Test
66+
void blankCommand() throws Exception {
67+
CommandDispatcher dispatcher = new CommandDispatcher(null, null, null, null);
68+
Script script = new Script(4, "test-script", true, null, null,
69+
new Permissions(true, false), List.of(),
70+
new Defaults(RunAs.CONSOLE, null, null, Duration.ZERO, Duration.ZERO),
71+
List.of(), List.of());
72+
CmdMapping mapping = new CmdMapping("", null, null, null, null, null);
73+
74+
Set<String> result = invokeMethod(dispatcher, script, mapping);
75+
76+
assertEquals(1, result.size());
77+
assertTrue(result.contains("commandbridge.command.test-script"));
78+
}
79+
80+
@Test
81+
void nullCommand() throws Exception {
82+
CommandDispatcher dispatcher = new CommandDispatcher(null, null, null, null);
83+
Script script = new Script(4, "test-script", true, null, null,
84+
new Permissions(true, false), List.of(),
85+
new Defaults(RunAs.CONSOLE, null, null, Duration.ZERO, Duration.ZERO),
86+
List.of(), List.of());
87+
CmdMapping mapping = new CmdMapping(null, null, null, null, null, null);
88+
89+
Set<String> result = invokeMethod(dispatcher, script, mapping);
90+
91+
assertEquals(1, result.size());
92+
assertTrue(result.contains("commandbridge.command.test-script"));
93+
}
94+
95+
@Test
96+
void whitespaceOnly() throws Exception {
97+
CommandDispatcher dispatcher = new CommandDispatcher(null, null, null, null);
98+
Script script = new Script(4, "test-script", true, null, null,
99+
new Permissions(true, false), List.of(),
100+
new Defaults(RunAs.CONSOLE, null, null, Duration.ZERO, Duration.ZERO),
101+
List.of(), List.of());
102+
CmdMapping mapping = new CmdMapping(" ", null, null, null, null, null);
103+
104+
Set<String> result = invokeMethod(dispatcher, script, mapping);
105+
106+
assertEquals(1, result.size());
107+
assertTrue(result.contains("commandbridge.command.test-script"));
108+
}
109+
110+
@Test
111+
void multipleSpaces() throws Exception {
112+
CommandDispatcher dispatcher = new CommandDispatcher(null, null, null, null);
113+
Script script = new Script(4, "test-script", true, null, null,
114+
new Permissions(true, false), List.of(),
115+
new Defaults(RunAs.CONSOLE, null, null, Duration.ZERO, Duration.ZERO),
116+
List.of(), List.of());
117+
CmdMapping mapping = new CmdMapping("gamemode creative", null, null, null, null, null);
118+
119+
Set<String> result = invokeMethod(dispatcher, script, mapping);
120+
121+
assertEquals(3, result.size());
122+
assertTrue(result.contains("commandbridge.command.test-script"));
123+
assertTrue(result.contains("gamemode"));
124+
assertTrue(result.contains("gamemode.*"));
125+
}
126+
127+
@SuppressWarnings("unchecked")
128+
private static Set<String> invokeMethod(CommandDispatcher dispatcher, Script script,
129+
CmdMapping mapping) throws Exception {
130+
Method method = CommandDispatcher.class.getDeclaredMethod("buildOPPermissions",
131+
Script.class, CmdMapping.class);
132+
method.setAccessible(true);
133+
return (Set<String>) method.invoke(dispatcher, script, mapping);
134+
}
135+
}

0 commit comments

Comments
 (0)