forked from LeetCode-OpenSource/vscode-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
102 lines (96 loc) · 3.8 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (c) jdneo. All rights reserved.
// Licensed under the MIT license.
import * as fse from "fs-extra";
import * as vscode from "vscode";
import { leetCodeExecutor } from "../leetCodeExecutor";
import { leetCodeManager } from "../leetCodeManager";
import { leetCodeResultProvider } from "../leetCodeResultProvider";
import { IQuickItemEx, UserStatus } from "../shared";
import { isWindows, usingCmd } from "../utils/osUtils";
import { DialogType, promptForOpenOutputChannel, showFileSelectDialog } from "../utils/uiUtils";
import { getActiveFilePath } from "../utils/workspaceUtils";
import * as wsl from "../utils/wslUtils";
export async function testSolution(uri?: vscode.Uri): Promise<void> {
try {
if (leetCodeManager.getStatus() === UserStatus.SignedOut) {
return;
}
const filePath: string | undefined = await getActiveFilePath(uri);
if (!filePath) {
return;
}
const picks: Array<IQuickItemEx<string>> = [];
picks.push(
{
label: "$(three-bars) Default test cases",
description: "",
detail: "Test with the default cases",
value: ":default",
},
{
label: "$(pencil) Write directly...",
description: "",
detail: "Write test cases in input box",
value: ":direct",
},
{
label: "$(file-text) Browse...",
description: "",
detail: "Test with the written cases in file",
value: ":file",
},
);
const choice: IQuickItemEx<string> | undefined = await vscode.window.showQuickPick(picks);
if (!choice) {
return;
}
let result: string | undefined;
switch (choice.value) {
case ":default":
result = await leetCodeExecutor.testSolution(filePath);
break;
case ":direct":
const testString: string | undefined = await vscode.window.showInputBox({
prompt: "Enter the test cases.",
validateInput: (s: string): string | undefined => s && s.trim() ? undefined : "Test case must not be empty.",
placeHolder: "Example: [1,2,3]\\n4",
ignoreFocusOut: true,
});
if (testString) {
result = await leetCodeExecutor.testSolution(filePath, parseTestString(testString));
}
break;
case ":file":
const testFile: vscode.Uri[] | undefined = await showFileSelectDialog();
if (testFile && testFile.length) {
const input: string = (await fse.readFile(testFile[0].fsPath, "utf-8")).trim();
if (input) {
result = await leetCodeExecutor.testSolution(filePath, parseTestString(input.replace(/\r?\n/g, "\\n")));
} else {
vscode.window.showErrorMessage("The selected test file must not be empty.");
}
}
break;
default:
break;
}
if (!result) {
return;
}
await leetCodeResultProvider.show(result);
} catch (error) {
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error);
}
}
function parseTestString(test: string): string {
if (wsl.useWsl() || !isWindows()) {
return `'${test}'`;
}
// In windows and not using WSL
if (usingCmd()) {
return `"${test.replace(/"/g, '\\"')}"`;
} else {
// Assume using PowerShell
return `'${test.replace(/"/g, '\\"')}'`;
}
}