Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sisilicon
GitHub Repository: sisilicon/worldedit-be
Path: blob/master/src/gametest/tests/commandSyntaxTest.ts
1784 views
1
import { registerAsync } from "@minecraft/server-gametest";
2
import { Server, Vector } from "@notbeer-api";
3
import { spawnWorldEditPlayer } from "gametest/utils";
4
import { commandArgList } from "library/@types/classes/CommandBuilder";
5
6
function getTestValue(type: string) {
7
type = type?.replace("...", "");
8
switch (type) {
9
case "int":
10
return ["1"];
11
case "float":
12
return ["1.0"];
13
case "string":
14
return ["test"];
15
case "xyz":
16
return ["0", "0", "0"];
17
case "xz":
18
return ["0", "0"];
19
case "Mask":
20
return ["stone"];
21
case "Pattern":
22
return ["stone"];
23
case "CommandName":
24
return ["worldedit"];
25
case "Direction":
26
return ["up"];
27
case "Expression":
28
return ["x>y"];
29
case "Biome":
30
return ["plains"];
31
case "Easing":
32
return ["in_out_circ"];
33
default:
34
if (type) throw "Unknown type for command syntax test: " + type;
35
return [];
36
}
37
}
38
39
function getPermutations(usage: commandArgList, index = 0): string[][] {
40
if (!usage[index]) return [[]];
41
42
const arg = usage[index];
43
const nextPerms = getPermutations(usage, index + 1);
44
45
if ("flag" in arg) {
46
return [...nextPerms, ...nextPerms.map((perm) => [`-${arg.flag}`, ...getTestValue(arg.type), ...perm])];
47
} else if ("subName" in arg) {
48
const subName = arg.subName.startsWith("_") ? [] : [arg.subName];
49
const subPerms = getPermutations(arg.args ?? []);
50
return [...subPerms.map((perm) => [...subName, ...perm]), ...nextPerms].filter((perm) => perm.length);
51
} else if (arg.default !== undefined) {
52
return [[], ...nextPerms.map((perm) => [...getTestValue(arg.type), ...perm])];
53
} else {
54
const value = "range" in arg ? [`${arg.range[0] ?? arg.range[1]}`] : getTestValue(arg.type);
55
return nextPerms.map((perm) => [...value, ...perm]);
56
}
57
}
58
59
registerAsync("worldedit", "commandSyntaxTest", async (test) => {
60
const player = await spawnWorldEditPlayer(test, Vector.ZERO, "commandSyntaxTestPlayer");
61
const errors = [];
62
for (const command of Server.command.getAllRegistration()) {
63
const permutations = getPermutations(command.usage ?? []);
64
for (const args of permutations) {
65
try {
66
Server.command.callCommand(player, command.name, args.join(" "), { noCallback: true });
67
} catch (e) {
68
errors.push(`Command "${command.name}" with args "${command.name} ${args.join(" ")}" failed: ${e}`);
69
}
70
}
71
}
72
73
if (errors.length) test.fail(errors.join("\n"));
74
else test.succeed();
75
})
76
.maxTicks(20)
77
.structureName("worldedit:scripts");
78
79