xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
directory_assert.h
Go to the documentation of this file.
1 #pragma once
5 #include "assert.h"
6 #include <sys/stat.h>
7 #if defined(__cpp_lib_filesystem)
8 #include <filesystem>
9 #endif
10 
12 namespace xtd {
14  namespace tunit {
24  class directory_assert final : private base_assert {
25  public:
27  directory_assert() = delete;
29 
30 #if defined(__cpp_lib_filesystem)
41  static void are_equal(const std::filesystem::directory_entry& expected, const std::filesystem::directory_entry& actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
42 
54  static void are_equal(const std::filesystem::directory_entry& expected, const std::filesystem::directory_entry& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
55 
67  static void are_equal(const std::filesystem::directory_entry& expected, const std::filesystem::directory_entry& actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
68 
81  static void are_equal(const std::filesystem::directory_entry& expected, const std::filesystem::directory_entry& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
82  if (expected != actual)
83  base_assert::fail(base_assert::to_string(expected.path().string()), base_assert::to_string(actual.path().string()), message, stack_frame);
84  else
85  assert::succeed(message, stack_frame);
86  }
87 
98  static void are_not_equal(const std::filesystem::directory_entry& expected, const std::filesystem::directory_entry& actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
99 
111  static void are_not_equal(const std::filesystem::directory_entry& expected, const std::filesystem::directory_entry& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
112 
124  static void are_not_equal(const std::filesystem::directory_entry& expected, const std::filesystem::directory_entry& actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
125 
138  static void are_not_equal(const std::filesystem::directory_entry& expected, const std::filesystem::directory_entry& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
139  if (expected != actual)
140  base_assert::fail(base_assert::to_string(expected.path().string()), base_assert::to_string(actual.path().string()), message, stack_frame);
141  else
142  assert::succeed(message, stack_frame);
143  }
144 #endif
145 
157  template<typename Char>
158  static void exists(const std::basic_string<Char>& directory) {exists(directory, "", xtd::diagnostics::stack_frame::empty());}
159 
172  template<typename Char>
173  static void exists(const std::basic_string<Char>& directory, const xtd::diagnostics::stack_frame& stack_frame) {exists(directory, "", stack_frame);}
174 
187  template<typename Char>
188  static void exists(const std::basic_string<Char>& directory, const std::string& message) {exists(directory, message, xtd::diagnostics::stack_frame::empty());}
189 
203  template<typename Char>
204  static void exists(const std::basic_string<Char>& directory, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
205  struct stat info;
206  if(stat(directory.c_str(), &info ) != 0 || (info.st_mode & S_IFDIR) != S_IFDIR)
207  base_assert::fail("directory exists", base_assert::to_string(directory), message, stack_frame);
208  else
209  assert::succeed(message, stack_frame);
210  }
211 
213  template<typename Char>
214  static void exists(const Char* directory) {exists(directory, "", xtd::diagnostics::stack_frame::empty());}
215  template<typename Char>
216  static void exists(const Char* directory, const xtd::diagnostics::stack_frame& stack_frame) {exists(directory, "", stack_frame);}
217  template<typename Char>
218  static void exists(const Char* directory, const std::string& message) {exists(directory, message, xtd::diagnostics::stack_frame::empty());}
219  template<typename Char>
220  static void exists(const Char* directory, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {exists(std::basic_string<Char>(directory), message, stack_frame);}
222 
234  template<typename Char>
235  static void does_not_exist(const std::basic_string<Char>& directory) {does_not_exist(directory, "", xtd::diagnostics::stack_frame::empty());}
236 
249  template<typename Char>
250  static void does_not_exist(const std::basic_string<Char>& directory, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(directory, "", stack_frame);}
251 
264  template<typename Char>
265  static void does_not_exist(const std::basic_string<Char>& directory, const std::string& message) {does_not_exist(directory, message, xtd::diagnostics::stack_frame::empty());}
266 
280  template<typename Char>
281  static void does_not_exist(const std::basic_string<Char>& directory, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
282  struct stat info;
283  if(stat(directory.c_str(), &info ) == 0 && (info.st_mode & S_IFDIR) == S_IFDIR)
284  base_assert::fail("not directory exists", base_assert::to_string(directory), message, stack_frame);
285  else
286  assert::succeed(message, stack_frame);
287  }
288 
290  template<typename Char>
291  static void does_not_exist(const Char* directory) {does_not_exist(directory, "", xtd::diagnostics::stack_frame::empty());}
292  template<typename Char>
293  static void does_not_exist(const Char* directory, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(directory, "", stack_frame);}
294  template<typename Char>
295  static void does_not_exist(const Char* directory, const std::string& message) {does_not_exist(directory, message, xtd::diagnostics::stack_frame::empty());}
296  template<typename Char>
297  static void does_not_exist(const Char* directory, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(std::basic_string<Char>(directory), message, stack_frame);}
299  };
300  }
301 }
302 
303 #define exists_(...) __CMD_ASSERT_ARGS(exists, __VA_ARGS__)
304 
305 #define does_not_exist_(...) __CMD_ASSERT_ARGS(does_not_exist, __VA_ARGS__)
Contains xtd::tunit::assert class.
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition: stack_frame.h:29
static stack_frame empty() noexcept
Return an empty stack frame.
The base class for assert.
Definition: base_assert.h:25
static void fail()
Throws an xtd::tunit::assertion_error exception. This is used by the other Assert functions.
Definition: base_assert.h:70
static void succeed()
Generates a success with a generic message. This is used by the other Assert functions.
Definition: base_assert.h:140
The directory_assert class contains a collection of static methods that implement the most directory ...
Definition: directory_assert.h:24
static void exists(const std::basic_string< Char > &directory, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that directory exists.
Definition: directory_assert.h:173
static void exists(const std::basic_string< Char > &directory, const std::string &message)
Asserts that directory exists.
Definition: directory_assert.h:188
static void does_not_exist(const std::basic_string< Char > &directory, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file not exists.
Definition: directory_assert.h:250
static void does_not_exist(const std::basic_string< Char > &directory, const std::string &message)
Asserts that file not exists.
Definition: directory_assert.h:265
static void exists(const std::basic_string< Char > &directory, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that directory exists.
Definition: directory_assert.h:204
static void exists(const std::basic_string< Char > &directory)
Asserts that directory exists.
Definition: directory_assert.h:158
static void does_not_exist(const std::basic_string< Char > &directory)
Asserts that file not exists.
Definition: directory_assert.h:235
static void does_not_exist(const std::basic_string< Char > &directory, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file not exists.
Definition: directory_assert.h:281
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition: to_string.h:37
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17