basedOpinionated utility library |
git clone git://git.dimitrijedobrota.com/based.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
scopeguard_test.cpp (2377B)
0 #define CATCH_CONFIG_RUNTIME_STATIC_REQUIRE 1 2 #include <stdexcept> 3 4 #include <catch2/catch_test_macros.hpp> 5 6 #include "based/template.hpp" 7 8 // NOLINTBEGIN(*cognitive-complexity*) 9 10 struct set 11 { 12 explicit set(int& val) 13 : m_val(&val) 14 { 15 } 16 17 void operator()() const { *m_val = 1; } 18 19 private: 20 int* m_val; 21 }; 22 23 template class based::scopeguard<set>; 24 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 } 38 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 } 51 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 } 64 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 } 78 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 } 91 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); 101 } 102 REQUIRE(test == 1); 103 } 104 } 105 106 TEST_CASE("exit", "[template/scopeguard]") 107 { 108 SECTION("success") 109 { 110 int test = 0; 111 try { 112 const based::scopeguard_exit guard = set(test); 113 } catch (...) { 114 REQUIRE(false); 115 } 116 REQUIRE(test == 1); 117 } 118 119 // breaks coverage for some reason 120 SECTION("failure") 121 { 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; 128 } 129 REQUIRE(test == 1); 130 } 131 } 132 133 // NOLINTEND(*cognitive-complexity*)