|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | +import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; |
| 3 | +import { AstNode, EmptyFileSystem } from 'langium'; |
| 4 | +import { clearDocuments } from 'langium/test'; |
| 5 | +import { getNodeOfType } from '../../helpers/nodeFinder.js'; |
| 6 | +import { |
| 7 | + isSdsAnnotation, |
| 8 | + isSdsFunction, |
| 9 | + isSdsParameter, |
| 10 | + isSdsResult, |
| 11 | + isSdsTypeParameter, |
| 12 | +} from '../../../src/language/generated/ast.js'; |
| 13 | + |
| 14 | +const services = createSafeDsServices(EmptyFileSystem).SafeDs; |
| 15 | +const documentationProvider = services.documentation.DocumentationProvider; |
| 16 | +const testDocumentation = 'Lorem ipsum.'; |
| 17 | + |
| 18 | +describe('SafeDsDocumentationProvider', () => { |
| 19 | + afterEach(async () => { |
| 20 | + await clearDocuments(services); |
| 21 | + }); |
| 22 | + |
| 23 | + const testCases: DocumentationProviderTest[] = [ |
| 24 | + { |
| 25 | + testName: 'module member', |
| 26 | + code: ` |
| 27 | + /** |
| 28 | + * ${testDocumentation} |
| 29 | + */ |
| 30 | + annotation MyAnnotation |
| 31 | + `, |
| 32 | + predicate: isSdsAnnotation, |
| 33 | + expectedDocumentation: testDocumentation, |
| 34 | + }, |
| 35 | + { |
| 36 | + testName: 'documented parameter', |
| 37 | + code: ` |
| 38 | + /** |
| 39 | + * @param param ${testDocumentation} |
| 40 | + */ |
| 41 | + fun myFunction(param: String) |
| 42 | + `, |
| 43 | + predicate: isSdsParameter, |
| 44 | + expectedDocumentation: testDocumentation, |
| 45 | + }, |
| 46 | + { |
| 47 | + testName: 'documented parameter (duplicate)', |
| 48 | + code: ` |
| 49 | + /** |
| 50 | + * @param param ${testDocumentation} |
| 51 | + * @param param bla |
| 52 | + */ |
| 53 | + fun myFunction(param: String) |
| 54 | + `, |
| 55 | + predicate: isSdsParameter, |
| 56 | + expectedDocumentation: testDocumentation, |
| 57 | + }, |
| 58 | + { |
| 59 | + testName: 'undocumented parameter', |
| 60 | + code: ` |
| 61 | + /** |
| 62 | + * @param param ${testDocumentation} |
| 63 | + */ |
| 64 | + fun myFunction(param2: String) |
| 65 | + `, |
| 66 | + predicate: isSdsParameter, |
| 67 | + expectedDocumentation: undefined, |
| 68 | + }, |
| 69 | + { |
| 70 | + testName: 'parameter (no documentation on containing callable)', |
| 71 | + code: ` |
| 72 | + fun myFunction(p: Int) |
| 73 | + `, |
| 74 | + predicate: isSdsParameter, |
| 75 | + expectedDocumentation: undefined, |
| 76 | + }, |
| 77 | + { |
| 78 | + testName: 'documented result', |
| 79 | + code: ` |
| 80 | + /** |
| 81 | + * @result res ${testDocumentation} |
| 82 | + */ |
| 83 | + fun myFunction() -> (res: String) |
| 84 | + `, |
| 85 | + predicate: isSdsResult, |
| 86 | + expectedDocumentation: testDocumentation, |
| 87 | + }, |
| 88 | + { |
| 89 | + testName: 'documented result (duplicate)', |
| 90 | + code: ` |
| 91 | + /** |
| 92 | + * @result res ${testDocumentation} |
| 93 | + * @result res bla |
| 94 | + */ |
| 95 | + fun myFunction() -> (res: String) |
| 96 | + `, |
| 97 | + predicate: isSdsResult, |
| 98 | + expectedDocumentation: testDocumentation, |
| 99 | + }, |
| 100 | + { |
| 101 | + testName: 'undocumented result', |
| 102 | + code: ` |
| 103 | + /** |
| 104 | + * @result res ${testDocumentation} |
| 105 | + */ |
| 106 | + fun myFunction() -> (res2: String) |
| 107 | + `, |
| 108 | + predicate: isSdsResult, |
| 109 | + expectedDocumentation: undefined, |
| 110 | + }, |
| 111 | + { |
| 112 | + testName: 'result (no documentation on containing callable)', |
| 113 | + code: ` |
| 114 | + fun myFunction() -> r: Int |
| 115 | + `, |
| 116 | + predicate: isSdsResult, |
| 117 | + expectedDocumentation: undefined, |
| 118 | + }, |
| 119 | + { |
| 120 | + testName: 'documented type parameter', |
| 121 | + code: ` |
| 122 | + enum MyEnum { |
| 123 | + /** |
| 124 | + * @typeParam T |
| 125 | + * ${testDocumentation} |
| 126 | + */ |
| 127 | + MyEnumVariant<T> |
| 128 | + } |
| 129 | + `, |
| 130 | + predicate: isSdsTypeParameter, |
| 131 | + expectedDocumentation: testDocumentation, |
| 132 | + }, |
| 133 | + { |
| 134 | + testName: 'documented type parameter (duplicate)', |
| 135 | + code: ` |
| 136 | + enum MyEnum { |
| 137 | + /** |
| 138 | + * @typeParam T ${testDocumentation} |
| 139 | + * @typeParam T bla |
| 140 | + */ |
| 141 | + MyEnumVariant<T> |
| 142 | + } |
| 143 | + `, |
| 144 | + predicate: isSdsTypeParameter, |
| 145 | + expectedDocumentation: testDocumentation, |
| 146 | + }, |
| 147 | + { |
| 148 | + testName: 'undocumented type parameter', |
| 149 | + code: ` |
| 150 | + enum MyEnum { |
| 151 | + /** |
| 152 | + * @typeParam T |
| 153 | + * ${testDocumentation} |
| 154 | + */ |
| 155 | + MyEnumVariant<T2> |
| 156 | + } |
| 157 | + `, |
| 158 | + predicate: isSdsTypeParameter, |
| 159 | + expectedDocumentation: undefined, |
| 160 | + }, |
| 161 | + { |
| 162 | + testName: 'type parameter (no documentation on containing callable)', |
| 163 | + code: ` |
| 164 | + fun myFunction<T>() |
| 165 | + `, |
| 166 | + predicate: isSdsTypeParameter, |
| 167 | + expectedDocumentation: undefined, |
| 168 | + }, |
| 169 | + ]; |
| 170 | + |
| 171 | + it.each(testCases)('$testName', async ({ code, predicate, expectedDocumentation }) => { |
| 172 | + const node = await getNodeOfType(services, code, predicate); |
| 173 | + expect(documentationProvider.getDocumentation(node)).toStrictEqual(expectedDocumentation); |
| 174 | + }); |
| 175 | + |
| 176 | + it('should resolve links', async () => { |
| 177 | + const code = ` |
| 178 | + /** |
| 179 | + * {@link myFunction2} |
| 180 | + */ |
| 181 | + fun myFunction1() |
| 182 | +
|
| 183 | + fun myFunction2() |
| 184 | + `; |
| 185 | + const node = await getNodeOfType(services, code, isSdsFunction); |
| 186 | + expect(documentationProvider.getDocumentation(node)).toMatch(/\[myFunction2\]\(.*\)/u); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +/** |
| 191 | + * A description of a test case for the documentation provider. |
| 192 | + */ |
| 193 | +interface DocumentationProviderTest { |
| 194 | + /** |
| 195 | + * A short description of the test case. |
| 196 | + */ |
| 197 | + testName: string; |
| 198 | + |
| 199 | + /** |
| 200 | + * The code to test. |
| 201 | + */ |
| 202 | + code: string; |
| 203 | + |
| 204 | + /** |
| 205 | + * A predicate to find the node to test. |
| 206 | + */ |
| 207 | + predicate: (node: unknown) => node is AstNode; |
| 208 | + |
| 209 | + /** |
| 210 | + * The expected documentation. |
| 211 | + */ |
| 212 | + expectedDocumentation: string | undefined; |
| 213 | +} |
0 commit comments