Skip to content

Commit

Permalink
Add two very simple examples of using doctest with CMake (#209)
Browse files Browse the repository at this point in the history
* add some very simple examples of using doctest after having installed it using cmake

* ran clang-format

* update naming in comments
  • Loading branch information
pr0g authored and onqtam committed Mar 24, 2019
1 parent ff5022c commit 6885508
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
14 changes: 14 additions & 0 deletions examples/dll_installed_doctest/CMakeLists.txt
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.0)

project(example_dll VERSION 0.0.1 LANGUAGES CXX)

find_package(doctest REQUIRED)

add_library("dll" SHARED dll.cpp)
target_compile_features("dll" PRIVATE cxx_std_17)
target_compile_definitions("dll" PUBLIC -D_EXPORT)
target_link_libraries("dll" doctest::doctest)

add_executable(${PROJECT_NAME} main.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
target_link_libraries(${PROJECT_NAME} dll)
22 changes: 22 additions & 0 deletions examples/dll_installed_doctest/dll.cpp
@@ -0,0 +1,22 @@
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#define DOCTEST_CONFIG_IMPLEMENT
#include <doctest/doctest.h>

#include "dll.h"
#include <stdio.h>

extern "C" {
void say_hello_dll() { printf("%s", "Hello, World!\n"); }
}

int factorial(int number) {
return number < 1 ? 1 : number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("testing the factorial function") {
CHECK(factorial(0) == 1);
CHECK(factorial(1) == 1);
CHECK(factorial(2) == 2);
CHECK(factorial(3) == 6);
CHECK(factorial(10) == 3628800);
}
7 changes: 7 additions & 0 deletions examples/dll_installed_doctest/dll.h
@@ -0,0 +1,7 @@
#pragma once

#include "exporting.h"

extern "C" {
DLL_API void say_hello_dll();
}
15 changes: 15 additions & 0 deletions examples/dll_installed_doctest/exporting.h
@@ -0,0 +1,15 @@
#pragma once

#ifdef _EXPORT
#ifdef _MSC_VER
#define DLL_API __declspec(dllexport)
#elif defined __GNUC__
#define DLL_API __attribute__((visibility("default")))
#endif
#else
#ifdef _MSC_VER
#define DLL_API __declspec(dllimport)
#elif defined __GNUC__
#define DLL_API __attribute__((visibility("hidden")))
#endif
#endif
34 changes: 34 additions & 0 deletions examples/dll_installed_doctest/main.cpp
@@ -0,0 +1,34 @@
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include <doctest/doctest.h>

#include "dll.h"

int main(int argc, char **argv) {
doctest::Context context;
context.applyCommandLine(argc, argv);

int res = context.run(); // run doctest

// important - query flags (and --exit) rely on the user doing this
if (context.shouldExit()) {
// propagate the result of the tests
return res;
}

say_hello_dll(); // test dll func
}

int square(const int number) { return number * number; }

TEST_CASE("testing the square function") {
CHECK(square(2) == 4);
CHECK(square(4) == 16);
CHECK(square(5) == 25);
CHECK(square(8) == 64);
}

// running notes
// ./example_dll --no-run (run normal program)
// ./example_dll --exit (run tests then exit)
// ./example_dll (run tests then run program)
// ./example_dll --success (print successful test casts)
9 changes: 9 additions & 0 deletions examples/executable_installed_doctest/CMakeLists.txt
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.0)

project(example_exe VERSION 0.0.1 LANGUAGES CXX)

find_package(doctest REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
target_link_libraries(${PROJECT_NAME} doctest::doctest)
35 changes: 35 additions & 0 deletions examples/executable_installed_doctest/main.cpp
@@ -0,0 +1,35 @@
#define DOCTEST_CONFIG_IMPLEMENT
#include <doctest/doctest.h>

int main(int argc, char **argv) {
doctest::Context context;
context.applyCommandLine(argc, argv);

int res = context.run(); // run doctest

// important - query flags (and --exit) rely on the user doing this
if (context.shouldExit()) {
// propagate the result of the tests
return res;
}

printf("%s", "Hello, World!");
}

int factorial(const int number) {
return number < 1 ? 1 : number <= 1 ? number : factorial(number - 1) * number;
}

TEST_CASE("testing the factorial function") {
CHECK(factorial(0) == 1);
CHECK(factorial(1) == 1);
CHECK(factorial(2) == 2);
CHECK(factorial(3) == 6);
CHECK(factorial(10) == 3628800);
}

// running notes
// ./example_exe --no-run (run normal program)
// ./example_exe --exit (run tests then exit)
// ./example_exe (run tests then run program)
// ./example_exe --success (print successful test casts)

0 comments on commit 6885508

Please sign in to comment.