xtd - Reference Guide  0.1.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Loading...
Searching...
No Matches
file_assert.h
Go to the documentation of this file.
1
4#pragma once
5#include "directory_assert.h"
6
8namespace xtd {
10 namespace tunit {
20 class file_assert final : private base_assert {
21 public:
23 file_assert() = delete;
25
43 template<typename Char>
44 static void are_equal(const std::basic_istream<Char>& expected, const std::basic_istream<Char>& actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
45
64 template<typename Char>
65 static void are_equal(const std::basic_istream<Char>& expected, const std::basic_istream<Char>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
66
85 template<typename Char>
86 static void are_equal(const std::basic_istream<Char>& expected, const std::basic_istream<Char>& actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
87
107 template<typename Char>
108 static void are_equal(const std::basic_istream<Char>& expected, const std::basic_istream<Char>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
109 std::basic_istream<Char> if_expected(expected.rdbuf());
110 std::basic_istream<Char> if_actual(actual.rdbuf());
111
112 if_expected.seekg(0, std::ios::end);
113 if_actual.seekg(0, std::ios::end);
114 size_t size_expected = if_expected.tellg();
115 size_t size_actual = if_actual.tellg();
116 if (size_expected != size_actual) {
117 base_assert::fail("istream length " + base_assert::to_string(size_expected), base_assert::to_string(size_actual), message, stack_frame);
118 return;
119 }
120
121 if_expected.seekg(0, std::ios::beg);
122 if_actual.seekg(0, std::ios::beg);
123 for (size_t offset = 0; offset < size_actual; ++offset) {
124 Char value_expected = if_expected.get();
125 Char value_actual = if_actual.get();
126 if (value_expected != value_actual) {
127 base_assert::fail("istream at offset " + base_assert::to_string(offset) + " value " + base_assert::to_string(value_expected), base_assert::to_string(value_actual), message, stack_frame);
128 return;
129 }
130 }
131 assert::succeed(message, stack_frame);
132 }
133
151 template<typename Char>
152 static void are_equal(const std::basic_string<Char>& expected, const std::basic_string<Char>& actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
153
172 template<typename Char>
173 static void are_equal(const std::basic_string<Char>& expected, const std::basic_string<Char>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
174
193 template<typename Char>
194 static void are_equal(const std::basic_string<Char>& expected, const std::basic_string<Char>& actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
195
215 template<typename Char>
216 static void are_equal(const std::basic_string<Char>& expected, const std::basic_string<Char>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(std::basic_ifstream<Char>(expected), std::basic_ifstream<Char>(actual), message, stack_frame);}
217
219 template<typename Char>
220 static void are_equal(const std::basic_string<Char>& expected, const Char* actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
221 template<typename Char>
222 static void are_equal(const std::basic_string<Char>& expected, const Char* actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
223 template<typename Char>
224 static void are_equal(const std::basic_string<Char>& expected, const Char* actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
225 template<typename Char>
226 static void are_equal(const std::basic_string<Char>& expected, const Char* actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(std::basic_ifstream<Char>(expected), std::basic_ifstream<Char>(actual), message, stack_frame);}
227 template<typename Char>
228 static void are_equal(const Char* expected, const std::basic_string<Char>& actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
229 template<typename Char>
230 static void are_equal(const Char* expected, const std::basic_string<Char>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
231 template<typename Char>
232 static void are_equal(const Char* expected, const std::basic_string<Char>& actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
233 template<typename Char>
234 static void are_equal(const Char* expected, const std::basic_string<Char>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(std::basic_ifstream<Char>(expected), std::basic_ifstream<Char>(actual), message, stack_frame);}
235 template<typename Char>
236 static void are_equal(const Char* expected, const Char* actual) {are_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
237 template<typename Char>
238 static void are_equal(const Char* expected, const Char* actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(expected, actual, "", stack_frame);}
239 template<typename Char>
240 static void are_equal(const Char* expected, const Char* actual, const std::string& message) {are_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
241 template<typename Char>
242 static void are_equal(const Char* expected, const Char* actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_equal(std::basic_ifstream<Char>(expected), std::basic_ifstream<Char>(actual), message, stack_frame);}
244
262 template<typename Char>
263 static void are_not_equal(const std::basic_istream<Char>& expected, const std::basic_istream<Char>& actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
264
283 template<typename Char>
284 static void are_not_equal(const std::basic_istream<Char>& expected, const std::basic_istream<Char>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
285
304 template<typename Char>
305 static void are_not_equal(const std::basic_istream<Char>& expected, const std::basic_istream<Char>& actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
306
326 template<typename Char>
327 static void are_not_equal(const std::basic_istream<Char>& expected, const std::basic_istream<Char>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
328 std::basic_istream<Char> if_expected(expected.rdbuf());
329 std::basic_istream<Char> if_actual(actual.rdbuf());
330
331 if_expected.seekg(0, std::ios::end);
332 if_actual.seekg(0, std::ios::end);
333 size_t size_expected = if_expected.tellg();
334 size_t size_actual = if_actual.tellg();
335 if (size_expected != size_actual) {
336 assert::succeed(message, stack_frame);
337 return;
338 }
339
340 if_expected.seekg(0, std::ios::beg);
341 if_actual.seekg(0, std::ios::beg);
342 for (size_t offset = 0; offset < size_actual; ++offset) {
343 Char value_expected = if_expected.get();
344 Char value_actual = if_actual.get();
345 if (value_expected != value_actual) {
346 assert::succeed(message, stack_frame);
347 return;
348 }
349 }
350 base_assert::fail("not equal <" + __tunit_demangle(typeid(expected).name()) + ">", "<" + __tunit_demangle(typeid(expected).name()) + ">", message, stack_frame);
351 }
352
370 template<typename Char>
371 static void are_not_equal(const std::basic_string<Char>& expected, const std::basic_string<Char>& actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
372
391 template<typename Char>
392 static void are_not_equal(const std::basic_string<Char>& expected, const std::basic_string<Char>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
393
412 template<typename Char>
413 static void are_not_equal(const std::basic_string<Char>& expected, const std::basic_string<Char>& actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
414
434 template<typename Char>
435 static void are_not_equal(const std::basic_string<Char>& expected, const std::basic_string<Char>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(std::basic_ifstream<Char>(expected), std::basic_ifstream<Char>(actual), message, stack_frame);}
436
438 template<typename Char>
439 static void are_not_equal(const std::basic_string<Char>& expected, const Char* actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
440 template<typename Char>
441 static void are_not_equal(const std::basic_string<Char>& expected, const Char* actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
442 template<typename Char>
443 static void are_not_equal(const std::basic_string<Char>& expected, const Char* actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
444 template<typename Char>
445 static void are_not_equal(const std::basic_string<Char>& expected, const Char* actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(std::basic_ifstream<Char>(expected), std::basic_ifstream<Char>(actual), message, stack_frame);}
446 template<typename Char>
447 static void are_not_equal(const Char* expected, const std::basic_string<Char>& actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
448 template<typename Char>
449 static void are_not_equal(const Char* expected, const std::basic_string<Char>& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
450 template<typename Char>
451 static void are_not_equal(const Char* expected, const std::basic_string<Char>& actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
452 template<typename Char>
453 static void are_not_equal(const Char* expected, const std::basic_string<Char>& actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(std::basic_ifstream<Char>(expected), std::basic_ifstream<Char>(actual), message, stack_frame);}
454 template<typename Char>
455 static void are_not_equal(const Char* expected, const Char* actual) {are_not_equal(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
456 template<typename Char>
457 static void are_not_equal(const Char* expected, const Char* actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(expected, actual, "", stack_frame);}
458 template<typename Char>
459 static void are_not_equal(const Char* expected, const Char* actual, const std::string& message) {are_not_equal(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
460 template<typename Char>
461 static void are_not_equal(const Char* expected, const Char* actual, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal(std::basic_ifstream<Char>(expected), std::basic_ifstream<Char>(actual), message, stack_frame);}
463
475 template<typename Char>
476 static void exists(const std::basic_string<Char>& file) {exists(file, "", xtd::diagnostics::stack_frame::empty());}
477
490 template<typename Char>
491 static void exists(const std::basic_string<Char>& file, const xtd::diagnostics::stack_frame& stack_frame) {exists(file, "", stack_frame);}
492
505 template<typename Char>
506 static void exists(const std::basic_string<Char>& file, const std::string& message) {exists(file, message, xtd::diagnostics::stack_frame::empty());}
507
521 template<typename Char>
522 static void exists(const std::basic_string<Char>& file, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
523 if (std::basic_ifstream<Char>(file).good() == false)
524 base_assert::fail("file exists", base_assert::to_string(file), message, stack_frame);
525 else
526 assert::succeed(message, stack_frame);
527 }
528
530 template<typename Char>
531 static void exists(const Char* file) {exists(file, "", xtd::diagnostics::stack_frame::empty());}
532 template<typename Char>
533 static void exists(const Char* file, const xtd::diagnostics::stack_frame& stack_frame) {exists(file, "", stack_frame);}
534 template<typename Char>
535 static void exists(const Char* file, const std::string& message) {exists(file, message, xtd::diagnostics::stack_frame::empty());}
536 template<typename Char>
537 static void exists(const Char* file, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {exists(std::basic_string<Char>(file), message, stack_frame);}
539
551 template<typename Char>
552 static void does_not_exist(const std::basic_string<Char>& file) {does_not_exist(file, "", xtd::diagnostics::stack_frame::empty());}
553
566 template<typename Char>
567 static void does_not_exist(const std::basic_string<Char>& file, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(file, "", stack_frame);}
568
581 template<typename Char>
582 static void does_not_exist(const std::basic_string<Char>& file, const std::string& message) {does_not_exist(file, message, xtd::diagnostics::stack_frame::empty());}
583
597 template<typename Char>
598 static void does_not_exist(const std::basic_string<Char>& file, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {
599 if (std::basic_ifstream<Char>(file).good() == true)
600 base_assert::fail("not file exists", base_assert::to_string(file), message, stack_frame);
601 else
602 assert::succeed(message, stack_frame);
603 }
604
606 template<typename Char>
607 static void does_not_exist(const Char* file) {does_not_exist(file, "", xtd::diagnostics::stack_frame::empty());}
608 template<typename Char>
609 static void does_not_exist(const Char* file, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(file, "", stack_frame);}
610 template<typename Char>
611 static void does_not_exist(const Char* file, const std::string& message) {does_not_exist(file, message, xtd::diagnostics::stack_frame::empty());}
612 template<typename Char>
613 static void does_not_exist(const Char* file, const std::string& message, const xtd::diagnostics::stack_frame& stack_frame) {does_not_exist(std::basic_string<Char>(file), message, stack_frame);}
615 };
616 }
617}
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 file_assert class contains a collection of static methods that implement the most file assertions...
Definition: file_assert.h:20
static void are_not_equal(const std::basic_istream< Char > &expected, const std::basic_istream< Char > &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are not equal.
Definition: file_assert.h:284
static void are_not_equal(const std::basic_istream< Char > &expected, const std::basic_istream< Char > &actual, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are not equal.
Definition: file_assert.h:327
static void exists(const std::basic_string< Char > &file, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file exists.
Definition: file_assert.h:522
static void does_not_exist(const std::basic_string< Char > &file, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file not exists.
Definition: file_assert.h:567
static void are_equal(const std::basic_string< Char > &expected, const std::basic_string< Char > &actual, const std::string &message)
Asserts that two files are equal.
Definition: file_assert.h:194
static void does_not_exist(const std::basic_string< Char > &file)
Asserts that file not exists.
Definition: file_assert.h:552
static void are_equal(const std::basic_istream< Char > &expected, const std::basic_istream< Char > &actual)
Asserts that two files are equal.
Definition: file_assert.h:44
static void are_equal(const std::basic_istream< Char > &expected, const std::basic_istream< Char > &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are equal.
Definition: file_assert.h:65
static void are_equal(const std::basic_istream< Char > &expected, const std::basic_istream< Char > &actual, const std::string &message)
Asserts that two files are equal.
Definition: file_assert.h:86
static void exists(const std::basic_string< Char > &file, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file exists.
Definition: file_assert.h:491
static void are_equal(const std::basic_istream< Char > &expected, const std::basic_istream< Char > &actual, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are equal.
Definition: file_assert.h:108
static void are_equal(const std::basic_string< Char > &expected, const std::basic_string< Char > &actual)
Asserts that two files are equal.
Definition: file_assert.h:152
static void are_not_equal(const std::basic_istream< Char > &expected, const std::basic_istream< Char > &actual)
Asserts that two files are not equal.
Definition: file_assert.h:263
static void are_not_equal(const std::basic_istream< Char > &expected, const std::basic_istream< Char > &actual, const std::string &message)
Asserts that two files are not equal.
Definition: file_assert.h:305
static void are_not_equal(const std::basic_string< Char > &expected, const std::basic_string< Char > &actual, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are not equal.
Definition: file_assert.h:435
static void are_not_equal(const std::basic_string< Char > &expected, const std::basic_string< Char > &actual)
Asserts that two files are not equal.
Definition: file_assert.h:371
static void does_not_exist(const std::basic_string< Char > &file, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that file not exists.
Definition: file_assert.h:598
static void are_not_equal(const std::basic_string< Char > &expected, const std::basic_string< Char > &actual, const std::string &message)
Asserts that two files are not equal.
Definition: file_assert.h:413
static void does_not_exist(const std::basic_string< Char > &file, const std::string &message)
Asserts that file not exists.
Definition: file_assert.h:582
static void exists(const std::basic_string< Char > &file)
Asserts that file exists.
Definition: file_assert.h:476
static void are_equal(const std::basic_string< Char > &expected, const std::basic_string< Char > &actual, const std::string &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are equal.
Definition: file_assert.h:216
static void are_equal(const std::basic_string< Char > &expected, const std::basic_string< Char > &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are equal.
Definition: file_assert.h:173
static void exists(const std::basic_string< Char > &file, const std::string &message)
Asserts that file exists.
Definition: file_assert.h:506
static void are_not_equal(const std::basic_string< Char > &expected, const std::basic_string< Char > &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two files are not equal.
Definition: file_assert.h:392
Contains xtd::tunit::directory_assert class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17