Skip to content

Commit 108a5fe

Browse files
committed
* reworked testing setup
* now generates a large binary which executes all tests for the github action
1 parent 46cb1a0 commit 108a5fe

File tree

7 files changed

+55
-18
lines changed

7 files changed

+55
-18
lines changed

CMakeLists.txt

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
cmake_minimum_required(VERSION 3.20)
1+
cmake_minimum_required(VERSION 3.15)
22
project(netlib)
33
set(CMAKE_CXX_STANDARD 20)
44

5-
set(NETLIB_SRC src/netlib.hpp src/socket.hpp src/error.hpp src/client.hpp
6-
src/server.hpp src/service_resolver.hpp
7-
src/endpoint_accessor.hpp src/thread_pool.hpp)
5+
set(NETLIB_SRC
6+
src/netlib.hpp
7+
src/socket.hpp
8+
src/error.hpp
9+
src/client.hpp
10+
src/server.hpp
11+
src/service_resolver.hpp
12+
src/endpoint_accessor.hpp
13+
src/thread_pool.hpp)
814

9-
# doctest
10-
include_directories(3rdparty)
11-
add_subdirectory(tests)
12-
add_subdirectory(examples)
15+
option(BUILD_TESTS "Build tests" ON)
16+
option(BUILD_EXAMPLES "Build example programs" ON)
17+
18+
if(BUILD_TESTS)
19+
# doctest
20+
include_directories(3rdparty)
21+
add_subdirectory(tests)
22+
endif()
23+
24+
if (BUILD_EXAMPLES)
25+
add_subdirectory(examples)
26+
endif()

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,20 @@ after trying ASIO, which I found way to heavy for my tastes.
1818
### How?
1919

2020
You can just add this repo as a git submodule, which at this point is
21-
probably (IMO) the best way to handle C++ dependencies. Just `#include "netlib.hpp"`
22-
somewhere.
21+
probably (IMO) the best way to handle C++ dependencies.
2322

23+
```shell
24+
git submodule add https://github.com/lpcvoid/cpp-net-lib.git extern/cpp-net-lib
25+
```
26+
27+
This will check out the lib as a submodule within your project. Now just `#include "extern/netlib.hpp"` somewhere.
28+
29+
There are some cmake flags you can use:
30+
31+
| Option | Default | Description |
32+
|---|---|---|
33+
| __BUILD_TESTS__ | __ON__ | Builds tests using `doctest`,which is then introduced as a dependency. |
34+
| __BUILD_EXAMPLES__ | __ON__ | Builds some small example programs. |
2435

2536
### Short introduction
2637

tests/CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
set(TEST_SOURCES test_client_examplecom.cpp test_client_server.cpp test_client_server_disconnect.cpp)
22

3-
foreach (testsource ${TEST_SOURCES})
4-
string(REPLACE ".cpp" "" testname ${testsource})
5-
add_executable(${testname} ${testsource})
6-
endforeach (testsource ${TEST_SOURCES})
3+
enable_testing()
4+
add_executable(test_libnetcpp test_main.cpp ${TEST_SOURCES})
5+
target_link_libraries(test_libnetcpp pthread)
6+
add_test(test_libnetcpp test_libnetcpp)

tests/test_client_examplecom.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
21
#include "../3rdparty/doctest/doctest/doctest.h"
32
#include "../src/netlib.hpp"
43

tests/test_client_server.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
21
#include "../3rdparty/doctest/doctest/doctest.h"
32
#include "../src/netlib.hpp"
3+
#include <random>
4+
#include <sstream>
45

56
using namespace std::chrono_literals;
67

78
static const std::string hello_msg = "hello!";
89
static const std::vector<uint8_t> client_message = {1,2,3};
910
static const std::vector<uint8_t> server_response_message = {1, 3, 3, 7};
10-
static const uint16_t test_port = 8888;
11+
12+
std::random_device rd;
13+
std::mt19937 gen(rd());
14+
std::uniform_int_distribution<> distr(10000, 65000);
15+
16+
static uint16_t test_port = distr(gen);
1117

1218
std::string to_hex_array(const std::vector<uint8_t> &data) {
1319
std::stringstream stream;

tests/test_client_server_disconnect.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
21
#include "../3rdparty/doctest/doctest/doctest.h"
32
#include "../src/netlib.hpp"
43

tests/test_main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#define DOCTEST_CONFIG_IMPLEMENT
2+
#include "../3rdparty/doctest/doctest/doctest.h"
3+
4+
int main(int argc, char** argv) {
5+
doctest::Context context;
6+
context.applyCommandLine(argc, argv);
7+
return context.run();
8+
}

0 commit comments

Comments
 (0)