Skip to content

Commit

Permalink
Merge pull request #86 from 1nVitr0/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
1nVitr0 authored Mar 29, 2024
2 parents 911d9e1 + 751da55 commit 169fc10
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 22 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/providers/StringSortProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
11 changes: 9 additions & 2 deletions src/test/fixtures/custom.ts
Original file line number Diff line number Diff line change
@@ -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: '"-_' },
},
];
21 changes: 8 additions & 13 deletions src/test/suite/BlockSortProvider.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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));
Expand Down Expand Up @@ -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) => {
Expand Down
6 changes: 6 additions & 0 deletions src/test/suite/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/custom.plaintext.expect
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions test/fixtures/custom.plaintext.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 169fc10

Please sign in to comment.