|
1 | 1 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
2 | 2 |
|
| 3 | +#define CATCH_CONFIG_MAIN |
| 4 | +#include <catch2/catch_all.hpp> |
| 5 | + |
| 6 | +// for g++-15 the order is important -- import after includes |
3 | 7 | import beman.scope; |
4 | | -// #include <stdexcept> |
5 | | -// #include <string> |
6 | | -#include <cassert> |
7 | | - |
8 | | -// #define CATCH_CONFIG_MAIN |
9 | | -// #include <catch2/catch_all.hpp> |
10 | | - |
11 | | -// clang-format off |
12 | | -// struct noisy_resource { |
13 | | -// noisy_resource() { std::print( "construct noisy\n" ); } |
14 | | -// ~noisy_resource() { std::print( "destroy noisy\n" ); } |
15 | | -// }; |
16 | | - |
17 | | -//TEST_CASE("module-test", "[scope_module_test]") { |
18 | | -int main() |
19 | | -{ |
20 | | - bool exit_ran, success_ran, fail_ran = false; |
| 8 | + |
| 9 | +struct DummyResource { |
| 10 | + bool& cleaned; |
| 11 | + |
| 12 | + DummyResource(bool& flag) : cleaned(flag) { cleaned = false; } |
| 13 | + |
| 14 | + bool is_clean() const { return cleaned; } |
| 15 | +}; |
| 16 | + |
| 17 | +TEST_CASE("module-test", "[scope_module_test]") { |
| 18 | + bool exit_ran, success_ran, fail_ran, cleaned = true; |
21 | 19 | { |
22 | | - beman::scope::scope_exit _([&exit_ran] { exit_ran = true; }); |
23 | | - beman::scope::scope_success _([&success_ran] { success_ran = true; }); |
24 | | - beman::scope::scope_fail _([&fail_ran] { fail_ran = true; }); |
25 | | - // auto resource_ptr = beman::scope::unique_resource(new noisy_resource(), |
26 | | - // // Cleanup function |
27 | | - // [](noisy_resource* ptr) { delete ptr; }); |
| 20 | + // clang-format off |
| 21 | + beman::scope::scope_exit _se([&exit_ran] { exit_ran = true; }); |
| 22 | + beman::scope::scope_success _ss([&success_ran] { success_ran = true; }); |
| 23 | + beman::scope::scope_fail _sf([&fail_ran] { fail_ran = true; }); |
| 24 | + auto resource_ptr = beman::scope::unique_resource(new DummyResource(cleaned), |
| 25 | + [](DummyResource* ptr) { ptr->cleaned =true; delete ptr; }); |
| 26 | + REQUIRE(cleaned == false); |
| 27 | + REQUIRE(resource_ptr->is_clean() == false); |
| 28 | + // clang-format on |
28 | 29 | } // Normal scope exit |
29 | 30 |
|
30 | | - assert(exit_ran == true); |
31 | | - assert(success_ran == true); |
32 | | - assert(fail_ran == false); |
| 31 | + REQUIRE(exit_ran == true); |
| 32 | + REQUIRE(success_ran == true); |
| 33 | + REQUIRE(fail_ran == false); |
| 34 | + REQUIRE(cleaned == true); |
33 | 35 |
|
34 | | - } |
| 36 | +} |
0 commit comments