Home | History | Annotate | Download | only in class.filesystem_error
      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 // <experimental/filesystem>
     13 
     14 // class filesystem_error
     15 
     16 // filesystem_error(const string& what_arg, error_code ec);
     17 // filesystem_error(const string& what_arg, const path& p1, error_code ec);
     18 // filesystem_error(const string& what_arg, const path& p1, const path& p2, error_code ec);
     19 // const std::error_code& code() const;
     20 // const char* what() const noexcept;
     21 // const path& path1() const noexcept;
     22 // const path& path2() const noexcept;
     23 
     24 #include <experimental/filesystem>
     25 #include <type_traits>
     26 #include <cassert>
     27 
     28 #include "test_macros.h"
     29 
     30 namespace fs = std::experimental::filesystem;
     31 
     32 void test_constructors() {
     33   using namespace fs;
     34 
     35   // The string returned by "filesystem_error::what() must contain runtime_error::what()
     36   const std::string what_arg = "Hello World";
     37   const std::string what_contains = std::runtime_error(what_arg).what();
     38   assert(what_contains.find(what_arg) != std::string::npos);
     39   auto CheckWhat = [what_contains](filesystem_error const& e) {
     40     std::string s = e.what();
     41     assert(s.find(what_contains) != std::string::npos);
     42   };
     43 
     44   std::error_code ec = std::make_error_code(std::errc::file_exists);
     45   const path p1("foo");
     46   const path p2("bar");
     47 
     48   // filesystem_error(const string& what_arg, error_code ec);
     49   {
     50     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, ec));
     51     filesystem_error e(what_arg, ec);
     52     CheckWhat(e);
     53     assert(e.code() == ec);
     54     assert(e.path1().empty() && e.path2().empty());
     55   }
     56   // filesystem_error(const string& what_arg, const path&, error_code ec);
     57   {
     58     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, ec));
     59     filesystem_error e(what_arg, p1, ec);
     60     CheckWhat(e);
     61     assert(e.code() == ec);
     62     assert(e.path1() == p1);
     63     assert(e.path2().empty());
     64   }
     65   // filesystem_error(const string& what_arg, const path&, const path&, error_code ec);
     66   {
     67     ASSERT_NOT_NOEXCEPT(filesystem_error(what_arg, p1, p2, ec));
     68     filesystem_error e(what_arg, p1, p2, ec);
     69     CheckWhat(e);
     70     assert(e.code() == ec);
     71     assert(e.path1() == p1);
     72     assert(e.path2() == p2);
     73   }
     74 }
     75 
     76 void test_signatures()
     77 {
     78   using namespace fs;
     79   const path p;
     80   std::error_code ec;
     81   const filesystem_error e("lala", ec);
     82   // const path& path1() const noexcept;
     83   {
     84     ASSERT_SAME_TYPE(path const&, decltype(e.path1()));
     85     ASSERT_NOEXCEPT(e.path1());
     86   }
     87   // const path& path2() const noexcept
     88   {
     89     ASSERT_SAME_TYPE(path const&, decltype(e.path2()));
     90     ASSERT_NOEXCEPT(e.path2());
     91   }
     92   // const char* what() const noexcept
     93   {
     94     ASSERT_SAME_TYPE(const char*, decltype(e.what()));
     95     ASSERT_NOEXCEPT(e.what());
     96   }
     97 }
     98 
     99 int main() {
    100   static_assert(std::is_base_of<std::system_error, fs::filesystem_error>::value, "");
    101   test_constructors();
    102   test_signatures();
    103 }
    104