Skip to content

Commit

Permalink
feat(tests): alias testing for CSS/SCSS @imports
Browse files Browse the repository at this point in the history
  • Loading branch information
SStranks committed Nov 1, 2023
1 parent 01ad668 commit 5915e94
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
23 changes: 22 additions & 1 deletion src/test/css/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { colorFrom256RGB, colorFromHSL, colorFromHWB } from '../../languageFacts

import {
TextDocument, DocumentHighlightKind, Range, Position, TextEdit, Color,
ColorInformation, DocumentLink, SymbolKind, SymbolInformation, Location, LanguageService, Stylesheet, getCSSLanguageService, DocumentSymbol,
ColorInformation, DocumentLink, SymbolKind, SymbolInformation, Location, LanguageService, Stylesheet, getCSSLanguageService, DocumentSymbol, LanguageSettings,
} from '../../cssLanguageService';

import { URI } from 'vscode-uri';
Expand Down Expand Up @@ -184,6 +184,17 @@ function getCSSLS() {
return getCSSLanguageService({ fileSystemProvider: getFsProvider() });
}

function aliasSettings(): LanguageSettings {
return {
"alias": {
"paths": {
"@SingleStylesheet": "/src/assets/styles.css",
"@AssetsDir/*": "/src/assets/*",
}
}
};
}

suite('CSS - Navigation', () => {

suite('Scope', () => {
Expand Down Expand Up @@ -364,6 +375,16 @@ suite('CSS - Navigation', () => {
]);
});

test('aliased @import links', async function () {
const settings = aliasSettings();
const ls = getCSSLS();
ls.configure(settings);

await assertLinks(ls, '@import "@SingleStylesheet"', [{ range: newRange(8, 27), target: "test://test/src/assets/styles.css"}]);

await assertLinks(ls, '@import "@AssetsDir/styles.css"', [{ range: newRange(8, 31), target: "test://test/src/assets/styles.css"}]);
});

test('links in rulesets', async () => {
const ls = getCSSLS();
await assertLinks(ls, `body { background-image: url(./foo.jpg)`, [
Expand Down
51 changes: 48 additions & 3 deletions src/test/scss/scssNavigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@

import * as nodes from '../../parser/cssNodes';
import { assertSymbolsInScope, assertScopesAndSymbols, assertHighlights, assertColorSymbols, assertLinks, newRange, getTestResource, assertDocumentSymbols } from '../css/navigation.test';
import { getSCSSLanguageService, DocumentLink, TextDocument, SymbolKind } from '../../cssLanguageService';
import { getSCSSLanguageService, DocumentLink, TextDocument, SymbolKind, LanguageSettings } from '../../cssLanguageService';
import * as assert from 'assert';
import * as path from 'path';
import { URI, Utils } from 'vscode-uri';
import { URI } from 'vscode-uri';
import { getFsProvider } from '../testUtil/fsProvider';
import { getDocumentContext } from '../testUtil/documentContext';

function getSCSSLS() {
return getSCSSLanguageService({ fileSystemProvider: getFsProvider() });
}

async function assertDynamicLinks(docUri: string, input: string, expected: DocumentLink[]) {
function aliasSettings(): LanguageSettings {
return {
"alias": {
"paths": {
"@SassStylesheet": "/src/assets/styles.scss",
"@NoUnderscoreDir/*": "/noUnderscore/*",
"@UnderscoreDir/*": "/underscore/*",
"@BothDir/*": "/both/*",
}
}
};
}

async function assertDynamicLinks(docUri: string, input: string, expected: DocumentLink[], settings?: LanguageSettings) {
const ls = getSCSSLS();
if (settings) {
ls.configure(settings);
}
const document = TextDocument.create(docUri, 'scss', 0, input);

const stylesheet = ls.parseStylesheet(document);
Expand Down Expand Up @@ -177,6 +193,35 @@ suite('SCSS - Navigation', () => {

});

test('SCSS aliased links', async function () {
const fixtureRoot = path.resolve(__dirname, '../../../../src/test/scss/linkFixture');
const getDocumentUri = (relativePath: string) => {
return URI.file(path.resolve(fixtureRoot, relativePath)).toString(true);
};

const settings = aliasSettings();
const ls = getSCSSLS();
ls.configure(settings);

await assertLinks(ls, '@import "@SassStylesheet"', [{ range: newRange(8, 25), target: "test://test/src/assets/styles.scss"}]);

await assertDynamicLinks(getDocumentUri('./'), `@import '@NoUnderscoreDir/foo'`, [
{ range: newRange(8, 30), target: getDocumentUri('./noUnderscore/foo.scss') }
], settings);

await assertDynamicLinks(getDocumentUri('./'), `@import '@UnderscoreDir/foo'`, [
{ range: newRange(8, 28), target: getDocumentUri('./underscore/_foo.scss') }
], settings);

await assertDynamicLinks(getDocumentUri('./'), `@import '@BothDir/foo'`, [
{ range: newRange(8, 22), target: getDocumentUri('./both/foo.scss') }
], settings);

await assertDynamicLinks(getDocumentUri('./'), `@import '@BothDir/_foo'`, [
{ range: newRange(8, 23), target: getDocumentUri('./both/_foo.scss') }
], settings);
});

test('SCSS module file links', async () => {
const fixtureRoot = path.resolve(__dirname, '../../../../src/test/scss/linkFixture/module');
const getDocumentUri = (relativePath: string) => {
Expand Down

0 comments on commit 5915e94

Please sign in to comment.