From c9d9686a78215aaf29858eec667a401006342903 Mon Sep 17 00:00:00 2001 From: William Killerud Date: Mon, 16 Dec 2024 20:07:18 +0100 Subject: [PATCH] VSCode test --- .../test/electron/hover/fixtures/other.scss | 3 + .../test/electron/hover/fixtures/styles.sass | 9 +++ extension/test/electron/hover/hover.test.js | 69 +++++++++++++++++++ extension/test/electron/hover/index.js | 25 +++++++ 4 files changed, 106 insertions(+) create mode 100644 extension/test/electron/hover/fixtures/other.scss create mode 100644 extension/test/electron/hover/fixtures/styles.sass create mode 100644 extension/test/electron/hover/hover.test.js create mode 100644 extension/test/electron/hover/index.js diff --git a/extension/test/electron/hover/fixtures/other.scss b/extension/test/electron/hover/fixtures/other.scss new file mode 100644 index 0000000..bf86e12 --- /dev/null +++ b/extension/test/electron/hover/fixtures/other.scss @@ -0,0 +1,3 @@ +/// Docstring. +/// @type String +$from-other: 'hello'; diff --git a/extension/test/electron/hover/fixtures/styles.sass b/extension/test/electron/hover/fixtures/styles.sass new file mode 100644 index 0000000..d5acfc8 --- /dev/null +++ b/extension/test/electron/hover/fixtures/styles.sass @@ -0,0 +1,9 @@ +@use "sass:string" +@use "other" + +$_id: string.unique-id() +$_prefix: other.$from-other + +.card + .body:has(:not(.stuff)) + padding: 4px diff --git a/extension/test/electron/hover/hover.test.js b/extension/test/electron/hover/hover.test.js new file mode 100644 index 0000000..0dda92b --- /dev/null +++ b/extension/test/electron/hover/hover.test.js @@ -0,0 +1,69 @@ +const assert = require('node:assert'); +const path = require('node:path'); +const vscode = require('vscode'); +const { showFile, sleepCI } = require('../util'); + +const stylesUri = vscode.Uri.file( + path.resolve(__dirname, 'fixtures', 'styles.sass') +); + +before(async () => { + await showFile(stylesUri); + await sleepCI(); +}); + +after(async () => { + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); +}); + +/** + * @param {import('vscode').Hover[]} hover + * @returns {string} + */ +function getHoverContents(hover) { + return hover + .flatMap((item) => { + return item.contents.map((content) => + typeof content === 'string' ? content : content.value + ); + }) + .join('\n'); +} + +/** + * @param {import('vscode').Uri} documentUri + * @param {import('vscode').Position} position + * @returns {Promise} + */ +async function hover(documentUri, position) { + const result = await vscode.commands.executeCommand( + 'vscode.executeHoverProvider', + documentUri, + position + ); + return result; +} + +test('gets hover information from the same document', async () => { + const result = await hover(stylesUri, new vscode.Position(7, 10)); + + assert.match( + getHoverContents(result), + /\.card \.body:has\(:not\(\.stuff\)\)/ + ); +}); + +test('gets hover information from the workspace', async () => { + const result = await hover(stylesUri, new vscode.Position(4, 19)); + + assert.match(getHoverContents(result), /Docstring/); +}); + +test('gets hover information for Sass built-in', async () => { + const result = await hover(stylesUri, new vscode.Position(3, 14)); + + assert.match( + getHoverContents(result), + /Returns a randomly-generated unquoted string/ + ); +}); diff --git a/extension/test/electron/hover/index.js b/extension/test/electron/hover/index.js new file mode 100644 index 0000000..83212dd --- /dev/null +++ b/extension/test/electron/hover/index.js @@ -0,0 +1,25 @@ +const path = require('node:path'); +const fs = require('node:fs/promises'); +const vscode = require('vscode'); +const { runMocha } = require('../mocha'); + +/** + * @returns {Promise} + */ +async function run() { + const filePaths = []; + + const dir = await fs.readdir(__dirname, { withFileTypes: true }); + for (let entry of dir) { + if (entry.isFile() && entry.name.endsWith('test.js')) { + filePaths.push(path.join(entry.parentPath, entry.name)); + } + } + + await runMocha( + filePaths, + vscode.Uri.file(path.resolve(__dirname, 'fixtures', 'styles.sass')) + ); +} + +module.exports = { run };