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.
string_assert.h
Go to the documentation of this file.
1 #pragma once
5 #include <xtd/ustring.h>
6 #include "assert.h"
7 #include <locale>
8 #include <cstring>
9 #include <regex>
10 
12 namespace xtd {
14  namespace tunit {
24  class string_assert final : private base_assert {
25  public:
27  string_assert() = delete;
29 
39  static void are_equal_ignoring_case(const xtd::ustring& expected, const xtd::ustring& actual) {are_equal_ignoring_case(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
40 
51  static void are_equal_ignoring_case(const xtd::ustring& expected, const xtd::ustring& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_equal_ignoring_case(expected, actual, "", stack_frame);}
52 
63  static void are_equal_ignoring_case(const xtd::ustring& expected, const xtd::ustring& actual, const xtd::ustring& message) {are_equal_ignoring_case(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
64 
76  static void are_equal_ignoring_case(const xtd::ustring& expected, const xtd::ustring& actual, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
77  if (actual.to_lower() == expected.to_lower())
78  assert::succeed(message, stack_frame);
79  else
80  base_assert::fail(base_assert::to_string(expected) + ", ignoring case", base_assert::to_string(actual), message, stack_frame);
81  }
82 
92  static void are_not_equal_ignoring_case(const xtd::ustring& expected, const xtd::ustring& actual) {are_not_equal_ignoring_case(expected, actual, "", xtd::diagnostics::stack_frame::empty());}
93 
104  static void are_not_equal_ignoring_case(const xtd::ustring& expected, const xtd::ustring& actual, const xtd::diagnostics::stack_frame& stack_frame) {are_not_equal_ignoring_case(expected, actual, "", stack_frame);}
105 
116  static void are_not_equal_ignoring_case(const xtd::ustring& expected, const xtd::ustring& actual, const xtd::ustring& message) {are_not_equal_ignoring_case(expected, actual, message, xtd::diagnostics::stack_frame::empty());}
117 
129  static void are_not_equal_ignoring_case(const xtd::ustring& expected, const xtd::ustring& actual, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
130  if (actual.to_lower() != expected.to_lower())
131  assert::succeed(message, stack_frame);
132  else
133  base_assert::fail("not " + base_assert::to_string(expected)+ ", ignoring case", base_assert::to_string(actual), message, stack_frame);
134  }
135 
146  static void contains(const xtd::ustring& item, const xtd::ustring& string) {contains(item, string, "", xtd::diagnostics::stack_frame::empty());}
147 
159  static void contains(const xtd::ustring& item, const xtd::ustring& string, const xtd::diagnostics::stack_frame& stack_frame) {contains(item, string, "", stack_frame);}
160 
172  static void contains(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message) {contains(item, string, message, xtd::diagnostics::stack_frame::empty());}
173 
186  static void contains(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
187  if (string.find(item) != xtd::ustring::npos)
188  assert::succeed(message, stack_frame);
189  else
190  base_assert::fail("string containing " + base_assert::to_string(item), base_assert::to_string(string), message, stack_frame);
191  }
192 
203  static void does_not_contain(const xtd::ustring& item, const xtd::ustring& string) {does_not_contain(item, string, "", xtd::diagnostics::stack_frame::empty());}
204 
216  static void does_not_contain(const xtd::ustring& item, const xtd::ustring& string, const xtd::diagnostics::stack_frame& stack_frame) {does_not_contain(item, string, "", stack_frame);}
217 
229  static void does_not_contain(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message) {does_not_contain(item, string, message, xtd::diagnostics::stack_frame::empty());}
230 
243  static void does_not_contain(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
244  if (string.find(item) == xtd::ustring::npos)
245  assert::succeed(message, stack_frame);
246  else
247  base_assert::fail("not string containing " + base_assert::to_string(item), base_assert::to_string(string), message, stack_frame);
248  }
249 
260  static void starts_with(const xtd::ustring& item, const xtd::ustring& string) {starts_with(item, string, "", xtd::diagnostics::stack_frame::empty());}
261 
273  static void starts_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::diagnostics::stack_frame& stack_frame) {starts_with(item, string, "", stack_frame);}
274 
286  static void starts_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message) {starts_with(item, string, message, xtd::diagnostics::stack_frame::empty());}
287 
300  static void starts_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
301  if (string.find(item) == 0)
302  assert::succeed(message, stack_frame);
303  else
304  base_assert::fail("string starting with " + base_assert::to_string(item), base_assert::to_string(string), message, stack_frame);
305  }
306 
317  static void does_not_start_with(const xtd::ustring& item, const xtd::ustring& string) {does_not_start_with(item, string, "", xtd::diagnostics::stack_frame::empty());}
318 
330  static void does_not_start_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::diagnostics::stack_frame& stack_frame) {does_not_start_with(item, string, "", stack_frame);}
331 
343  static void does_not_start_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message) {does_not_start_with(item, string, message, xtd::diagnostics::stack_frame::empty());}
344 
357  static void does_not_start_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
358  if (string.find(item) != 0)
359  assert::succeed(message, stack_frame);
360  else
361  base_assert::fail("not string starting with " + base_assert::to_string(item), base_assert::to_string(string), message, stack_frame);
362  }
363 
374  static void ends_with(const xtd::ustring& item, const xtd::ustring& string) {ends_with(item, string, "", xtd::diagnostics::stack_frame::empty());}
375 
387  static void ends_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::diagnostics::stack_frame& stack_frame) {ends_with(item, string, "", stack_frame);}
388 
400  static void ends_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message) {ends_with(item, string, message, xtd::diagnostics::stack_frame::empty());}
401 
414  static void ends_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
415  if (string.rfind(item) != xtd::ustring::npos)
416  assert::succeed(message, stack_frame);
417  else
418  base_assert::fail("string ending with " + base_assert::to_string(item), base_assert::to_string(string), message, stack_frame);
419  }
420 
431  static void does_not_end_with(const xtd::ustring& item, const xtd::ustring& string) {does_not_end_with(item, string, "", xtd::diagnostics::stack_frame::empty());}
432 
444  static void does_not_end_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::diagnostics::stack_frame& stack_frame) {does_not_end_with(item, string, "", stack_frame);}
445 
457  static void does_not_end_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message) {does_not_end_with(item, string, message, xtd::diagnostics::stack_frame::empty());}
458 
471  static void does_not_end_with(const xtd::ustring& item, const xtd::ustring& string, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
472  if (string.rfind(item) == xtd::ustring::npos)
473  assert::succeed(message, stack_frame);
474  else
475  base_assert::fail("not string ending with " + base_assert::to_string(item), base_assert::to_string(string), message, stack_frame);
476  }
477 
487  static void matches(const xtd::ustring& regex_pattern, const xtd::ustring& actual) {matches(regex_pattern, actual, "", xtd::diagnostics::stack_frame::empty());}
488 
499  static void matches(const xtd::ustring& regex_pattern, const xtd::ustring& actual, const xtd::diagnostics::stack_frame& stack_frame) {matches(regex_pattern, actual, "", stack_frame);}
500 
511  static void matches(const xtd::ustring& regex_pattern, const xtd::ustring& actual, const xtd::ustring& message) {matches(regex_pattern, actual, message, xtd::diagnostics::stack_frame::empty());}
512 
524  static void matches(const xtd::ustring& regex_pattern, const xtd::ustring& actual, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
525  std::string pattern(regex_pattern);
526  std::regex r(pattern);
527  std::smatch m;
528  std::string act(actual);
529  if (std::regex_search(act, m, r) == true)
530  assert::succeed(message, stack_frame);
531  else
532  base_assert::fail("string matching " + base_assert::to_string(regex_pattern), base_assert::to_string(actual), message, stack_frame);
533  }
534 
544  static void does_not_match(const xtd::ustring& regex_pattern, const xtd::ustring& actual) {does_not_match(regex_pattern, actual, "", xtd::diagnostics::stack_frame::empty());}
545 
556  static void does_not_match(const xtd::ustring& regex_pattern, const xtd::ustring& actual, const xtd::diagnostics::stack_frame& stack_frame) {does_not_match(regex_pattern, actual, "", stack_frame);}
557 
568  static void does_not_match(const xtd::ustring& regex_pattern, const xtd::ustring& actual, const xtd::ustring& message) {does_not_match(regex_pattern, actual, message, xtd::diagnostics::stack_frame::empty());}
569 
581  static void does_not_match(const xtd::ustring& regex_pattern, const xtd::ustring& actual, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
582  std::string pattern(regex_pattern);
583  std::regex r(pattern);
584  std::smatch m;
585  std::string act(actual);
586  if (std::regex_search(act, m, r) == false)
587  assert::succeed(message, stack_frame);
588  else
589  base_assert::fail("not string matching " + base_assert::to_string(regex_pattern), base_assert::to_string(actual), message, stack_frame);
590  }
591  };
592  }
593 }
594 
595 #define are_equal_ignoring_case_(...) __CMD_ASSERT_ARGS(are_equal_ignoring_case, __VA_ARGS__)
596 
597 #define are_not_equal_ignoring_case_(...) __CMD_ASSERT_ARGS(are_not_equal_ignoring_case, __VA_ARGS__)
598 
599 #define does_not_end_with_(...) __CMD_ASSERT_ARGS(does_not_end_with, __VA_ARGS__)
600 
601 #define does_not_match_(...) __CMD_ASSERT_ARGS(does_not_match, __VA_ARGS__)
602 
603 #define does_not_start_with_(...) __CMD_ASSERT_ARGS(does_not_start_with, __VA_ARGS__)
604 
605 #define ends_with_(...) __CMD_ASSERT_ARGS(ends_with, __VA_ARGS__)
606 
607 #define matches_(...) __CMD_ASSERT_ARGS(matches, __VA_ARGS__)
608 
609 #define starts_with_(...) __CMD_ASSERT_ARGS(starts_with, __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 string_assert class contains a collection of static methods that implement the most string assert...
Definition: string_assert.h:24
static void does_not_start_with(const xtd::ustring &item, const xtd::ustring &string)
Asserts that string does not start with a specific item.
Definition: string_assert.h:317
static void matches(const xtd::ustring &regex_pattern, const xtd::ustring &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that matches regex pattern.
Definition: string_assert.h:499
static void does_not_start_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string starts with item.
Definition: string_assert.h:357
static void does_not_start_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message)
Asserts that string starts with item.
Definition: string_assert.h:343
static void does_not_match(const xtd::ustring &regex_pattern, const xtd::ustring &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that does not match regex pattern.
Definition: string_assert.h:556
static void ends_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string starts with item.
Definition: string_assert.h:387
static void does_not_end_with(const xtd::ustring &item, const xtd::ustring &string)
Asserts that string does not end with a specific item.
Definition: string_assert.h:431
static void contains(const xtd::ustring &item, const xtd::ustring &string)
Asserts that string contains an item.
Definition: string_assert.h:146
static void does_not_end_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message)
Asserts that string starts with item.
Definition: string_assert.h:457
static void does_not_contain(const xtd::ustring &item, const xtd::ustring &string, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string contains an item.
Definition: string_assert.h:216
static void starts_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message)
Asserts that string starts with item.
Definition: string_assert.h:286
static void does_not_contain(const xtd::ustring &item, const xtd::ustring &string)
Asserts that string contains an item.
Definition: string_assert.h:203
static void does_not_match(const xtd::ustring &regex_pattern, const xtd::ustring &actual, const xtd::ustring &message)
Asserts that does not match regex pattern.
Definition: string_assert.h:568
static void does_not_match(const xtd::ustring &regex_pattern, const xtd::ustring &actual, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that does not match regex pattern.
Definition: string_assert.h:581
static void contains(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string contains an item.
Definition: string_assert.h:186
static void does_not_match(const xtd::ustring &regex_pattern, const xtd::ustring &actual)
Asserts that does not match regex pattern.
Definition: string_assert.h:544
static void does_not_end_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string starts with item.
Definition: string_assert.h:471
static void are_equal_ignoring_case(const xtd::ustring &expected, const xtd::ustring &actual, const xtd::ustring &message)
Asserts that two type are equal ignoring case.
Definition: string_assert.h:63
static void does_not_contain(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string contains an item.
Definition: string_assert.h:243
static void starts_with(const xtd::ustring &item, const xtd::ustring &string)
Asserts that string starts witdh item.
Definition: string_assert.h:260
static void contains(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message)
Asserts that string contains an item.
Definition: string_assert.h:172
static void starts_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string starts with item.
Definition: string_assert.h:273
static void ends_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string starts with item.
Definition: string_assert.h:414
static void are_not_equal_ignoring_case(const xtd::ustring &expected, const xtd::ustring &actual)
Asserts that two type are not equal ignoring case.
Definition: string_assert.h:92
static void matches(const xtd::ustring &regex_pattern, const xtd::ustring &actual, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that matches regex pattern.
Definition: string_assert.h:524
static void does_not_start_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string starts with item.
Definition: string_assert.h:330
static void ends_with(const xtd::ustring &item, const xtd::ustring &string)
Asserts that string ends with a specific item.
Definition: string_assert.h:374
static void are_not_equal_ignoring_case(const xtd::ustring &expected, const xtd::ustring &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two type are not equal ignoring case.
Definition: string_assert.h:104
static void are_equal_ignoring_case(const xtd::ustring &expected, const xtd::ustring &actual)
Asserts that two type are equal ignoring case.
Definition: string_assert.h:39
static void are_not_equal_ignoring_case(const xtd::ustring &expected, const xtd::ustring &actual, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two type are not equal ignoring case.
Definition: string_assert.h:129
static void matches(const xtd::ustring &regex_pattern, const xtd::ustring &actual, const xtd::ustring &message)
Asserts that matches regex pattern.
Definition: string_assert.h:511
static void starts_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string starts with item.
Definition: string_assert.h:300
static void are_not_equal_ignoring_case(const xtd::ustring &expected, const xtd::ustring &actual, const xtd::ustring &message)
Asserts that two type are not equal ignoring case.
Definition: string_assert.h:116
static void ends_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message)
Asserts that string starts with item.
Definition: string_assert.h:400
static void are_equal_ignoring_case(const xtd::ustring &expected, const xtd::ustring &actual, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two type are equal ignoring case.
Definition: string_assert.h:51
static void does_not_end_with(const xtd::ustring &item, const xtd::ustring &string, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string starts with item.
Definition: string_assert.h:444
static void contains(const xtd::ustring &item, const xtd::ustring &string, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that string contains an item.
Definition: string_assert.h:159
static void matches(const xtd::ustring &regex_pattern, const xtd::ustring &actual)
Asserts that matches regex pattern.
Definition: string_assert.h:487
static void does_not_contain(const xtd::ustring &item, const xtd::ustring &string, const xtd::ustring &message)
Asserts that string contains an item.
Definition: string_assert.h:229
static void are_equal_ignoring_case(const xtd::ustring &expected, const xtd::ustring &actual, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Asserts that two type are equal ignoring case.
Definition: string_assert.h:76
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
ustring to_lower() const noexcept
Returns a copy of the current string converted to lowercase.
@ r
The R key.
@ m
The M key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17
Contains xtd::ustring class.