qxLib
expected_assert.h
Go to the documentation of this file.
1 /**
2 
3  @file expected_assert.h
4  @author Khrapov
5  @date 31.10.2026
6  @copyright (c) Nick Khrapov, 2026. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/asserts/asserts.h>
13 
15 #include <gtest/gtest.h>
17 
18 #include <atomic>
19 #include <optional>
20 #include <regex>
21 
22 namespace qx
23 {
24 
25 class expected_assert;
26 
27 /**
28 
29  @class expected_assert_builder
30  @brief Selects assertion properties and the number of expected occurrences.
31  @details Only specified properties are matched. Strings match exactly unless regex search is enabled; a selected category
32  must outlive the expectation. The default expected count is one.
33  @author Khrapov
34  @date 31.10.2026
35 
36 **/
38 {
39  friend class expected_assert;
40 
41  class string_matcher
42  {
43  public:
44  string_matcher(string_view svValue, bool bRegex);
45  bool matches(string_view svValue) const;
46  string_view get_pattern() const;
47  bool is_regex() const;
48 
49  private:
50  string m_Value;
51  std::optional<std::basic_regex<char_type>> m_Regex;
52  };
53 
54 public:
55  /**
56  @brief Match the failed condition text.
57  @param svCondition - expected text or regex pattern
58  @param bRegex - search using a regex instead of comparing the complete text
59  **/
60  [[nodiscard]] expected_assert_builder& condition(string_view svCondition, bool bRegex = false);
61 
62  /**
63  @brief Require the specified assertion category.
64  @param category - expected value
65  **/
67 
68  /**
69  @brief Require the specified assertion type.
70  @param eType - expected value
71  **/
72  [[nodiscard]] expected_assert_builder& type(assert_type eType);
73 
74  /**
75  @brief Match the formatted user message.
76  @param svPattern - expected text or regex pattern
77  @param bRegex - search using a regex instead of comparing the complete text
78  **/
79  [[nodiscard]] expected_assert_builder& message(string_view svPattern, bool bRegex = false);
80 
81  /**
82  @brief Match the function name.
83  @param svFunction - expected text or regex pattern
84  @param bRegex - search using a regex instead of comparing the complete text
85  **/
86  [[nodiscard]] expected_assert_builder& function(string_view svFunction, bool bRegex = false);
87 
88  /**
89  @brief Match the source file name.
90  @param svFile - expected text or regex pattern
91  @param bRegex - search using a regex instead of comparing the complete text
92  **/
93  [[nodiscard]] expected_assert_builder& file(string_view svFile, bool bRegex = false);
94 
95  /**
96  @brief Require the exact source line number.
97  @param nLine - expected value
98  **/
99  [[nodiscard]] expected_assert_builder& line(i32 nLine);
100 
101  /**
102  @brief Require exactly this many matching assertions.
103  @param nCount - expected value
104  **/
105  [[nodiscard]] expected_assert_builder& count(size_t nCount);
106 
107  /**
108  @brief Start observing assertions using the selected criteria.
109  @return - scoped expectation that reports a count mismatch on destruction
110  **/
111  [[nodiscard]] expected_assert build() const;
112 
113 private:
114  std::optional<string_matcher> m_Condition;
115  const qx::category* m_pCategory = nullptr;
116  std::optional<assert_type> m_Type;
117  std::optional<string_matcher> m_Message;
118  std::optional<string_matcher> m_Function;
119  std::optional<string_matcher> m_File;
120  std::optional<i32> m_Line;
121  size_t m_nCount = 1;
122 };
123 
124 /**
125 
126  @class expected_assert
127  @brief Counts assertions matching the criteria selected by expected_assert_builder.
128  @details Matched assertions are consumed in registration order; other assertions follow normal handling.
129  A matched QX_EXPECT or QX_ENSURE suppresses logging, the assertion callback and debug break;
130  QX_EXPECT control-flow variants still perform their return, break or continue. QX_ENSURE and
131  QX_ASSERT are disabled when QX_WITH_DEBUG_ASSERTS is false. A matched QX_ASSERT or QX_VERIFY
132  also suppresses reporting and debug break, but still calls the fatal exit handler. With the default
133  handler the process terminates without unwinding, so this object's destructor cannot report the count.
134  Otherwise the destructor reports a Google Test failure if the observed count differs from the expected count.
135  @author Khrapov
136  @date 31.10.2026
137 
138 **/
140 {
141  friend class expected_assert_builder;
142 
144 
145 public:
146  ~expected_assert();
147 
148 private:
149  explicit expected_assert(const expected_assert_builder& builder);
150 
151 private:
152  expected_assert_builder m_Builder;
153  std::atomic_size_t m_nActual = 0;
154 };
155 
156 } // namespace qx
157 
A category is a class that identifies a particular piece of code. This code can be located in differe...
Definition: category.h:62
Selects assertion properties and the number of expected occurrences.
expected_assert_builder & file(string_view svFile, bool bRegex=false)
Match the source file name.
expected_assert_builder & condition(string_view svCondition, bool bRegex=false)
Match the failed condition text.
expected_assert_builder & category(const qx::category &category)
Require the specified assertion category.
expected_assert build() const
Start observing assertions using the selected criteria.
expected_assert_builder & type(assert_type eType)
Require the specified assertion type.
expected_assert_builder & message(string_view svPattern, bool bRegex=false)
Match the formatted user message.
expected_assert_builder & count(size_t nCount)
Require exactly this many matching assertions.
expected_assert_builder & line(i32 nLine)
Require the exact source line number.
Counts assertions matching the criteria selected by expected_assert_builder.
#define QX_NONCOPYMOVABLE(className)
Define class as non copyable and non movable.
#define QX_PUSH_SUPPRESS_ALL_WARNINGS()
Disable all warnings.
#define QX_POP_SUPPRESS_WARNINGS()
Enable all warnings.
std::int32_t i32
− 9 223 372 036 854 775 808 .. 9 223 372 036 854 775 807
Definition: typedefs.h:32