diff --git a/test/path/fixtures/file b/test/path/fixtures/file new file mode 100644 index 0000000..9b5c6b7 --- /dev/null +++ b/test/path/fixtures/file @@ -0,0 +1 @@ +This is test data \ No newline at end of file diff --git a/test/path/fixtures/file_symlink b/test/path/fixtures/file_symlink new file mode 120000 index 0000000..7b22242 --- /dev/null +++ b/test/path/fixtures/file_symlink @@ -0,0 +1 @@ +./file \ No newline at end of file diff --git a/test/path/platform_test.dart b/test/path/platform_test.dart new file mode 100644 index 0000000..8736008 --- /dev/null +++ b/test/path/platform_test.dart @@ -0,0 +1,60 @@ +// ignore_for_file: pattern_never_matches_value_type, unused_local_variable + +import 'package:rust/rust.dart'; +import 'package:test/expect.dart'; +import 'package:test/scaffolding.dart'; + +void main() { + test("readLinkSync", () { + if (Path.isIoSupported()) { + expect( + Path("test/path/fixtures/file_symlink").readLinkSync().unwrap(), + endsWith("test/path/fixtures/file"), + ); + } else { + expect( + () => Path("test/path/fixtures/file_symlink").readLinkSync(), + throwsA(isA()), + ); + } + }); + + test("readLink", () async { + if (Path.isIoSupported()) { + expect( + (await Path("test/path/fixtures/file_symlink").readLink()).unwrap(), + endsWith("test/path/fixtures/file"), + ); + } else { + expect( + () async => await Path("test/path/fixtures/file_symlink").readLink(), + throwsA(isA()), + ); + } + }); + + test("metadata", () async { + if (Path.isIoSupported()) { + final metadata = await Path("test/path/fixtures/file").metadata(); + // Dev Note: uncommenting below will cause a compilation error when the target is web. + // DateTime accessed = metadata.accessed; + } else { + expect( + () async => await Path("test/path/fixtures/file").metadata(), + throwsA(isA()), + ); + } + }); + + test("metadataSync", () { + if (Path.isIoSupported()) { + final metadata = Path("test/path/fixtures/file").metadataSync(); + // Dev Note: uncommenting below will cause a compilation error when the target is web. + } else { + expect( + () => Path("test/path/fixtures/file").metadataSync(), + throwsA(isA()), + ); + } + }); +} diff --git a/test/path/unix_path_test.dart b/test/path/unix_path_test.dart new file mode 100644 index 0000000..c6cc089 --- /dev/null +++ b/test/path/unix_path_test.dart @@ -0,0 +1,183 @@ +// ignore_for_file: pattern_never_matches_value_type + +import 'package:rust/rust.dart'; +import 'package:test/expect.dart'; +import 'package:test/scaffolding.dart'; + +void main() { + test("filePrefix", () { + expect(UnixPath("foo.rs").filePrefix().unwrap(), "foo"); + expect(UnixPath("foo/").filePrefix().unwrap(), "foo"); + expect(UnixPath(".foo").filePrefix().unwrap(), ".foo"); + expect(UnixPath(".foo.rs").filePrefix().unwrap(), "foo"); + expect(UnixPath("foo").filePrefix().unwrap(), "foo"); + expect(UnixPath("foo.tar.gz").filePrefix().unwrap(), "foo"); + expect(UnixPath("temp/foo.tar.gz").filePrefix().unwrap(), "foo"); + expect(UnixPath("/foo/.tmp.bar.tar").filePrefix().unwrap(), "tmp"); + expect(UnixPath("").filePrefix().isNone(), true); + + expect( + UnixPath("/Downloads/The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .filePrefix() + .unwrap(), + "The Annual Report on the Health of the Parish of St"); + }); + + test("fileStem", () { + expect(UnixPath("foo.rs").fileStem().unwrap(), "foo"); + expect(UnixPath("foo/").filePrefix().unwrap(), "foo"); + expect(UnixPath(".foo").fileStem().unwrap(), ".foo"); + expect(UnixPath(".foo.rs").fileStem().unwrap(), ".foo"); + expect(UnixPath("foo").fileStem().unwrap(), "foo"); + expect(UnixPath("foo.tar.gz").fileStem().unwrap(), "foo.tar"); + expect(UnixPath("temp/foo.tar.gz").fileStem().unwrap(), "foo.tar"); + expect(UnixPath("").fileStem().isNone(), true); + + expect( + UnixPath("/Downloads/The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .fileStem() + .unwrap(), + "The Annual Report on the Health of the Parish of St"); + }); + + test("parent", () { + expect(UnixPath("temp/foo.rs").parent().unwrap(), UnixPath("temp")); + expect(UnixPath("foo/").parent().unwrap(), UnixPath("")); + expect(UnixPath("/foo/").parent().unwrap(), UnixPath("/")); + expect(UnixPath(".foo").parent().unwrap(), UnixPath("")); + expect(UnixPath(".foo.rs").parent().unwrap(), UnixPath("")); + expect(UnixPath("foo").parent().unwrap(), UnixPath("")); + expect(UnixPath("foo.tar.gz").parent().unwrap(), UnixPath("")); + expect(UnixPath("temp/foo.tar.gz").parent().unwrap(), UnixPath("temp")); + expect( + UnixPath("temp1/temp2/foo.tar.gz").parent().unwrap(), UnixPath("temp1/temp2")); + expect( + UnixPath("temp1/temp2//foo.tar.gz").parent().unwrap(), UnixPath("temp1/temp2")); + expect(UnixPath("").parent().isNone(), true); + + expect( + UnixPath("/Downloads/The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .parent() + .unwrap(), + UnixPath("/Downloads")); + }); + + test("ancestors", () { + var ancestors = UnixPath("/foo/bar").ancestors().iterator; + ancestors.moveNext(); + expect(ancestors.current, UnixPath("/foo/bar")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("/foo")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("/")); + expect(ancestors.moveNext(), false); + + ancestors = UnixPath("../foo/bar").ancestors().iterator; + ancestors.moveNext(); + expect(ancestors.current, UnixPath("../foo/bar")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("../foo")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("..")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("")); + expect(ancestors.moveNext(), false); + + ancestors = UnixPath("foo/bar").ancestors().iterator; + ancestors.moveNext(); + expect(ancestors.current, UnixPath("foo/bar")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("foo")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("")); + expect(ancestors.moveNext(), false); + + ancestors = UnixPath("foo/..").ancestors().iterator; + ancestors.moveNext(); + expect(ancestors.current, UnixPath("foo/..")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("foo")); + + ancestors = UnixPath( + "/Downloads/The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .ancestors() + .iterator; + ancestors.moveNext(); + expect( + ancestors.current, + UnixPath( + "/Downloads/The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("/Downloads")); + ancestors.moveNext(); + expect(ancestors.current, UnixPath("/")); + }); + + test("withExtension", () { + expect(UnixPath("foo").withExtension("rs"), UnixPath("foo.rs")); + expect(UnixPath("foo.rs").withExtension("rs"), UnixPath("foo.rs")); + expect(UnixPath("foo.tar.gz").withExtension("rs"), UnixPath("foo.tar.rs")); + expect(UnixPath("foo.tar.gz").withExtension(""), UnixPath("foo.tar")); + expect(UnixPath("foo.tar.gz").withExtension("tar.gz"), UnixPath("foo.tar.tar.gz")); + expect(UnixPath("/tmp/foo.tar.gz").withExtension("tar.gz"), + UnixPath("/tmp/foo.tar.tar.gz")); + expect(UnixPath("tmp/foo").withExtension("tar.gz"), UnixPath("tmp/foo.tar.gz")); + expect( + UnixPath("tmp/.foo.tar").withExtension("tar.gz"), UnixPath("tmp/.foo.tar.gz")); + + expect( + UnixPath("/Downloads/The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .withExtension(""), + UnixPath("/Downloads/The Annual Report on the Health of the Parish of St")); + }); + + test("withFileName", () { + expect(UnixPath("foo").withFileName("bar"), UnixPath("bar")); + expect(UnixPath("foo.rs").withFileName("bar"), UnixPath("bar")); + expect(UnixPath("foo.tar.gz").withFileName("bar"), UnixPath("bar")); + expect(UnixPath("/tmp/foo.tar.gz").withFileName("bar"), UnixPath("/tmp/bar")); + expect(UnixPath("tmp/foo").withFileName("bar"), UnixPath("tmp/bar")); + expect(UnixPath("/var").withFileName("bar"), UnixPath("/bar")); + + expect( + UnixPath("/Downloads/The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .withFileName("dave"), + UnixPath("/Downloads/dave")); + }); + + test("extension", () { + expect(UnixPath("foo").extension(), ""); + expect(UnixPath("foo.rs").extension(), "rs"); + expect(UnixPath("foo.tar.gz").extension(), "gz"); + expect(UnixPath("/tmp/foo.tar.gz").extension(), "gz"); + expect(UnixPath("tmp/foo").extension(), ""); + expect(UnixPath(".foo").extension(), ""); + expect(UnixPath("/var").extension(), ""); + expect(UnixPath("/var..d").extension(), "d"); + expect(UnixPath("/..d").extension(), "d"); + + expect( + UnixPath("/Downloads/The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .extension(), + " Mary Abbotts, Kensington, during the year 1874"); + }); + + //************************************************************************// + + test("Option UnixPath", () { + final optionUnixPath = Option.from(UnixPath("UnixPath")); + switch (optionUnixPath) { + case Some(v: final _): + break; + default: + fail("Should be Some"); + } + final Option optionString = Option.from("string"); + switch (optionString) { + case Some(v: final _): + break; + default: + fail("Should be Some"); + } + }); +} diff --git a/test/path/windows_path_test.dart b/test/path/windows_path_test.dart new file mode 100644 index 0000000..cd7e3fd --- /dev/null +++ b/test/path/windows_path_test.dart @@ -0,0 +1,165 @@ +import 'package:rust/rust.dart'; +import 'package:test/expect.dart'; +import 'package:test/scaffolding.dart'; + +void main() { + test("filePrefix", () { + expect(WindowsPath("foo.rs").filePrefix().unwrap(), "foo"); + expect(WindowsPath("foo\\").filePrefix().unwrap(), "foo"); + expect(WindowsPath(".foo").filePrefix().unwrap(), ".foo"); + expect(WindowsPath(".foo.rs").filePrefix().unwrap(), "foo"); + expect(WindowsPath("foo").filePrefix().unwrap(), "foo"); + expect(WindowsPath("foo.tar.gz").filePrefix().unwrap(), "foo"); + expect(WindowsPath("temp\\foo.tar.gz").filePrefix().unwrap(), "foo"); + expect(WindowsPath("C:\\foo\\.tmp.bar.tar").filePrefix().unwrap(), "tmp"); + expect(WindowsPath("").filePrefix().isNone(), true); + + expect( + WindowsPath("\\Downloads\\The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .filePrefix() + .unwrap(), + "The Annual Report on the Health of the Parish of St"); + }); + + test("fileStem", () { + expect(WindowsPath("foo.rs").fileStem().unwrap(), "foo"); + expect(WindowsPath("foo\\").fileStem().unwrap(), "foo"); + expect(WindowsPath(".foo").fileStem().unwrap(), ".foo"); + expect(WindowsPath(".foo.rs").fileStem().unwrap(), ".foo"); + expect(WindowsPath("foo").fileStem().unwrap(), "foo"); + expect(WindowsPath("foo.tar.gz").fileStem().unwrap(), "foo.tar"); + expect(WindowsPath("temp\\foo.tar.gz").fileStem().unwrap(), "foo.tar"); + expect(WindowsPath("").fileStem().isNone(), true); + + expect( + WindowsPath("\\Downloads\\The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .fileStem() + .unwrap(), + "The Annual Report on the Health of the Parish of St"); + }); + + test("parent", () { + expect(WindowsPath("temp\\foo.rs").parent().unwrap(), WindowsPath("temp")); + expect(WindowsPath("foo\\").parent().unwrap(), WindowsPath("")); + expect(WindowsPath("C:\\foo\\").parent().unwrap(), WindowsPath("C:")); + expect(WindowsPath(".foo").parent().unwrap(), WindowsPath("")); + expect(WindowsPath(".foo.rs").parent().unwrap(), WindowsPath("")); + expect(WindowsPath("foo").parent().unwrap(), WindowsPath("")); + expect(WindowsPath("foo.tar.gz").parent().unwrap(), WindowsPath("")); + expect(WindowsPath("temp\\foo.tar.gz").parent().unwrap(), WindowsPath("temp")); + expect(WindowsPath("temp1\\temp2\\foo.tar.gz").parent().unwrap(), + WindowsPath("temp1\\temp2")); + expect(WindowsPath("temp1\\temp2\\\\foo.tar.gz").parent().unwrap(), + WindowsPath("temp1\\temp2")); + expect(WindowsPath("").parent().isNone(), true); + + expect( + WindowsPath("\\Downloads\\The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .parent() + .unwrap(), + WindowsPath("\\Downloads")); + }); + + test("ancestors", () { + var ancestors = WindowsPath("C:\\foo\\bar").ancestors().iterator; + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("C:\\foo\\bar")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("C:\\foo")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("C:")); + expect(ancestors.moveNext(), false); + + // Relative WindowsPaths should work similarly but without drive letters + ancestors = WindowsPath("..\\foo\\bar").ancestors().iterator; + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("..\\foo\\bar")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("..\\foo")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("..")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("")); + expect(ancestors.moveNext(), false); + + ancestors = WindowsPath("foo\\bar").ancestors().iterator; + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("foo\\bar")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("foo")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("")); + expect(ancestors.moveNext(), false); + + ancestors = WindowsPath("foo\\..").ancestors().iterator; + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("foo\\..")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("foo")); + + ancestors = WindowsPath( + "\\Downloads\\The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .ancestors() + .iterator; + ancestors.moveNext(); + expect( + ancestors.current, + WindowsPath( + "\\Downloads\\The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("\\Downloads")); + ancestors.moveNext(); + expect(ancestors.current, WindowsPath("\\")); + }); + + test("withExtension", () { + expect(WindowsPath("foo").withExtension("rs"), WindowsPath("foo.rs")); + expect(WindowsPath("foo.rs").withExtension("rs"), WindowsPath("foo.rs")); + expect(WindowsPath("foo.tar.gz").withExtension("rs"), WindowsPath("foo.tar.rs")); + expect(WindowsPath("foo.tar.gz").withExtension(""), WindowsPath("foo.tar")); + expect(WindowsPath("foo.tar.gz").withExtension("tar.gz"), WindowsPath("foo.tar.tar.gz")); + expect(WindowsPath("C:\\tmp\\foo.tar.gz").withExtension("tar.gz"), + WindowsPath("C:\\tmp\\foo.tar.tar.gz")); + expect(WindowsPath("tmp\\foo").withExtension("tar.gz"), WindowsPath("tmp\\foo.tar.gz")); + expect(WindowsPath("tmp\\.foo.tar").withExtension("tar.gz"), + WindowsPath("tmp\\.foo.tar.gz")); + + expect( + WindowsPath("\\Downloads\\The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .withExtension(""), + WindowsPath( + "\\Downloads\\The Annual Report on the Health of the Parish of St")); + }); + + test("withFileName", () { + expect(WindowsPath("foo").withFileName("bar"), WindowsPath("bar")); + expect(WindowsPath("foo.rs").withFileName("bar"), WindowsPath("bar")); + expect(WindowsPath("foo.tar.gz").withFileName("bar"), WindowsPath("bar")); + expect( + WindowsPath("C:\\tmp\\foo.tar.gz").withFileName("bar"), WindowsPath("C:\\tmp\\bar")); + expect(WindowsPath("tmp\\foo").withFileName("bar"), WindowsPath("tmp\\bar")); + expect(WindowsPath("C:\\var").withFileName("bar"), WindowsPath("C:\\bar")); + + expect( + WindowsPath("\\Downloads\\The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .withFileName("dave"), + WindowsPath("\\Downloads\\dave")); + }); + + test("extension", () { + expect(WindowsPath("foo").extension(), ""); + expect(WindowsPath("foo.rs").extension(), "rs"); + expect(WindowsPath("foo.tar.gz").extension(), "gz"); + expect(WindowsPath("\\tmp\\foo.tar.gz").extension(), "gz"); + expect(WindowsPath("tmp\\foo").extension(), ""); + expect(WindowsPath(".foo").extension(), ""); + expect(WindowsPath("\\var").extension(), ""); + expect(WindowsPath("\\var..d").extension(), "d"); + expect(WindowsPath("\\..d").extension(), "d"); + + expect( + WindowsPath("\\Downloads\\The Annual Report on the Health of the Parish of St. Mary Abbotts, Kensington, during the year 1874") + .extension(), + " Mary Abbotts, Kensington, during the year 1874"); + }); +}