Skip to content
Permalink
Browse files

Add test framework to Dawg (#12)

Squashed commit to add a testing framework to Dawg
Fixes #11 

* Dawg-11 Add Test structure to Dawg
 - Add Boost library deps to top-level CMakeLists.txt file
in order for it to be visible to the unit tests
 - Add CMake policy
 - Add CMake build guards and configuration guards
 - Linked libtestu01 to CMake
 - Add FindTestu01.cmake module

* Test Framework Update
 - Add conditional to check if TESTU01 lib is found
 - Add PATH_SUFFIX for FreeBSD to FindTESTU01
 - TestU01 No longer required

* update the handling of TestU01 unit tsts
  • Loading branch information...
zryan3 authored and reedacartwright committed Sep 22, 2016
1 parent 8b4dd5b commit 4c478f4173d2c560173c27690fe569a4937dfce3
@@ -5,6 +5,9 @@
# intelliJ stuff
.idea/

# KDE stuff
*kde*

# Compiled Object files
*.slo
*.lo
@@ -2,7 +2,8 @@
# Dawg's Project Configuration
#

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR)
CMAKE_POLICY(SET CMP0028 NEW)
PROJECT(Dawg)

set(NEW_PACKAGE_NAME "dawg")
@@ -27,8 +28,25 @@ ENDIF(USE_STATIC_LIBS)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)

#### RPATH Support #####
# guard against in-source builds
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt. ")
endif()

## Guard against bad build-type strings
IF(CMAKE_BUILD_TYPE)
string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_tolower)
if( NOT cmake_build_type_tolower STREQUAL "debug"
AND NOT cmake_build_type_tolower STREQUAL "release"
AND NOT cmake_build_type_tolower STREQUAL "relwithdebinfo"
AND NOT cmake_build_type_tolower STREQUAL "minsizerel"
AND NOT cmake_build_type_tolower STREQUAL "none")
message(FATAL_ERROR "Unsupported build type \"${CMAKE_BUILD_TYPE}\". Allowed values are Debug, Release, RelWithDebInfo, MinSizeRel, and None (case-insensitive).")
endif()
message(STATUS "Compiling with build type '${CMAKE_BUILD_TYPE}'.")
ENDIF()

#### RPATH Support #####
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
@@ -52,6 +70,81 @@ SET(CMAKE_MAN_DIR "man" CACHE STRING
"Install location for man pages (relative to prefix).")
MARK_AS_ADVANCED(CMAKE_DATA_DIR CMAKE_DOC_DIR CMAKE_MAN_DIR)

## Turn on unit and executable testing
ENABLE_TESTING()

################################################################################
# Detect Version Information
FIND_PACKAGE(Git)
SET(GIT_COMMIT_ID "unknown")
IF(GIT_FOUND)
EXECUTE_PROCESS(COMMAND ${GIT_EXECUTABLE} describe --tags --dirty
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}"
RESULT_VARIABLE res_var OUTPUT_VARIABLE GIT_COM_ID
ERROR_QUIET)
IF(${res_var} EQUAL 0 )
STRING( REPLACE "\n" "" GIT_COMMIT_ID "${GIT_COM_ID}" )
ENDIF()
ENDIF()

# try directory name
IF(NOT GIT_COMMIT_ID AND PROJECT_SOURCE_DIR MATCHES "${NEW_PACKAGE_NAME}-([-a-zA-Z0-9.]*)$" )
SET(GIT_COMMIT_ID "${CMAKE_MATCH_1}")
ENDIF()

# extract project info
IF(GIT_COMMIT_ID MATCHES
"^v?([0-9]+)\\.([0-9]+)\\.?([0-9]+)?((-[a-zA-Z]+)?(-[0-9]+)?(-g[0-9A-Fa-f]+)?(-dirty)?)$" )
IF(NOT DEFINED DAWG_VERSION_MAJOR)
SET(DAWG_VERSION_MAJOR "${CMAKE_MATCH_1}")
ENDIF()
IF(NOT DEFINED DAWG_VERSION_MINOR)
SET(DAWG_VERSION_MINOR "${CMAKE_MATCH_2}")
ENDIF()
IF(NOT DEFINED DAWG_VERSION_PATCH)
SET(DAWG_VERSION_PATCH "${CMAKE_MATCH_3}")
ENDIF()
IF(NOT DEFINED DAWG_VERSION_COMMIT)
STRING(REGEX REPLACE "^-" "" DAWG_VERSION_COMMIT "${CMAKE_MATCH_4}")
ENDIF()
ELSE()
# We were unable to determine project information, use the default values
INCLUDE("${CMAKE_CURRENT_SOURCE_DIR}/src/version.h.in")
ENDIF()

SET(DAWG_VERSION "${DAWG_VERSION_MAJOR}.${DAWG_VERSION_MINOR}")
SET(DAWG_VERSION_SHORT "${DAWG_VERSION_MAJOR}.${DAWG_VERSION_MINOR}")
IF(DEFINED DAWG_VERSION_PATCH)
SET(DAWG_VERSION "${DAWG_VERSION}.${DAWG_VERSION_PATCH}")
SET(DAWG_VERSION_SHORT "${DAWG_VERSION_SHORT}.${DAWG_VERSION_PATCH}")
ENDIF()
IF(DAWG_VERSION_COMMIT)
SET(DAWG_VERSION "${DAWG_VERSION}-${DAWG_VERSION_COMMIT}")
SET(DAWG_VERSION_SHORT "${DAWG_VERSION_SHORT}-${DAWG_VERSION_COMMIT}")
ENDIF()

MESSAGE(STATUS "${DAWG_NAME} version: ${DAWG_VERSION}.")

################################################################################
# Find Boost dependency
SET(Boost_ADDITIONAL_VERSIONS 1.53 1.53.0 1.52 1.52.0 1.51 1.51.0
1.50 1.50.0 1.49 1.49.0 1.48 1.48.0 1.47 1.47.0)
FIND_PACKAGE(Boost 1.47.0 REQUIRED COMPONENTS program_options unit_test_framework)
IF(Boost_FOUND)
ADD_DEFINITIONS(-DBOOST_ALL_NO_LIB -DBOOST_PROGRAM_OPTIONS_NO_LIB)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
IF(NOT Boost_USE_STATIC_LIBS)
ADD_DEFINITIONS(-DBOOST_DYN_LINK -DBOOST_PROGRAM_OPTIONS_DYN_LINK -DBOOST_TEST_DYN_LINK)
ENDIF(NOT Boost_USE_STATIC_LIBS)
# IF(Boost_VERSION LESS 104700)
# MESSAGE(STATUS "WARNING: Headers from Boost 1.47+ are needed for successful compiling.")
# ENDIF(Boost_VERSION LESS 104700)
ENDIF(Boost_FOUND)

################################################################################
# Packaging Information

SET(CPACK_SOURCE_IGNORE_FILES
"dawg\\\\.exe" "dawg$"
)
@@ -0,0 +1,47 @@
# Locate the testu01 library
#
# This module defines the following variables:
#
# TESTU01_LIBRARY the name of the library;
# TESTU01_INCLUDE_DIR where to find testu01 include files.
# TESTU01_FOUND true if both the TESTU01_LIBRARY and TESTU01_INCLUDE_DIR have been found.
#
# To help locate the library and include file, you can define a
# variable called TESTU01_ROOT which points to the root of the TESTU01 library
# installation.
#

# default search dirs
set( _testu01_HEADER_SEARCH_DIRS
"/usr/include"
"/usr/local/include"
"C:/Program Files (x86)/testu01/include" )
set( _testu01_LIB_SEARCH_DIRS
"/usr/lib"
"/usr/local/lib"
"C:/Program Files (x86)/testu01/lib-msvc110" )

# Check environment for root search directory
set( _testu01_ENV_ROOT $ENV{testu01_ROOT} )
if( NOT TESTU01_ROOT AND _testu01_ENV_ROOT )
set(TESTU01_ROOT ${_testu01_ENV_ROOT} )
endif()

# Put user specified location at beginning of search
if( TESTU01_ROOT )
list( INSERT _testu01_HEADER_SEARCH_DIRS 0 "${TESTU01_ROOT}/include" )
list( INSERT _testu01_LIB_SEARCH_DIRS 0 "${TESTU01_ROOT}/testu01" )
endif()

# Search for the header
FIND_PATH(TESTU01_INCLUDE_DIR "TestU01.h"
PATHS ${_testu01_HEADER_SEARCH_DIRS}
PATH_SUFFIXES TestU01)

# Search for the library
FIND_LIBRARY(TESTU01_LIBRARY NAMES testu01
PATHS ${_testu01_LIB_SEARCH_DIRS} )

INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(TESTU01 DEFAULT_MSG
TESTU01_LIBRARY TESTU01_INCLUDE_DIR)
File renamed without changes.
@@ -43,21 +43,6 @@ IF(APPLE_BUNDLE_APP)
SET(PROJ_MACOSX_BUNDLE MACOSX_BUNDLE)
ENDIF(APPLE_BUNDLE_APP)

SET(Boost_ADDITIONAL_VERSIONS 1.53 1.53.0 1.52 1.52.0 1.51 1.51.0
1.50 1.50.0 1.49 1.49.0 1.48 1.48.0 1.47 1.47.0)
FIND_PACKAGE(Boost 1.47.0 REQUIRED COMPONENTS program_options)
IF(Boost_FOUND)
ADD_DEFINITIONS(-DBOOST_ALL_NO_LIB -DBOOST_PROGRAM_OPTIONS_NO_LIB)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
IF(NOT Boost_USE_STATIC_LIBS)
ADD_DEFINITIONS(-DBOOST_DYN_LINK -DBOOST_PROGRAM_OPTIONS_DYN_LINK)
ENDIF(NOT Boost_USE_STATIC_LIBS)
# IF(Boost_VERSION LESS 104700)
# MESSAGE(STATUS "WARNING: Headers from Boost 1.47+ are needed for successful compiling.")
# ENDIF(Boost_VERSION LESS 104700)
ENDIF(Boost_FOUND)

INCLUDE_DIRECTORIES(BEFORE "${CMAKE_CURRENT_SOURCE_DIR}/include")
INCLUDE_DIRECTORIES(BEFORE "${CMAKE_CURRENT_BINARY_DIR}")
INCLUDE_DIRECTORIES(BEFORE "${CMAKE_CURRENT_BINARY_DIR}/include")
@@ -0,0 +1,18 @@
#pragma once
#ifndef DAWG_VERSION_H
#define DAWG_VERSION_H

#if 0
SET(DAWG_VERSION_MAJOR 2)
SET(DAWG_VERSION_MINOR 0)
SET(DAWG_VERSION_PATCH 1)
SET(DAWG_VERSION_COMMIT "${GIT_COMMIT_ID}")
#endif

#define PACKAGE_NAME "@DAWG_NAME@"
#define PACKAGE_VERSION "@DAWG_VERSION@"
#define PACKAGE_VERSION_SHORT "@DAWG_VERSION_SHORT@"

#define PACKAGE_STRING (PACKAGE_NAME " v" PACKAGE_VERSION)

#endif
@@ -0,0 +1,34 @@
###############################################################################
# Determine the location of the executables/scripts and any data sets required for full package testing
set(DAWG_EXE ${CMAKE_BINARY_DIR}/src/dawg)

###############################################################################
# AddCMakeTest
# A macro to create a test based on a .cmake.in file
# Example: AddCMakeTest(DngCall.Bam "" "Build.dng-call;Build.testdata")
# - Creates a test called DngCall.Bam
# - Makes that test depend on Build.dng-call and Build.testdata
# - Processes the file ./DngCall/BamTest.cmake.in
# - When the test is called, it calls the .cmake script with possible PREARGS and post ARGs

macro(AddCMakeTest NAME PREARGS DEPENDS)
string(REPLACE "." "/" pathname "${NAME}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/${pathname}Test.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${pathname}Test.cmake" @ONLY)
add_test(NAME ${NAME}
COMMAND ${CMAKE_COMMAND} ${PREARGS}
-P "${CMAKE_CURRENT_BINARY_DIR}/${pathname}Test.cmake" ${ARGN})
if("${DEPENDS}")
set_tests_properties(${NAME} PROPERTIES DEPENDS "${DEPENDS}")
endif()
endmacro()

###############################################################################
# Add Tests Here

add_test(Source.CheckBuildDir ${CMAKE_COMMAND} -E md5sum "${CMAKE_SOURCE_DIR}/build/.gitkeep")
set_tests_properties(Source.CheckBuildDir PROPERTIES
PASS_REGULAR_EXPRESSION "d41d8cd98f00b204e9800998ecf8427e"
)

add_subdirectory(Unit)
@@ -0,0 +1,103 @@
macro(ESCAPE_STRING STR)
string(REPLACE "\\" "\\\\" ${STR} "${${STR}}")
string(REPLACE "\n" "\\n" ${STR} "${${STR}}")
string(REPLACE "\t" "\\t" ${STR} "${${STR}}")
endmacro()

function(CheckProcessTest PREFIX TEST)
message(STATUS "Test ${PREFIX}.${TEST}...")

execute_process( COMMAND ${${TEST}-CMD}
OUTPUT_VARIABLE stdout
ERROR_VARIABLE stderr
RESULT_VARIABLE result
WORKING_DIRECTORY "${${TEST}-WD}"
)
string(REPLACE "\n" "\n out> " out " out> ${stdout}")
string(REPLACE "\n" "\n err> " err " err> ${stderr}")

if(DEFINED ${TEST}-RESULT)
if(NOT "${result}" STREQUAL "${${TEST}-RESULT}")
message(FATAL_ERROR
"Test result does not match \"${${TEST}-RESULT}\".\n"
"Test result: ${result}\n"
"Test output:\n"
"${out}\n"
"${err}" )
endif()
endif()

if(DEFINED ${TEST}-RESULT-FAIL)
if("${result}" STREQUAL "${${TEST}-RESULT-FAIL}")
message(FATAL_ERROR
"Test result unexpectedly matches \"${${TEST}-RESULT-FAIL}\".\n"
"Test result: ${result}\n"
"Test output:\n"
"${out}\n"
"${err}" )
endif()
endif()

if(DEFINED ${TEST}-STDERR)
foreach(test_str ${${TEST}-STDERR})
if(NOT "${stderr}" MATCHES "${test_str}")
ESCAPE_STRING(test_str)
message(FATAL_ERROR
"Test stderr does not match \"${test_str}\".\n"
"Test result: ${result}\n"
"Test output:\n"
"${out}\n"
"${err}" )
endif()
endforeach()
endif()

if(DEFINED ${TEST}-STDERR-FAIL)
foreach(test_str ${${TEST}-STDERR-FAIL})
if("${stderr}" MATCHES "${test_str}")
ESCAPE_STRING(test_str)
message(FATAL_ERROR
"Test stderr unexpectedly matches \"${test_str}\".\n"
"Test result: ${result}\n"
"Test output:\n"
"${out}\n"
"${err}" )
endif()
endforeach()
endif()

if(DEFINED ${TEST}-STDOUT)
foreach(test_str ${${TEST}-STDOUT})
if(NOT "${stdout}" MATCHES "${test_str}")
ESCAPE_STRING(test_str)
message(FATAL_ERROR
"Test stdout does not match \"${test_str}\".\n"
"Test result: ${result}\n"
"Test output:\n"
"${out}\n"
"${err}" )
endif()
endforeach()
endif()

if(DEFINED ${TEST}-STDOUT-FAIL)
foreach(test_str ${${TEST}-STDOUT-FAIL})
if("${stdout}" MATCHES "${test_str}")
ESCAPE_STRING(test_str)
message(FATAL_ERROR
"Test stdout unexpectedly matches \"${test_str}\".\n"
"Test result: ${result}\n"
"Test output:\n"
"${out}\n"
"${err}" )
endif()
endforeach()
endif()

endfunction()

function(CheckProcessTests PREFIX)
foreach(TEST ${ARGN})
CheckProcessTest("${PREFIX}" "${TEST}")
endforeach()
endfunction()
@@ -0,0 +1,8 @@
set(Help-CMD @DAWG_CALL_EXE@ --help)
set(Help-WD ".")
set(Help-RESULT 0)
set(Help-STDERR "Usage:\n dawg")

include("@CMAKE_CURRENT_SOURCE_DIR@/CheckProcessTest.cmake")

CheckProcessTests(Dawg.Run Help)

0 comments on commit 4c478f4

Please sign in to comment.
You can’t perform that action at this time.