Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions __tests__/languages/nim.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Nim from '@/runners/nim';
import axiosMock from '../__mocks__/axios';

jest.mock('axios');

test('valid 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....\nHint: gcc -c -w -fmax-errors=3 -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_io.nim.c.o /usercode/nimcache/stdlib_io.nim.c [Exec]\nHint: gcc -c -w -fmax-errors=3 -I/playground/nim/lib -I/usercode -o /usercode/nimcache/stdlib_system.nim.c.o /usercode/nimcache/stdlib_system.nim.c [Exec]\nHint: gcc -c -w -fmax-errors=3 -I/playground/nim/lib -I/usercode -o /usercode/nimcache/@min.nim.c.o /usercode/nimcache/@min.nim.c [Exec]\nHint: [Link]\nHint: 22157 lines; 1.150s; 25.562MiB peakmem; Debug build; proj: /usercode/in.nim; out: /usercode/in [SuccessX]\n",
log: "Hello world\n"
}
};
const mockResponse = Promise.resolve(response);
axiosMock.post.mockResolvedValueOnce(mockResponse);
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'";
const mockResponse = Promise.resolve(response);
axiosMock.post.mockResolvedValueOnce(mockResponse);
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";
const mockResponse = Promise.resolve(response);
axiosMock.post.mockResolvedValueOnce(mockResponse);
const result = await Nim.execute("code");
expect(result).toEqual({ success: false, output: errorResult });
});
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hackbot",
"version": "2.3.1",
"version": "2.4.0",
"description": "Discord bot for the Cascades Tech Club Discord server.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -37,7 +37,7 @@
"glob": "^7.1.3",
"lodash.camelcase": "^4.3.0",
"moment": "^2.22.2",
"ts-node": "^8.1.0",
"ts-node": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"typescript": "^3.1.6"
},
Expand Down
42 changes: 42 additions & 0 deletions src/runners/nim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import IRunner from '@/library/interfaces/iRunner';
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
const success = /\[SuccessX\]/.test(response.compileLog);
const output = success ? response.log : (/Error:.+(?=\n)/.exec(response.compileLog) || [])[0];

return {
success,
output: output || "Nim LanguageRunner encountered an internal error",
};
})
.catch(() => {
return {
success: false,
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,
compilationTarget: "c",
outputFormat: "raw",
version: "latest",
}).then((response: AxiosResponse) => {
return {
compileLog: response.data.compileLog,
log: response.data.log,
};
});
}
};