Skip to content

Commit

Permalink
test: switch to midori, add and fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxuum committed Dec 1, 2023
1 parent 01fa40a commit a24f654
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 59 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"typescript": "^5.2.2"
},
"dependencies": {
"@rbxts/midori": "^0.1.3",
"@rbxts/pretty-react-hooks": "^0.3.3",
"@rbxts/react-reflex": "^0.2.0",
"@rbxts/react-roblox": "^0.2.0",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions src/shared/core/path.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { TestProps } from "@rbxts/midori";
import { CommandPath } from "./path";

export = (x: TestProps) => {
x.test("create a path from a string", () => {
const testString = "root/part1/part2";
const path = CommandPath.fromString(testString);
x.assertEqual(path.toString(), testString);
});

x.nested("operations", () => {
x.test("append parts to the path", () => {
const path = CommandPath.fromString("root/part1/part2");
path.append("part3");
x.assertEqual(path.getPart(path.getSize() - 1), "part3");

path.append("part4");
x.assertEqual(path.getPart(path.getSize() - 1), "part4");
});

x.test("remove parts from the path", () => {
const path = CommandPath.fromString("root/part1/part2");
path.remove(0);
x.assertEqual(path.getPart(0), "part1");

path.remove(path.getSize() - 1);
x.assertEqual(path.getPart(path.getSize() - 1), "part1");
});

x.test("remove all parts from the path", () => {
const path = CommandPath.fromString("root/part1/part");
path.clear();
x.assertEqual(path.getSize(), 0);
});

x.test("return a slice of the path", () => {
let path = CommandPath.fromString("root/part1/part2/part3");
path = path.slice(1, 2);
x.assertEqual(path.toString(), "part1/part2");
});
});

x.nested("descendant operations", () => {
x.test("check if a path is a child of another", () => {
const path = CommandPath.fromString("root/part1/part2");
const path2 = CommandPath.fromString("root/part1/part2/part3");
x.assertEqual(path2.isChildOf(path), true);
x.assertEqual(path.isChildOf(path2), false);
});

x.test("check if a path is a descendant of another", () => {
const path = CommandPath.fromString("root/part1/part2");
const path2 = CommandPath.fromString("root/part1/part2/part3/part4");
x.assertEqual(path2.isDescendantOf(path), true);
x.assertEqual(path.isDescendantOf(path2), false);
});
});
};
59 changes: 0 additions & 59 deletions src/shared/test/path.ts

This file was deleted.

38 changes: 38 additions & 0 deletions src/shared/util/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { TestProps } from "@rbxts/midori";
import { endsWithSpace, formatPartsAsPath, splitStringBySpace } from "./string";

export = (x: TestProps) => {
x.nested("split string", () => {
x.test("splits one space", () => {
const testString = "part1 part2 part3";
const parts = splitStringBySpace(testString);
x.assertEqual(parts.size(), 3);
});

x.test("splits multiple spaces", () => {
const testString = "part1 part2 part3 part4 ";
const parts = splitStringBySpace(testString);
x.assertEqual(parts.size(), 4);
});

x.test("takes into account quoted sentences", () => {
const testString = `part1 "part2 but quoted" part3 'part4' "part5"`;
const parts = splitStringBySpace(testString);
x.assertEqual(parts.size(), 5);
x.assertEqual(parts[1], "part2 but quoted");
x.assertEqual(parts[3], "part4");
x.assertEqual(parts[4], "part5");
});
});

x.test("determines if a string ends in a space", () => {
x.assertEqual(endsWithSpace("test"), false);
x.assertEqual(endsWithSpace("test "), true);
});

x.test("formats string array as a path string", () => {
const pathString = "part1/part2/part3";
const parts = pathString.split("/");
x.assertEqual(formatPartsAsPath(parts), pathString);
});
};
17 changes: 17 additions & 0 deletions test/src/server/main.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { CommanderServer } from "@rbxts/commander";
import { runTests } from "@rbxts/midori";

// Run unit tests
const getInstanceFromPath = (parts: string[]) => {
let last: Instance = game;
for (const part of parts) {
const partInstance = last.FindFirstChild(part);
if (partInstance === undefined) return undefined;
last = partInstance;
}
return last;
};

const [root, parts] = $getModuleTree("@rbxts/commander");
const srcPath = getInstanceFromPath([root.ClassName, ...parts]);
if (srcPath !== undefined) runTests(srcPath);

// Start commander
CommanderServer.start((registry) => {
const commandContainer = script.Parent!.WaitForChild("commands");
registry.registerCommandsIn(commandContainer);
Expand Down

0 comments on commit a24f654

Please sign in to comment.