GCC Code Coverage Report


Directory: include/tts/
File: include/tts/test/exceptions.hpp
Date: 2026-10-14 16:28:14
Exec Total Coverage
Lines: 10 13 76.9%
Functions: 2 2 100.0%
Branches: 8 12 66.7%

Line Branch Exec Source
1 //======================================================================================================================
2 //! @file
3 /*
4 TTS - Tiny Test System
5 Copyright : TTS Contributors & Maintainers
6 SPDX-License-Identifier: BSL-1.0
7 */
8 //======================================================================================================================
9 #pragma once
10
11 #include <tts/tools/preprocessor.hpp>
12 #include <tts/engine/info.hpp>
13 #include <tts/engine/logger.hpp>
14 #include <tts/engine/check.hpp>
15
16 namespace tts::_
17 {
18 5 inline logger check_throw(bool caught, check_site const& site)
19 {
20
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if(caught)
21 {
22
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if(check_passed())
23 ✗ check_report(site, ::tts::text {"Expected: %s throws %s.", site.lhs, site.rhs});
24 3 return logger {false};
25 }
26
27
1/1
✓ Branch 2 taken 2 times.
2 return check_failed(site, ::tts::text {"Expected: %s failed to throw %s.", site.lhs, site.rhs});
28 ✗ }
29
30 4 inline logger check_no_throw(bool caught, check_site const& site)
31 {
32
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if(!caught)
33 {
34
1/3
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 5 not taken.
3 if(check_passed()) check_report(site, ::tts::text {"Expected: %s does not throw.", site.lhs});
35 3 return logger {false};
36 }
37
38
1/1
✓ Branch 2 taken 1 times.
1 return check_failed(site, ::tts::text {"Expected: %s throws unexpectedly.", site.lhs});
39 ✗ }
40 }
41
42 #define TTS_THROW_IMPL(EXPR, EXCEPTION, FAILURE) \
43 ::tts::_::check_throw( \
44 [ & ]() -> bool \
45 { \
46 try \
47 { \
48 EXPR; \
49 } \
50 catch(EXCEPTION&) \
51 { \
52 return true; \
53 } \
54 catch(...) \
55 { \
56 } \
57 return false; \
58 }(), \
59 TTS_CHECK_SITE( \
60 FAILURE, TTS_STRING(TTS_REMOVE_PARENS(EXPR)), "", TTS_STRING(TTS_REMOVE_PARENS(EXCEPTION)), ""))
61 /**/
62
63 //======================================================================================================================
64 /**
65 @defgroup test-exceptions Exception Tests Macros
66 @brief Macros for performing checks related to exceptions handling.
67 @{
68 **/
69 //======================================================================================================================
70
71 //======================================================================================================================
72 /**
73 @def TTS_THROW
74 @brief Checks if a given expression throws an exception of a given type
75
76 @param EXPR Expression to evaluate.
77 @param EXCEPTION Expected exception type to be thrown.
78 @param ... Optional tag. If equals to `REQUIRED`, this test will stop the program if it
79 fails.
80
81 @groupheader{Example}
82 @snippet doc/throw.cpp snippet
83 **/
84 //======================================================================================================================
85 #if defined(TTS_DOXYGEN_INVOKED)
86 #define TTS_THROW(EXPR, EXCEPTION, ...)
87 #else
88 #define TTS_THROW(EXPR, EXCEPTION, ...) TTS_THROW_##__VA_ARGS__(EXPR, EXCEPTION)
89 #endif
90
91 #define TTS_THROW_(EXPR, EXCEPTION) TTS_THROW_IMPL(EXPR, EXCEPTION, TTS_FAIL)
92 #define TTS_THROW_REQUIRED(EXPR, EXCEPTION) TTS_THROW_IMPL(EXPR, EXCEPTION, TTS_FATAL)
93
94 #define TTS_NO_THROW_IMPL(EXPR, FAILURE) \
95 ::tts::_::check_no_throw( \
96 [ & ]() -> bool \
97 { \
98 try \
99 { \
100 EXPR; \
101 } \
102 catch(...) \
103 { \
104 return true; \
105 } \
106 return false; \
107 }(), \
108 TTS_CHECK_SITE(FAILURE, TTS_STRING(TTS_REMOVE_PARENS(EXPR)), "", "", ""))
109 /**/
110
111 //======================================================================================================================
112 /**
113 @def TTS_NO_THROW
114 @brief Checks if a given expression throws no exception
115
116 @param EXPR Expression to evaluate.
117 @param ... Optional tag. If equals to `REQUIRED`, this test will stop the program if it
118 fails.
119
120 @groupheader{Example}
121 @snippet doc/no_throw.cpp snippet
122 **/
123 //======================================================================================================================
124 #if defined(TTS_DOXYGEN_INVOKED)
125 #define TTS_NO_THROW(EXPR, ...)
126 #else
127 #define TTS_NO_THROW(EXPR, ...) TTS_NO_THROW_##__VA_ARGS__(EXPR)
128 #endif
129
130 #define TTS_NO_THROW_(EXPR) TTS_NO_THROW_IMPL(EXPR, TTS_FAIL)
131 #define TTS_NO_THROW_REQUIRED(EXPR) TTS_NO_THROW_IMPL(EXPR, TTS_FATAL)
132
133 //======================================================================================================================
134 /**
135 @}
136 **/
137 //======================================================================================================================
138