based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

scopeguard_test.cpp (2388B)


0 #define CATCH_CONFIG_RUNTIME_STATIC_REQUIRE
2 #include <stdexcept>
4 #include "based/utility/scopeguard.hpp"
6 #include <catch2/catch_test_macros.hpp>
8 // NOLINTBEGIN(*cognitive-complexity*)
10 struct set
11 {
12 explicit set(int& val)
13 : m_val(&val)
14 {
15 }
17 void operator()() const { *m_val = 1; }
19 private:
20 int* m_val;
21 };
23 template class based::scopeguard<set>;
25 TEST_CASE("manual", "[template/scopeguard]")
26 {
27 SECTION("commit")
28 {
29 int test = 0;
30 try {
31 based::scopeguard guard = set(test);
32 guard.commit();
33 } catch (...) {
34 REQUIRE(false);
35 }
36 REQUIRE(test == 1);
37 }
39 // breaks coverage for some reason
40 SECTION("no commit")
41 {
42 int test = 0;
43 try {
44 based::scopeguard guard = set(test); // NOLINT(*const*)
45 } catch (...) {
46 test *= 1;
47 }
48 REQUIRE(test == 0);
49 }
50 }
52 TEST_CASE("on success", "[template/scopeguard]")
53 {
54 SECTION("success")
55 {
56 int test = 0;
57 try {
58 const based::scopeguard_success guard = set(test);
59 } catch (...) {
60 test *= 1;
61 }
62 REQUIRE(test == 1);
63 }
65 // breaks coverage for some reason
66 SECTION("failure")
67 {
68 int test = 0;
69 try {
70 const based::scopeguard_success guard = set(test);
71 throw std::runtime_error {"should not leak"};
72 } catch (...) {
73 test *= 1;
74 }
75 REQUIRE(test == 0);
76 }
77 }
79 TEST_CASE("on failure", "[template/scopeguard]")
80 {
81 SECTION("success")
82 {
83 int test = 0;
84 try {
85 const based::scopeguard_failure guard = set(test);
86 } catch (...) {
87 test *= 1;
88 }
89 REQUIRE(test == 0);
90 }
92 // breaks coverage for some reason
93 SECTION("failure")
94 {
95 int test = 0;
96 try {
97 const based::scopeguard_failure guard = set(test);
98 throw std::runtime_error {"should not leak"};
99 } catch (...) {
100 REQUIRE(true);
102 REQUIRE(test == 1);
106 TEST_CASE("exit", "[template/scopeguard]")
108 SECTION("success")
110 int test = 0;
111 try {
112 const based::scopeguard_exit guard = set(test);
113 } catch (...) {
114 REQUIRE(false);
116 REQUIRE(test == 1);
119 // breaks coverage for some reason
120 SECTION("failure")
122 int test = 0;
123 try {
124 const based::scopeguard_exit guard = set(test);
125 throw std::runtime_error {"should not leak"};
126 } catch (...) {
127 test *= 1;
129 REQUIRE(test == 1);
133 // NOLINTEND(*cognitive-complexity*)