From 6802dab2a17ef9730b5e0a19f049cacddbca6e36 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 24 Mar 2024 23:58:14 +0700 Subject: [PATCH] test: use test case in `inputs.test.ts` testing --- src/inputs.test.ts | 157 ++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 79 deletions(-) diff --git a/src/inputs.test.ts b/src/inputs.test.ts index cd4a06f..869dd0c 100644 --- a/src/inputs.test.ts +++ b/src/inputs.test.ts @@ -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; + } - 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"], - }); - }); + } });