Skip to content

Commit 35d2e8c

Browse files
committed
feat: Implement initial C++ library
1 parent f9a498b commit 35d2e8c

File tree

12 files changed

+376
-0
lines changed

12 files changed

+376
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ include(CTest)
1010

1111
add_subdirectory(testcontainers-bridge)
1212
add_subdirectory(testcontainers-c)
13+
add_subdirectory(testcontainers-cpp)
1314
add_subdirectory(modules)
1415
if(NOT DEFINED SKIP_DEMOS)
1516
add_subdirectory(demo)

demo/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
add_subdirectory(generic-container)
33
add_subdirectory(wiremock)
44
add_subdirectory(google-test)
5+
add_subdirectory(google-test-cpp)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Google Test demo
2+
# This is based on https://google.github.io/googletest/quickstart-cmake.html
3+
cmake_minimum_required (VERSION 3.26)
4+
project (google-test-cpp-demo
5+
VERSION 0.1.0
6+
DESCRIPTION "Demonstrates usage of Testcontainers C++ in Google Test"
7+
LANGUAGES CXX
8+
)
9+
10+
set(TARGET_OUT ${PROJECT_NAME}.out)
11+
12+
# GoogleTest requires at least C++14
13+
set(CMAKE_CXX_STANDARD 23)
14+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
15+
include(FetchContent)
16+
FetchContent_Declare(
17+
googletest
18+
GIT_REPOSITORY https://github.com/google/googletest.git
19+
GIT_TAG v1.17.0
20+
)
21+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
22+
FetchContent_MakeAvailable(googletest)
23+
24+
enable_testing()
25+
file(COPY test_data DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
26+
27+
add_executable(${TARGET_OUT} test.cpp)
28+
target_link_libraries(${TARGET_OUT} PRIVATE testcontainers-cpp)
29+
target_link_libraries(${TARGET_OUT} PRIVATE GTest::gtest_main)
30+
31+
include(GoogleTest)
32+
gtest_discover_tests(${TARGET_OUT})

demo/google-test-cpp/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Using Testcontainers C++ in Google Test
2+
3+
Demonstrates usage of Testcontainers C++ in [Google
4+
Test](https://github.com/google/googletest). See
5+
[test.cpp](./test.cpp) for the code.
6+
7+
## Run the demo
8+
9+
```bash
10+
cmake -S . -B /tmp/tc-native
11+
cmake --build /tmp/tc-native/
12+
ctest --output-on-failure -R Class
13+
```

demo/google-test-cpp/test.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <iostream>
4+
#include <string>
5+
6+
#include "testcontainers.hpp"
7+
8+
using namespace testcontainers;
9+
10+
class WireMockTestContainerClass : public ::testing::Test {
11+
const char* WIREMOCK_IMAGE = "wiremock/wiremock:3.0.1-1";
12+
const char* WIREMOCK_ADMIN_MAPPING_ENDPOINT = "/__admin/mappings";
13+
14+
protected:
15+
void SetUp() override {
16+
using namespace std::literals;
17+
18+
builder.expose_port(TcpPort{8080}).wait_for_http(TcpPort{8080}, WIREMOCK_ADMIN_MAPPING_ENDPOINT);
19+
}
20+
21+
Container::Builder builder = Container::Builder{WIREMOCK_IMAGE};
22+
};
23+
24+
/// This test runs a "Hello World" example.
25+
TEST_F(WireMockTestContainerClass, HelloWorld) {
26+
auto container = builder.with_file("test_data/hello.json", "/home/wiremock/mappings/hello.json").build();
27+
ASSERT_TRUE(container.has_value()) << "Failed to run the container";
28+
29+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello");
30+
ASSERT_EQ(code, 200) << "Expected 200 OK. Received response: " << response;
31+
}
32+
33+
/// This test responds to an HTTP request with response from a
34+
/// prepared file.
35+
TEST_F(WireMockTestContainerClass, HelloWorldFromResource) {
36+
auto container = builder.with_file("test_data/hello_with_resource.json", "/home/wiremock/mappings/hello2.json")
37+
.with_file("test_data/response.xml", "/home/wiremock/__files/response.xml")
38+
.build();
39+
ASSERT_TRUE(container.has_value()) << "Failed to run the container";
40+
41+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-resource");
42+
ASSERT_EQ(code, 200) << "Expected 200 OK. Received response: " << response;
43+
}
44+
45+
/// This test performs a request that is guaranteed to fail and
46+
/// return an HTTP-500.
47+
TEST_F(WireMockTestContainerClass, HelloWorldFromMissingResource) {
48+
auto container =
49+
builder.with_file("test_data/hello_with_missing_resource.json", "/home/wiremock/mappings/hello3.json").build();
50+
ASSERT_TRUE(container.has_value()) << "Failed to run the container";
51+
52+
auto [code, response] = container->send_http(HttpMethod::Get, TcpPort{8080}, "/hello-from-missing-resource");
53+
ASSERT_EQ(code, 500) << "Expected 500 Internal Server Error. Received response: " << response;
54+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"request": {
3+
"method": "GET",
4+
"url": "/hello"
5+
},
6+
7+
"response": {
8+
"status": 200,
9+
"body": "Hello, world!"
10+
}
11+
}
12+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"request": {
3+
"method": "GET",
4+
"url": "/hello-from-missing-resource"
5+
},
6+
"response": {
7+
"status": 200,
8+
"bodyFileName": "response_missing.xml"
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"request": {
3+
"method": "GET",
4+
"url": "/hello-from-resource"
5+
},
6+
"response": {
7+
"status": 200,
8+
"bodyFileName": "response.xml"
9+
}
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<note>
2+
<to>you</to>
3+
<from>WireMock</from>
4+
<heading>Response</heading>
5+
<body>Hello, world!</body>
6+
</note>

testcontainers-cpp/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
set(TARGET testcontainers-cpp)
2+
set(TARGET_NAME ${TARGET})
3+
set(TARGET_DESCRIPTION "Testcontainer library for C++")
4+
set(TARGET_VERSION ${PROJECT_VERSION})
5+
6+
add_library(${TARGET} INTERFACE)
7+
8+
set(CMAKE_CXX_STANDARD 23)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
11+
target_sources(${TARGET}
12+
PUBLIC FILE_SET HEADERS
13+
BASE_DIRS .
14+
FILES testcontainers.hpp
15+
)
16+
17+
target_link_libraries(${TARGET} INTERFACE testcontainers-c)
18+
19+
configure_file(cmake.pc.in ${TARGET}.pc @ONLY)
20+
install(TARGETS ${TARGET}
21+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
22+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
23+
install(FILES ${CMAKE_BINARY_DIR}/${TARGET}.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)

0 commit comments

Comments
 (0)