mirror of
https://github.com/esm7/obsidian-vimrc-support.git
synced 2026-07-22 05:00:25 +00:00
refactor(tests): deduplicate code fence motion specs
This commit is contained in:
parent
827dea58a1
commit
fdc3aa054e
1 changed files with 46 additions and 77 deletions
|
|
@ -5,113 +5,82 @@ import {
|
|||
} from "../motions/jumpToCodeFence";
|
||||
import { createFakeCodeMirrorEditor } from "./createFakeCodeMirrorEditor";
|
||||
|
||||
const CODE_FENCE_CONTENT_LINES = [
|
||||
"intro",
|
||||
"```ts",
|
||||
"const answer = 42;",
|
||||
"```",
|
||||
"outro",
|
||||
];
|
||||
|
||||
describe("jumpToNextCodeFence", () => {
|
||||
test("jumps to the next opening code fence", () => {
|
||||
const cm = createFakeCodeMirrorEditor([
|
||||
"intro",
|
||||
"```ts",
|
||||
"const answer = 42;",
|
||||
"```",
|
||||
"outro",
|
||||
]);
|
||||
|
||||
const nextCodeFence = jumpToNextCodeFence(
|
||||
cm as any,
|
||||
expectNextCodeFencePosition(
|
||||
{ line: 0, ch: 0 },
|
||||
{ repeat: 1 }
|
||||
{ line: 1, ch: 0 }
|
||||
);
|
||||
|
||||
expect(nextCodeFence).toEqual({ line: 1, ch: 0 });
|
||||
});
|
||||
|
||||
test("jumps to the next closing code fence", () => {
|
||||
const cm = createFakeCodeMirrorEditor([
|
||||
"intro",
|
||||
"```ts",
|
||||
"const answer = 42;",
|
||||
"```",
|
||||
"outro",
|
||||
]);
|
||||
|
||||
const nextCodeFence = jumpToNextCodeFence(
|
||||
cm as any,
|
||||
expectNextCodeFencePosition(
|
||||
{ line: 2, ch: 0 },
|
||||
{ repeat: 1 }
|
||||
{ line: 3, ch: 0 }
|
||||
);
|
||||
|
||||
expect(nextCodeFence).toEqual({ line: 3, ch: 0 });
|
||||
});
|
||||
|
||||
test("wraps to the first code fence after the last one", () => {
|
||||
const cm = createFakeCodeMirrorEditor([
|
||||
"intro",
|
||||
"```ts",
|
||||
"const answer = 42;",
|
||||
"```",
|
||||
"outro",
|
||||
]);
|
||||
|
||||
const nextCodeFence = jumpToNextCodeFence(
|
||||
cm as any,
|
||||
expectNextCodeFencePosition(
|
||||
{ line: 4, ch: 0 },
|
||||
{ repeat: 1 }
|
||||
{ line: 1, ch: 0 }
|
||||
);
|
||||
|
||||
expect(nextCodeFence).toEqual({ line: 1, ch: 0 });
|
||||
});
|
||||
|
||||
test("ignores backticks that are not code fence lines", () => {
|
||||
const cm = createFakeCodeMirrorEditor([
|
||||
"here are inline backticks ``` not a fence",
|
||||
"```ts",
|
||||
"const answer = 42;",
|
||||
"```",
|
||||
]);
|
||||
|
||||
const nextCodeFence = jumpToNextCodeFence(
|
||||
cm as any,
|
||||
expectNextCodeFencePosition(
|
||||
{ line: 0, ch: 0 },
|
||||
{ repeat: 1 }
|
||||
{ line: 1, ch: 0 },
|
||||
[
|
||||
"here are inline backticks ``` not a fence",
|
||||
"```ts",
|
||||
"const answer = 42;",
|
||||
"```",
|
||||
]
|
||||
);
|
||||
|
||||
expect(nextCodeFence).toEqual({ line: 1, ch: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("jumpToPreviousCodeFence", () => {
|
||||
test("jumps to the previous closing code fence", () => {
|
||||
const cm = createFakeCodeMirrorEditor([
|
||||
"intro",
|
||||
"```ts",
|
||||
"const answer = 42;",
|
||||
"```",
|
||||
"outro",
|
||||
]);
|
||||
|
||||
const previousCodeFence = jumpToPreviousCodeFence(
|
||||
cm as any,
|
||||
expectPreviousCodeFencePosition(
|
||||
{ line: 4, ch: 0 },
|
||||
{ repeat: 1 }
|
||||
{ line: 3, ch: 0 }
|
||||
);
|
||||
|
||||
expect(previousCodeFence).toEqual({ line: 3, ch: 0 });
|
||||
});
|
||||
|
||||
test("jumps to the previous opening code fence", () => {
|
||||
const cm = createFakeCodeMirrorEditor([
|
||||
"intro",
|
||||
"```ts",
|
||||
"const answer = 42;",
|
||||
"```",
|
||||
"outro",
|
||||
]);
|
||||
|
||||
const previousCodeFence = jumpToPreviousCodeFence(
|
||||
cm as any,
|
||||
expectPreviousCodeFencePosition(
|
||||
{ line: 2, ch: 0 },
|
||||
{ repeat: 1 }
|
||||
{ line: 1, ch: 0 }
|
||||
);
|
||||
|
||||
expect(previousCodeFence).toEqual({ line: 1, ch: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
function expectNextCodeFencePosition(
|
||||
cursorPosition: { line: number; ch: number },
|
||||
expectedPosition: { line: number; ch: number },
|
||||
contentLines: string[] = CODE_FENCE_CONTENT_LINES
|
||||
): void {
|
||||
const cm = createFakeCodeMirrorEditor(contentLines);
|
||||
const nextCodeFence = jumpToNextCodeFence(cm as any, cursorPosition, { repeat: 1 });
|
||||
expect(nextCodeFence).toEqual(expectedPosition);
|
||||
}
|
||||
|
||||
function expectPreviousCodeFencePosition(
|
||||
cursorPosition: { line: number; ch: number },
|
||||
expectedPosition: { line: number; ch: number },
|
||||
contentLines: string[] = CODE_FENCE_CONTENT_LINES
|
||||
): void {
|
||||
const cm = createFakeCodeMirrorEditor(contentLines);
|
||||
const previousCodeFence = jumpToPreviousCodeFence(cm as any, cursorPosition, { repeat: 1 });
|
||||
expect(previousCodeFence).toEqual(expectedPosition);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue