Path: blob/main/src/vs/base/test/common/iconLabels.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { IMatch } from '../../common/filters.js';7import { escapeIcons, getCodiconAriaLabel, IParsedLabelWithIcons, markdownEscapeEscapedIcons, matchesFuzzyIconAware, parseLabelWithIcons, stripIcons } from '../../common/iconLabels.js';8import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';910interface IIconFilter {11// Returns null if word doesn't match.12(query: string, target: IParsedLabelWithIcons): IMatch[] | null;13}1415function filterOk(filter: IIconFilter, word: string, target: IParsedLabelWithIcons, highlights?: { start: number; end: number }[]) {16const r = filter(word, target);17assert(r);18if (highlights) {19assert.deepStrictEqual(r, highlights);20}21}2223suite('Icon Labels', () => {24test('Can get proper aria labels', () => {25// note, the spaces in the results are important26const testCases = new Map<string, string>([27['', ''],28['asdf', 'asdf'],29['asdf$(squirrel)asdf', 'asdf squirrel asdf'],30['asdf $(squirrel) asdf', 'asdf squirrel asdf'],31['$(rocket)asdf', 'rocket asdf'],32['$(rocket) asdf', 'rocket asdf'],33['$(rocket)$(rocket)$(rocket)asdf', 'rocket rocket rocket asdf'],34['$(rocket) asdf $(rocket)', 'rocket asdf rocket'],35['$(rocket)asdf$(rocket)', 'rocket asdf rocket'],36]);3738for (const [input, expected] of testCases) {39assert.strictEqual(getCodiconAriaLabel(input), expected);40}41});4243test('matchesFuzzyIconAware', () => {4445// Camel Case4647filterOk(matchesFuzzyIconAware, 'ccr', parseLabelWithIcons('$(codicon)CamelCaseRocks$(codicon)'), [48{ start: 10, end: 11 },49{ start: 15, end: 16 },50{ start: 19, end: 20 }51]);5253filterOk(matchesFuzzyIconAware, 'ccr', parseLabelWithIcons('$(codicon) CamelCaseRocks $(codicon)'), [54{ start: 11, end: 12 },55{ start: 16, end: 17 },56{ start: 20, end: 21 }57]);5859filterOk(matchesFuzzyIconAware, 'iut', parseLabelWithIcons('$(codicon) Indent $(octico) Using $(octic) Tpaces'), [60{ start: 11, end: 12 },61{ start: 28, end: 29 },62{ start: 43, end: 44 },63]);6465// Prefix6667filterOk(matchesFuzzyIconAware, 'using', parseLabelWithIcons('$(codicon) Indent Using Spaces'), [68{ start: 18, end: 23 },69]);7071// Broken Codicon7273filterOk(matchesFuzzyIconAware, 'codicon', parseLabelWithIcons('This $(codicon Indent Using Spaces'), [74{ start: 7, end: 14 },75]);7677filterOk(matchesFuzzyIconAware, 'indent', parseLabelWithIcons('This $codicon Indent Using Spaces'), [78{ start: 14, end: 20 },79]);8081// Testing #5934382filterOk(matchesFuzzyIconAware, 'unt', parseLabelWithIcons('$(primitive-dot) $(file-text) Untitled-1'), [83{ start: 30, end: 33 },84]);8586// Testing #13617287filterOk(matchesFuzzyIconAware, 's', parseLabelWithIcons('$(loading~spin) start'), [88{ start: 16, end: 17 },89]);90});9192test('stripIcons', () => {93assert.strictEqual(stripIcons('Hello World'), 'Hello World');94assert.strictEqual(stripIcons('$(Hello World'), '$(Hello World');95assert.strictEqual(stripIcons('$(Hello) World'), ' World');96assert.strictEqual(stripIcons('$(Hello) W$(oi)rld'), ' Wrld');97});9899100test('escapeIcons', () => {101assert.strictEqual(escapeIcons('Hello World'), 'Hello World');102assert.strictEqual(escapeIcons('$(Hello World'), '$(Hello World');103assert.strictEqual(escapeIcons('$(Hello) World'), '\\$(Hello) World');104assert.strictEqual(escapeIcons('\\$(Hello) W$(oi)rld'), '\\$(Hello) W\\$(oi)rld');105});106107test('markdownEscapeEscapedIcons', () => {108assert.strictEqual(markdownEscapeEscapedIcons('Hello World'), 'Hello World');109assert.strictEqual(markdownEscapeEscapedIcons('$(Hello) World'), '$(Hello) World');110assert.strictEqual(markdownEscapeEscapedIcons('\\$(Hello) World'), '\\\\$(Hello) World');111});112113ensureNoDisposablesAreLeakedInTestSuite();114});115116117