-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(path): improve test coverage (#5038)
- Loading branch information
Showing
6 changed files
with
185 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals, assertThrows } from "@std/assert"; | ||
import { assertPath } from "./assert_path.ts"; | ||
|
||
Deno.test("assertPath()", () => { | ||
assertEquals(assertPath(""), undefined); | ||
assertEquals(assertPath("foo"), undefined); | ||
}); | ||
|
||
Deno.test("assertPath() throws", () => { | ||
assertThrows( | ||
() => assertPath(undefined), | ||
TypeError, | ||
"Path must be a string. Received undefined", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals, assertThrows } from "@std/assert"; | ||
import { assertArgs, lastPathSegment, stripSuffix } from "./basename.ts"; | ||
import { CHAR_FORWARD_SLASH } from "./constants.ts"; | ||
|
||
Deno.test("assertArgs()", () => { | ||
assertEquals(assertArgs("", ""), ""); | ||
assertEquals(assertArgs("foo", "bar"), undefined); | ||
// @ts-expect-error - testing invalid input for suffix | ||
assertEquals(assertArgs("", undefined), ""); | ||
}); | ||
|
||
Deno.test("assertArgs() throws", () => { | ||
assertThrows( | ||
// @ts-expect-error - testing invalid input | ||
() => assertArgs(undefined, "bar"), | ||
TypeError, | ||
"Path must be a string. Received undefined", | ||
); | ||
assertThrows( | ||
// @ts-expect-error - testing invalid input | ||
() => assertArgs("foo", undefined), | ||
TypeError, | ||
"Suffix must be a string. Received undefined", | ||
); | ||
}); | ||
|
||
Deno.test("lastPathSegment()", () => { | ||
assertEquals( | ||
lastPathSegment("foo", (char) => char === CHAR_FORWARD_SLASH), | ||
"foo", | ||
); | ||
assertEquals( | ||
lastPathSegment("foo/bar", (char) => char === CHAR_FORWARD_SLASH), | ||
"bar", | ||
); | ||
assertEquals( | ||
lastPathSegment("foo/bar/baz", (char) => char === CHAR_FORWARD_SLASH), | ||
"baz", | ||
); | ||
}); | ||
|
||
Deno.test("stripSuffix()", () => { | ||
assertEquals(stripSuffix("foo", "bar"), "foo"); | ||
assertEquals(stripSuffix("foobar", "bar"), "foo"); | ||
assertEquals(stripSuffix("foobar", "baz"), "foobar"); | ||
assertEquals(stripSuffix("foobar", "foobar"), "foobar"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals, assertThrows } from "@std/assert"; | ||
import { _format, assertArg } from "./format.ts"; | ||
|
||
Deno.test("_format()", () => { | ||
assertEquals(_format("", {}), ""); | ||
assertEquals(_format("", { root: "/" }), "/"); | ||
assertEquals(_format("", { dir: "/foo/bar" }), "/foo/bar"); | ||
assertEquals(_format("", { base: "baz" }), "baz"); | ||
assertEquals(_format("", { name: "baz" }), "baz"); | ||
assertEquals(_format("", { ext: ".js" }), ".js"); | ||
assertEquals(_format("", { name: "baz", ext: ".js" }), "baz.js"); | ||
assertEquals(_format("", { root: "/", base: "baz" }), "/baz"); | ||
assertEquals(_format("", { root: "/", name: "baz" }), "/baz"); | ||
assertEquals(_format("", { root: "/", ext: ".js" }), "/.js"); | ||
assertEquals(_format("", { root: "/", name: "baz", ext: ".js" }), "/baz.js"); | ||
assertEquals(_format("/", { dir: "/foo/bar", base: "baz" }), "/foo/bar/baz"); | ||
assertEquals( | ||
_format("/", { dir: "/foo/bar", base: "baz", ext: ".js" }), | ||
"/foo/bar/baz", | ||
); | ||
assertEquals( | ||
_format("/", { dir: "/foo/bar", name: "baz", ext: ".js" }), | ||
"/foo/bar/baz.js", | ||
); | ||
}); | ||
|
||
Deno.test("assertArg()", () => { | ||
assertEquals(assertArg({}), undefined); | ||
assertEquals(assertArg({ root: "/" }), undefined); | ||
assertEquals(assertArg({ dir: "/foo/bar" }), undefined); | ||
}); | ||
|
||
Deno.test("assertArg() throws", () => { | ||
assertThrows( | ||
// @ts-expect-error - testing invalid input | ||
() => assertArg(null), | ||
TypeError, | ||
`The "pathObject" argument must be of type Object. Received type object`, | ||
); | ||
assertThrows( | ||
// @ts-expect-error - testing invalid input | ||
() => assertArg(undefined), | ||
TypeError, | ||
`The "pathObject" argument must be of type Object. Received type undefined`, | ||
); | ||
assertThrows( | ||
// @ts-expect-error - testing invalid input | ||
() => assertArg(""), | ||
TypeError, | ||
`The "pathObject" argument must be of type Object. Received type string`, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "@std/assert"; | ||
import { CHAR_FORWARD_SLASH } from "./constants.ts"; | ||
import { normalizeString } from "./normalize_string.ts"; | ||
|
||
function isSeparator(code: number): boolean { | ||
return code === CHAR_FORWARD_SLASH; | ||
} | ||
|
||
Deno.test("normalizeSring()", () => { | ||
assertEquals(normalizeString("", true, "/", isSeparator), ""); | ||
assertEquals(normalizeString("", false, "/", isSeparator), ""); | ||
assertEquals(normalizeString("a/../b", true, "/", isSeparator), "b"); | ||
assertEquals(normalizeString("foo/bar/", true, "/", isSeparator), "foo/bar"); | ||
assertEquals(normalizeString("/foo/bar", true, "/", isSeparator), "foo/bar"); | ||
assertEquals(normalizeString("./foo/bar", true, "/", isSeparator), "foo/bar"); | ||
assertEquals( | ||
normalizeString("../foo/bar/baz/", true, "/", isSeparator), | ||
"../foo/bar/baz", | ||
); | ||
assertEquals( | ||
normalizeString("/foo/../../bar", true, "/", isSeparator), | ||
"../bar", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "@std/assert"; | ||
import { encodeWhitespace } from "./to_file_url.ts"; | ||
|
||
Deno.test("encodeWhitespace()", () => { | ||
assertEquals(encodeWhitespace("foo"), "foo"); | ||
assertEquals(encodeWhitespace("foo\tbar"), "foo%09bar"); | ||
assertEquals(encodeWhitespace("foo\nbar"), "foo%0Abar"); | ||
assertEquals(encodeWhitespace("foo\vbar"), "foo%0Bbar"); | ||
assertEquals(encodeWhitespace("foo\fbar"), "foo%0Cbar"); | ||
assertEquals(encodeWhitespace("foo\rbar"), "foo%0Dbar"); | ||
assertEquals(encodeWhitespace("foo bar"), "foo%20bar"); | ||
assertEquals(encodeWhitespace("foo\u0009bar"), "foo%09bar"); | ||
assertEquals(encodeWhitespace("foo\u000Abar"), "foo%0Abar"); | ||
assertEquals(encodeWhitespace("foo\u000Bbar"), "foo%0Bbar"); | ||
assertEquals(encodeWhitespace("foo\u000Cbar"), "foo%0Cbar"); | ||
assertEquals(encodeWhitespace("foo\u000Dbar"), "foo%0Dbar"); | ||
assertEquals(encodeWhitespace("foo\u0020bar"), "foo%20bar"); | ||
assertEquals(encodeWhitespace("foo\ufeffbar"), "foobar"); | ||
}); |