Skip to content

Commit

Permalink
added the example code from issue doctest#436
Browse files Browse the repository at this point in the history
  • Loading branch information
onqtam committed Dec 25, 2020
1 parent b745201 commit 4be9f6e
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ if(MAIN_PROJECT AND DOCTEST_WITH_TESTS)
if(NOT DEFINED ENV{CODE_COVERAGE})
add_subdirectory(examples/exe_with_static_libs)
add_subdirectory(examples/executable_dll_and_plugin)
add_subdirectory(examples/combining_the_same_tests_built_differently_in_multiple_shared_objects)
add_subdirectory(scripts/playground)
add_subdirectory(examples/mpi)
endif()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# for more information about this example refer to the GitHub issue: https://github.com/onqtam/doctest/issues/436

add_library(default SHARED default.cpp)
target_link_libraries(default PUBLIC doctest)
set_target_properties(default PROPERTIES CXX_VISIBILITY_PRESET hidden)

add_library(return42 SHARED return42.cpp)
target_link_libraries(return42 PUBLIC doctest)
set_target_properties(return42 PROPERTIES CXX_VISIBILITY_PRESET hidden)

add_executable(same_tests_multiple_configurations main.cpp)
target_link_libraries(same_tests_multiple_configurations PUBLIC default return42 doctest)


# create the test runner, to which all other targets will link to
add_library(test_runner SHARED test_runner.cpp)
target_link_libraries(test_runner PUBLIC doctest)

# link all targets to the test runner
target_link_libraries(same_tests_multiple_configurations PUBLIC test_runner)
target_link_libraries(default PUBLIC test_runner)
target_link_libraries(return42 PUBLIC test_runner)

doctest_add_test(NAME same_tests_multiple_configurations COMMAND $<TARGET_FILE:same_tests_multiple_configurations> --no-version)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include "doctest/doctest.h"

#include "foo.h"

DOCTEST_SYMBOL_EXPORT void default_cpp_force_link() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include "doctest/doctest.h"
#include <type_traits>

DOCTEST_GCC_SUPPRESS_WARNING("-Wmissing-declarations")

#ifdef RETURN_42
#define TEST_LABEL "[return42] "
#else
#define TEST_LABEL "[default] "
#endif

int bar() {
#ifdef RETURN_42
return 42;
#else
return 11;
#endif
}

#ifdef DOCTEST_LIBRARY_INCLUDED

#ifdef RETURN_42
TEST_CASE(TEST_LABEL "bartest"){
INFO("Running " TEST_LABEL "bartest"); MESSAGE("");
CHECK_EQ(42, bar());
}
#endif

#ifndef RETURN_42
TEST_CASE(TEST_LABEL "bartest"){
INFO("Running " TEST_LABEL "bartest"); MESSAGE("");
CHECK_EQ(11, bar());
}
#endif

TEST_CASE(TEST_LABEL "commontest"){
INFO("Running " TEST_LABEL "commontest"); MESSAGE("");
auto x = bar();
CHECK(std::is_same<decltype(x), int>::value);
}
#endif // DOCTEST_LIBRARY_INCLUDED
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest/doctest.h"

DOCTEST_SYMBOL_IMPORT void default_cpp_force_link();
DOCTEST_SYMBOL_IMPORT void return42_cpp_force_link();

TEST_CASE("main") { MESSAGE("hello from <main.cpp>"); }

