libcxx

libcxx mirror with random patches
git clone https://git.neptards.moe/neptards/libcxx.git
Log | Files | Refs

is_symlink.pass.cpp (2639B)


      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03
     11 
     12 // <filesystem>
     13 
     14 // bool is_symlink(file_status s) noexcept
     15 // bool is_symlink(path const& p);
     16 // bool is_symlink(path const& p, std::error_code& ec) noexcept;
     17 
     18 #include "filesystem_include.hpp"
     19 #include <type_traits>
     20 #include <cassert>
     21 
     22 #include "test_macros.h"
     23 #include "rapid-cxx-test.hpp"
     24 #include "filesystem_test_helper.hpp"
     25 
     26 using namespace fs;
     27 
     28 TEST_SUITE(is_symlink_test_suite)
     29 
     30 TEST_CASE(signature_test)
     31 {
     32     file_status s; ((void)s);
     33     const path p; ((void)p);
     34     std::error_code ec; ((void)ec);
     35     ASSERT_NOEXCEPT(is_symlink(s));
     36     ASSERT_NOEXCEPT(is_symlink(p, ec));
     37     ASSERT_NOT_NOEXCEPT(is_symlink(p));
     38 }
     39 
     40 TEST_CASE(is_symlink_status_test)
     41 {
     42     struct TestCase {
     43         file_type type;
     44         bool expect;
     45     };
     46     const TestCase testCases[] = {
     47         {file_type::none, false},
     48         {file_type::not_found, false},
     49         {file_type::regular, false},
     50         {file_type::directory, false},
     51         {file_type::symlink, true},
     52         {file_type::block, false},
     53         {file_type::character, false},
     54         {file_type::fifo, false},
     55         {file_type::socket, false},
     56         {file_type::unknown, false}
     57     };
     58     for (auto& TC : testCases) {
     59         file_status s(TC.type);
     60         TEST_CHECK(is_symlink(s) == TC.expect);
     61     }
     62 }
     63 
     64 TEST_CASE(static_env_test)
     65 {
     66     struct TestCase {
     67         path p;
     68         bool expect;
     69     };
     70     const TestCase testCases[] = {
     71         {StaticEnv::File, false},
     72         {StaticEnv::Dir, false},
     73         {StaticEnv::SymlinkToFile, true},
     74         {StaticEnv::SymlinkToDir, true},
     75         {StaticEnv::BadSymlink, true}
     76     };
     77     for (auto& TC : testCases) {
     78         TEST_CHECK(is_symlink(TC.p) == TC.expect);
     79     }
     80 }
     81 
     82 TEST_CASE(test_exist_not_found)
     83 {
     84     const path p = StaticEnv::DNE;
     85     TEST_CHECK(is_symlink(p) == false);
     86     std::error_code ec;
     87     TEST_CHECK(is_symlink(p, ec) == false);
     88     TEST_CHECK(ec);
     89 }
     90 
     91 TEST_CASE(test_is_symlink_fails)
     92 {
     93     scoped_test_env env;
     94     const path dir = env.create_dir("dir");
     95     const path file = env.create_file("dir/file", 42);
     96     permissions(dir, perms::none);
     97 
     98     std::error_code ec;
     99     TEST_CHECK(is_symlink(file, ec) == false);
    100     TEST_CHECK(ec);
    101 
    102     TEST_CHECK_THROW(filesystem_error, is_symlink(file));
    103 }
    104 
    105 TEST_SUITE_END()