test: use test case in inputs.test.ts testing

This commit is contained in:
Alfi Maulana 2024-03-24 23:58:14 +07:00
parent 1ac900e15c
commit 6802dab2a1
No known key found for this signature in database
GPG Key ID: 2242A64C2A8DF5A4

View File

@ -8,88 +8,87 @@ jest.unstable_mockModule("@actions/core", () => ({
}));
describe("get action inputs", () => {
const defaultInputs: Inputs = {
sourceDir: ".",
buildDir: "build",
generator: "",
cCompiler: "",
cxxCompiler: "",
cFlags: "",
cxxFlags: "",
options: [],
args: [],
runBuild: true,
buildArgs: [],
};
interface TestCase {
name: string;
booleanInputs?: { [key: string]: boolean };
stringInputs?: { [key: string]: string };
multilineInputs?: { [key: string]: string[] };
expectedInputs?: Partial<Inputs>;
}
let booleanInputs: { [key: string]: boolean };
let stringInputs: { [key: string]: string };
let multilineInputs: { [key: string]: string[] };
const testCases: TestCase[] = [
{
name: "with nothing specified",
},
{
name: "with all specified",
booleanInputs: {
"run-build": false,
},
stringInputs: {
"source-dir": "project",
"build-dir": "output",
generator: "Ninja",
"c-compiler": "clang",
"cxx-compiler": "clang++",
},
multilineInputs: {
"c-flags": ["-Werror -Wall", "-Wextra"],
"cxx-flags": ["-Werror -Wall", "-Wextra -Wpedantic"],
options: ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev -Wdeprecated", "--fresh"],
"build-args": ["--target foo", "--parallel 8"],
},
expectedInputs: {
sourceDir: "project",
buildDir: "output",
generator: "Ninja",
cCompiler: "clang",
cxxCompiler: "clang++",
cFlags: "-Werror -Wall -Wextra",
cxxFlags: "-Werror -Wall -Wextra -Wpedantic",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev", "-Wdeprecated", "--fresh"],
runBuild: false,
buildArgs: ["--target", "foo", "--parallel", "8"],
},
},
];
beforeEach(async () => {
const core = await import("@actions/core");
for (const testCase of testCases) {
it(`should get the action inputs ${testCase.name}`, async () => {
const { getInputs } = await import("./inputs.js");
const core = await import("@actions/core");
booleanInputs = { "run-build": true };
stringInputs = {};
multilineInputs = {};
const booleanInputs = { "run-build": true, ...testCase.booleanInputs };
jest.mocked(core.getBooleanInput).mockImplementation((name) => {
return booleanInputs[name] ?? false;
});
jest.mocked(core.getBooleanInput).mockImplementation((name) => {
return booleanInputs[name] ?? false;
const stringInputs = { ...testCase.stringInputs };
jest.mocked(core.getInput).mockImplementation((name) => {
return stringInputs[name] ?? "";
});
const multilineInputs = { ...testCase.multilineInputs };
jest.mocked(core.getMultilineInput).mockImplementation((name) => {
return multilineInputs[name] ?? [];
});
expect(getInputs()).toStrictEqual({
sourceDir: ".",
buildDir: "build",
generator: "",
cCompiler: "",
cxxCompiler: "",
cFlags: "",
cxxFlags: "",
options: [],
args: [],
runBuild: true,
buildArgs: [],
...testCase.expectedInputs,
});
});
jest.mocked(core.getInput).mockImplementation((name) => {
return stringInputs[name] ?? "";
});
jest.mocked(core.getMultilineInput).mockImplementation((name) => {
return multilineInputs[name] ?? [];
});
});
it("should get the action inputs with nothing specified", async () => {
const { getInputs } = await import("./inputs.js");
expect(getInputs()).toStrictEqual(defaultInputs);
});
it("should get the action inputs with all specified", async () => {
const { getInputs } = await import("./inputs.js");
booleanInputs = {
...booleanInputs,
"run-build": false,
};
stringInputs = {
...stringInputs,
"source-dir": "project",
"build-dir": "output",
generator: "Ninja",
"c-compiler": "clang",
"cxx-compiler": "clang++",
};
multilineInputs = {
...multilineInputs,
"c-flags": ["-Werror -Wall", "-Wextra"],
"cxx-flags": ["-Werror -Wall", "-Wextra -Wpedantic"],
options: ["BUILD_TESTING=ON BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev -Wdeprecated", "--fresh"],
"build-args": ["--target foo", "--parallel 8"],
};
expect(getInputs()).toStrictEqual({
sourceDir: "project",
buildDir: "output",
generator: "Ninja",
cCompiler: "clang",
cxxCompiler: "clang++",
cFlags: "-Werror -Wall -Wextra",
cxxFlags: "-Werror -Wall -Wextra -Wpedantic",
options: ["BUILD_TESTING=ON", "BUILD_EXAMPLES=ON", "BUILD_DOCS=ON"],
args: ["-Wdev", "-Wdeprecated", "--fresh"],
runBuild: false,
buildArgs: ["--target", "foo", "--parallel", "8"],
});
});
}
});