|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { clearDocuments, parseHelper } from 'langium/test'; |
| 3 | +import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; |
| 4 | +import { Position } from 'vscode-languageserver'; |
| 5 | +import { NodeFileSystem } from 'langium/node'; |
| 6 | +import { findTestChecks } from '../../helpers/testChecks.js'; |
| 7 | +import { URI } from 'langium'; |
| 8 | + |
| 9 | +const services = createSafeDsServices(NodeFileSystem).SafeDs; |
| 10 | +const inlayHintProvider = services.lsp.InlayHintProvider!; |
| 11 | +const workspaceManager = services.shared.workspace.WorkspaceManager; |
| 12 | +const parse = parseHelper(services); |
| 13 | + |
| 14 | +describe('SafeDsInlayHintProvider', async () => { |
| 15 | + beforeEach(async () => { |
| 16 | + // Load the builtin library |
| 17 | + await workspaceManager.initializeWorkspace([]); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(async () => { |
| 21 | + await clearDocuments(services); |
| 22 | + }); |
| 23 | + |
| 24 | + const testCases: InlayHintProviderTest[] = [ |
| 25 | + { |
| 26 | + testName: 'resolved positional argument', |
| 27 | + code: ` |
| 28 | + fun f(p: Int) |
| 29 | +
|
| 30 | + pipeline myPipeline { |
| 31 | + // $TEST$ before "p = " |
| 32 | + f(»«1); |
| 33 | + } |
| 34 | + `, |
| 35 | + }, |
| 36 | + { |
| 37 | + testName: 'unresolved positional argument', |
| 38 | + code: ` |
| 39 | + fun f() |
| 40 | +
|
| 41 | + pipeline myPipeline { |
| 42 | + f(1); |
| 43 | + } |
| 44 | + `, |
| 45 | + }, |
| 46 | + { |
| 47 | + testName: 'named argument', |
| 48 | + code: ` |
| 49 | + fun f(p: Int) |
| 50 | +
|
| 51 | + pipeline myPipeline { |
| 52 | + f(p = 1); |
| 53 | + } |
| 54 | + `, |
| 55 | + }, |
| 56 | + { |
| 57 | + testName: 'block lambda result', |
| 58 | + code: ` |
| 59 | + pipeline myPipeline { |
| 60 | + () { |
| 61 | + // $TEST$ after ": literal<1>" |
| 62 | + yield r»« = 1; |
| 63 | + }; |
| 64 | + } |
| 65 | + `, |
| 66 | + }, |
| 67 | + { |
| 68 | + testName: 'placeholder', |
| 69 | + code: ` |
| 70 | + pipeline myPipeline { |
| 71 | + // $TEST$ after ": literal<1>" |
| 72 | + val x»« = 1; |
| 73 | + } |
| 74 | + `, |
| 75 | + }, |
| 76 | + { |
| 77 | + testName: 'wildcard', |
| 78 | + code: ` |
| 79 | + pipeline myPipeline { |
| 80 | + _ = 1; |
| 81 | + } |
| 82 | + `, |
| 83 | + }, |
| 84 | + { |
| 85 | + testName: 'yield', |
| 86 | + code: ` |
| 87 | + segment s() -> r: Int { |
| 88 | + // $TEST$ after ": literal<1>" |
| 89 | + yield r»« = 1; |
| 90 | + } |
| 91 | + `, |
| 92 | + }, |
| 93 | + ]; |
| 94 | + |
| 95 | + it.each(testCases)('should assign the correct inlay hints ($testName)', async ({ code }) => { |
| 96 | + const actualInlayHints = await getActualInlayHints(code); |
| 97 | + const expectedInlayHints = getExpectedInlayHints(code); |
| 98 | + |
| 99 | + expect(actualInlayHints).toStrictEqual(expectedInlayHints); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +const getActualInlayHints = async (code: string): Promise<SimpleInlayHint[] | undefined> => { |
| 104 | + const document = await parse(code); |
| 105 | + const inlayHints = await inlayHintProvider.getInlayHints(document, { |
| 106 | + range: document.parseResult.value.$cstNode!.range, |
| 107 | + textDocument: { uri: document.textDocument.uri }, |
| 108 | + }); |
| 109 | + |
| 110 | + return inlayHints?.map((hint) => { |
| 111 | + if (typeof hint.label === 'string') { |
| 112 | + return { |
| 113 | + label: hint.label, |
| 114 | + position: hint.position, |
| 115 | + }; |
| 116 | + } else { |
| 117 | + return { |
| 118 | + label: hint.label.join(''), |
| 119 | + position: hint.position, |
| 120 | + }; |
| 121 | + } |
| 122 | + }); |
| 123 | +}; |
| 124 | + |
| 125 | +const getExpectedInlayHints = (code: string): SimpleInlayHint[] => { |
| 126 | + const testChecks = findTestChecks(code, URI.file('file:///test.sdstest'), { failIfFewerRangesThanComments: true }); |
| 127 | + if (testChecks.isErr) { |
| 128 | + throw new Error(testChecks.error.message); |
| 129 | + } |
| 130 | + |
| 131 | + return testChecks.value.map((check) => { |
| 132 | + const range = check.location!.range; |
| 133 | + |
| 134 | + const afterMatch = /after "(?<label>[^"]*)"/gu.exec(check.comment); |
| 135 | + if (afterMatch) { |
| 136 | + return { |
| 137 | + label: afterMatch.groups!.label, |
| 138 | + position: { |
| 139 | + line: range.start.line, |
| 140 | + character: range.start.character - 1, |
| 141 | + }, |
| 142 | + }; |
| 143 | + } |
| 144 | + |
| 145 | + const beforeMatch = /before "(?<label>[^"]*)"/gu.exec(check.comment); |
| 146 | + if (beforeMatch) { |
| 147 | + return { |
| 148 | + label: beforeMatch.groups!.label, |
| 149 | + position: { |
| 150 | + line: range.end.line, |
| 151 | + character: range.end.character + 1, |
| 152 | + }, |
| 153 | + }; |
| 154 | + } |
| 155 | + |
| 156 | + throw new Error('Incorrect test comment format'); |
| 157 | + }); |
| 158 | +}; |
| 159 | + |
| 160 | +/** |
| 161 | + * A description of a test case for the inlay hint provider. |
| 162 | + */ |
| 163 | +interface InlayHintProviderTest { |
| 164 | + /** |
| 165 | + * A short description of the test case. |
| 166 | + */ |
| 167 | + testName: string; |
| 168 | + |
| 169 | + /** |
| 170 | + * The code to parse. |
| 171 | + */ |
| 172 | + code: string; |
| 173 | +} |
| 174 | + |
| 175 | +/** |
| 176 | + * A simple inlay hint with some information removed. |
| 177 | + */ |
| 178 | +interface SimpleInlayHint { |
| 179 | + /** |
| 180 | + * The text of the inlay hint. |
| 181 | + */ |
| 182 | + label: string; |
| 183 | + |
| 184 | + /** |
| 185 | + * The position of the inlay hint. |
| 186 | + */ |
| 187 | + position: Position; |
| 188 | +} |
0 commit comments