Skip to content

Commit

Permalink
Merge pull request #1439 from onqtam/doctest
Browse files Browse the repository at this point in the history
moved from Catch to doctest for unit tests
  • Loading branch information
nlohmann authored Apr 3, 2019
2 parents b21c04c + da5b783 commit d1ef753
Show file tree
Hide file tree
Showing 52 changed files with 6,270 additions and 11,843 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ To make changes, you need to edit the following files:

1. [`include/nlohmann/*`](https://github.com/nlohmann/json/tree/develop/include/nlohmann) - These files are the sources of the library. Before testing or creating a pull request, execute `make amalgamate` to regenerate `single_include/nlohmann/json.hpp`.

2. [`test/src/unit-*.cpp`](https://github.com/nlohmann/json/tree/develop/test/src) - These files contain the [Catch](https://github.com/philsquared/Catch) unit tests which currently cover [100 %](https://coveralls.io/github/nlohmann/json) of the library's code.
2. [`test/src/unit-*.cpp`](https://github.com/nlohmann/json/tree/develop/test/src) - These files contain the [doctest](https://github.com/onqtam/doctest) unit tests which currently cover [100 %](https://coveralls.io/github/nlohmann/json) of the library's code.

If you add or change a feature, please also add a unit test to this file. The unit tests can be compiled and executed with

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ doctest:
# -Wno-c++2a-compat: u8 literals will behave differently in C++20...
# -Wno-deprecated-declarations: the library deprecated some functions
# -Wno-documentation-unknown-command: code uses user-defined commands like @complexity
# -Wno-exit-time-destructors: warning in Catch code
# -Wno-exit-time-destructors: warning in json code triggered by NLOHMANN_JSON_SERIALIZE_ENUM
# -Wno-float-equal: not all comparisons in the tests can be replaced by Approx
# -Wno-keyword-macro: unit-tests use "#define private public"
# -Wno-padded: padding is nothing to warn about
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1270,13 +1270,13 @@ The library itself consists of a single header file licensed under the MIT licen
- [**American fuzzy lop**](http://lcamtuf.coredump.cx/afl/) for fuzz testing
- [**AppVeyor**](https://www.appveyor.com) for [continuous integration](https://ci.appveyor.com/project/nlohmann/json) on Windows
- [**Artistic Style**](http://astyle.sourceforge.net) for automatic source code identation
- [**Catch**](https://github.com/philsquared/Catch) for the unit tests
- [**Clang**](http://clang.llvm.org) for compilation with code sanitizers
- [**CMake**](https://cmake.org) for build automation
- [**Codacity**](https://www.codacy.com) for further [code analysis](https://www.codacy.com/app/nlohmann/json)
- [**Coveralls**](https://coveralls.io) to measure [code coverage](https://coveralls.io/github/nlohmann/json)
- [**Coverity Scan**](https://scan.coverity.com) for [static analysis](https://scan.coverity.com/projects/nlohmann-json)
- [**cppcheck**](http://cppcheck.sourceforge.net) for static analysis
- [**doctest**](https://github.com/onqtam/doctest) for the unit tests
- [**Doozer**](https://doozer.io) for [continuous integration](https://doozer.io/nlohmann/json) on Linux (CentOS, Raspbian, Fedora)
- [**Doxygen**](http://www.stack.nl/~dimitri/doxygen/) to generate [documentation](https://nlohmann.github.io/json/)
- [**git-update-ghpages**](https://github.com/rstacruz/git-update-ghpages) to upload the documentation to gh-pages
Expand Down
29 changes: 14 additions & 15 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if(JSON_NoExceptions)
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DJSON_NOEXCEPTION")
endif()
set(CATCH_TEST_FILTER -e)
set(DOCTEST_TEST_FILTER --no-throw)
endif()

if(JSON_Coverage)
Expand Down Expand Up @@ -54,22 +54,22 @@ if(JSON_Coverage)
endif()

#############################################################################
# Catch library with the main function to speed up build
# doctest library with the main function to speed up build
#############################################################################

add_library(catch_main OBJECT
add_library(doctest_main OBJECT
"src/unit.cpp"
)
set_target_properties(catch_main PROPERTIES
set_target_properties(doctest_main PROPERTIES
COMPILE_DEFINITIONS "$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>"
COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>"
)
if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
target_compile_features(catch_main PUBLIC cxx_range_for)
if (${CMAKE_VERSION} VERSION_LESS "3.8.0")
target_compile_features(doctest_main PUBLIC cxx_range_for)
else()
target_compile_features(catch_main PUBLIC cxx_std_11)
target_compile_features(doctest_main PUBLIC cxx_std_11)
endif()
target_include_directories(catch_main PRIVATE "thirdparty/catch")
target_include_directories(doctest_main PRIVATE "thirdparty/doctest")

# https://stackoverflow.com/questions/2368811/how-to-set-warning-level-in-cmake
if(MSVC)
Expand Down Expand Up @@ -99,37 +99,36 @@ foreach(file ${files})
get_filename_component(file_basename ${file} NAME_WE)
string(REGEX REPLACE "unit-([^$]+)" "test-\\1" testcase ${file_basename})

add_executable(${testcase} $<TARGET_OBJECTS:catch_main> ${file})
add_executable(${testcase} $<TARGET_OBJECTS:doctest_main> ${file})
target_compile_definitions(${testcase} PRIVATE
CATCH_CONFIG_FAST_COMPILE
$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>
DOCTEST_CONFIG_SUPER_FAST_ASSERTS
)
target_compile_options(${testcase} PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
)
target_include_directories(${testcase} PRIVATE
thirdparty/catch
thirdparty/doctest
thirdparty/fifo_map
)
target_link_libraries(${testcase} ${NLOHMANN_JSON_TARGET_NAME})

add_test(NAME "${testcase}_default"
COMMAND ${testcase} ${CATCH_TEST_FILTER}
COMMAND ${testcase} ${DOCTEST_TEST_FILTER}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
set_tests_properties("${testcase}_default" PROPERTIES LABELS "default")

add_test(NAME "${testcase}_all"
COMMAND ${testcase} ${CATCH_TEST_FILTER} "*"
COMMAND ${testcase} ${DOCTEST_TEST_FILTER} --no-skip
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
set_tests_properties("${testcase}_all" PROPERTIES LABELS "all")

if(JSON_Valgrind)
add_test(NAME "${testcase}_valgrind"
COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase} ${CATCH_TEST_FILTER}
COMMAND ${memcheck_command} ${CMAKE_CURRENT_BINARY_DIR}/${testcase} ${DOCTEST_TEST_FILTER}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
set_tests_properties("${testcase}_valgrind" PROPERTIES LABELS "valgrind")
Expand Down
8 changes: 4 additions & 4 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# additional flags
CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic -Wcast-align -Wcast-qual -Wno-ctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder -Wdeprecated -Wno-float-equal
CPPFLAGS += -I ../single_include -I . -I thirdparty/catch -I thirdparty/fifo_map -DCATCH_CONFIG_FAST_COMPILE
CPPFLAGS += -I ../single_include -I . -I thirdparty/doctest -I thirdparty/fifo_map -DDOCTEST_CONFIG_SUPER_FAST_ASSERTS

SOURCES = src/unit.cpp \
src/unit-algorithms.cpp \
Expand Down Expand Up @@ -63,11 +63,11 @@ clean:
# single test file
##############################################################################

json_unit: $(OBJECTS) ../single_include/nlohmann/json.hpp thirdparty/catch/catch.hpp
json_unit: $(OBJECTS) ../single_include/nlohmann/json.hpp thirdparty/doctest/doctest.h
@echo "[CXXLD] $@"
@$(CXX) $(CXXFLAGS) $(LDFLAGS) $(OBJECTS) -o $@

%.o: %.cpp ../single_include/nlohmann/json.hpp thirdparty/catch/catch.hpp
%.o: %.cpp ../single_include/nlohmann/json.hpp thirdparty/doctest/doctest.h
@echo "[CXX] $@"
@$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c $< -o $@

Expand All @@ -76,7 +76,7 @@ json_unit: $(OBJECTS) ../single_include/nlohmann/json.hpp thirdparty/catch/catch
# individual test cases
##############################################################################

test-%: src/unit-%.o src/unit.o ../single_include/nlohmann/json.hpp thirdparty/catch/catch.hpp
test-%: src/unit-%.o src/unit.o ../single_include/nlohmann/json.hpp thirdparty/doctest/doctest.h
@echo "[CXXLD] $@"
@$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $< src/unit.o -o $@

Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>
using nlohmann::json;
Expand Down
3 changes: 2 additions & 1 deletion test/src/unit-allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "doctest_compatibility.h"

#define private public
#include <nlohmann/json.hpp>
using nlohmann::json;
#undef private

// special test case to check if memory is leaked if constructor throws

Expand Down
3 changes: 2 additions & 1 deletion test/src/unit-alt-string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>

#include <string>
#include <utility>

Expand Down
18 changes: 10 additions & 8 deletions test/src/unit-bson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>
using nlohmann::json;

#include <fstream>
#include <sstream>

TEST_CASE("BSON")
{
Expand Down Expand Up @@ -1138,15 +1140,15 @@ TEST_CASE("BSON numerical data")
};

CHECK_THROWS_AS(json::to_bson(j), json::out_of_range&);
CHECK_THROWS_WITH(json::to_bson(j), "[json.exception.out_of_range.407] integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64");
CHECK_THROWS_WITH_STD_STR(json::to_bson(j), "[json.exception.out_of_range.407] integer number " + std::to_string(i) + " cannot be represented by BSON as it does not fit int64");
}
}

}
}
}

TEST_CASE("BSON roundtrips", "[hide]")
TEST_CASE("BSON roundtrips" * doctest::skip())
{
SECTION("reference files")
{
Expand All @@ -1161,8 +1163,8 @@ TEST_CASE("BSON roundtrips", "[hide]")
{
CAPTURE(filename)

SECTION(filename + ": std::vector<uint8_t>")
{
INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
Expand All @@ -1179,8 +1181,8 @@ TEST_CASE("BSON roundtrips", "[hide]")
CHECK(j1 == j2);
}

SECTION(filename + ": std::ifstream")
{
INFO_WITH_TEMP(filename + ": std::ifstream");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
Expand All @@ -1194,8 +1196,8 @@ TEST_CASE("BSON roundtrips", "[hide]")
CHECK(j1 == j2);
}

SECTION(filename + ": uint8_t* and size")
{
INFO_WITH_TEMP(filename + ": uint8_t* and size");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
Expand All @@ -1212,8 +1214,8 @@ TEST_CASE("BSON roundtrips", "[hide]")
CHECK(j1 == j2);
}

SECTION(filename + ": output to output adapters")
{
INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
Expand All @@ -1224,8 +1226,8 @@ TEST_CASE("BSON roundtrips", "[hide]")
(std::istreambuf_iterator<char>(f_bson)),
std::istreambuf_iterator<char>());

SECTION(filename + ": output adapters: std::vector<uint8_t>")
{
INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
std::vector<uint8_t> vec;
json::to_bson(j1, vec);

Expand Down
2 changes: 1 addition & 1 deletion test/src/unit-capacity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "doctest_compatibility.h"

#include <nlohmann/json.hpp>
using nlohmann::json;
Expand Down
30 changes: 19 additions & 11 deletions test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "catch.hpp"
#include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")

#include <nlohmann/json.hpp>
using nlohmann::json;

#include <fstream>
#include <sstream>
#include <iomanip>
#include <set>

class SaxCountdown
{
Expand Down Expand Up @@ -1586,7 +1590,8 @@ TEST_CASE("single CBOR roundtrip")
}
}

TEST_CASE("CBOR regressions", "[!throws]")
#if not defined(JSON_NOEXCEPTION)
TEST_CASE("CBOR regressions")
{
SECTION("fuzz test results")
{
Expand Down Expand Up @@ -1655,12 +1660,13 @@ TEST_CASE("CBOR regressions", "[!throws]")
}
}
}
#endif

TEST_CASE("CBOR roundtrips", "[hide]")
TEST_CASE("CBOR roundtrips" * doctest::skip())
{
SECTION("input from flynn")
{
// most of these are exluded due to differences in key order (not a real problem)
// most of these are excluded due to differences in key order (not a real problem)
auto exclude_packed = std::set<std::string>
{
"test/data/json.org/1.json",
Expand Down Expand Up @@ -1827,8 +1833,8 @@ TEST_CASE("CBOR roundtrips", "[hide]")
{
CAPTURE(filename)

SECTION(filename + ": std::vector<uint8_t>")
{
INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
Expand All @@ -1845,8 +1851,8 @@ TEST_CASE("CBOR roundtrips", "[hide]")
CHECK(j1 == j2);
}

SECTION(filename + ": std::ifstream")
{
INFO_WITH_TEMP(filename + ": std::ifstream");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
Expand All @@ -1860,8 +1866,8 @@ TEST_CASE("CBOR roundtrips", "[hide]")
CHECK(j1 == j2);
}

SECTION(filename + ": uint8_t* and size")
{
INFO_WITH_TEMP(filename + ": uint8_t* and size");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
Expand All @@ -1878,8 +1884,8 @@ TEST_CASE("CBOR roundtrips", "[hide]")
CHECK(j1 == j2);
}

SECTION(filename + ": output to output adapters")
{
INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file
std::ifstream f_json(filename);
json j1 = json::parse(f_json);
Expand All @@ -1892,8 +1898,8 @@ TEST_CASE("CBOR roundtrips", "[hide]")

if (!exclude_packed.count(filename))
{
SECTION(filename + ": output adapters: std::vector<uint8_t>")
{
INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
std::vector<uint8_t> vec;
json::to_cbor(j1, vec);
CHECK(vec == packed);
Expand All @@ -1904,7 +1910,8 @@ TEST_CASE("CBOR roundtrips", "[hide]")
}
}

TEST_CASE("all CBOR first bytes", "[!throws]")
#if not defined(JSON_NOEXCEPTION)
TEST_CASE("all CBOR first bytes")
{
// these bytes will fail immediately with exception parse_error.112
std::set<uint8_t> unsupported =
Expand Down Expand Up @@ -1968,7 +1975,7 @@ TEST_CASE("all CBOR first bytes", "[!throws]")
{
// check that parse_error.112 is only thrown if the
// first byte is in the unsupported set
CAPTURE(e.what())
INFO_WITH_TEMP(e.what());
if (std::find(unsupported.begin(), unsupported.end(), byte) != unsupported.end())
{
CHECK(e.id == 112);
Expand All @@ -1980,6 +1987,7 @@ TEST_CASE("all CBOR first bytes", "[!throws]")
}
}
}
#endif

TEST_CASE("examples from RFC 7049 Appendix A")
{
Expand Down
Loading

0 comments on commit d1ef753

Please sign in to comment.