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