Skip to content

Commit 39a3f22

Browse files
committed
Added random char function
1 parent e55266c commit 39a3f22

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

package-lock.json

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/index.test.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { run } from "../index";
1+
import { generateRandomCharacters } from "../index";
22
import * as core from "@actions/core";
33
import { mocked } from "jest-mock";
44

@@ -20,18 +20,16 @@ beforeEach(() => {
2020
}));
2121
});
2222

23-
test("run function handles GraphQL error", async () => {
23+
test("function returns random chars", async () => {
2424
// Mock the input values
25-
mockedGetInput.mockReturnValueOnce("{{ github.token }}");
26-
mockedGetInput.mockReturnValueOnce("Test");
27-
mockedGetInput.mockReturnValueOnce("5");
25+
mockedGetInput.mockReturnValueOnce("50");
2826

2927
// Run the `run` function
30-
await run();
28+
await generateRandomCharacters();
3129

3230
// Assertions
33-
expect(mockedGetInput).toHaveBeenCalledTimes(3);
34-
expect(mockedSetOutput).toHaveBeenCalledWith("token", "{{ github.token }}");
35-
expect(mockedSetOutput).toHaveBeenCalledWith("output1", "Test");
36-
expect(mockedSetOutput).toHaveBeenCalledWith("output2", "5");
31+
expect(mockedGetInput).toHaveBeenCalledTimes(1);
32+
expect(mockedGetInput).toHaveBeenCalledWith("input1");
33+
expect(mockedSetOutput).toHaveBeenCalledTimes(1);
34+
expect(mockedSetOutput).toHaveBeenCalledWith("output1", expect.any(String));
3735
});

src/index.ts

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

3-
export async function run() {
4-
const token = getInput("GITHUB_TOKEN");
5-
const input1 = getInput("input1");
6-
const input2 = getInput("input2");
7-
await setOutput("token", token);
8-
await setOutput("output1", input1);
9-
await setOutput("output2", input2);
3+
export async function generateRandomCharacters() {
4+
const numOfCharacters = getInput("input1");
5+
const startRange = 0x0020;
6+
const endRange = 0x007e;
7+
const result: string[] = [];
8+
9+
for (let i = 0; i < Number(numOfCharacters); i++) {
10+
const randomCodePoint = Math.floor(Math.random() * (endRange - startRange + 1)) + startRange;
11+
const randomCharacter = String.fromCodePoint(randomCodePoint);
12+
result.push(randomCharacter);
13+
}
14+
await setOutput("output1", result.join(""));
1015
}
1116

12-
run();
17+
generateRandomCharacters();

0 commit comments

Comments
 (0)