int main(int argc, char** argv) {

default_cpp_force_link();
return42_cpp_force_link();

doctest::Context context(argc, argv);
int res = context.run();

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

int client_stuff_return_code = 0;
// your program - if the testing framework is integrated in your production code

return res + client_stuff_return_code; // the result from doctest is propagated here as well
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#include "doctest/doctest.h"

#define RETURN_42
#include "foo.h"

DOCTEST_SYMBOL_EXPORT void return42_cpp_force_link() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[doctest] run with "--help" for options
===============================================================================
foo.h(0):
TEST CASE: [return42] bartest

foo.h(0): MESSAGE:
logged: Running [return42] bartest

===============================================================================
foo.h(0):
TEST CASE: [default] bartest

foo.h(0): MESSAGE:
logged: Running [default] bartest

===============================================================================
foo.h(0):
TEST CASE: [default] commontest

foo.h(0): MESSAGE:
logged: Running [default] commontest

===============================================================================
foo.h(0):
TEST CASE: [return42] commontest

foo.h(0): MESSAGE:
logged: Running [return42] commontest

===============================================================================
main.cpp(0):
TEST CASE: main

main.cpp(0): MESSAGE: hello from <main.cpp>

===============================================================================
test_runner.cpp(0):
TEST CASE: test_runner

test_runner.cpp(0): MESSAGE: hello from <test_runner.cpp>

===============================================================================
[doctest] test cases: 6 | 6 passed | 0 failed | 0 skipped
[doctest] assertions: 4 | 4 passed | 0 failed |
[doctest] Status: SUCCESS!
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="same_tests_multiple_configurations" errors="0" failures="0" tests="4">
<testcase classname="foo.h" name="[return42] bartest" status="run"/>
<testcase classname="foo.h" name="[default] bartest" status="run"/>
<testcase classname="foo.h" name="[default] commontest" status="run"/>
<testcase classname="foo.h" name="[return42] commontest" status="run"/>
<testcase classname="main.cpp" name="main" status="run"/>
<testcase classname="test_runner.cpp" name="test_runner" status="run"/>
</testsuite>
</testsuites>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctest binary="same_tests_multiple_configurations">
<Options order_by="file" rand_seed="0" first="0" last="4294967295" abort_after="0" subcase_filter_levels="2147483647" case_sensitive="false" no_throw="false" no_skip="false"/>
<TestSuite>
<TestCase name="[return42] bartest" filename="foo.h" line="0">
<Message type="WARNING" filename="foo.h" line="0">
<Text/>
<Info>
Running [return42] bartest
</Info>
</Message>
<OverallResultsAsserts successes="1" failures="0"/>
</TestCase>
<TestCase name="[default] bartest" filename="foo.h" line="0">
<Message type="WARNING" filename="foo.h" line="0">
<Text/>
<Info>
Running [default] bartest
</Info>
</Message>
<OverallResultsAsserts successes="1" failures="0"/>
</TestCase>
<TestCase name="[default] commontest" filename="foo.h" line="0">
<Message type="WARNING" filename="foo.h" line="0">
<Text/>
<Info>
Running [default] commontest
</Info>
</Message>
<OverallResultsAsserts successes="1" failures="0"/>
</TestCase>
<TestCase name="[return42] commontest" filename="foo.h" line="0">
<Message type="WARNING" filename="foo.h" line="0">
<Text/>
<Info>
Running [return42] commontest
</Info>
</Message>
<OverallResultsAsserts successes="1" failures="0"/>
</TestCase>
<TestCase name="main" filename="main.cpp" line="0">
<Message type="WARNING" filename="main.cpp" line="0">
<Text>
hello from &lt;main.cpp>
</Text>
</Message>
<OverallResultsAsserts successes="0" failures="0"/>
</TestCase>
<TestCase name="test_runner" filename="test_runner.cpp" line="0">
<Message type="WARNING" filename="test_runner.cpp" line="0">
<Text>
hello from &lt;test_runner.cpp>
</Text>
</Message>
<OverallResultsAsserts successes="0" failures="0"/>
</TestCase>
</TestSuite>
<OverallResultsAsserts successes="4" failures="0"/>
<OverallResultsTestCases successes="6" failures="0" skipped="0"/>
</doctest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#define DOCTEST_CONFIG_IMPLEMENTATION_IN_DLL
#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest/doctest.h"

TEST_CASE("test_runner") { MESSAGE("hello from <test_runner.cpp>"); }

0 comments on commit 4be9f6e

Please sign in to comment.