Skip to content

Commit

Permalink
test(step): Use step from Deno.TestContext (#82)
Browse files Browse the repository at this point in the history
It makes the result clean and readable.
  • Loading branch information
5ouma committed Jun 17, 2024
1 parent 4471af7 commit 39d8aee
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 129 deletions.
4 changes: 2 additions & 2 deletions src/libs/io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export async function readTOML(file: string): Promise<Lists> {
return convertFromTOML(data);
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
throw new Error(`File not found: "${file}"`);
throw new Error(`file not found: "${file}"`);
} else if (error instanceof Deno.errors.PermissionDenied) {
throw new Error(`Permission denied: "${file}"`);
throw new Error(`permission denied: "${file}"`);
} else throw error;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libs/sites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export function transcodeXmlUrl(
type: string | undefined,
id: string | undefined,
): URL {
if (!type) throw new Error(`Parameter not set: "type" of "${title}"`);
if (!id) throw new Error(`Parameter not set: "id" of "${title}"`);
if (!type) throw new Error(`parameter not set: "type" of "${title}"`);
if (!id) throw new Error(`parameter not set: "id" of "${title}"`);

const url: URL | undefined = sites
.find((site: site) => site.type === type)?.url;
if (!url) throw new Error(`Site not found: "${type}" of "${title}"`);
if (!url) throw new Error(`site not found: "${type}" of "${title}"`);

return new URL(url.href.replace(encodeURI("{id}"), id));
}
98 changes: 51 additions & 47 deletions test/libs/convert_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { assertEquals } from "@std/assert";
import { convertFromTOML, convertToOPML } from "../../src/libs/convert.ts";
import type { List, Lists } from "../../src/types/mod.ts";

Deno.test("Parse TOML (RSS)", () => {
const toml = `
Deno.test("Parse TOML", async (t: Deno.TestContext) => {
await t.step("rss", () => {
const toml = `
[[lists]]
name = "list name"
Expand All @@ -12,21 +13,21 @@ title = "feed title"
xmlUrl = "https://example.com/feed"
`;

const feeds: Lists = {
lists: [{
name: "list name",
feeds: [{
title: "feed title",
xmlUrl: new URL("https://example.com/feed"),
const feeds: Lists = {
lists: [{
name: "list name",
feeds: [{
title: "feed title",
xmlUrl: new URL("https://example.com/feed"),
}],
}],
}],
};
};

assertEquals(convertFromTOML(toml), feeds);
});
assertEquals(convertFromTOML(toml), feeds);
});

Deno.test("Parse TOML (Site)", () => {
const toml = `
await t.step("site", () => {
const toml = `
[[lists]]
name = "list name"
Expand All @@ -36,23 +37,25 @@ type = "bluesky"
id = "username"
`;

const feeds: Lists = {
lists: [{
name: "list name",
feeds: [{
title: "feed title",
type: "bluesky",
id: "username",
xmlUrl: new URL("https://bsky.app/profile/username/rss"),
const feeds: Lists = {
lists: [{
name: "list name",
feeds: [{
title: "feed title",
type: "bluesky",
id: "username",
xmlUrl: new URL("https://bsky.app/profile/username/rss"),
}],
}],
}],
};
};

assertEquals(convertFromTOML(toml), feeds);
assertEquals(convertFromTOML(toml), feeds);
});
});

Deno.test("Convert Lists to OPML (RSS)", () => {
const xml = `\
Deno.test("Convert Lists to OPML", async (t: Deno.TestContext) => {
await t.step("rss", () => {
const xml = `\
<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<body>
Expand All @@ -61,19 +64,19 @@ Deno.test("Convert Lists to OPML (RSS)", () => {
</opml>
`;

const list: List = {
name: "list name",
feeds: [{
title: "feed title",
xmlUrl: new URL("https://example.com/feed"),
}],
};
const list: List = {
name: "list name",
feeds: [{
title: "feed title",
xmlUrl: new URL("https://example.com/feed"),
}],
};

assertEquals(convertToOPML(list), xml);
});
assertEquals(convertToOPML(list), xml);
});

Deno.test("Convert Lists to OPML (Site)", () => {
const xml = `\
await t.step("site", () => {
const xml = `\
<?xml version="1.0" encoding="UTF-8"?>
<opml version="2.0">
<body>
Expand All @@ -82,14 +85,15 @@ Deno.test("Convert Lists to OPML (Site)", () => {
</opml>
`;

const list: List = {
name: "list name",
feeds: [{
title: "feed title",
type: "bluesky",
id: "username",
}],
};
const list: List = {
name: "list name",
feeds: [{
title: "feed title",
type: "bluesky",
id: "username",
}],
};

assertEquals(convertToOPML(list), xml);
assertEquals(convertToOPML(list), xml);
});
});
78 changes: 38 additions & 40 deletions test/libs/io_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { readTOML, writeXML } from "../../src/libs/io.ts";
import { convertFromTOML, convertToOPML } from "../../src/libs/convert.ts";
import type { Lists } from "../../src/types/mod.ts";

Deno.test("Read TOML", async () => {
const toml = `
Deno.test("Read TOML", async (t: Deno.TestContext) => {
await t.step("normal", async () => {
const toml = `
[[lists]]
name = "list name"
Expand All @@ -14,52 +15,49 @@ title = "feed title"
xmlUrl = "https://example.com/feed"
`;

const file: string = await Deno.makeTempFile({ suffix: ".toml" });
await Deno.writeTextFile(file, toml);
const lists: Lists = await readTOML(file);
const file: string = await Deno.makeTempFile({ suffix: ".toml" });
await Deno.writeTextFile(file, toml);
const lists: Lists = await readTOML(file);

assertEquals(convertFromTOML(toml), lists);
});
assertEquals(convertFromTOML(toml), lists);
});

Deno.test("Read TOML (File not found)", async () => {
try {
await readTOML("file-not-found.toml");
} catch (error) {
assertEquals(error.message, 'File not found: "file-not-found.toml"');
}
});
await t.step("file not found", async () => {
try {
await readTOML("file-not-found.toml");
} catch (error) {
assertEquals(error.message, 'file not found: "file-not-found.toml"');
}
});

Deno.test("Read TOML (Permission denied)", async () => {
const file: string = await Deno.makeTempFile({ suffix: ".toml" });
await Deno.chmod(file, 0o000);
try {
await readTOML(file);
} catch (error) {
assertEquals(error.message, `Permission denied: "${file}"`);
}
});
await t.step("permission denied", async () => {
const file: string = await Deno.makeTempFile({ suffix: ".toml" });
await Deno.chmod(file, 0o000);
try {
await readTOML(file);
} catch (error) {
assertEquals(error.message, `permission denied: "${file}"`);
}
});

Deno.test("Read TOML (Unexpected error)", async () => {
try {
await readTOML("");
} catch (error) {
assertIsError(error);
}
await t.step("unexpected error", async () => {
try {
await readTOML("");
} catch (error) {
assertIsError(error);
}
});
});

Deno.test("Write XML", async () => {
const feeds: Lists = {
lists: [
{
name: "list name",
feeds: [
{
title: "feed title",
xmlUrl: new URL("https://example.com/feed"),
},
],
},
],
lists: [{
name: "list name",
feeds: [{
title: "feed title",
xmlUrl: new URL("https://example.com/feed"),
}],
}],
};

const dir: string = await Deno.makeTempDir();
Expand Down
80 changes: 43 additions & 37 deletions test/libs/sites_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,50 @@ import { assertEquals } from "@std/assert";
import { transcodeXmlUrl } from "../../src/libs/sites.ts";
import type { Feed } from "../../src/types/mod.ts";

Deno.test("Get XML URL (Site)", () => {
const feed: Feed = {
title: "feed title",
type: "bluesky",
id: "username",
};
assertEquals(
transcodeXmlUrl(feed.title, feed.type, feed.id),
new URL("https://bsky.app/profile/username/rss"),
);
});

Deno.test("Get XML URL (type not set)", () => {
const feed: Feed = { title: "feed title", id: "username" };
try {
transcodeXmlUrl(feed.title, undefined, feed.id);
} catch (error) {
assertEquals(error.message, `Parameter not set: "type" of "${feed.title}"`);
}
});

Deno.test("Get XML URL (id not set)", () => {
const feed: Feed = { title: "feed title", type: "bluesky" };
try {
transcodeXmlUrl(feed.title, feed.type, undefined);
} catch (error) {
assertEquals(error.message, `Parameter not set: "id" of "${feed.title}"`);
}
});
Deno.test("Get XML URL", async (t: Deno.TestContext) => {
await t.step("site", () => {
const feed: Feed = {
title: "feed title",
type: "bluesky",
id: "username",
};

Deno.test("Get XML URL (Site not found)", () => {
const feed: Feed = { title: "feed title", type: "unknown", id: "username" };
try {
transcodeXmlUrl(feed.title, feed.type, feed.id);
} catch (error) {
assertEquals(
error.message,
`Site not found: "${feed.type}" of "${feed.title}"`,
transcodeXmlUrl(feed.title, feed.type, feed.id),
new URL("https://bsky.app/profile/username/rss"),
);
}
});

await t.step("type not set", () => {
const feed: Feed = { title: "feed title", id: "username" };
try {
transcodeXmlUrl(feed.title, undefined, feed.id);
} catch (error) {
assertEquals(
error.message,
`parameter not set: "type" of "${feed.title}"`,
);
}
});

await t.step("id not set", () => {
const feed: Feed = { title: "feed title", type: "bluesky" };
try {
transcodeXmlUrl(feed.title, feed.type, undefined);
} catch (error) {
assertEquals(error.message, `parameter not set: "id" of "${feed.title}"`);
}
});

await t.step("site not found", () => {
const feed: Feed = { title: "feed title", type: "unknown", id: "username" };
try {
transcodeXmlUrl(feed.title, feed.type, feed.id);
} catch (error) {
assertEquals(
error.message,
`site not found: "${feed.type}" of "${feed.title}"`,
);
}
});
});

0 comments on commit 39d8aee

Please sign in to comment.