update testing api

This commit is contained in:
Connor Peet
2021-03-04 11:17:05 -08:00
parent faa545275b
commit 463571f99a
2 changed files with 30 additions and 50 deletions

View File

@ -20,25 +20,10 @@
"commands": [
{
"command": "test-provider-sample.runTests",
"title": "Run Tests",
"title": "Custom Run Tests",
"icon": "$(debug-start)"
}
],
"menus": {
"commandPalette": [
{
"command": "test-provider-sample.runTests",
"when": "false"
}
],
"view/item/context": [
{
"command": "test-provider-sample.runTests",
"when": "view == sampleTestExplorerView && viewItem =~ /runnable/",
"group": "inline"
}
]
},
"views": {
"explorer": [
{
@ -70,4 +55,4 @@
"typescript": "^4.2.2",
"vscode-dts": "^0.3.1"
}
}
}

View File

@ -70,9 +70,9 @@ export class MathTestProvider implements vscode.TestProvider {
if (test instanceof TestCase) {
if (cancellation.isCancellationRequested) {
run.setState(test, { state: vscode.TestRunState.Skipped });
run.setState(test, new vscode.TestState(vscode.TestResult.Skipped));
} else {
run.setState(test, { state: vscode.TestRunState.Running });
run.setState(test, new vscode.TestState(vscode.TestResult.Running));
run.setState(test, await test.run());
}
} else if (test.children) {
@ -110,17 +110,20 @@ type Operator = '+' | '-' | '*' | '/';
const testRe = /^([0-9]+)\s*([+*/-])\s*([0-9]+)\s*=\s*([0-9]+)/;
const headingRe = /^(#+)\s*(.+)$/;
class TestRoot implements vscode.TestItem {
public readonly label = 'Markdown Tests';
public readonly id = 'markdown';
public children = [] as TestFile[];
class TestRoot extends vscode.TestItem {
public children: TestFile[] = [];
constructor() {
super('markdown', 'Markdown Tests');
}
}
class TestFile implements vscode.TestItem {
public readonly label = this.uri.path.split('/').pop()!;
public readonly id = `markdown/${this.uri.toString()}`;
class TestFile extends vscode.TestItem {
public children: (TestHeading | TestCase)[] = [];
constructor(public readonly uri: vscode.Uri) {}
constructor(public readonly uri: vscode.Uri) {
super(`markdown/${uri.toString()}`, uri.path.split('/').pop()!);
}
public async updateTestsFromFs() {
let text: string;
@ -173,48 +176,40 @@ class TestFile implements vscode.TestItem {
}
}
class TestHeading implements vscode.TestItem {
public readonly id = `markdown/${this.location.uri.toString()}/${this.label}`;
class TestHeading extends vscode.TestItem {
public readonly children: (TestHeading | TestCase)[] = [];
constructor(
public readonly level: number,
public readonly label: string,
label: string,
public readonly location: vscode.Location,
) {}
) {
super(`markdown/${location.uri.toString()}/${label}`, label);
}
}
class TestCase implements vscode.TestItem {
public get label() {
return `${this.a} + ${this.b} = ${this.expected}`;
}
public get id() {
return `markdown/${this.location.uri.toString()}/${this.label}`;
}
class TestCase extends vscode.TestItem {
constructor(
private readonly a: number,
private readonly operator: Operator,
private readonly b: number,
private readonly expected: number,
public readonly location: vscode.Location,
) {}
) {
super( `markdown/${location.uri.toString()}/${a} + ${b} = ${expected}`, `${a} + ${b} = ${expected}`);
}
async run(): Promise<vscode.TestState> {
await new Promise(resolve => setTimeout(resolve, 200 + Math.random() * 3000));
const actual = this.evaluate();
if (actual === this.expected) {
return { state: vscode.TestRunState.Passed};
return new vscode.TestState(vscode.TestResult.Passed);
} else {
return { state: vscode.TestRunState.Failed, messages: [
{
message: `Expected ${this.label}`,
expectedOutput: String(this.expected),
actualOutput: String(actual),
location: this.location,
},
]};
const state = new vscode.TestState(vscode.TestResult.Failed);
const message = vscode.TestMessage.diff(`Expected ${this.label}`, String(this.expected), String(actual));
message.location = this.location;
state.messages.push(message);
return state;
}
}