deno.land / std@0.224.0 / path / basename_test.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// Copyright the Browserify authors. MIT License.// Ported from https://github.com/browserify/path-browserify/import { assertEquals } from "../assert/mod.ts";import { basename } from "./basename.ts";import * as posix from "./posix/mod.ts";import * as windows from "./windows/mod.ts";
// Test suite from "GNU core utilities"// https://github.com/coreutils/coreutils/blob/master/tests/misc/basename.plconst COREUTILS_TESTSUITE = [ [["d/f"], "f"], [["/d/f"], "f"], [["d/f/"], "f"], [["d/f//"], "f"], [["f"], "f"], [["/"], "/"], [["///"], "/"], [["///a///"], "a"], [[""], ""], [["aa", "a"], "a"], [["a-a", "-a"], "a"], [["f.s", ".s"], "f"], [["fs", "s"], "f"], [["fs", "fs"], "fs"], [["fs/", "s"], "f"], [["dir/file.suf", ".suf"], "file"], [["fs", "x"], "fs"], [["fs", ""], "fs"], [["fs/", "s/"], "fs"],] as const;
const POSIX_TESTSUITE = [ [[""], ""], [["/dir/basename.ext"], "basename.ext"], [["/basename.ext"], "basename.ext"], [["basename.ext"], "basename.ext"], [["basename.ext/"], "basename.ext"], [["basename.ext//"], "basename.ext"], [["aaa/bbb", "/bbb"], "bbb"], [["aaa/bbb", "a/bbb"], "bbb"], [["aaa/bbb", "bbb"], "bbb"], [["aaa/bbb//", "bbb"], "bbb"], [["aaa/bbb", "bb"], "b"], [["aaa/bbb", "b"], "bb"], [["/aaa/bbb", "/bbb"], "bbb"], [["/aaa/bbb", "a/bbb"], "bbb"], [["/aaa/bbb", "bbb"], "bbb"], [["/aaa/bbb//", "bbb"], "bbb"], [["/aaa/bbb//", "a/bbb"], "bbb"], [["/aaa/bbb", "bb"], "b"], [["/aaa/bbb", "b"], "bb"], [["/aaa/bbb"], "bbb"], [["/aaa/"], "aaa"], [["/aaa/b"], "b"], [["/a/b"], "b"], [["//a"], "a"], [["///"], "/"], [["///", "bbb"], "/"], [["//", "bbb"], "/"],] as const;
const WIN32_TESTSUITE = [ [["\\dir\\basename.ext"], "basename.ext"], [["\\basename.ext"], "basename.ext"], [["basename.ext"], "basename.ext"], [["basename.ext\\"], "basename.ext"], [["basename.ext\\\\"], "basename.ext"], [["foo"], "foo"], [["aaa\\bbb", "\\bbb"], "bbb"], [["aaa\\bbb", "a\\bbb"], "bbb"], [["aaa\\bbb", "bbb"], "bbb"], [["aaa\\bbb\\\\\\\\", "bbb"], "bbb"], [["aaa\\bbb", "bb"], "b"], [["aaa\\bbb", "b"], "bb"], [["/aaa/bbb", "bb"], "b"], [["C:"], ""], [["C:."], "."], [["C:\\"], "\\"], [["C:\\dir\\base.ext"], "base.ext"], [["C:\\basename.ext"], "basename.ext"], [["C:basename.ext"], "basename.ext"], [["C:basename.ext\\"], "basename.ext"], [["C:basename.ext\\\\"], "basename.ext"], [["C:foo"], "foo"], [["file:stream"], "file:stream"],] as const;
Deno.test("posix.basename()", function () { for (const [[name, suffix], expected] of COREUTILS_TESTSUITE) { assertEquals(basename(name, suffix), expected); }
for (const [[name, suffix], expected] of POSIX_TESTSUITE) { assertEquals(posix.basename(name, suffix), expected); }
// On unix a backslash is just treated as any other character. assertEquals( posix.basename("\\dir\\basename.ext"), "\\dir\\basename.ext", ); assertEquals(posix.basename("\\basename.ext"), "\\basename.ext"); assertEquals(posix.basename("basename.ext"), "basename.ext"); assertEquals(posix.basename("basename.ext\\"), "basename.ext\\"); assertEquals(posix.basename("basename.ext\\\\"), "basename.ext\\\\"); assertEquals(posix.basename("foo"), "foo");
// POSIX filenames may include control characters const controlCharFilename = "Icon" + String.fromCharCode(13); assertEquals( posix.basename("/a/b/" + controlCharFilename), controlCharFilename, );});
Deno.test("windows.basename()", function () { for (const [[name, suffix], expected] of WIN32_TESTSUITE) { assertEquals(windows.basename(name, suffix), expected); }
// windows should pass all "forward slash" posix tests as well. for (const [[name, suffix], expected] of COREUTILS_TESTSUITE) { assertEquals(windows.basename(name, suffix), expected); }
for (const [[name, suffix], expected] of POSIX_TESTSUITE) { assertEquals(windows.basename(name, suffix), expected); }});
Version Info