Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split unittest into multiple files #485

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
*.app

example
unittest
Testing/*
include/crow/tags

Expand Down
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#####################################
# Define Project-Wide Settings
#####################################
cmake_minimum_required(VERSION 3.15.0 FATAL_ERROR)
cmake_minimum_required(VERSION 3.16.0 FATAL_ERROR)

# Define the project name and language
project(Crow
Expand Down Expand Up @@ -92,19 +92,17 @@ if(CROW_BUILD_EXAMPLES)
endif()

# Tests
if(NOT MSVC AND CROW_BUILD_TESTS)
if(CROW_BUILD_TESTS)
if(NOT "compression" IN_LIST CROW_FEATURES)
message(STATUS "Compression tests are omitted. (Configure with CROW_FEATURES containing 'compression' to enable them)")
endif()
if(NOT "ssl" IN_LIST CROW_FEATURES)
message(STATUS "SSL tests are omitted. (Configure with CROW_FEATURES containing 'ssl' to enable them)")
else()
add_test(NAME ssl_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/ssl/ssltest)
endif()

add_subdirectory(tests)
enable_testing()
add_test(NAME crow_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/unittest)
add_test(NAME crow_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/unittest/unittest)
add_test(NAME template_test COMMAND ${CMAKE_CURRENT_BINARY_DIR}/tests/template/test.py WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/tests/template)
endif()

Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.16)
project(crow_examples)

include(${CMAKE_SOURCE_DIR}/cmake/compiler_options.cmake)
Expand Down
21 changes: 2 additions & 19 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.16)
project(crow_test)

include(${CMAKE_SOURCE_DIR}/cmake/compiler_options.cmake)

set(TEST_SRCS
unittest.cpp
)

add_executable(unittest ${TEST_SRCS})
target_link_libraries(unittest Crow::Crow)
add_warnings_optimizations(unittest)

if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set_target_properties(unittest PROPERTIES COMPILE_FLAGS "--coverage -fprofile-arcs -ftest-coverage")
target_link_libraries(unittest gcov)
endif()

add_subdirectory(unittest)
add_subdirectory(template)
add_subdirectory(multi_file)
if ("ssl" IN_LIST CROW_FEATURES)
add_subdirectory(ssl)
endif()
add_subdirectory(img)
2 changes: 1 addition & 1 deletion tests/img/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.16)

file(COPY . DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
18 changes: 0 additions & 18 deletions tests/ssl/CMakeLists.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tests/template/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.15)
cmake_minimum_required(VERSION 3.16)
project(template_test)

set(PROJECT_INCLUDE_DIR
Expand Down
45 changes: 45 additions & 0 deletions tests/unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.16)
project(crow_test)

include(${CMAKE_SOURCE_DIR}/cmake/compiler_options.cmake)

set(TEST_SRCS
src/app.cpp
src/black_magic.cpp
src/blueprints.cpp
src/json.cpp
src/middlewares.cpp
src/query_string.cpp
src/requests.cpp
src/responses.cpp
src/routing.cpp
src/task_timer.cpp
src/templates.cpp
src/utility.cpp
src/websocket.cpp

src/catch.cpp
)

if("ssl" IN_LIST CROW_FEATURES)
list(APPEND TEST_SRCS "src/ssl.cpp")
endif()
if("compression" IN_LIST CROW_FEATURES)
list(APPEND TEST_SRCS "src/compression.cpp")
endif()

add_executable(unittest ${TEST_SRCS})
target_include_directories(unittest PRIVATE include)
target_link_libraries(unittest Crow::Crow)
target_precompile_headers(unittest PRIVATE include/catch.hpp)
add_warnings_optimizations(unittest)
set_source_files_properties(src/catch.cpp
PROPERTIES
SKIP_PRECOMPILE_HEADERS ON
)


if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set_target_properties(unittest PROPERTIES COMPILE_FLAGS "--coverage -fprofile-arcs -ftest-coverage")
target_link_libraries(unittest gcov)
endif()
127 changes: 127 additions & 0 deletions tests/unittest/include/Middlewares.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#pragma once
#include "crow.h"
#include <vector>

using namespace crow;

struct NullMiddleware
{
struct context
{};

template<typename AllContext>
void before_handle(request&, response&, context&, AllContext&)
{}

template<typename AllContext>
void after_handle(request&, response&, context&, AllContext&)
{}
};

struct NullSimpleMiddleware
{
struct context
{};

void before_handle(request& /*req*/, response& /*res*/, context& /*ctx*/) {}

void after_handle(request& /*req*/, response& /*res*/, context& /*ctx*/) {}
};

struct IntSettingMiddleware
{
struct context
{
int val;
};

template<typename AllContext>
void before_handle(request&, response&, context& ctx, AllContext&)
{
ctx.val = 1;
}

template<typename AllContext>
void after_handle(request&, response&, context& ctx, AllContext&)
{
ctx.val = 2;
}
};

static std::vector<std::string> test_middleware_context_vector;

struct empty_type
{};

template<bool Local>
struct FirstMW : public std::conditional<Local, crow::ILocalMiddleware, empty_type>::type
{
struct context
{
std::vector<string> v;
};

void before_handle(request& /*req*/, response& /*res*/, context& ctx)
{
ctx.v.push_back("1 before");
}

void after_handle(request& /*req*/, response& /*res*/, context& ctx)
{
ctx.v.push_back("1 after");
test_middleware_context_vector = ctx.v;
}
};

template<bool Local>
struct SecondMW : public std::conditional<Local, crow::ILocalMiddleware, empty_type>::type
{
struct context
{};
template<typename AllContext>
void before_handle(request& req, response& res, context&, AllContext& all_ctx)
{
all_ctx.template get<FirstMW<Local>>().v.push_back("2 before");
if (req.url.find("/break") != std::string::npos) res.end();
}

template<typename AllContext>
void after_handle(request&, response&, context&, AllContext& all_ctx)
{
all_ctx.template get<FirstMW<Local>>().v.push_back("2 after");
}
};

template<bool Local>
struct ThirdMW : public std::conditional<Local, crow::ILocalMiddleware, empty_type>::type
{
struct context
{};
template<typename AllContext>
void before_handle(request&, response&, context&, AllContext& all_ctx)
{
all_ctx.template get<FirstMW<Local>>().v.push_back("3 before");
}

template<typename AllContext>
void after_handle(request&, response&, context&, AllContext& all_ctx)
{
all_ctx.template get<FirstMW<Local>>().v.push_back("3 after");
}
};


struct LocalSecretMiddleware : crow::ILocalMiddleware
{
struct context
{};

void before_handle(request& /*req*/, response& res, context& /*ctx*/)
{
res.code = 403;
res.end();
}

void after_handle(request& /*req*/, response& /*res*/, context& /*ctx*/)
{}
};
File renamed without changes.
9 changes: 9 additions & 0 deletions tests/unittest/include/unittest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#define CROW_LOG_LEVEL 0
#define LOCALHOST_ADDRESS "127.0.0.1"

#include "crow.h"

using namespace std;
luca-schlecker marked this conversation as resolved.
Show resolved Hide resolved
using namespace crow;
Loading