Skip to content

Commit

Permalink
test: improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed Apr 3, 2024
1 parent 75134d8 commit fe87c05
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,24 +170,26 @@ export class SafeDsDocumentationProvider extends JSDocDocumentationProvider {
if (!linkRenderer) {
const nameSegment = this.nameProvider.getNameNode(target);
if (!nameSegment) {
/* c8 ignore next 2 */
return display;
}

const line = nameSegment.range.start.line + 1;
const character = nameSegment.range.start.character + 1;
const uri = AstUtils.getDocument(target).uri.with({ fragment: `L${line},${character}` });
return `[${display}](${uri.toString()})`;
} else {
return linkRenderer(target, display);
}

return linkRenderer(target, display);
},
};
}

private findTarget(node: AstNode, namePath: string): SdsDeclaration | undefined {
let [globalName, ...rest] = this.tokenizeNamepath(namePath);
// `rest` contains pairs of separators and names
if (!globalName || rest.length % 2 !== 0) {
// `rest` contains pairs of separators and names
/* c8 ignore next 2 */
return undefined;
}

Expand All @@ -202,6 +204,7 @@ export class SafeDsDocumentationProvider extends JSDocDocumentationProvider {
} else if (separator === '#') {
current = this.findInstanceMember(current, name);
} else {
/* c8 ignore next 2 */
return undefined;
}
}
Expand Down Expand Up @@ -274,4 +277,4 @@ const isTag = (element: JSDocElement): element is JSDocTag => {
return 'name' in element;
};

type LinkRenderer = (target: SdsDeclaration | undefined, display: string) => string | undefined;
type LinkRenderer = (target: SdsDeclaration, display: string) => string | undefined;
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,7 @@ export class SafeDsMarkdownGenerator {

private renderDescription(node: SdsDeclaration) {
return this.documentationProvider.getDescription(node, (target, display) => {
if (target) {
return `[${display}][${getQualifiedName(target)}]`;
} else {
return display;
}
return `[${display}][${getQualifiedName(target)}]`;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,17 +343,6 @@ describe('SafeDsDocumentationProvider', () => {

describe('findTarget', () => {
const testCases: DocumentationProviderTest[] = [
{
testName: 'link (unresolved)',
code: `
/**
* {@link myFunction2}
*/
fun myFunction1()
`,
predicate: isSdsFunction,
expectedDocumentation: `myFunction2`,
},
{
testName: 'link (global)',
code: `
Expand Down Expand Up @@ -474,6 +463,39 @@ describe('SafeDsDocumentationProvider', () => {
predicate: isSdsFunction,
expectedDocumentation: `[MyClass.NestedClass#myMethod](`,
},
{
testName: 'link (unresolved global)',
code: `
/**
* {@link myFunction2}
*/
fun myFunction1()
`,
predicate: isSdsFunction,
expectedDocumentation: `myFunction2`,
},
{
testName: 'link (wrong container for instance)',
code: `
/**
* {@link myFunction1#test}
*/
fun myFunction1()
`,
predicate: isSdsFunction,
expectedDocumentation: `myFunction1#test`,
},
{
testName: 'link (wrong container for static)',
code: `
/**
* {@link myFunction1.test}
*/
fun myFunction1()
`,
predicate: isSdsFunction,
expectedDocumentation: `myFunction1.test`,
},
];

it.each(testCases)('$testName', async ({ code, predicate, expectedDocumentation }) => {
Expand Down

0 comments on commit fe87c05

Please sign in to comment.