|
| 1 | +#include <gtest/gtest.h> |
| 2 | + |
| 3 | +#include <sourcemeta/core/uri.h> |
| 4 | + |
| 5 | +TEST(URI_from_path, unix_absolute) { |
| 6 | + const std::filesystem::path example{"/foo/bar/baz"}; |
| 7 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 8 | + EXPECT_EQ(uri.recompose(), "file:///foo/bar/baz"); |
| 9 | +} |
| 10 | + |
| 11 | +TEST(URI_from_path, unix_with_space_and_reserved) { |
| 12 | + const std::filesystem::path example{"/foo/My Folder/has#hash?value%"}; |
| 13 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 14 | + EXPECT_EQ(uri.recompose(), "file:///foo/My%20Folder/has%23hash%3Fvalue%25"); |
| 15 | +} |
| 16 | + |
| 17 | +TEST(URI_from_path, unix_trailing_slash) { |
| 18 | + const std::filesystem::path example{"/foo/bar/"}; |
| 19 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 20 | + EXPECT_EQ(uri.recompose(), "file:///foo/bar/"); |
| 21 | +} |
| 22 | + |
| 23 | +TEST(URI_from_path, windows_drive_absolute) { |
| 24 | + const std::filesystem::path example{R"(C:\Program Files\Test)"}; |
| 25 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 26 | + EXPECT_EQ(uri.recompose(), "file:///C:/Program%20Files/Test"); |
| 27 | +} |
| 28 | + |
| 29 | +TEST(URI_from_path, windows_drive_lowercase) { |
| 30 | + const std::filesystem::path example{R"(c:\temp\logs)"}; |
| 31 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 32 | + EXPECT_EQ(uri.recompose(), "file:///c:/temp/logs"); |
| 33 | +} |
| 34 | + |
| 35 | +TEST(URI_from_path, windows_drive_root) { |
| 36 | + const std::filesystem::path example{R"(D:\)"}; |
| 37 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 38 | + EXPECT_EQ(uri.recompose(), "file:///D:/"); |
| 39 | +} |
| 40 | + |
| 41 | +TEST(URI_from_path, windows_trailing_slash) { |
| 42 | + const std::filesystem::path example{R"(C:\foo\bar\)"}; |
| 43 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 44 | + EXPECT_EQ(uri.recompose(), "file:///C:/foo/bar/"); |
| 45 | +} |
| 46 | + |
| 47 | +TEST(URI_from_path, windows_percent_and_plus) { |
| 48 | + // '%' → %25, '+' is allowed unencoded |
| 49 | + const std::filesystem::path example{R"(C:\path\50%+plus.txt)"}; |
| 50 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 51 | + EXPECT_EQ(uri.recompose(), "file:///C:/path/50%25+plus.txt"); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(URI_from_path, windows_unc_simple) { |
| 55 | + const std::filesystem::path example{R"(\\server\share\file.txt)"}; |
| 56 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 57 | + EXPECT_EQ(uri.recompose(), |
| 58 | + // For UNC, host=server, path=/share/file.txt |
| 59 | + "file://server/share/file.txt"); |
| 60 | +} |
| 61 | + |
| 62 | +TEST(URI_from_path, windows_unc_with_space) { |
| 63 | + const std::filesystem::path example{R"(\\srv\My Docs\a b.txt)"}; |
| 64 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 65 | + EXPECT_EQ(uri.recompose(), "file://srv/My%20Docs/a%20b.txt"); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(URI_from_path, unicode_unix) { |
| 69 | + // U+00E9 (é) should be UTF-8 percent-encoded as %C3%A9 |
| 70 | + const std::filesystem::path example{u8"/data/éclair.txt"}; |
| 71 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 72 | + EXPECT_EQ(uri.recompose(), "file:///data/%C3%A9clair.txt"); |
| 73 | +} |
| 74 | + |
| 75 | +TEST(URI_from_path, unicode_windows) { |
| 76 | + // U+00E9 (é) should be UTF-8 percent-encoded as %C3%A9 |
| 77 | + const std::filesystem::path example{u8R"(C:\data\résumé.doc)"}; |
| 78 | + const auto uri{sourcemeta::core::URI::from_path(example)}; |
| 79 | + EXPECT_EQ(uri.recompose(), "file:///C:/data/r%C3%A9sum%C3%A9.doc"); |
| 80 | +} |
| 81 | + |
| 82 | +TEST(URI_from_path, unix_relative_simple) { |
| 83 | + const std::filesystem::path example{"foo/bar/baz"}; |
| 84 | + EXPECT_THROW(sourcemeta::core::URI::from_path(example), |
| 85 | + sourcemeta::core::URIError); |
| 86 | +} |
| 87 | + |
| 88 | +TEST(URI_from_path, unix_relative_with_dot) { |
| 89 | + const std::filesystem::path example{"./foo/bar"}; |
| 90 | + EXPECT_THROW(sourcemeta::core::URI::from_path(example), |
| 91 | + sourcemeta::core::URIError); |
| 92 | +} |
| 93 | + |
| 94 | +TEST(URI_from_path, unix_relative_with_dotdot) { |
| 95 | + const std::filesystem::path example{"../parent/dir"}; |
| 96 | + EXPECT_THROW(sourcemeta::core::URI::from_path(example), |
| 97 | + sourcemeta::core::URIError); |
| 98 | +} |
| 99 | + |
| 100 | +TEST(URI_from_path, unix_empty_path) { |
| 101 | + const std::filesystem::path example{""}; |
| 102 | + EXPECT_THROW(sourcemeta::core::URI::from_path(example), |
| 103 | + sourcemeta::core::URIError); |
| 104 | +} |
| 105 | + |
| 106 | +TEST(URI_from_path, windows_relative_simple) { |
| 107 | + const std::filesystem::path example{"folder\\file.txt"}; |
| 108 | + EXPECT_THROW(sourcemeta::core::URI::from_path(example), |
| 109 | + sourcemeta::core::URIError); |
| 110 | +} |
| 111 | + |
| 112 | +TEST(URI_from_path, windows_relative_with_dot) { |
| 113 | + const std::filesystem::path example{".\\foo\\bar"}; |
| 114 | + EXPECT_THROW(sourcemeta::core::URI::from_path(example), |
| 115 | + sourcemeta::core::URIError); |
| 116 | +} |
| 117 | + |
| 118 | +TEST(URI_from_path, windows_relative_with_dotdot) { |
| 119 | + const std::filesystem::path example{"..\\up\\one\\level"}; |
| 120 | + EXPECT_THROW(sourcemeta::core::URI::from_path(example), |
| 121 | + sourcemeta::core::URIError); |
| 122 | +} |
0 commit comments