diff --git a/package-lock.json b/package-lock.json index 3c06c5c..72147a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3526,9 +3526,9 @@ "dev": true }, "node_modules/follow-redirects": { - "version": "1.15.4", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", - "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", "dev": true, "funding": [ { @@ -13796,9 +13796,9 @@ "dev": true }, "follow-redirects": { - "version": "1.15.4", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", - "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", "dev": true }, "foreground-child": { diff --git a/src/providers/StringSortProvider.ts b/src/providers/StringSortProvider.ts index 1cd50a3..f728ca2 100644 --- a/src/providers/StringSortProvider.ts +++ b/src/providers/StringSortProvider.ts @@ -43,7 +43,7 @@ export class StringSortProvider extends Intl.Collator { const aIndex = customSortOrder.indexOf(ignoreCase ? a[i].toLowerCase() : a[i]); const bIndex = customSortOrder.indexOf(ignoreCase ? b[i].toLowerCase() : b[i]); - if (aIndex === -1 || bIndex === -1) break; + if (aIndex === -1 || bIndex === -1) continue; else if (aIndex !== bIndex) return (aIndex - bIndex) * sign; else if (i === minLength - 1) return (a.length - b.length) * sign; } diff --git a/src/test/fixtures/custom.ts b/src/test/fixtures/custom.ts index 901d8e3..e5f0f74 100644 --- a/src/test/fixtures/custom.ts +++ b/src/test/fixtures/custom.ts @@ -1,10 +1,17 @@ import { Range } from "vscode"; -import { CompareTest } from "../suite/types"; +import { CompareTest, CustomSortTest } from "../suite/types"; -export const customSortTests: CompareTest[] = [ +export const customSortTests: CustomSortTest[] = [ { file: "custom.plaintext.fixture", compareFile: "custom.plaintext.expect", ranges: [new Range(0, 0, 5, 4)], + collatorOptions: { customSortOrder: "abcdefghijklmnopqrstuvwxyz:#" }, + }, + { + file: "custom.plaintext.fixture", + compareFile: "custom.plaintext.expect", + ranges: [new Range(7, 0, 21, 1)], + collatorOptions: { customSortOrder: '"-_' }, }, ]; diff --git a/src/test/suite/BlockSortProvider.unit.test.ts b/src/test/suite/BlockSortProvider.unit.test.ts index f8bb003..353d31d 100644 --- a/src/test/suite/BlockSortProvider.unit.test.ts +++ b/src/test/suite/BlockSortProvider.unit.test.ts @@ -3,7 +3,7 @@ import { join } from "path"; import { window, workspace, Selection, CancellationTokenSource, CancellationToken, languages } from "vscode"; import BlockSortProvider from "../../providers/BlockSortProvider"; import { expandTests, fixtureDir, sortTests, multilevelSortTests, cancelSortTests } from "../fixtures"; -import { CompareTest } from "./types"; +import { CompareTest, CustomSortTest } from "./types"; import { naturalSortTests } from "../fixtures/natural"; import { ExpandSelectionOptions } from "../../types/BlockSortOptions"; import { StringSortProvider } from "../../providers/StringSortProvider"; @@ -16,16 +16,12 @@ const defaultExpandOptions: ExpandSelectionOptions = { indentationComplete: true, }; -function sortTest( - tests: CompareTest[], - title = "Sort Blocks", - sortProvider: StringSortProvider = new StringSortProvider(), - sortChildren = 0 -) { - tests.forEach(({ file, compareFile, ranges, only, skip }) => { +function sortTest(tests: CustomSortTest[], title = "Sort Blocks", sortChildren = 0) { + tests.forEach(({ file, compareFile, ranges, only, skip, collatorOptions, direction }) => { ranges.forEach((range, i) => { const descriptor = file.match(/(.*)\.(.*)\.fixture/); const [_, type, lang] = descriptor || ["", "generic", "generic"]; + const sortProvider = new StringSortProvider(collatorOptions, direction); const testFunc = only ? test.only : skip ? test.skip : test; testFunc(`${title} (${type}, lang ${lang}) #${i + 1}`, async () => { const compareDocument = await workspace.openTextDocument(join(fixtureDir, compareFile)); @@ -104,14 +100,13 @@ suite("Unit Suite for BlockSortProvider", async () => { }); sortTest(sortTests, "Sort Blocks"); - sortTest(multilevelSortTests, "Deep Sort Blocks", new StringSortProvider(), -1); - sortTest(naturalSortTests, "Natural Sort Blocks", new StringSortProvider({ numeric: true }), 0); + sortTest(multilevelSortTests, "Deep Sort Blocks", -1); sortTest( - customSortTests, - "Custom Sort Blocks", - new StringSortProvider({ customSortOrder: "abcdefghijklmnopqrstuvwxyz:#" }), + naturalSortTests.map((test) => ({ ...test, collatorOptions: { numeric: true } })), + "Natural Sort Blocks", 0 ); + sortTest(customSortTests, "Custom Sort Blocks", 0); cancelSortTests.forEach(({ file, ranges, performanceThreshold, only, skip }) => { ranges.forEach((range, i) => { diff --git a/src/test/suite/types.ts b/src/test/suite/types.ts index 771bf98..a52cf24 100644 --- a/src/test/suite/types.ts +++ b/src/test/suite/types.ts @@ -1,5 +1,6 @@ import { CodeActionKind, Position, Range } from "vscode"; import { ExpandSelectionOptions } from "../../types/BlockSortOptions"; +import { BlockSortCollatorOptions } from "../../providers/ConfigurationProvider"; export interface BaseTest { file: string; @@ -12,6 +13,11 @@ export interface CompareTest extends BaseTest { compareFile: string; } +export interface CustomSortTest extends CompareTest { + collatorOptions?: BlockSortCollatorOptions; + direction?: "asc" | "desc"; +} + export interface RangeTest extends BaseTest { targetRanges: Range[]; expand?: boolean | ExpandSelectionOptions; diff --git a/test/fixtures/custom.plaintext.expect b/test/fixtures/custom.plaintext.expect index 51265fe..f80829b 100644 --- a/test/fixtures/custom.plaintext.expect +++ b/test/fixtures/custom.plaintext.expect @@ -4,3 +4,19 @@ bbbb :bbb #aaa #bbb + +variable "grafana" { + type = bool +} +variable "grafana-count" { + type = number +} +variable "grafana-username" { + type = string +} +variable "grafana_address" { + type = string +} +variable "grafana_password" { + type = string +} diff --git a/test/fixtures/custom.plaintext.fixture b/test/fixtures/custom.plaintext.fixture index f37ee27..cb935d0 100644 --- a/test/fixtures/custom.plaintext.fixture +++ b/test/fixtures/custom.plaintext.fixture @@ -4,3 +4,19 @@ bbbb #bbb aaaa #aaa + +variable "grafana_address" { + type = string +} +variable "grafana" { + type = bool +} +variable "grafana-username" { + type = string +} +variable "grafana_password" { + type = string +} +variable "grafana-count" { + type = number +}