Skip to content

Commit eb7bc03

Browse files
authored
Add tests generated by LLM to improve coverage (#1195)
1 parent 48c3cbb commit eb7bc03

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export interface Result {
2+
value: number;
3+
message: string;
4+
}
5+
6+
export class Calculator {
7+
private result: number;
8+
9+
constructor(initial: number = 0) {
10+
this.result = initial;
11+
}
12+
13+
public add(x: number): Calculator {
14+
this.result += x;
15+
return this;
16+
}
17+
18+
public getResult(): Result {
19+
return {
20+
value: this.result,
21+
message: `Result is ${this.result}`
22+
};
23+
}
24+
}
25+
26+
export function createCalculator(initial?: number): Calculator {
27+
return new Calculator(initial);
28+
}
29+
30+
export function getValue(): number {
31+
return 42;
32+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createCalculator } from './f1';
2+
3+
function test(): void {
4+
createCalculator().
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Calculator } from './f1';
2+
3+
function test(): void {
4+
const calc = new Calculator(10);
5+
calc.add(5).getResult();
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Calculator } from './f1';
2+
3+
function test(): void {
4+
const calc = new Calculator();
5+
calc.getResult().message;
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Calculator } from './f1';
2+
3+
function test(): void {
4+
const calcs = [new Calculator(), new Calculator()];
5+
calcs[0].getResult();
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Calculator } from './f1';
2+
3+
interface DeepNest {
4+
a: { b: { c: { d: { e: { f: Calculator } } } } };
5+
}
6+
7+
function test(obj: DeepNest): void {
8+
obj.a.b.c.d.e.f.getResult();
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"rootDir": "./source"
4+
}
5+
}
6+

src/extension/typescriptContext/serverPlugin/src/node/test/simple.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,58 @@ suite('PropertyTypes', () => {
345345
});
346346
});
347347

348+
suite('TypeOfExpressionRunnable', () => {
349+
let session: testing.TestSession;
350+
beforeAll(() => {
351+
session = create(path.join(root, 'p14'));
352+
});
353+
354+
test('ignores property access without identifier', () => {
355+
const context = computeContext(session, path.join(root, 'p14/source/f2.ts'), { line: 3, character: 19 }, ContextKind.Snippet);
356+
assertContextItems(context, []);
357+
});
358+
359+
test('type from method chain', () => {
360+
const expected: testing.ExpectedCodeSnippet[] = [{
361+
kind: ContextKind.Snippet,
362+
value: 'declare class Calculator { constructor(initial: number = 0); public add(x: number): Calculator; public getResult(): Result; }',
363+
fileName: /p14\/source\/f1.ts$/
364+
}];
365+
const context = computeContext(session, path.join(root, 'p14/source/f3.ts'), { line: 4, character: 22 }, ContextKind.Snippet);
366+
assertContextItems(context, expected, 'contains');
367+
});
368+
369+
test('type from method return (interface)', () => {
370+
const expected: testing.ExpectedCodeSnippet[] = [{
371+
kind: ContextKind.Snippet,
372+
value: 'interface Result { value: number; message: string; }',
373+
fileName: /p14\/source\/f1.ts$/
374+
}];
375+
const context = computeContext(session, path.join(root, 'p14/source/f4.ts'), { line: 4, character: 25 }, ContextKind.Snippet);
376+
assertContextItems(context, expected, 'contains');
377+
});
378+
379+
test('type from element access chain', () => {
380+
const expected: testing.ExpectedCodeSnippet[] = [{
381+
kind: ContextKind.Snippet,
382+
value: 'declare class Calculator { constructor(initial: number = 0); public add(x: number): Calculator; public getResult(): Result; }',
383+
fileName: /p14\/source\/f1.ts$/
384+
}];
385+
const context = computeContext(session, path.join(root, 'p14/source/f5.ts'), { line: 4, character: 19 }, ContextKind.Snippet);
386+
assertContextItems(context, expected, 'contains');
387+
});
388+
389+
test('type from deeply nested property access', () => {
390+
const expected: testing.ExpectedCodeSnippet[] = [{
391+
kind: ContextKind.Snippet,
392+
value: 'declare class Calculator { constructor(initial: number = 0); public add(x: number): Calculator; public getResult(): Result; }',
393+
fileName: /p14\/source\/f1.ts$/
394+
}];
395+
const context = computeContext(session, path.join(root, 'p14/source/f6.ts'), { line: 7, character: 25 }, ContextKind.Snippet);
396+
assertContextItems(context, expected, 'contains');
397+
});
398+
});
399+
348400
suite('Traits', () => {
349401
test('complete traits', () => {
350402
const session: testing.TestSession = create(path.join(root, 'p1'));

0 commit comments

Comments
 (0)