Skip to content

Commit

Permalink
Add test for loader
Browse files Browse the repository at this point in the history
  • Loading branch information
uga-rosa committed Dec 7, 2023
1 parent e400711 commit 96f87ed
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 1 deletion.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"pre-commit": "deno task fmt && deno task check && deno task test",
"fmt": "deno fmt **/*.ts",
"check": "deno check **/*.ts && deno fmt --check **/*.ts && deno lint",
"test": "deno test -A",
"test": "deno test -A denops/**/*_test.ts",
}
}
77 changes: 77 additions & 0 deletions denops/denippet/loader_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Loader } from "./loader.ts";
import { op, test } from "./deps/denops.ts";
import { assertEquals, path } from "./deps/std.ts";

const testDataDir = new URL("../../test/data", import.meta.url).pathname;

test({
mode: "all",
name: "loader",
fn: async (denops, t) => {
const loader = new Loader(denops);

await loader.load(path.join(testDataDir, "global.json"), "*");
await loader.load(path.join(testDataDir, "typescript.toml"), "typescript");
await loader.load(path.join(testDataDir, "lua.yaml"), "lua");
await loader.load(path.join(testDataDir, "vim.ts"), "vim");
await loader.load(path.join(testDataDir, "global.code-snippets"), "*");

await t.step({
name: "global",
fn: async () => {
const ft = "";
await op.filetype.set(denops, ft);
const snippets = await loader.get(ft);
assertEquals(snippets.length, 1);
},
});

await t.step({
name: "toml",
fn: async () => {
const ft = "typescript";
await op.filetype.set(denops, ft);
const snippets = await loader.get(ft);
assertEquals(snippets.length, 3);
},
});

await t.step({
name: "yaml",
fn: async () => {
const ft = "lua";
await op.filetype.set(denops, ft);
const snippets = await loader.get(ft);
assertEquals(snippets.length, 4);
},
});

await t.step({
name: "typescript",
fn: async () => {
const ft = "vim";
await op.filetype.set(denops, ft);
const snippets = await loader.get(ft);
assertEquals(snippets.length, 2);
},
});

await t.step({
name: "code-snippets",
fn: async () => {
{
const ft = "foo";
await op.filetype.set(denops, ft);
const snippets = await loader.get(ft);
assertEquals(snippets.length, 2);
}
{
const ft = "bar";
await op.filetype.set(denops, ft);
const snippets = await loader.get(ft);
assertEquals(snippets.length, 2);
}
},
});
},
});
12 changes: 12 additions & 0 deletions test/data/global.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"foo": {
"prefix": "foo",
"scope": "foo",
"body": "foo $1"
},
"bar": {
"prefix": "bar",
"scope": "bar",
"body": "bar $1"
}
}
6 changes: 6 additions & 0 deletions test/data/global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"global": {
"prefix": "global",
"body": "global $1"
}
}
17 changes: 17 additions & 0 deletions test/data/lua.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local:
prefix: l
body: local $1 = $0

function:
prefix: fn
body:
- function $1($2)
- "\t$0"
- end

if:
prefix: if
body: |-
if $1 then
$0
end
11 changes: 11 additions & 0 deletions test/data/typescript.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[print]
prefix = "p"
body = "console.log($0)"

[if]
prefix = "if"
body = """
if ($1) {
$0
}\
"""
18 changes: 18 additions & 0 deletions test/data/vim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TSSnippet } from "../../denops/denippet/loader.ts";
import { Denops, fn } from "../../denops/denippet/deps/denops.ts";

export const snippets: Record<string, TSSnippet> = {
"autoload function": {
prefix: "fn",
body: async (denops: Denops) => {
const path = await fn.expand(denops, "%:p") as string;
const match = path.match(/autoload\/(.+)\.vim$/);
const prefix = match ? match[1].replaceAll("/", "#") + "#" : "";
return [
`function ${prefix}$1($2) abort`,
"\t$0",
"endfunction",
];
},
},
};

0 comments on commit 96f87ed

Please sign in to comment.