Skip to content

Commit bbe6fdb

Browse files
committed
TypeScriptのテストを追加
1 parent 17154f1 commit bbe6fdb

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

app/terminal/editor.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const AceEditor = dynamic(
1414
await import("ace-builds/src-min-noconflict/mode-ruby");
1515
await import("ace-builds/src-min-noconflict/mode-c_cpp");
1616
await import("ace-builds/src-min-noconflict/mode-javascript");
17+
await import("ace-builds/src-min-noconflict/mode-typescript");
1718
await import("ace-builds/src-min-noconflict/mode-json");
1819
await import("ace-builds/src-min-noconflict/mode-csv");
1920
await import("ace-builds/src-min-noconflict/mode-text");
@@ -30,7 +31,15 @@ import { langConstants } from "./runtime";
3031
// snippetを有効化するにはsnippetもimportする必要がある: import "ace-builds/src-min-noconflict/snippets/python";
3132

3233
// mode-xxxx.js のファイル名と、AceEditorの mode プロパティの値が対応する
33-
export type AceLang = "python" | "ruby" | "c_cpp" | "javascript" | "json" | "csv" | "text";
34+
export type AceLang =
35+
| "python"
36+
| "ruby"
37+
| "c_cpp"
38+
| "javascript"
39+
| "typescript"
40+
| "json"
41+
| "csv"
42+
| "text";
3443
export function getAceLang(lang: string | undefined): AceLang {
3544
// Markdownで指定される可能性のある言語名からAceLangを取得
3645
switch (lang) {
@@ -46,6 +55,9 @@ export function getAceLang(lang: string | undefined): AceLang {
4655
case "javascript":
4756
case "js":
4857
return "javascript";
58+
case "typescript":
59+
case "ts":
60+
return "typescript";
4961
case "json":
5062
return "json";
5163
case "csv":

app/terminal/highlight.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ function getPrismLanguage(language: RuntimeLang): PrismLang {
2525
case "javascript":
2626
return "javascript";
2727
case "cpp":
28+
case "typescript":
2829
throw new Error(
2930
`highlight for ${language} is disabled because it should not support REPL`
3031
);

app/terminal/page.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useJSEval } from "./worker/jsEval";
1212
import { ReplTerminal } from "./repl";
1313
import { EditorComponent, getAceLang } from "./editor";
1414
import { ExecFile } from "./exec";
15+
import { useTypeScript } from "./typescript/runtime";
1516

1617
export default function RuntimeTestPage() {
1718
return (
@@ -130,13 +131,15 @@ function RuntimeSample({
130131
function MochaTest() {
131132
const pyodide = usePyodide();
132133
const ruby = useRuby();
133-
const javascript = useJSEval();
134+
const jsEval = useJSEval();
135+
const typescript = useTypeScript(jsEval);
134136
const wandboxCpp = useWandbox("cpp");
135137
const runtimeRef = useRef<Record<RuntimeLang, RuntimeContext>>(null!);
136138
runtimeRef.current = {
137139
python: pyodide,
138140
ruby: ruby,
139-
javascript: javascript,
141+
javascript: jsEval,
142+
typescript: typescript,
140143
cpp: wandboxCpp,
141144
};
142145

app/terminal/runtime.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export function langConstants(lang: RuntimeLang | AceLang): LangConstants {
120120
returnPrefix: "=> ",
121121
};
122122
case "javascript":
123+
case "typescript":
123124
return {
124125
tabSize: 2,
125126
prompt: "> ",

app/terminal/tests.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { emptyMutex, RuntimeContext, RuntimeLang } from "./runtime";
55
export function defineTests(
66
lang: RuntimeLang,
77
runtimeRef: RefObject<Record<RuntimeLang, RuntimeContext>>,
8-
filesRef: RefObject<Readonly<Record<string, string>>>,
8+
filesRef: RefObject<Readonly<Record<string, string>>>
99
) {
1010
describe(`${lang} Runtime`, function () {
1111
this.timeout(
@@ -35,6 +35,7 @@ export function defineTests(
3535
ruby: `puts "${msg}"`,
3636
cpp: null,
3737
javascript: `console.log("${msg}")`,
38+
typescript: null,
3839
} satisfies Record<RuntimeLang, string | null>
3940
)[lang];
4041
if (!printCode) {
@@ -64,6 +65,7 @@ export function defineTests(
6465
`var ${varName} = ${value}`,
6566
`console.log(${varName})`,
6667
],
68+
typescript: [null, null],
6769
} satisfies Record<RuntimeLang, string[] | null[]>
6870
)[lang];
6971
if (!setIntVarCode || !printIntVarCode) {
@@ -92,6 +94,7 @@ export function defineTests(
9294
ruby: `raise "${errorMsg}"`,
9395
cpp: null,
9496
javascript: `throw new Error("${errorMsg}")`,
97+
typescript: null,
9598
} satisfies Record<RuntimeLang, string | null>
9699
)[lang];
97100
if (!errorCode) {
@@ -117,6 +120,7 @@ export function defineTests(
117120
`while(true) {}`,
118121
`console.log(testVar)`,
119122
],
123+
typescript: [null, null, null],
120124
} satisfies Record<RuntimeLang, (string | null)[]>
121125
)[lang];
122126
if (!setIntVarCode || !infLoopCode || !printIntVarCode) {
@@ -163,12 +167,15 @@ export function defineTests(
163167
`#include <iostream>\nint main() {\n std::cout << "${msg}" << std::endl;\n return 0;\n}\n`,
164168
],
165169
javascript: ["test.js", `console.log("${msg}")`],
170+
typescript: ["test.ts", `console.log("${msg}")`],
166171
} satisfies Record<RuntimeLang, [string, string] | [null, null]>
167172
)[lang];
168173
if (!filename || !code) {
169174
this.skip();
170175
}
171-
const result = await runtimeRef.current[lang].runFiles([filename], {[filename]: code});
176+
const result = await runtimeRef.current[lang].runFiles([filename], {
177+
[filename]: code,
178+
});
172179
console.log(`${lang} single file stdout test: `, result);
173180
expect(result).to.be.deep.equal([
174181
{
@@ -189,12 +196,16 @@ export function defineTests(
189196
`#include <stdexcept>\nint main() {\n throw std::runtime_error("${errorMsg}");\n return 0;\n}\n`,
190197
],
191198
javascript: ["test_error.js", `throw new Error("${errorMsg}");\n`],
199+
// TODO: tscが出す型エラーのテストはできていない
200+
typescript: ["test_error.ts", `throw new Error("${errorMsg}");\n`],
192201
} satisfies Record<RuntimeLang, [string, string] | [null, null]>
193202
)[lang];
194203
if (!filename || !code) {
195204
this.skip();
196205
}
197-
const result = await runtimeRef.current[lang].runFiles([filename], {[filename]: code});
206+
const result = await runtimeRef.current[lang].runFiles([filename], {
207+
[filename]: code,
208+
});
198209
console.log(`${lang} single file error capture test: `, result);
199210
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
200211
expect(result.filter((r) => r.message.includes(errorMsg))).to.not.be
@@ -231,6 +242,7 @@ export function defineTests(
231242
["test_multi_main.cpp", "test_multi_sub.cpp"],
232243
],
233244
javascript: [null, null],
245+
typescript: [null, null],
234246
} satisfies Record<
235247
RuntimeLang,
236248
[Record<string, string>, string[]] | [null, null]
@@ -239,7 +251,10 @@ export function defineTests(
239251
if (!codes || !execFiles) {
240252
this.skip();
241253
}
242-
const result = await runtimeRef.current[lang].runFiles(execFiles, codes);
254+
const result = await runtimeRef.current[lang].runFiles(
255+
execFiles,
256+
codes
257+
);
243258
console.log(`${lang} multifile stdout test: `, result);
244259
expect(result).to.be.deep.equal([
245260
{

0 commit comments

Comments
 (0)