Skip to content

Commit

Permalink
Edit: quick linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ctsstc committed Oct 21, 2020
1 parent 53e7231 commit 5032f2b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 7 additions & 7 deletions __tests__/languages/nim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@ test('valid code', async () => {
log: "Hello world\n"
}
};
let mockResponse = Promise.resolve(response);
const mockResponse = Promise.resolve(response);
axiosMock.post.mockResolvedValueOnce(mockResponse);
let result = await Nim.execute("code");
const result = await Nim.execute("code");
expect(result).toEqual({ success: true, output: "Hello world\n" });
});

test('invalid code', async () => {
const response = { data: { compileLog: "Hint: used config file '/playground/nim/config/nim.cfg' [Conf]\nHint: used config file '/playground/nim/config/config.nims' [Conf]\n....\n/usercode/in.nim(1, 1) Error: undeclared identifier: 'ejkfladso'\n", log: "\n" } };
const errorResult = "Error: undeclared identifier: 'ejkfladso'";
let mockResponse = Promise.resolve(response);
const mockResponse = Promise.resolve(response);
axiosMock.post.mockResolvedValueOnce(mockResponse);
let result = await Nim.execute("code");
const result = await Nim.execute("code");
expect(result).toEqual({ success: false, output: errorResult });
});

test('invalid response', async () => {
const response = { data: { error: "Server error" } };
const errorResult = "Nim LanguageRunner encountered an internal error";
let mockResponse = Promise.resolve(response);
const mockResponse = Promise.resolve(response);
axiosMock.post.mockResolvedValueOnce(mockResponse);
let result = await Nim.execute("code");
const result = await Nim.execute("code");
expect(result).toEqual({ success: false, output: errorResult });
})
});
8 changes: 3 additions & 5 deletions src/runners/nim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import axios, { AxiosResponse } from 'axios';
let Nim: IRunner;
export default Nim = class {


public static execute(code: string): Promise<{ success: boolean, output: string }> {
return this.runCode(code)
.then((response) => {
//Credit to Bryce G. for this section
// Credit to Bryce G. for this section
const success = /\[SuccessX\]/.test(response.compileLog);
const output = success ? response.log : (/Error:.+(?=\n)/.exec(response.compileLog) || [])[0];

Expand All @@ -23,14 +22,13 @@ export default Nim = class {
output: "Nim LanguageRunner encountered an internal error",
};
});
};

}

// Sends code to the nim playground for execution
private static runCode(code: string): Promise<{ compileLog: string, log: string }> {
const url = "https://play.nim-lang.org/compile";
return axios.post(url, {
code: code,
code,
compilationTarget: "c",
outputFormat: "raw",
version: "latest",
Expand Down

0 comments on commit 5032f2b

Please sign in to comment.