xtd 0.2.0
file_assert.h
Go to the documentation of this file.
1 #pragma once
5 #include "directory_assert.h"
6 
8 namespace xtd {
10  namespace tunit {
23  class tunit_export_ file_assert final : private base_assert {
24  public:
26  file_assert() = delete;
28 
30 
49  template<typename char_t>
50  static void are_equal(const std::basic_istream<char_t>& expected, const std::basic_istream<char_t>& actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
69  template<typename char_t>
70  static void are_equal(const std::basic_istream<char_t>& expected, const std::basic_istream<char_t>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
89  template<typename char_t>
90  static void are_equal(const std::basic_istream<char_t>& expected, const std::basic_istream<char_t>& actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
110  template<typename char_t>
111  static void are_equal(const std::basic_istream<char_t>& expected, const std::basic_istream<char_t>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
112  std::basic_istream<char_t> if_expected(expected.rdbuf());
113  std::basic_istream<char_t> if_actual(actual.rdbuf());
114 
115  if_expected.seekg(0, std::ios::end);
116  if_actual.seekg(0, std::ios::end);
117  size_t size_expected = static_cast<size_t>(if_expected.tellg());
118  size_t size_actual = static_cast<size_t>(if_actual.tellg());
119  if (size_expected != size_actual) {
120  fail("istream length " + to_string(size_expected), to_string(size_actual), message, stack_frame);
121  return;
122  }
123 
124  if_expected.seekg(0, std::ios::beg);
125  if_actual.seekg(0, std::ios::beg);
126  for (size_t offset = 0; offset < size_actual; ++offset) {
127  char_t value_expected = static_cast<char_t>(if_expected.get());
128  char_t value_actual = static_cast<char_t>(if_actual.get());
129  if (value_expected != value_actual) {
130  fail("istream at offset " + to_string(offset) + " value " + to_string(value_expected), to_string(value_actual), message, stack_frame);
131  return;
132  }
133  }
134  assert::succeed(message, stack_frame);
135  }
153  template<typename char_t>
154  static void are_equal(const std::basic_string<char_t>& expected, const std::basic_string<char_t>& actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
173  template<typename char_t>
174  static void are_equal(const std::basic_string<char_t>& expected, const std::basic_string<char_t>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
193  template<typename char_t>
194  static void are_equal(const std::basic_string<char_t>& expected, const std::basic_string<char_t>& actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
214  template<typename char_t>
215  static void are_equal(const std::basic_string<char_t>& expected, const std::basic_string<char_t>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(std::basic_ifstream<char_t>(expected), std::basic_ifstream<char_t>(actual), message, stack_frame);}
216 
218  template<typename char_t>
219  static void are_equal(const std::basic_string<char_t>& expected, const char_t* actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
220  template<typename char_t>
221  static void are_equal(const std::basic_string<char_t>& expected, const char_t* actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
222  template<typename char_t>
223  static void are_equal(const std::basic_string<char_t>& expected, const char_t* actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
224  template<typename char_t>
225  static void are_equal(const std::basic_string<char_t>& expected, const char_t* actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(std::basic_ifstream<char_t>(expected), std::basic_ifstream<char_t>(actual), message, stack_frame);}
226  template<typename char_t>
227  static void are_equal(const char_t* expected, const std::basic_string<char_t>& actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
228  template<typename char_t>
229  static void are_equal(const char_t* expected, const std::basic_string<char_t>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
230  template<typename char_t>
231  static void are_equal(const char_t* expected, const std::basic_string<char_t>& actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
232  template<typename char_t>
233  static void are_equal(const char_t* expected, const std::basic_string<char_t>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(std::basic_ifstream<char_t>(expected), std::basic_ifstream<char_t>(actual), message, stack_frame);}
234  template<typename char_t>
235  static void are_equal(const char_t* expected, const char_t* actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
236  template<typename char_t>
237  static void are_equal(const char_t* expected, const char_t* actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
238  template<typename char_t>
239  static void are_equal(const char_t* expected, const char_t* actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
240  template<typename char_t>
241  static void are_equal(const char_t* expected, const char_t* actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(std::basic_ifstream<char_t>(expected), std::basic_ifstream<char_t>(actual), message, stack_frame);}
243 
261  template<typename char_t>
262  static void are_not_equal(const std::basic_istream<char_t>& expected, const std::basic_istream<char_t>& actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
281  template<typename char_t>
282  static void are_not_equal(const std::basic_istream<char_t>& expected, const std::basic_istream<char_t>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
301  template<typename char_t>
302  static void are_not_equal(const std::basic_istream<char_t>& expected, const std::basic_istream<char_t>& actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
322  template<typename char_t>
323  static void are_not_equal(const std::basic_istream<char_t>& expected, const std::basic_istream<char_t>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
324  std::basic_istream<char_t> if_expected(expected.rdbuf());
325  std::basic_istream<char_t> if_actual(actual.rdbuf());
326 
327  if_expected.seekg(0, std::ios::end);
328  if_actual.seekg(0, std::ios::end);
329  size_t size_expected = static_cast<size_t>(if_expected.tellg());
330  size_t size_actual = static_cast<size_t>(if_actual.tellg());
331  if (size_expected != size_actual) {
332  assert::succeed(message, stack_frame);
333  return;
334  }
335 
336  if_expected.seekg(0, std::ios::beg);
337  if_actual.seekg(0, std::ios::beg);
338  for (size_t offset = 0; offset < size_actual; ++offset) {
339  char_t value_expected = static_cast<char_t>(if_expected.get());
340  char_t value_actual = static_cast<char_t>(if_actual.get());
341  if (value_expected != value_actual) {
342  assert::succeed(message, stack_frame);
343  return;
344  }
345  }
346  fail("not equal <" + typeof_(expected).full_name() + ">", "<" + typeof_(expected).full_name() + ">", message, stack_frame);
347  }
365  template<typename char_t>
366  static void are_not_equal(const std::basic_string<char_t>& expected, const std::basic_string<char_t>& actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
367 
386  template<typename char_t>
387  static void are_not_equal(const std::basic_string<char_t>& expected, const std::basic_string<char_t>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
406  template<typename char_t>
407  static void are_not_equal(const std::basic_string<char_t>& expected, const std::basic_string<char_t>& actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
427  template<typename char_t>
428  static void are_not_equal(const std::basic_string<char_t>& expected, const std::basic_string<char_t>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(std::basic_ifstream<char_t>(expected), std::basic_ifstream<char_t>(actual), message, stack_frame);}
429 
431  template<typename char_t>
432  static void are_not_equal(const std::basic_string<char_t>& expected, const char_t* actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
433  template<typename char_t>
434  static void are_not_equal(const std::basic_string<char_t>& expected, const char_t* actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
435  template<typename char_t>
436  static void are_not_equal(const std::basic_string<char_t>& expected, const char_t* actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
437  template<typename char_t>
438  static void are_not_equal(const std::basic_string<char_t>& expected, const char_t* actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(std::basic_ifstream<char_t>(expected), std::basic_ifstream<char_t>(actual), message, stack_frame);}
439  template<typename char_t>
440  static void are_not_equal(const char_t* expected, const std::basic_string<char_t>& actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
441  template<typename char_t>
442  static void are_not_equal(const char_t* expected, const std::basic_string<char_t>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
443  template<typename char_t>
444  static void are_not_equal(const char_t* expected, const std::basic_string<char_t>& actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
445  template<typename char_t>
446  static void are_not_equal(const char_t* expected, const std::basic_string<char_t>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(std::basic_ifstream<char_t>(expected), std::basic_ifstream<char_t>(actual), message, stack_frame);}
447  template<typename char_t>
448  static void are_not_equal(const char_t* expected, const char_t* actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
449  template<typename char_t>
450  static void are_not_equal(const char_t* expected, const char_t* actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
451  template<typename char_t>
452  static void are_not_equal(const char_t* expected, const char_t* actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
453  template<typename char_t>
454  static void are_not_equal(const char_t* expected, const char_t* actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(std::basic_ifstream<char_t>(expected), std::basic_ifstream<char_t>(actual), message, stack_frame);}
456 
468  template<typename char_t>
469  static void does_not_exist(const std::basic_string<char_t>& file) {does_not_exist(file, "", xtd::diagnostics::stack_frame::empty());}
482  template<typename char_t>
483  static void does_not_exist(const std::basic_string<char_t>& file, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(file, "", stack_frame);}
496  template<typename char_t>
497  static void does_not_exist(const std::basic_string<char_t>& file, const std::string& message) {does_not_exist(file, message, xtd::diagnostics::stack_frame::empty());}
511  template<typename char_t>
512  static void does_not_exist(const std::basic_string<char_t>& file, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
513  auto is = std::basic_ifstream<char_t>(file);
514  if (is.good() == true) {
515  is.close();
516  fail("not file exists", to_string(file), message, stack_frame);
517  } else
518  assert::succeed(message, stack_frame);
519  }
520 
522  template<typename char_t>
523  static void does_not_exist(const char_t* file) {does_not_exist(file, "", xtd::diagnostics::stack_frame::empty());}
524  template<typename char_t>
525  static void does_not_exist(const char_t* file, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(file, "", stack_frame);}
526  template<typename char_t>
527  static void does_not_exist(const char_t* file, const std::string& message) {does_not_exist(file, message, xtd::diagnostics::stack_frame::empty());}
528  template<typename char_t>
529  static void does_not_exist(const char_t* file, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(std::basic_string<char_t>(file), message, stack_frame);}
531 
543  template<typename char_t>
544  static void exists(const std::basic_string<char_t>& file) {exists(file, "", xtd::diagnostics::stack_frame::empty());}
557  template<typename char_t>
558  static void exists(const std::basic_string<char_t>& file, const xtd::diagnostics::stack_frame& stack_frame) {exists(file, "", stack_frame);}
571  template<typename char_t>
572  static void exists(const std::basic_string<char_t>& file, const std::string& message) {exists(file, message, xtd::diagnostics::stack_frame::empty());}
586  template<typename char_t>
587  static void exists(const std::basic_string<char_t>& file, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
588  auto is = std::basic_ifstream<char_t>(file);
589  if (is.good() == false)
590  fail("file exists", to_string(file), message, stack_frame);
591  else {
592  is.close();
593  assert::succeed(message, stack_frame);
594  }
595  }
596 
598  template<typename char_t>
599  static void exists(const char_t* file) {exists(file, "", xtd::diagnostics::stack_frame::empty());}
600  template<typename char_t>
601  static void exists(const char_t* file, const xtd::diagnostics::stack_frame& stack_frame) {exists(file, "", stack_frame);}
602  template<typename char_t>
603  static void exists(const char_t* file, const std::string& message) {exists(file, message, xtd::diagnostics::stack_frame::empty());}
604  template<typename char_t>
605  static void exists(const char_t* file, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {exists(std::basic_string<char_t>(file), message, stack_frame);}
608  };
609  }
610 }
The base class for assert.
Definition: base_assert.h:29
static void exists(const std::basic_string< char_t > &file, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file exists.
Definition: file_assert.h:587
static void succeed()
Generates a success with a generic message. This is used by the other assert functions.
static void does_not_exist(const std::basic_string< char_t > &file, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file not exists.
Definition: file_assert.h:483
static void exists(const std::basic_string< char_t > &file, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file exists.
Definition: file_assert.h:558
static void are_equal(const std::basic_istream< char_t > &expected, const std::basic_istream< char_t > &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are equal.
Definition: file_assert.h:70
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
static void does_not_exist(const std::basic_string< char_t > &file, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file not exists.
Definition: file_assert.h:512
static void are_equal(const std::basic_string< char_t > &expected, const std::basic_string< char_t > &actual, const std::string &message)
Asserts that two files are equal.
Definition: file_assert.h:194
Contains xtd::tunit::directory_assert class.
static void does_not_exist(const std::basic_string< char_t > &file, const std::string &message)
Asserts that file not exists.
Definition: file_assert.h:497
static void are_not_equal(const std::basic_istream< char_t > &expected, const std::basic_istream< char_t > &actual, const std::string &message)
Asserts that two files are not equal.
Definition: file_assert.h:302
std::string to_string(const date_time &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition: date_time.h:1063
static void are_equal(const std::basic_istream< char_t > &expected, const std::basic_istream< char_t > &actual, const std::string &message)
Asserts that two files are equal.
Definition: file_assert.h:90
The file_assert class contains a collection of static methods that implement the most file assertions...
Definition: file_assert.h:23
static void are_not_equal(const std::basic_string< char_t > &expected, const std::basic_string< char_t > &actual)
Asserts that two files are not equal.
Definition: file_assert.h:366
static void are_equal(const std::basic_string< char_t > &expected, const std::basic_string< char_t > &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are equal.
Definition: file_assert.h:174
static void are_not_equal(const std::basic_string< char_t > &expected, const std::basic_string< char_t > &actual, const std::string &message)
Asserts that two files are not equal.
Definition: file_assert.h:407
static void are_not_equal(const std::basic_istream< char_t > &expected, const std::basic_istream< char_t > &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are not equal.
Definition: file_assert.h:282
static void are_not_equal(const std::basic_string< char_t > &expected, const std::basic_string< char_t > &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are not equal.
Definition: file_assert.h:387
#define typeof_
Used to obtain the type string for a type. A typeof_ expression takes the following form: ...
Definition: typeof.h:42
static void exists(const std::basic_string< char_t > &file, const std::string &message)
Asserts that file exists.
Definition: file_assert.h:572
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition: stack_frame.h:37
static stack_frame empty() noexcept
Return an empty stack frame.
static void are_equal(const std::basic_string< char_t > &expected, const std::basic_string< char_t > &actual)
Asserts that two files are equal.
Definition: file_assert.h:154
static void are_equal(const std::basic_string< char_t > &expected, const std::basic_string< char_t > &actual, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are equal.
Definition: file_assert.h:215
static void are_equal(const std::basic_istream< char_t > &expected, const std::basic_istream< char_t > &actual, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are equal.
Definition: file_assert.h:111
static void are_equal(const std::basic_istream< char_t > &expected, const std::basic_istream< char_t > &actual)
Asserts that two files are equal.
Definition: file_assert.h:50
static void exists(const std::basic_string< char_t > &file)
Asserts that file exists.
Definition: file_assert.h:544
static void are_not_equal(const std::basic_istream< char_t > &expected, const std::basic_istream< char_t > &actual, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are not equal.
Definition: file_assert.h:323
static void are_not_equal(const std::basic_string< char_t > &expected, const std::basic_string< char_t > &actual, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are not equal.
Definition: file_assert.h:428
static void does_not_exist(const std::basic_string< char_t > &file)
Asserts that file not exists.
Definition: file_assert.h:469
static void are_not_equal(const std::basic_istream< char_t > &expected, const std::basic_istream< char_t > &actual)
Asserts that two files are not equal.
Definition: file_assert.h:262
bool is(std::any value)
Checks if the result of an expression is compatible with a given type.
Definition: is.h:335