-
Notifications
You must be signed in to change notification settings - Fork 655
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support test solution * escape the line seperator * update readme
- Loading branch information
Showing
11 changed files
with
144 additions
and
20 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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,86 @@ | ||
"use strict"; | ||
|
||
import * as fse from "fs-extra"; | ||
import * as vscode from "vscode"; | ||
import { leetCodeManager } from "../leetCodeManager"; | ||
import { IQuickItemEx, leetCodeBinaryPath, UserStatus } from "../shared"; | ||
import { executeCommand } from "../utils/cpUtils"; | ||
import { DialogType, promptForOpenOutputChannel, showFileSelectDialog, showResultFile } from "../utils/uiUtils"; | ||
|
||
export async function testSolution(channel: vscode.OutputChannel): Promise<void> { | ||
try { | ||
if (leetCodeManager.getStatus() === UserStatus.SignedOut) { | ||
return; | ||
} | ||
|
||
const activeText: vscode.TextEditor | undefined = vscode.window.activeTextEditor; | ||
if (!activeText) { | ||
vscode.window.showErrorMessage("Please open a LeetCode solution file first."); | ||
return; | ||
} | ||
|
||
const filePath = activeText.document.uri.fsPath; | ||
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 writen 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 executeCommand(channel, "node", [leetCodeBinaryPath, "test", filePath]); | ||
break; | ||
case ":direct": | ||
const testString: string | undefined = await vscode.window.showInputBox({ | ||
prompt: "Enter the test cases.", | ||
validateInput: (s: string) => s && s.trim() ? undefined : "Test case must not be empty.", | ||
placeHolder: "Example: [1,2,3]\\n4", | ||
ignoreFocusOut: true, | ||
}); | ||
if (testString) { | ||
result = await executeCommand(channel, "node", [leetCodeBinaryPath, "test", filePath, "-t", `"${testString.replace(/"/g, "")}"`]); | ||
} | ||
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"); | ||
if (input.trim()) { | ||
result = await executeCommand(channel, "node", [leetCodeBinaryPath, "test", filePath, "-t", `"${input.replace(/"/g, "").replace(/\r?\n/g, "\\n")}"`]); | ||
} else { | ||
vscode.window.showErrorMessage("The selected test file must not be empty."); | ||
} | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
if (!result) { | ||
return; | ||
} | ||
await showResultFile(result); | ||
} catch (error) { | ||
await promptForOpenOutputChannel("Failed to test the solution. Please open the output channel for details.", DialogType.error, channel); | ||
} | ||
} |
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