|
| 1 | +import * as assert from "assert"; |
| 2 | +import * as vscode from "vscode"; |
| 3 | +import { getCompletions, getHovers, getLinks, hoverToText } from "./helper"; |
| 4 | + |
| 5 | +type AssertCompletionsOptions = { |
| 6 | + doc: vscode.TextDocument; |
| 7 | + lines: string[]; |
| 8 | + expects: string[]; |
| 9 | +}; |
| 10 | + |
| 11 | +export async function assertCompletions({ |
| 12 | + doc, |
| 13 | + lines, |
| 14 | + expects, |
| 15 | +}: AssertCompletionsOptions): Promise<void> { |
| 16 | + const text = doc.getText(); |
| 17 | + |
| 18 | + for (const line of lines) { |
| 19 | + const lineIndex = text.indexOf(line); |
| 20 | + |
| 21 | + assert.ok( |
| 22 | + lineIndex !== -1, |
| 23 | + `Could not find test case '${line}' in fixture`, |
| 24 | + ); |
| 25 | + |
| 26 | + const match = line.match(/'([^']+)'/); |
| 27 | + |
| 28 | + assert.ok(match, `Could not extract quoted value from '${line}'`); |
| 29 | + |
| 30 | + const value = match[1]; |
| 31 | + const valueOffset = lineIndex + line.indexOf(value); |
| 32 | + const completions = await getCompletions( |
| 33 | + doc, |
| 34 | + doc.positionAt(valueOffset), |
| 35 | + ); |
| 36 | + |
| 37 | + const labels = completions.items.map((entry) => |
| 38 | + typeof entry.label === "string" ? entry.label : entry.label.label, |
| 39 | + ); |
| 40 | + |
| 41 | + for (const expected of expects) { |
| 42 | + assert.ok( |
| 43 | + labels.some((label) => label.includes(expected)), |
| 44 | + `Case '${line}': Should suggest '${expected}'. Got: ${labels.join(", ")}`, |
| 45 | + ); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export async function assertLinks({ |
| 51 | + doc, |
| 52 | + lines, |
| 53 | +}: { |
| 54 | + doc: vscode.TextDocument; |
| 55 | + lines: { |
| 56 | + line: string; |
| 57 | + target: string; |
| 58 | + argument?: string; |
| 59 | + }[]; |
| 60 | +}): Promise<void> { |
| 61 | + const text = doc.getText(); |
| 62 | + const links = await getLinks(doc); |
| 63 | + |
| 64 | + assert.ok(links.length > 0, "Expected links to be provided"); |
| 65 | + |
| 66 | + for (const item of lines) { |
| 67 | + const lineIndex = text.indexOf(item.line); |
| 68 | + |
| 69 | + assert.ok( |
| 70 | + lineIndex !== -1, |
| 71 | + `Could not find link case '${item.line}' in fixture`, |
| 72 | + ); |
| 73 | + |
| 74 | + const selector = item.argument ?? item.line.match(/'([^']+)'/)?.[1]; |
| 75 | + |
| 76 | + if (!selector) { |
| 77 | + assert.fail( |
| 78 | + `Could not extract selector from '${item.line}'. Provide 'argument' explicitly for this case.`, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + const selectorOffset = lineIndex + item.line.indexOf(selector); |
| 83 | + |
| 84 | + const matchedLink = links.find((link) => { |
| 85 | + return ( |
| 86 | + selectorOffset >= doc.offsetAt(link.range.start) && |
| 87 | + selectorOffset < doc.offsetAt(link.range.end) |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + assert.ok( |
| 92 | + matchedLink?.target, |
| 93 | + `Case '${item.line}': Expected link target`, |
| 94 | + ); |
| 95 | + |
| 96 | + assert.ok( |
| 97 | + matchedLink?.target?.fsPath.includes(item.target), |
| 98 | + `Case '${item.line}': Expected link target to include '${item.target}'. Got: ${matchedLink?.target?.toString()}`, |
| 99 | + ); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +export async function assertHovers({ |
| 104 | + doc, |
| 105 | + lines, |
| 106 | +}: { |
| 107 | + doc: vscode.TextDocument; |
| 108 | + lines: { |
| 109 | + line: string; |
| 110 | + contains: string[]; |
| 111 | + argument?: string; |
| 112 | + }[]; |
| 113 | +}): Promise<void> { |
| 114 | + const text = doc.getText(); |
| 115 | + |
| 116 | + for (const item of lines) { |
| 117 | + const lineIndex = text.indexOf(item.line); |
| 118 | + |
| 119 | + assert.ok( |
| 120 | + lineIndex !== -1, |
| 121 | + `Could not find hover case '${item.line}' in fixture`, |
| 122 | + ); |
| 123 | + |
| 124 | + const selector = item.argument ?? item.line.match(/'([^']+)'/)?.[1]; |
| 125 | + |
| 126 | + if (!selector) { |
| 127 | + assert.fail( |
| 128 | + `Could not extract selector from '${item.line}'. Provide 'argument' explicitly for this case.`, |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + const selectorOffset = lineIndex + item.line.indexOf(selector); |
| 133 | + const hovers = await getHovers(doc, doc.positionAt(selectorOffset + 1)); |
| 134 | + |
| 135 | + assert.ok( |
| 136 | + hovers.length > 0, |
| 137 | + `Case '${item.line}': Expected hover information`, |
| 138 | + ); |
| 139 | + |
| 140 | + const hoverText = hoverToText(hovers[0]); |
| 141 | + |
| 142 | + for (const expected of item.contains) { |
| 143 | + assert.ok( |
| 144 | + hoverText.includes(expected), |
| 145 | + `Case '${item.line}': Expected hover to include '${expected}'. Got: ${hoverText}`, |
| 146 | + ); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments