From 639d63217e8d1e1d1b2ced521fd9971466f0091e Mon Sep 17 00:00:00 2001
From: Robert Marki <gsmiko@gmail.com>
Date: Wed, 18 May 2016 11:25:36 +0200
Subject: [PATCH] Define CMake/CTest tests

Instead of copying the test executable and the JSON files used by
the tests at install time, define CMake/CTest tests for running
the json_unit executable from any build directory with the project's
source directory as its working directory.

- call enable_testing in the main lists file to allow the definition
of tests
- remove install commands from the test directory's lists file
- define two tests
  - json_unit_default for running the default tests by executing
json_unit without any arguments
  - json_unit_all for running all the tests by executing json_unit
with the "*" argument
- update the AppVeyor configuration file to use the new testing method
---
 CMakeLists.txt      |  2 ++
 appveyor.yml        |  3 +--
 test/CMakeLists.txt | 22 +++++++++++++---------
 3 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 18e9c651..c0488b3b 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.0)
 # define the project
 project(nlohmann_json VERSION 2.0.0 LANGUAGES CXX)
 
+enable_testing()
+
 option(BuildTests "Build the unit tests" ON)
 
 # define project variables
diff --git a/appveyor.yml b/appveyor.yml
index 63902ced..a527ef76 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -7,5 +7,4 @@ build_script:
 - cmake . -G "Visual Studio 14 2015"
 - cmake --build . --config Release
 test_script:
-- test\Release\json_unit.exe
-- test\Release\json_unit.exe "*"
+- ctest -C Release -V
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index b63e5a39..c66b19c8 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -1,21 +1,25 @@
 # The unit test executable.
-add_executable(json_unit
+set(JSON_UNITTEST_TARGET_NAME "json_unit")
+add_executable(${JSON_UNITTEST_TARGET_NAME}
     "src/catch.hpp"
     "src/unit.cpp"
 )
 
-set_target_properties(json_unit PROPERTIES
+set_target_properties(${JSON_UNITTEST_TARGET_NAME} PROPERTIES
     CXX_STANDARD 11
     CXX_STANDARD_REQUIRED ON
     COMPILE_DEFINITIONS "$<$<CXX_COMPILER_ID:MSVC>:_SCL_SECURE_NO_WARNINGS>"
     COMPILE_OPTIONS "$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>"
 )
 
-# Install the test binary.
-install(TARGETS json_unit RUNTIME DESTINATION test/bin)
+target_include_directories(${JSON_UNITTEST_TARGET_NAME} PRIVATE "src")
+target_link_libraries(${JSON_UNITTEST_TARGET_NAME} ${JSON_TARGET_NAME})
 
-# Copy the test data to the install tree.
-install(DIRECTORY data/ DESTINATION test/data)
-
-target_include_directories(json_unit PRIVATE "src")
-target_link_libraries(json_unit ${JSON_TARGET_NAME})
+add_test(NAME "${JSON_UNITTEST_TARGET_NAME}_default"
+	COMMAND ${JSON_UNITTEST_TARGET_NAME}
+	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+)
+add_test(NAME "${JSON_UNITTEST_TARGET_NAME}_all"
+	COMMAND ${JSON_UNITTEST_TARGET_NAME} "*"
+	WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
+)