-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from osu-cascades/dev
- Loading branch information
Showing
4 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}); | ||
} | ||
}; |