Skip to content

Commit c413b87

Browse files
committed
add unique resource test
1 parent 11ede22 commit c413b87

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

tests/CMakeLists.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ FetchContent_Declare(
99
)
1010
FetchContent_MakeAvailable(Catch2)
1111

12-
add_executable(tests.scope)
13-
target_sources(tests.scope PRIVATE scope.test.cpp)
12+
set(ALL_TESTNAMES scope unique_resource)
1413

15-
target_link_libraries(tests.scope PRIVATE Catch2::Catch2WithMain)
16-
17-
target_include_directories(tests.scope PRIVATE ${CMAKE_SOURCE_DIR}/include)
14+
message("Tests to be built: ${ALL_TESTS}")
1815

1916
include(CTest)
2017
include(Catch)
21-
catch_discover_tests(tests.scope)
18+
19+
20+
foreach(testname ${ALL_TESTNAMES})
21+
add_executable(test.${testname})
22+
target_sources(test.${testname} PRIVATE ${testname}.test.cpp)
23+
target_include_directories(test.${testname} PRIVATE ${CMAKE_SOURCE_DIR}/include)
24+
target_link_libraries(test.{testname} PRIVATE Catch2::Catch2WithMain)
25+
endforeach()
26+
27+
28+
catch_discover_tests(test.scope test.unique_resource)

tests/unique_resource.test.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <memory>
2+
#include <cstdio>
3+
#include <beman/scope/scope.hpp>
4+
5+
constexpr bool open_file_good = false;
6+
constexpr bool close_file_good = false;
7+
8+
// define only in one cpp file
9+
#define CATCH_CONFIG_MAIN
10+
#include <catch2/catch_all.hpp>
11+
12+
TEST_CASE("Construct file unique_resource") {
13+
14+
{
15+
auto file = beman::scope::unique_resource(
16+
fopen("example.txt", "w"), // Acquire the FILE*
17+
[](FILE* f) {
18+
if (f) {
19+
fclose(f); // Release (cleanup) the resource
20+
close_file_good = true;
21+
}
22+
}
23+
);
24+
25+
if (!file.get()) {
26+
return 1;
27+
}
28+
open_file_good = true;
29+
}
30+
31+
32+
REQUIRE(open_file_good == true);
33+
REQUIRE(cloes_file_good == true);
34+
}

0 commit comments

Comments
 (0)