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