Skip to content

Commit 5559cb4

Browse files
committed
updated tests and index to handle various invalid inputs
1 parent 39a3f22 commit 5559cb4

3 files changed

Lines changed: 69 additions & 20 deletions

File tree

dist/index.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
1616
});
1717
};
1818
Object.defineProperty(exports, "__esModule", ({ value: true }));
19-
exports.run = void 0;
19+
exports.generateRandomCharacters = void 0;
2020
const core_1 = __nccwpck_require__(186);
21-
function run() {
21+
function generateRandomCharacters() {
2222
return __awaiter(this, void 0, void 0, function* () {
23-
const token = (0, core_1.getInput)("GITHUB_TOKEN");
24-
const input1 = (0, core_1.getInput)("input1");
25-
const input2 = (0, core_1.getInput)("input2");
26-
yield (0, core_1.setOutput)("token", token);
27-
yield (0, core_1.setOutput)("output1", input1);
28-
yield (0, core_1.setOutput)("output2", input2);
23+
const numOfCharacters = (0, core_1.getInput)("input1");
24+
const startRange = 0x0020;
25+
const endRange = 0x007E;
26+
const result = [];
27+
for (let i = 0; i < Number(numOfCharacters); i++) {
28+
const randomCodePoint = Math.floor(Math.random() * (endRange - startRange + 1)) + startRange;
29+
const randomCharacter = String.fromCodePoint(randomCodePoint);
30+
result.push(randomCharacter);
31+
}
32+
yield (0, core_1.setOutput)("output1", result.join(''));
2933
});
3034
}
31-
exports.run = run;
32-
run();
35+
exports.generateRandomCharacters = generateRandomCharacters;
36+
generateRandomCharacters();
3337

3438

3539
/***/ }),

src/__tests__/index.test.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,49 @@ beforeEach(() => {
2222

2323
test("function returns random chars", async () => {
2424
// Mock the input values
25-
mockedGetInput.mockReturnValueOnce("50");
25+
const numOfChars = "100";
26+
mockedGetInput.mockReturnValueOnce(numOfChars);
27+
mockedGetInput.mockReturnValueOnce("0x0020");
28+
mockedGetInput.mockReturnValueOnce("0x007e");
2629

2730
// Run the `run` function
2831
await generateRandomCharacters();
2932

3033
// Assertions
31-
expect(mockedGetInput).toHaveBeenCalledTimes(1);
32-
expect(mockedGetInput).toHaveBeenCalledWith("input1");
34+
expect(mockedGetInput).toHaveBeenCalledTimes(3);
35+
expect(mockedGetInput).toHaveBeenCalledWith("numOfChars");
36+
expect(mockedGetInput).toHaveBeenCalledWith("startRange");
37+
expect(mockedGetInput).toHaveBeenCalledWith("endRange");
3338
expect(mockedSetOutput).toHaveBeenCalledTimes(1);
34-
expect(mockedSetOutput).toHaveBeenCalledWith("output1", expect.any(String));
39+
expect(mockedSetOutput).toHaveBeenCalledWith("output", expect.any(String));
40+
const lengthRegex = new RegExp(`^.{${numOfChars}}$`);
41+
expect(mockedSetOutput).toHaveBeenCalledWith("output", expect.stringMatching(lengthRegex));
42+
});
43+
44+
test("should handle numOfChars = 0", async () => {
45+
mockedGetInput.mockReturnValueOnce("0");
46+
await generateRandomCharacters();
47+
expect(mockedSetOutput).toHaveBeenCalledWith("output", "");
48+
});
49+
50+
it("should handle a negative numOfChars", async () => {
51+
mockedGetInput.mockReturnValueOnce("-10");
52+
// Expect an error to be thrown, or handle it appropriately in your code.
53+
await expect(generateRandomCharacters()).rejects.toThrowError("numOfChars must be a positive integer");
54+
});
55+
56+
it("should handle invalid hexadecimal values", async () => {
57+
mockedGetInput.mockReturnValueOnce("100");
58+
mockedGetInput.mockReturnValueOnce("invalidStart");
59+
mockedGetInput.mockReturnValueOnce("invalidEnd");
60+
// Expect an error to be thrown
61+
await expect(generateRandomCharacters()).rejects.toThrowError("Invalid code point NaN");
62+
});
63+
64+
it("should handle startRange > endRange", async () => {
65+
mockedGetInput.mockReturnValueOnce("100");
66+
mockedGetInput.mockReturnValueOnce("0x007e"); // End range is smaller
67+
mockedGetInput.mockReturnValueOnce("0x0020"); // Start range is greater
68+
// Expect an error to be thrown
69+
await expect(generateRandomCharacters()).rejects.toThrowError("startRange must be less than or equal to endRange");
3570
});

src/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import { getInput, setOutput } from "@actions/core";
22

33
export async function generateRandomCharacters() {
4-
const numOfCharacters = getInput("input1");
5-
const startRange = 0x0020;
6-
const endRange = 0x007e;
4+
const numOfChars = getInput("numOfChars");
5+
if (Number(numOfChars) < 0) {
6+
throw new TypeError("numOfChars must be a positive integer");
7+
}
8+
// const startRange = 0x0020;
9+
const startRange = getInput("startRange");
10+
// const endRange = 0x007e;
11+
const endRange = getInput("endRange");
12+
if (Number(startRange) > Number(endRange)) {
13+
throw new TypeError("startRange must be less than or equal to endRange");
14+
}
715
const result: string[] = [];
816

9-
for (let i = 0; i < Number(numOfCharacters); i++) {
10-
const randomCodePoint = Math.floor(Math.random() * (endRange - startRange + 1)) + startRange;
17+
for (let i = 0; i < Number(numOfChars); i++) {
18+
const randomCodePoint =
19+
Math.floor(Math.random() * (Number(endRange) - Number(startRange) + 1)) + Number(startRange);
1120
const randomCharacter = String.fromCodePoint(randomCodePoint);
1221
result.push(randomCharacter);
1322
}
14-
await setOutput("output1", result.join(""));
23+
console.log(result.join(""));
24+
await setOutput("output", result.join(""));
1525
}
1626

1727
generateRandomCharacters();

0 commit comments

Comments
 (0)