diff --git a/.travis.yml b/.travis.yml index e8c397c5..1b733315 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,8 +17,6 @@ addons: - cmake - lcov - g++ - - binutils-dev - - libiberty-dev - valgrind before_install: diff --git a/CMakeLists.txt b/CMakeLists.txt index 8012a1ce..37fd470a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,8 +80,6 @@ endif() include(ConfigureChecks.cmake) configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h) -# check subdirectories -add_subdirectory(src) # Dependency on our own library so we can use CGREEN_LIBRARY in all subdirectories if (CGREEN_WITH_STATIC_LIBRARY) set(CGREEN_LIBRARY ${CGREEN_STATIC_LIBRARY}) @@ -89,8 +87,11 @@ else () set(CGREEN_LIBRARY ${CGREEN_SHARED_LIBRARY}) endif() +# check subdirectories +add_subdirectory(src) add_subdirectory(doc) add_subdirectory(include) + if (UNIX OR MSYS) # reflective runner only supported on UNIX/binutils platforms add_subdirectory(tools) diff --git a/DefineOptions.cmake b/DefineOptions.cmake index 62489e5c..fdbc3d6a 100644 --- a/DefineOptions.cmake +++ b/DefineOptions.cmake @@ -1,5 +1,4 @@ option(CGREEN_WITH_STATIC_LIBRARY "Build with a static library" OFF) option(CGREEN_WITH_UNIT_TESTS "Build unit tests" ON) -option(CGREEN_BUILD_RUNNER "Build auto-discovering cgreen-runner using libbfd" ON) option(CGREEN_INTERNAL_WITH_GCOV "Build with test coverage instrumentation" OFF) mark_as_advanced(CGREEN_INTERNAL_WITH_GCOV) diff --git a/Makefile b/Makefile index 5c4ee5e3..42887c49 100755 --- a/Makefile +++ b/Makefile @@ -90,31 +90,29 @@ DIFF_TOOL_ARGUMENTS = $(1)_tests \ .PHONY: unit unit: build-it - if [ -f build/tools/cgreen-runner ]; then \ - cd build ; \ - $(LDPATH) tools/cgreen-runner -c `find tests -name $(PREFIX)cgreen_c_tests$(SUFFIX)` ; \ - r=$$((r + $$?)) ; \ - $(LDPATH) tools/cgreen-runner -c `find tests -name $(PREFIX)cgreen_cpp_tests$(SUFFIX)` ; \ - r=$$((r + $$?)) ; \ - $(LDPATH) tools/cgreen-runner -c `find tools/tests -name $(PREFIX)cgreen_runner_tests$(SUFFIX)` ; \ - r=$$((r + $$?)) ; \ - cd tests ; \ - $(LDPATH) $(XML_DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,xml_output) ; \ - r=$$((r + $$?)) ; \ - $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,assertion_messages) ; \ - r=$$((r + $$?)) ; \ - $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,mock_messages) ; \ - r=$$((r + $$?)) ; \ - $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,constraint_messages) ; \ - r=$$((r + $$?)) ; \ - $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,custom_constraint_messages) ; \ - r=$$((r + $$?)) ; \ - $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,ignore_messages) ; \ - r=$$((r + $$?)) ; \ - $(LDPATH) CGREEN_PER_TEST_TIMEOUT=1 $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,failure_messages) ; \ - r=$$((r + $$?)) ; \ - exit $$r; \ - fi + cd build ; \ + $(LDPATH) tools/cgreen-runner -c `find tests -name $(PREFIX)cgreen_c_tests$(SUFFIX)` ; \ + r=$$((r + $$?)) ; \ + $(LDPATH) tools/cgreen-runner -c `find tests -name $(PREFIX)cgreen_cpp_tests$(SUFFIX)` ; \ + r=$$((r + $$?)) ; \ + $(LDPATH) tools/cgreen-runner -c `find tools/tests -name $(PREFIX)cgreen_runner_tests$(SUFFIX)` ; \ + r=$$((r + $$?)) ; \ + cd tests ; \ + $(LDPATH) $(XML_DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,xml_output) ; \ + r=$$((r + $$?)) ; \ + $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,assertion_messages) ; \ + r=$$((r + $$?)) ; \ + $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,mock_messages) ; \ + r=$$((r + $$?)) ; \ + $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,constraint_messages) ; \ + r=$$((r + $$?)) ; \ + $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,custom_constraint_messages) ; \ + r=$$((r + $$?)) ; \ + $(LDPATH) $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,ignore_messages) ; \ + r=$$((r + $$?)) ; \ + $(LDPATH) CGREEN_PER_TEST_TIMEOUT=1 $(DIFF_TOOL) $(call DIFF_TOOL_ARGUMENTS,failure_messages) ; \ + r=$$((r + $$?)) ; \ + exit $$r .PHONY: doc doc: build @@ -133,20 +131,30 @@ chunked: doc asciidoctor-chunker build/doc/cgreen-guide-en.html -o docs echo open $(PWD)/docs/index.html -############# Internal -.PHONY:build-it -build-it: build/Makefile - cmake --build build +.PHONY:valgrind +valgrind: build-it + @echo -n "Running all tests under Valgrind " + @> valgrind.log + @for lib in `ls build/tests/$(PREFIX)*_tests$(SUFFIX)` ; \ + do \ + echo -n "." ; \ + LD_LIBRARY_PATH=build/src valgrind --leak-check=full build/tools/cgreen-runner $$lib >> valgrind.log 2>&1 ; \ + done + @echo + grep --with-filename --line-number " lost: " valgrind.log | grep -v " 0 bytes" ; \ + if [ $$? -eq 1 ] ; then echo "Nothing lost" ; fi -build: - mkdir -p build -build/Makefile: | build + +############# Internal +.PHONY:build-it +build-it: ifeq ($(OS),Darwin) #cmake -DCMAKE_OSX_ARCHITECTURES="arm64e;x86_64" -S . -B build cmake -S . -B build else cmake -S . -B build endif + $(MAKE) -C build .SILENT: diff --git a/README.md b/README.md index 2a37f6ab..5e44781d 100644 --- a/README.md +++ b/README.md @@ -67,43 +67,12 @@ You can also clone the repository or download the source zip from [GitHub](http: You need the [CMake](http://www.cmake.org) build system. Most standard C/C++ compilers should work. GCC definitely does. -Perl, diff, find and sed are required to run unit-tests. Most distro will have -those already installed. - -To build cgreen-runner, you need binutils development package. At runtime, -cgreen-runner only requires binutils library. Some distro do not distinguish the -two. - -|Distro | Build | Run | ---- | --- | --- -|Arch|bintuils|bintuils| -|Cygwin|cygwin64-libbfd|cygwin64-libbfd| -|Debian|binutils-dev|libbinutils| -|Fedora|bintuils-devel|bintuils-devel| -|OpenSUSE|bintuils-devel|bintuils-devel| +Perl, diff, find and sed are required to run Cgreen's own +unit-tests. Most distro will have those already installed. In the root directory run ``make``. That will configure and build the library and the `cgreen-runner`, both supporting both C and C++. See -also the documentation. The later will only be build if build requirements are met. - -Here are some example of how to build using docker for various distro: - -``` -# debian build (will not clean) -$ docker run --rm -v $PWD:/cgreen debian bash -c \ - "apt update && apt -y install git cmake gcc g++ binutils-dev && cd /cgreen && make unit test" -# debian run (we cannot use make unit test since cmake will want to recompile) -$ docker run --rm -v $PWD:/cgreen debian bash -c \ - 'apt update && apt -y install libbinutils && cd /cgreen && ./build/tools/cgreen-runner $(find . -name *.so) rm -rf build' - -# Fedora -$ docker run -t --rm -v $PWD:/cgreen fedora bash -c \ - "/usr/bin/yum -y install cmake gcc g++ git diffutils findutils && cd /cgreen && make unit test && make clean" - -# OpenSUSE -$ docker run --rm -v $PWD:/cgreen opensuse/tumbleweed bash -c \ - '/usr/bin/zypper refresh && /usr/bin/zypper --non-interactive install git cmake gcc gcc-c++ git binutils-devel diffutils findutils && cd /cgreen && make unit test && make clean' -``` +also the documentation. ## Using It diff --git a/cmake/Modules/FindLibBfd.cmake b/cmake/Modules/FindLibBfd.cmake deleted file mode 100644 index 259a1061..00000000 --- a/cmake/Modules/FindLibBfd.cmake +++ /dev/null @@ -1,87 +0,0 @@ -# - Try to find libbfd -# Once done this will define -# -# LIBBFD_FOUND - system has libbfd -# LIBBFD_INCLUDE_DIRS - the libbfd include directory -# LIBBFD_LIBRARIES - Link these to use libbfd -# LIBBFD_DEFINITIONS - Compiler switches required for using libbfd -# -# Based on: -# -# Copyright (c) 2008 Bernhard Walle -# -# Redistribution and use is allowed according to the terms of the New -# BSD license. -# For details see the accompanying COPYING-CMAKE-SCRIPTS file. -# - - -if (LIBBFD_LIBRARIES AND LIBBFD_INCLUDE_DIRS) - set (LIBBFD_FIND_QUIETLY TRUE) -endif () - -find_path (LIBBFD_INCLUDE_DIRS - NAMES - bfd.h - dis-asm.h - PATHS - /usr/include - /usr/local/include - /opt/local/include - /opt/include - ENV CPATH) - -# Ugly, yes ugly... -find_library (LIBBFD_BFD_LIBRARY - NAMES - bfd - PATHS - /usr/lib - /usr/lib64 - /usr/local/lib - /usr/local/lib64 - /usr/include - /opt/local/lib - /opt/usr/lib64 - ENV LIBRARY_PATH - ENV LD_LIBRARY_PATH) - -find_library (LIBIBERTY_LIBRARY - NAMES - iberty - PATHS - /usr/lib - /usr/lib64 - /usr/local/lib - /usr/local/lib64 - /usr/include - /opt/local/lib - /opt/usr/lib64 - ENV LIBRARY_PATH - ENV LD_LIBRARY_PATH) - -find_library (LIBZ_LIBRARY - NAMES - z - PATHS - /usr/lib - /usr/lib64 - /usr/local/lib - /usr/local/lib64 - /usr/include - /opt/local/lib - /opt/usr/lib64 - ENV LIBRARY_PATH - ENV LD_LIBRARY_PATH) - -include (FindPackageHandleStandardArgs) - -# handle the QUIETLY and REQUIRED arguments and set LIBBFD_FOUND to TRUE if all listed variables are TRUE -FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibBfd DEFAULT_MSG - LIBBFD_BFD_LIBRARY - LIBBFD_INCLUDE_DIRS - LIBIBERTY_LIBRARY - LIBZ_LIBRARY) - -set(LIBBFD_LIBRARIES "${LIBBFD_BFD_LIBRARY}" "${LIBIBERTY_LIBRARY}" "${LIBZ_LIBRARY}") -mark_as_advanced(LIBBFD_INCLUDE_DIRS LIBBFD_LIBRARIES LIBBFD_BFD_LIBRARY LIBIBERTY_LIBRARY LIBZ_LIBRARY) diff --git a/cmake/Modules/FindValgrind.cmake b/cmake/Modules/FindValgrind.cmake index 026a7613..ce05b433 100644 --- a/cmake/Modules/FindValgrind.cmake +++ b/cmake/Modules/FindValgrind.cmake @@ -8,4 +8,4 @@ if (NOT Valgrind_FOUND) set(Valgrind_FOUND ${Valgrind_FOUND} CACHE BOOL "Flag whether Valgrind package was found") mark_as_advanced(Valgrind_FOUND Valgrind_EXECUTABLE) -endif () +endif() diff --git a/cmake/Modules/MacroAddValgrindTest.cmake b/cmake/Modules/MacroAddValgrindTest.cmake index 1c6c09e5..e2035a27 100644 --- a/cmake/Modules/MacroAddValgrindTest.cmake +++ b/cmake/Modules/MacroAddValgrindTest.cmake @@ -19,7 +19,7 @@ macro (macro_add_valgrind_test) ) set_tests_properties( valgrind_${libname} PROPERTIES - FAIL_REGULAR_EXPRESSION "(definitely|indirectly|possible) lost: [1-9]" + FAIL_REGULAR_EXPRESSION "(definitely|indirectly|possibly) lost: [1-9]" ) if (CYGWIN OR WIN32) set_tests_properties(${ARGV1} PROPERTIES ENVIRONMENT PATH=${PROJECT_BINARY_DIR}/src:$ENV{PATH}) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b89c7e00..a4095ce4 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,6 +17,14 @@ endif(MSVC) include_directories(${CGREEN_TEST_INCLUDE_DIRS}) +# Dependency on our own library +if (CGREEN_WITH_STATIC_LIBRARY) + set(CGREEN_LIBRARY ${CGREEN_STATIC_LIBRARY}) +else () + set(CGREEN_LIBRARY ${CGREEN_SHARED_LIBRARY}) +endif() + + # C tests, library to use with runner, and a main program # NOTE that some of these are "pure" unit tests which only # need to work in C, since Cgreen's internal is C. @@ -69,11 +77,9 @@ set(TEST_TARGET_LIBRARIES ${CGREEN_LIBRARY}) macro_add_unit_test(test_cgreen_c "${c_tests_SRCS}" "${TEST_TARGET_LIBRARIES}") macro_add_test(NAME test_cgreen_c_run_named_test COMMAND test_cgreen_c integer_one_should_assert_true) -find_package(LibBfd) -if(LibBfd_FOUND) - # run them with cgreen-runner also - macro_add_test(NAME runner_test_cgreen_c COMMAND cgreen-runner -x TEST ./${CMAKE_SHARED_LIBRARY_PREFIX}${CGREEN_C_TESTS_LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}) -endif() +# run them with cgreen-runner also +macro_add_test(NAME runner_test_cgreen_c COMMAND cgreen-runner -x TEST ./${CMAKE_SHARED_LIBRARY_PREFIX}${CGREEN_C_TESTS_LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}) + # C++ tests, library to use with runner, and a main program # NOTE that .cpp-versions should be links to .c-versions so we avoid duplication @@ -107,145 +113,133 @@ SET_SOURCE_FILES_PROPERTIES(${cpp_tests_SRCS} PROPERTIES LANGUAGE CXX) macro_add_unit_test(test_cgreen_cpp "${cpp_tests_SRCS}" "${TEST_TARGET_LIBRARIES}" ${CMAKE_CXX_IMPLICIT_LINK_LIBRARIES}) macro_add_test(NAME test_cgreen_cpp_run_named_test COMMAND test_cgreen_cpp different_pointers_with_same_contents_should_assert_equal) - -find_package(LibBfd) -if(LibBfd_FOUND) - macro_add_test(NAME runner_test_cgreen_cpp COMMAND cgreen-runner -x TEST ./${CMAKE_SHARED_LIBRARY_PREFIX}${CGREEN_CPP_TESTS_LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}) - - # Temporary single CUT test library - # Maybe we should do this for all tests instead - # and then aggregate the DLL/so/dylibs instead... - # Then this might be possible to turn into a macro - # which could allow "add_runner_lib(text_reporter)" - foreach(case - assertion - breadcrumb - cdash_reporter - cgreen_value - constraint - cute_reporter - double - message_formatting - messaging - mocks - parameters - reflective - reflective_runner_no_teardown - text_reporter - unit - vector - xml_reporter - ) - set(${case}_tests_SRCS - ${case}_tests.c - ) - set(${case}_tests_library - ${case}_tests - ) - add_library(${${case}_tests_library} SHARED ${${case}_tests_SRCS}) - target_link_libraries(${${case}_tests_library} ${CGREEN_LIBRARY}) - macro_add_valgrind_test(${${case}_tests_library}) - endforeach(case) - - - # Libraries for a set of output comparing tests to run with runner - - set(constraint_messages_library constraint_messages_tests) - set(constraint_messages_library_SRCS constraint_messages_tests.c) - add_library(${constraint_messages_library} SHARED ${constraint_messages_library_SRCS}) - target_link_libraries(${constraint_messages_library} ${CGREEN_LIBRARY}) - macro_add_valgrind_test(${constraint_messages_library}) - - set(custom_constraint_messages_library custom_constraint_messages_tests) - set(custom_constraint_messages_library_SRCS custom_constraint_messages_tests.c) - add_library(${custom_constraint_messages_library} SHARED ${custom_constraint_messages_library_SRCS}) - target_link_libraries(${custom_constraint_messages_library} ${CGREEN_LIBRARY}) - macro_add_valgrind_test(${custom_constraint_messages_library}) - - set(mock_messages_library mock_messages_tests) - set(mock_messages_library_SRCS mock_messages_tests.c) - add_library(${mock_messages_library} SHARED ${mock_messages_library_SRCS}) - target_link_libraries(${mock_messages_library} ${CGREEN_LIBRARY}) - macro_add_valgrind_test(${mock_messages_library}) - - set(failure_messages_library failure_messages_tests) - set(failure_messages_library_SRCS failure_messages_tests.c) - add_library(${failure_messages_library} SHARED ${failure_messages_library_SRCS}) - target_link_libraries(${failure_messages_library} ${CGREEN_LIBRARY}) - macro_add_valgrind_test(${failure_messages_library}) - - set(assertion_messages_library assertion_messages_tests) - set(assertion_messages_library_SRCS assertion_messages_tests.c) - add_library(${assertion_messages_library} SHARED ${assertion_messages_library_SRCS}) - target_link_libraries(${assertion_messages_library} ${CGREEN_LIBRARY}) - macro_add_valgrind_test(${assertion_messages_library}) - - set(ignore_messages_library ignore_messages_tests) - set(ignore_messages_library_SRCS ignore_messages_tests.c) - add_library(${ignore_messages_library} SHARED ${ignore_messages_library_SRCS}) - target_link_libraries(${ignore_messages_library} ${CGREEN_LIBRARY}) - macro_add_valgrind_test(${ignore_messages_library}) - - set(xml_output_library xml_output_tests) - set(xml_output_library_SRCS xml_output_tests.c) - add_library(${xml_output_library} SHARED ${xml_output_library_SRCS}) - target_link_libraries(${xml_output_library} ${CGREEN_LIBRARY}) - macro_add_valgrind_test(${xml_output_library}) - - set(TEST_TARGET_LIBRARIES ${CGREEN_LIBRARY}) - - macro_add_test( - NAME constraint_messsages - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff - constraint_messages_tests # Name - ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are - ${constraint_messages_library}.expected +macro_add_test(NAME runner_test_cgreen_cpp COMMAND cgreen-runner -x TEST ./${CMAKE_SHARED_LIBRARY_PREFIX}${CGREEN_CPP_TESTS_LIBRARY}${CMAKE_SHARED_LIBRARY_SUFFIX}) + +# Temporary single CUT test library +# Maybe we should do this for all tests instead +# and then aggregate the DLL/so/dylibs instead... +# Then this might be possible to turn into a macro +# which could allow "add_runner_lib(text_reporter)" +foreach(case + assertion + breadcrumb + cdash_reporter + cgreen_value + constraint + cute_reporter + double + message_formatting + messaging + mocks + parameters + reflective + reflective_runner_no_teardown + text_reporter + unit + vector + xml_reporter ) - - macro_add_test( - NAME custom_constraint_messsages - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff - custom_constraint_messages_tests # Name - ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are - ${custom_constraint_messages_library}.expected + set(${case}_tests_SRCS + ${case}_tests.c ) - - macro_add_test( - NAME mock_messsages - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff - mock_messages_tests # Name - ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are - ${mock_messages_library}.expected + set(${case}_tests_library + ${case}_tests ) + add_library(${${case}_tests_library} SHARED ${${case}_tests_SRCS}) + target_link_libraries(${${case}_tests_library} ${CGREEN_LIBRARY}) +endforeach(case) - macro_add_test(NAME failure_messages - COMMAND env "CGREEN_PER_TEST_TIMEOUT=2" ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff - failure_messages_tests # Name - ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are - ${failure_messages_library}.expected - ) - macro_add_test(NAME assertion_messages - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff - assertion_messages_tests # Name - ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are - ${assertion_messages_library}.expected - ) +# Libraries for a set of output comparing tests to run with runner - macro_add_test(NAME ignore_messages - COMMAND env "CGREEN_PER_TEST_TIMEOUT=2" ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff - ignore_messages_tests # Name - ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are - ${ignore_messages_library}.expected - ) +set(constraint_messages_library constraint_messages_tests) +set(constraint_messages_library_SRCS constraint_messages_tests.c) +add_library(${constraint_messages_library} SHARED ${constraint_messages_library_SRCS}) +target_link_libraries(${constraint_messages_library} ${CGREEN_LIBRARY}) - macro_add_test(NAME xml_output - COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_xml_output_diff - xml_output_tests # Name - ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are - ${xml_output_library}.expected - ) +set(custom_constraint_messages_library custom_constraint_messages_tests) +set(custom_constraint_messages_library_SRCS custom_constraint_messages_tests.c) +add_library(${custom_constraint_messages_library} SHARED ${custom_constraint_messages_library_SRCS}) +target_link_libraries(${custom_constraint_messages_library} ${CGREEN_LIBRARY}) + +set(mock_messages_library mock_messages_tests) +set(mock_messages_library_SRCS mock_messages_tests.c) +add_library(${mock_messages_library} SHARED ${mock_messages_library_SRCS}) +target_link_libraries(${mock_messages_library} ${CGREEN_LIBRARY}) + +set(failure_messages_library failure_messages_tests) +set(failure_messages_library_SRCS failure_messages_tests.c) +add_library(${failure_messages_library} SHARED ${failure_messages_library_SRCS}) +target_link_libraries(${failure_messages_library} ${CGREEN_LIBRARY}) + +set(assertion_messages_library assertion_messages_tests) +set(assertion_messages_library_SRCS assertion_messages_tests.c) +add_library(${assertion_messages_library} SHARED ${assertion_messages_library_SRCS}) +target_link_libraries(${assertion_messages_library} ${CGREEN_LIBRARY}) + +set(ignore_messages_library ignore_messages_tests) +set(ignore_messages_library_SRCS ignore_messages_tests.c) +add_library(${ignore_messages_library} SHARED ${ignore_messages_library_SRCS}) +target_link_libraries(${ignore_messages_library} ${CGREEN_LIBRARY}) + +set(xml_output_library xml_output_tests) +set(xml_output_library_SRCS xml_output_tests.c) +add_library(${xml_output_library} SHARED ${xml_output_library_SRCS}) +target_link_libraries(${xml_output_library} ${CGREEN_LIBRARY}) + +set(TEST_TARGET_LIBRARIES ${CGREEN_LIBRARY}) + +macro_add_test( + NAME constraint_messsages + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff + constraint_messages_tests # Name + ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are + ${constraint_messages_library}.expected +) + +macro_add_test( + NAME custom_constraint_messsages + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff + custom_constraint_messages_tests # Name + ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are + ${custom_constraint_messages_library}.expected +) + +macro_add_test( + NAME mock_messsages + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff + mock_messages_tests # Name + ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are + ${mock_messages_library}.expected +) + +macro_add_test(NAME failure_messages + COMMAND env "CGREEN_PER_TEST_TIMEOUT=2" ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff + failure_messages_tests # Name + ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are + ${failure_messages_library}.expected +) + +macro_add_test(NAME assertion_messages + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff + assertion_messages_tests # Name + ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are + ${assertion_messages_library}.expected +) + +macro_add_test(NAME ignore_messages + COMMAND env "CGREEN_PER_TEST_TIMEOUT=2" ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_runner_output_diff + ignore_messages_tests # Name + ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are + ${ignore_messages_library}.expected +) + +macro_add_test(NAME xml_output + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/../tools/cgreen_xml_output_diff + xml_output_tests # Name + ${CMAKE_CURRENT_SOURCE_DIR} # Where sources are + ${xml_output_library}.expected +) -endif() # add verification that all public api is available as it should add_subdirectory(api) diff --git a/tests/Makefile b/tests/Makefile index 837442c8..ba816d68 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -40,8 +40,6 @@ TEST_LIBRARIES = $(patsubst %.c,lib%.so, $(TEST_SOURCES)) all: $(TEST_LIBRARIES) cd ..; make --no-print-directory - if [ -f ../build/tools/cgreen-runner ]; then \ - $(LOAD_PATH) ../build/tools/cgreen-runner -s unittests $?; \ - fi + $(LOAD_PATH) ../build/tools/cgreen-runner -s unittests $? lib%_tests.so : %_tests.o ; $(CC) -shared -o $@ $^ -L$(LIBRARY_PATH) -lcgreen diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 49ee2bd9..4ccf4362 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,45 +1,28 @@ include_directories(${CGREEN_PUBLIC_INCLUDE_DIRS} ${PROJECT_BINARY_DIR}) -find_package(LibBfd) +set(RUNNER_SRCS + ${CMAKE_CURRENT_SOURCE_DIR}/cgreen-runner.c + ${CMAKE_CURRENT_SOURCE_DIR}/gopt.c + ${CMAKE_CURRENT_SOURCE_DIR}/gopt-errors.c + ${CMAKE_CURRENT_SOURCE_DIR}/runner.c + ${CMAKE_CURRENT_SOURCE_DIR}/discoverer.c + ${CMAKE_CURRENT_SOURCE_DIR}/test_item.c + ${CMAKE_CURRENT_SOURCE_DIR}/io.c +) +set_source_files_properties(${RUNNER_SRCS} PROPERTIES LANGUAGE C) -# Need to explicitly add libintl - Cygwin issue with binutils-2.37-2 -if (CYGWIN) - find_library(EXPLICIT_INTLIB_ONLY_REQUIRED_FOR_CYGWIN NAMES intl libintl) -else () - set(EXPLICIT_INTLIB_ONLY_REQUIRED_FOR_CYGWIN "") -endif() +include(DefineRelativeFilePaths) +cmake_define_relative_file_paths ("${RUNNER_SRCS}") -if(LibBfd_FOUND) - set(RUNNER_SRCS - ${CMAKE_CURRENT_SOURCE_DIR}/cgreen-runner.c - ${CMAKE_CURRENT_SOURCE_DIR}/gopt.c - ${CMAKE_CURRENT_SOURCE_DIR}/gopt-errors.c - ${CMAKE_CURRENT_SOURCE_DIR}/runner.c - ${CMAKE_CURRENT_SOURCE_DIR}/discoverer.c - ${CMAKE_CURRENT_SOURCE_DIR}/test_item.c - ${CMAKE_CURRENT_SOURCE_DIR}/bfd_adapter.c - ) - set_source_files_properties(${RUNNER_SRCS} PROPERTIES LANGUAGE C) - include(DefineRelativeFilePaths) - cmake_define_relative_file_paths ("${RUNNER_SRCS}") +add_executable(cgreen-runner ${RUNNER_SRCS}) +target_link_libraries(cgreen-runner ${CGREEN_SHARED_LIBRARY} ${CMAKE_DL_LIBS}) - add_executable(cgreen-runner ${RUNNER_SRCS}) - target_link_libraries(cgreen-runner PRIVATE - ${CGREEN_LIBRARY} - ${CMAKE_DL_LIBS} - ${LIBBFD_LIBRARIES} - ${EXPLICIT_INTLIB_ONLY_REQUIRED_FOR_CYGWIN} - ) +install(TARGETS cgreen-runner + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + DESTINATION ${CMAKE_INSTALL_BINDIR} +) - install(TARGETS cgreen-runner - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - DESTINATION ${CMAKE_INSTALL_BINDIR} - ) - - install(PROGRAMS cgreen-debug - DESTINATION ${CMAKE_INSTALL_BINDIR}) -else() - message(WARNING “libbfd not found, cgreen-runner will not be built”) -endif() +install(PROGRAMS cgreen-debug + DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/tools/bfd_adapter.c b/tools/bfd_adapter.c deleted file mode 100644 index 5f2f2dd6..00000000 --- a/tools/bfd_adapter.c +++ /dev/null @@ -1,40 +0,0 @@ -#include "bfd_adapter.h" - -#include -/* Adapter to libbfd */ -bfd *bfd_adapter_openr(const char *filename, const char *target) { - - return bfd_openr(filename, target); -} - -bfd_boolean bfd_adapter_close(bfd *abfd){ - return bfd_close(abfd); -} - -long bfd_adapter_get_dynamic_symtab_upper_bound(bfd *abfd) { - return bfd_get_dynamic_symtab_upper_bound(abfd); -} - -flagword bfd_adapter_get_file_flags(const bfd *abfd){ - return bfd_get_file_flags(abfd); -} - -struct bfd *bfd_adapter_asymbol_bfd(const asymbol *sy) { - return bfd_asymbol_bfd(sy); -} - -bfd_boolean bfd_adapter_is_target_special_symbol(bfd *abfd, asymbol *symbols) { - return bfd_is_target_special_symbol(abfd, symbols); -} - -long bfd_adapter_canonicalize_dynamic_symtab(bfd *abfd, asymbol **symbols) { - return bfd_canonicalize_dynamic_symtab(abfd, symbols); -} - -bfd_boolean bfd_adapter_check_format(bfd *abfd, bfd_format format) { - return bfd_check_format(abfd, format); -} - -void *bfd_adapter_alloc(bfd *abfd, bfd_size_type wanted) { - return bfd_alloc(abfd, wanted); -} diff --git a/tools/bfd_adapter.h b/tools/bfd_adapter.h deleted file mode 100644 index f0d66d54..00000000 --- a/tools/bfd_adapter.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef BFD_ADAPTER_H -#define BFD_ADAPTER_H - -// bfd.h needs to have PACKAGE and PACKAGE_VERSION defined -#define PACKAGE "cgreen-runner" -#define PACKAGE_VERSION "unknown" -#include - - -#ifdef UNITTESTING -#define bfd_adapter_openr bfd_adapter_openr_unittesting -#define bfd_adapter_close bfd_adapter_close_unittesting -#define bfd_adapter_get_dynamic_symtab_upper_bound bfd_adapter_get_dynamic_symtab_upper_bound_unittesting -#define bfd_adapter_get_file_flags bfd_adapter_get_file_flags_unittesting -#define bfd_adapter_asymbol_bfd bfd_adapter_asymbol_bfd_unittesting -#define bfd_adapter_is_target_special_symbol bfd_adapter_is_target_special_symbol_unittesting -#define bfd_adapter_canonicalize_dynamic_symtab bfd_adapter_canonicalize_dynamic_symtab_unittesting -#define bfd_adapter_check_format bfd_adapter_check_format_unittesting -#define bfd_adapter_alloc bfd_adapter_alloc_unittesting -#endif - -bfd *bfd_adapter_openr(const char *filename, const char *target); -bfd_boolean bfd_adapter_close(bfd *abfd); - -long bfd_adapter_get_dynamic_symtab_upper_bound(bfd *abfd); -flagword bfd_adapter_get_file_flags(const bfd *abfd); - -struct bfd *bfd_adapter_asymbol_bfd(const asymbol *sy); -bfd_boolean bfd_adapter_is_target_special_symbol(bfd *abfd, asymbol *symbols); -long bfd_adapter_canonicalize_dynamic_symtab(bfd *abfd, asymbol **symbols); - -bfd_boolean bfd_adapter_check_format(bfd *abfd, bfd_format format); -void *bfd_adapter_alloc(bfd *abfd, bfd_size_type wanted); - -#endif diff --git a/tools/bfd_adapter.mocks b/tools/bfd_adapter.mocks deleted file mode 100644 index 6ab1ab03..00000000 --- a/tools/bfd_adapter.mocks +++ /dev/null @@ -1,38 +0,0 @@ -#include "bfd_adapter.h" - -/* Adapter to libbfd */ -bfd *bfd_adapter_openr(const char *filename, const char *target) { - return (bfd *) mock(filename, target); -} - -bfd_boolean bfd_adapter_close(bfd *abfd){ - return (bfd_boolean) mock(abfd); -} - -long bfd_adapter_get_dynamic_symtab_upper_bound(bfd *abfd) { - return (long) mock(abfd); -} - -flagword bfd_adapter_get_file_flags(const bfd *abfd){ - return (flagword) mock(abfd); -} - -struct bfd *bfd_adapter_asymbol_bfd(const asymbol *sy) { - return (struct bfd *) mock(sy); -} - -bfd_boolean bfd_adapter_is_target_special_symbol(bfd *abfd, asymbol *symbols) { - return (bfd_boolean) mock(abfd, symbols); -} - -long bfd_adapter_canonicalize_dynamic_symtab(bfd *abfd, asymbol **symbols) { - return (long) mock(abfd, symbols); -} - -bfd_boolean bfd_adapter_check_format(bfd *abfd, bfd_format format) { - return (bfd_boolean) mock(abfd, format); -} - -void *bfd_adapter_alloc(bfd *abfd, bfd_size_type wanted) { - return (void *) mock(abfd, wanted); -} diff --git a/tools/discoverer.c b/tools/discoverer.c index a55bbc36..3c55b597 100644 --- a/tools/discoverer.c +++ b/tools/discoverer.c @@ -1,22 +1,17 @@ #include "discoverer.h" -#include "bfd_adapter.h" +#include "io.h" #include #include -#include #include #include #include "../src/utils.h" -#include "test_item.h" -#ifdef UNITTESTING -int printf_unittesting(const char *fmt, ...); -#define printf printf_unittesting -#endif +#include "test_item.h" static const char *cgreen_spec_start_of(const char *line) { return strstr(line, CGREEN_SPEC_PREFIX CGREEN_SEPARATOR); @@ -26,92 +21,52 @@ static bool contains_cgreen_spec(const char *line) { return cgreen_spec_start_of(line) != NULL; } -static void add_all_tests_from(asymbol **symbols, long symbol_count, CgreenVector *tests, bool verbose) { - for (long index = 0; index < symbol_count; ++index, ++symbols) { - bfd *cur_bfd; - if ((*symbols != NULL) - && ((cur_bfd = bfd_adapter_asymbol_bfd(*symbols)) != NULL) - && (!bfd_adapter_is_target_special_symbol(cur_bfd, *symbols)) - && contains_cgreen_spec((*symbols)->name)) { - TestItem *test_item = create_test_item_from(cgreen_spec_start_of((*symbols)->name)); - if (verbose) - printf("Discovered %s:%s (%s)\n", test_item->context_name, test_item->test_name, - test_item->specification_name); - cgreen_vector_add(tests, test_item); - } - } +static bool is_definition(const char *line) { + return strstr(line, " D ") != NULL; } -/* Read in the dynamic symbols. */ - -static asymbol **get_symbols_table(bfd *abfd, long *symbol_count, bool verbose, const char *filename) -{ - if ((bfd_adapter_get_file_flags(abfd) & DYNAMIC) == 0){ - printf("%s: can only handle libraries with dynamic symbol tables for now\n"); - *symbol_count = 0; - return NULL; - } +static bool complete_line_read(char line[]) { + return line[strlen(line)-1] == '\n'; +} - long storage = bfd_adapter_get_dynamic_symtab_upper_bound(abfd); - if (storage <= 0) { - if (verbose) { - if (storage < 0) - printf("%s: bfd_get_dynamic_symtab_upper_bounds returned %ld\n", filename, storage); - else - printf("%s: no symbols\n", filename); - } - *symbol_count = 0; - return NULL; - } - asymbol **symbols = (asymbol **) bfd_adapter_alloc(abfd, storage); - if (symbols == NULL) { - if (verbose) - printf("%s: bfd_alloc failed while retrieving symbols\n", filename); - *symbol_count = 0; - return NULL; - } +static void strip_newline_from(char *name) { + if (name[strlen(name)-1] == '\n') + name[strlen(name)-1] = '\0'; +} - *symbol_count = bfd_adapter_canonicalize_dynamic_symtab(abfd, symbols); - if (*symbol_count < 0) { - if (verbose) - printf("%s: failed to get symbols count\n", filename); - *symbol_count = 0; - return NULL; +static void add_all_tests_from(FILE *nm_output_pipe, CgreenVector *tests, bool verbose) { + char line[1000]; + int length = read_line(nm_output_pipe, line, sizeof(line)-1); + while (length > -1) { /* TODO: >0 ? */ + if (!complete_line_read(line)) + PANIC("Too long line in nm output"); + if (contains_cgreen_spec(line) && is_definition(line)) { + strip_newline_from(line); + TestItem *test_item = create_test_item_from(cgreen_spec_start_of(line)); + if (verbose) + printf("Discovered %s:%s (%s)\n", test_item->context_name, test_item->test_name, + test_item->specification_name); + cgreen_vector_add(tests, test_item); + } + length = read_line(nm_output_pipe, line, sizeof(line)-1); } - - return symbols; } CgreenVector *discover_tests_in(const char *filename, bool verbose) { - bfd* abfd = bfd_adapter_openr(filename, NULL); - if (!abfd) { - if (verbose) - printf("%s: bfd_adapter_openr failed\n", filename); + FILE *library = open_file(filename, "r"); + if (library == NULL) return NULL; - } - - bfd_adapter_check_format(abfd, bfd_object); - if ((bfd_adapter_get_file_flags(abfd) & HAS_SYMS) == 0) { - if (verbose) - printf("%s: incorrect format\n", filename); - bfd_adapter_close(abfd); - return NULL; - } + close_file(library); - asymbol **symbols; - long symbol_count; - symbols = get_symbols_table(abfd, &symbol_count, verbose, filename); - - if ((symbols == NULL) || (symbol_count <= 0)) { - bfd_adapter_close(abfd); + char nm_command[1000]; + sprintf(nm_command, "/usr/bin/nm '%s'", filename); + FILE *nm_output_pipe = open_process(nm_command, "r"); + if (nm_output_pipe == NULL) return NULL; - } CgreenVector *tests = create_cgreen_vector((GenericDestructor)&destroy_test_item); - add_all_tests_from(symbols, symbol_count, tests, verbose); - - bfd_adapter_close(abfd); - + add_all_tests_from(nm_output_pipe, tests, verbose); + close_process(nm_output_pipe); return tests; } diff --git a/tools/tests/discoverer_acceptance_tests.c b/tools/discoverer_acceptance_tests.c similarity index 80% rename from tools/tests/discoverer_acceptance_tests.c rename to tools/discoverer_acceptance_tests.c index cba851eb..6841bc41 100644 --- a/tools/tests/discoverer_acceptance_tests.c +++ b/tools/discoverer_acceptance_tests.c @@ -28,3 +28,9 @@ Ensure(Discoverer, reads_a_library_and_finds_the_tests) { CgreenVector *tests = discover_tests_in(filename, verbose); assert_that(cgreen_vector_size(tests), is_equal_to(count_tests_in(filename))); } + +TestSuite *discoverer_acceptance_tests(void) { + TestSuite *suite = create_test_suite(); + add_test_with_context(suite, Discoverer, reads_a_library_and_finds_the_tests); + return suite; +} diff --git a/tools/discoverer_unit_tests.c b/tools/discoverer_unit_tests.c new file mode 100644 index 00000000..74b242dc --- /dev/null +++ b/tools/discoverer_unit_tests.c @@ -0,0 +1,177 @@ +#include +#include + +#include "discoverer.h" +#include "test_item.h" + +#include +#include +#include + +#include "io.h" +#include "io.mocks" + + +Describe(Discoverer); +BeforeEach(Discoverer) {} +AfterEach(Discoverer) {} + + +static bool verbose = false; + + +/* Helper functions */ +static void expect_open_file(const char *filename, void *result) { + expect(open_file, when(filename, + is_equal_to_string(filename)), + will_return(result)); +} + +static void expect_open_process(const char *partial_command, void *result) { + expect(open_process, when(command, contains_string(partial_command)), + will_return(result)); +} + +static void given_a_file_with_no_lines(const char *filename) { + expect_open_file(filename, (void *)1); + expect(close_file, when(file, is_equal_to(1))); + expect_open_process("nm ", (void *)2); + expect(read_line, when(file, is_equal_to(2)), + will_return(EOF)); /* End of input */ + expect(close_process, when(file, is_equal_to(2))); +} + +static void expect_read_line_from(int file_id, const char *line) { + if (line == NULL) + expect(read_line, when(file, is_equal_to(file_id)), + will_return(EOF)); + else + expect(read_line, when(file, is_equal_to(file_id)), + will_set_contents_of_parameter(buffer, line, strlen(line)+1), + will_return(strlen(line)+1)); +} + +static void given_a_file_with_two_lines(const char *filename, const char *line1, const char *line2) { + static char command[100]; + expect_open_file(filename, (void *)1); + expect(close_file, when(file, is_equal_to(1))); + sprintf(command, "nm %s", filename); + expect_open_process(command, (void *)2); + expect_read_line_from(2, line1); + expect_read_line_from(2, line2); + expect_read_line_from(2, NULL); + expect(close_process, when(file, is_equal_to(2))); +} + +static void given_a_file_with_one_line(const char *filename, const char *line) { + expect_open_file(filename, (void *)1); + expect(close_file, when(file, is_equal_to(1))); + expect_open_process("nm ", (void *)2); + expect_read_line_from(2, line); + expect_read_line_from(2, NULL); + expect(close_process, when(file, is_equal_to(2))); +} + + + +/*======================================================================*/ +Ensure(Discoverer, should_find_no_tests_in_non_existing_file) { + expect_open_file("non-existing-file", NULL); + + CgreenVector *tests = discover_tests_in("non-existing-file", verbose); + + assert_that(tests, is_null); +} + + +/*======================================================================*/ +Ensure(Discoverer, should_find_no_tests_in_existing_empty_file) { + given_a_file_with_no_lines("empty-file"); + + CgreenVector *tests = discover_tests_in("empty-file", verbose); + + assert_that(cgreen_vector_size(tests), is_equal_to(0)); +} + + +/*======================================================================*/ +Ensure(Discoverer, should_find_one_test_in_file_with_one_line_containing_testname_pattern) { + given_a_file_with_one_line("some-file", + "0000000000202160 D CgreenSpec__A_context__a_test__\n"); + + CgreenVector *tests = discover_tests_in("some-file", verbose); + + assert_that(cgreen_vector_size(tests), is_equal_to(1)); +} + + +/*======================================================================*/ +Ensure(Discoverer, should_find_two_test_in_two_line_file_with_two_lines_containing_testname_pattern) { + given_a_file_with_two_lines("some-file", + "0000000000202160 D CgreenSpec__Context1__test1__\n", + "0000000000202160 D CgreenSpec__Context2__test2__\n"); + + CgreenVector *tests = discover_tests_in("some-file", verbose); + + assert_that(cgreen_vector_size(tests), is_equal_to(2)); +} + + +/*======================================================================*/ +Ensure(Discoverer, should_find_one_test_in_two_line_file_with_one_line_containing_testname_pattern) { + given_a_file_with_two_lines("some-file", + "0000000000202160 D CgreenSpec__Discoverer__test1__\n", + "0000000000202160 D ID\n"); + + CgreenVector *tests = discover_tests_in("some-file", verbose); + + assert_that(cgreen_vector_size(tests), is_equal_to(1)); +} + + +/*======================================================================*/ +Ensure(Discoverer, should_find_no_test_in_file_with_no_definiton) { + given_a_file_with_one_line("some-file", + "0000000000202160 U CgreenSpec__Discoverer__test1__\n"); + + CgreenVector *tests = discover_tests_in("some-file", verbose); + + assert_that(cgreen_vector_size(tests), is_equal_to(0)); +} + + +/*======================================================================*/ +Ensure(Discoverer, should_strip_newline_at_end_of_specification_name) { + given_a_file_with_one_line("some-file", + "0000000000202160 U CgreenSpec__Discoverer__test1__\n"); + + CgreenVector *tests = discover_tests_in("some-file", verbose); + + assert_that(cgreen_vector_size(tests), is_equal_to(0)); +} + + +/*======================================================================*/ +Ensure(Discoverer, should_return_valid_test_item_for_a_line_containing_testname_pattern) { + given_a_file_with_one_line("some-file", + "0000000000202160 D CgreenSpec__Context1__test_1__\n"); + + CgreenVector *tests = discover_tests_in("some-file", verbose); + + assert_that(cgreen_vector_size(tests), is_equal_to(1)); + + TestItem *test_item = (TestItem*)cgreen_vector_get(tests, 0); + assert_that(test_item->specification_name, is_equal_to_string("CgreenSpec__Context1__test_1__")); + assert_that(test_item->context_name, is_equal_to_string("Context1")); + assert_that(test_item->test_name, is_equal_to_string("test_1")); +} + +/*======================================================================*/ +Ensure(Discoverer, can_handle_truncated_testnames) { + given_a_file_with_one_line("some-file", "0000000000202160 D CgreenSpec__Context1__a\n"); + + CgreenVector *tests = discover_tests_in("some-file", verbose); + + TestItem *test_item = (TestItem*)cgreen_vector_get(tests, 0); + assert_that(test_item->test_name, is_equal_to_string("a")); +} diff --git a/tools/io.c b/tools/io.c new file mode 100644 index 00000000..daf7a8c5 --- /dev/null +++ b/tools/io.c @@ -0,0 +1,34 @@ +#include "io.h" + +/* Adapter to C I/O functions */ + +#include + + +FILE *open_file(const char *filename, const char *mode) { + return fopen(filename, mode); +} + +int close_file(FILE *file) { + return fclose(file); +} + +FILE *open_process(const char *command, const char *mode) { + return popen(command, mode); +} + +int close_process(FILE *process) { + return pclose(process); +} + + +/* + read_line() - read characters from a FILE + - behaves like fgets() except will return + - number of characters read and + - EOF if at end of file +*/ +int read_line(FILE *file, char *buffer, int max_length) { + char *result = fgets(buffer, max_length, file); + return result==NULL? EOF : (signed)strlen(result); +} diff --git a/tools/io.h b/tools/io.h new file mode 100644 index 00000000..abce821a --- /dev/null +++ b/tools/io.h @@ -0,0 +1,22 @@ +#ifndef IO_H +#define IO_H + +#include + +#ifdef UNITTESTING +#define open_file open_file_unittesting +#define close_file close_file_unittesting +#define open_process open_process_file_unittesting +#define close_process close_process_unittesting +#define read_line read_line_unittesting +#endif + +extern FILE *open_file(const char *filename, const char *mode); +extern int close_file(FILE *file); + +extern FILE *open_process(const char *command, const char *mode); +extern int close_process(FILE *file); + +extern int read_line(FILE *file, char *buffer, int max_length); + +#endif diff --git a/tools/io.mocks b/tools/io.mocks new file mode 100644 index 00000000..72a3e2d6 --- /dev/null +++ b/tools/io.mocks @@ -0,0 +1,22 @@ +#include "io.h" + +FILE *open_file(const char *filename, const char *mode) { + return (FILE *) mock(filename, mode); +} + +int close_file(FILE *file) { + return (int) mock(file); +} + +FILE *open_process(const char *command, const char *mode) { + return (FILE *) mock(command, mode); +} + +int close_process(FILE *file) { + return (int) mock(file); +} + +int read_line(FILE *file, char *buffer, int max_length) { + return (int) mock(file, buffer, max_length); +} + diff --git a/tools/tests/CMakeLists.txt b/tools/tests/CMakeLists.txt index 198ab8b0..36f86ab7 100644 --- a/tools/tests/CMakeLists.txt +++ b/tools/tests/CMakeLists.txt @@ -3,117 +3,70 @@ include_directories( ${CGREEN_PUBLIC_INCLUDE_DIRS} ${CURRENT_BINARY_DIR}) -find_package(LibBfd) -if(LibBfd_FOUND) - - set(CGREEN_RUNNER_TESTS_LIBRARY - cgreen_runner_tests - CACHE INTERNAL "cgreen-runner tests shared library" - ) - set(RUNNER_TESTS_SRCS - runner_unit_tests.c - ../discoverer.c - ../bfd_adapter.c - ../test_item.c) - add_library(${CGREEN_RUNNER_TESTS_LIBRARY} SHARED ${RUNNER_TESTS_SRCS}) - target_link_libraries(${CGREEN_RUNNER_TESTS_LIBRARY} - ${CGREEN_LIBRARY} - ${CMAKE_DL_LIBS} - ${LIBBFD_LIBRARIES}) - - set(DISCOVERER_ACCEPTANCE_TESTS_LIBRARY - discoverer_acceptance_tests - CACHE INTERNAL "discoverer acceptance tests" - ) - set(DISCOVERER_ACCEPTANCE_TESTS_SRCS - discoverer_acceptance_tests.c - ../discoverer.c - ../bfd_adapter.c - ../test_item.c) - add_library(${DISCOVERER_ACCEPTANCE_TESTS_LIBRARY} SHARED ${DISCOVERER_ACCEPTANCE_TESTS_SRCS}) - - target_link_libraries(${DISCOVERER_ACCEPTANCE_TESTS_LIBRARY} - ${CGREEN_LIBRARY} - ${CMAKE_DL_LIBS} - ${LIBBFD_LIBRARIES}) - - set(DISCOVERER_UNIT_TESTS_LIBRARY - discoverer_unit_tests - CACHE INTERNAL "discoverer unit tests" - ) - set(DISCOVERER_UNIT_TESTS_SRCS - discoverer_unit_tests.c - ../discoverer.c - ../test_item.c) - add_library(${DISCOVERER_UNIT_TESTS_LIBRARY} SHARED ${DISCOVERER_UNIT_TESTS_SRCS}) - - target_compile_definitions(${DISCOVERER_UNIT_TESTS_LIBRARY} PUBLIC UNITTESTING) - target_link_libraries(${DISCOVERER_UNIT_TESTS_LIBRARY} - ${CGREEN_LIBRARY} - ${CMAKE_DL_LIBS} - ${LIBBFD_LIBRARIES}) - - SET(CGREEN_RUNNER_TESTS_LIBRARY "$/$") - SET(DISCOVERER_ACCEPTANCE_TESTS_LIBRARY "$/$") - SET(DISCOVERER_UNIT_TESTS_LIBRARY "$/$") - - # Due to some (of many) CMake irregularities to reference the test libraries - # we can't just use its CMake name variable, but have to look it up with - # some special attributes of the library: - # $/$ - # - macro_add_test(NAME cgreen_runner_unit_tests - COMMAND cgreen-runner ${CGREEN_RUNNER_TESTS_LIBRARY}) - - macro_add_test(NAME cgreen_runner_usage - COMMAND cgreen-runner --help) - - macro_add_test(NAME cgreen_runner_quiet - COMMAND cgreen-runner -q ${CGREEN_RUNNER_TESTS_LIBRARY}) - - macro_add_test(NAME cgreen_runner_suite_name - COMMAND cgreen-runner -s Suite ${CGREEN_RUNNER_TESTS_LIBRARY}) +set(CGREEN_RUNNER_TESTS_LIBRARY + cgreen_runner_tests + CACHE INTERNAL "cgreen-runner tests shared library" +) +set(RUNNER_TESTS_SRCS + runner_unit_tests.c + ../discoverer.c + ../io.c + ../test_item.c) +add_library(${CGREEN_RUNNER_TESTS_LIBRARY} SHARED ${RUNNER_TESTS_SRCS}) - macro_add_test(NAME cgreen_runner_fail_on_non_existing_library - COMMAND cgreen-runner Suite non_existent_library) - set_tests_properties(cgreen_runner_fail_on_non_existing_library PROPERTIES WILL_FAIL true) +target_link_libraries(${CGREEN_RUNNER_TESTS_LIBRARY} ${CGREEN_SHARED_LIBRARY} ${CMAKE_DL_LIBS}) - macro_add_test(NAME cgreen_runner_fail_on_non_existing_library_with_suite - COMMAND cgreen-runner -s Suite non_existent_library - WILL_FAIL) - set_tests_properties(cgreen_runner_fail_on_non_existing_library_with_suite PROPERTIES WILL_FAIL true) +SET(CGREEN_RUNNER_TESTS_LIBRARY "$/$") + +# Due to some (of many) CMake irregularities to reference the test libraries +# we can't just use its CMake name variable, but have to look it up with +# some special attributes of the library: +# $/$ +# +macro_add_test(NAME cgreen_runner_unit_tests + COMMAND cgreen-runner ${CGREEN_RUNNER_TESTS_LIBRARY}) - macro_add_test(NAME cgreen_runner_verbose - COMMAND cgreen-runner -v -C ${CGREEN_RUNNER_TESTS_LIBRARY}) +macro_add_test(NAME cgreen_runner_usage + COMMAND cgreen-runner --help) - macro_add_test(NAME cgreen_runner_version - COMMAND cgreen-runner --version) +macro_add_test(NAME cgreen_runner_quiet + COMMAND cgreen-runner -q ${CGREEN_RUNNER_TESTS_LIBRARY}) - macro_add_test(NAME cgreen_runner_single_explicit_named_test - COMMAND cgreen-runner $/$ Runner:can_match_test_name) +macro_add_test(NAME cgreen_runner_verbose + COMMAND cgreen-runner -v -C ${CGREEN_RUNNER_TESTS_LIBRARY}) - macro_add_test(NAME cgreen_runner_patternmatched_testnames - COMMAND cgreen-runner $/$ Runner:can*) +macro_add_test(NAME cgreen_runner_version + COMMAND cgreen-runner --version) - macro_add_test(NAME cgreen_runner_patternmatched_testnames_in_patternmatched_context - COMMAND cgreen-runner $/$ Run*:can*) +macro_add_test(NAME cgreen_runner_single_explicit_named_test + COMMAND cgreen-runner $/$ Runner:can_match_test_name) - macro_add_test(NAME cgreen_runner_wildcarded_tests_in_named_context - COMMAND cgreen-runner $/$ Runner:*) +macro_add_test(NAME cgreen_runner_patternmatched_testnames + COMMAND cgreen-runner $/$ Runner:can*) - macro_add_test(NAME cgreen_runner_wildcarded_tests_in_wildcarded_context - COMMAND cgreen-runner $/$ *:*) +macro_add_test(NAME cgreen_runner_suite_name + COMMAND cgreen-runner -s Suite ${CGREEN_RUNNER_TESTS_LIBRARY}) + +macro_add_test(NAME cgreen_runner_fail_on_non_existing_library + COMMAND cgreen-runner Suite non_existent_library) +set_tests_properties(cgreen_runner_fail_on_non_existing_library PROPERTIES WILL_FAIL true) + +macro_add_test(NAME cgreen_runner_fail_on_non_existing_library_with_suite + COMMAND cgreen-runner -s Suite non_existent_library + WILL_FAIL) +set_tests_properties(cgreen_runner_fail_on_non_existing_library_with_suite PROPERTIES WILL_FAIL true) - macro_add_test(NAME cgreen_runner_with_xml_reporter - COMMAND cgreen-runner --xml TEST --suite cgreen_runner_tests $/$) +macro_add_test(NAME cgreen_runner_patternmatched_testnames_in_patternmatched_context + COMMAND cgreen-runner $/$ Run*:can*) - macro_add_test(NAME cgreen_runner_multiple_libraries - COMMAND cgreen-runner ${CGREEN_RUNNER_TESTS_LIBRARY} ${CGREEN_RUNNER_TESTS_LIBRARY} ${CGREEN_RUNNER_TESTS_LIBRARY}) +macro_add_test(NAME cgreen_runner_wildcarded_tests_in_named_context + COMMAND cgreen-runner $/$ Runner:*) - macro_add_test(NAME discoverer_acceptance_tests - COMMAND cgreen-runner ${DISCOVERER_ACCEPTANCE_TESTS_LIBRARY}) +macro_add_test(NAME cgreen_runner_wildcarded_tests_in_wildcarded_context + COMMAND cgreen-runner $/$ *:*) - macro_add_test(NAME discoverer_unit_tests - COMMAND cgreen-runner ${DISCOVERER_UNIT_TESTS_LIBRARY}) +macro_add_test(NAME cgreen_runner_with_xml_reporter + COMMAND cgreen-runner --xml TEST --suite cgreen_runner_tests $/$) -endif() +macro_add_test(NAME cgreen_runner_multiple_libraries + COMMAND cgreen-runner ${CGREEN_RUNNER_TESTS_LIBRARY} ${CGREEN_RUNNER_TESTS_LIBRARY} ${CGREEN_RUNNER_TESTS_LIBRARY}) diff --git a/tools/tests/discoverer_unit_tests.c b/tools/tests/discoverer_unit_tests.c deleted file mode 100644 index 4fece2c6..00000000 --- a/tools/tests/discoverer_unit_tests.c +++ /dev/null @@ -1,382 +0,0 @@ -#include -#include - -#include "discoverer.h" -#include "test_item.h" - -#include -#include -#include - -#include "bfd_adapter.h" -#include "bfd_adapter.mocks" - - -int printf_unittesting(const char *fmt, ...) { - return (int) mock(fmt); -} - -Describe(Discoverer); -BeforeEach(Discoverer) {} -AfterEach(Discoverer) {} - - -static bool verbose = true; - - -/* Helper functions */ -static void expect_open_file(const char *filename, void *result) { - expect(bfd_adapter_openr, - when(filename, is_equal_to_string(filename)), - when(target, is_null), - will_return(result)); -} - -static void given_a_file_with_dynamic_symbols(const char *filename, bfd *expect_abfd) { - expect_open_file(filename, expect_abfd); - expect(bfd_adapter_check_format, - when(abfd, is_equal_to(expect_abfd)), - when(format, is_equal_to(bfd_object))); - always_expect(bfd_adapter_get_file_flags, - when(abfd, is_equal_to(expect_abfd)), - will_return(HAS_SYMS|DYNAMIC)); -} - -static void given_a_file_with_two_lines(const char *filename, const char *line1, const char *line2) { - static bfd *expect_abfd = (bfd *) 1; - static long expect_storage = 1; - static asymbol symbols[2]; - symbols[0].name = line1; - symbols[1].name = line2; - static asymbol *symbols_ptr[2] = { &symbols[0], &symbols[1] }; - static asymbol **expect_symbols = (asymbol **) &symbols_ptr; - - given_a_file_with_dynamic_symbols(filename, expect_abfd); - - expect(bfd_adapter_get_dynamic_symtab_upper_bound, - when(abfd, is_equal_to(expect_abfd)), - will_return(expect_storage)); - expect(bfd_adapter_alloc, - when(abfd, is_equal_to(expect_abfd)), - when(wanted, is_equal_to(expect_storage)), - will_return(expect_symbols)); - expect(bfd_adapter_canonicalize_dynamic_symtab, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(expect_symbols)), - will_return(2)); - expect(bfd_adapter_asymbol_bfd, - when(sy, is_equal_to(symbols_ptr[0])), - will_return(expect_abfd)); - expect(bfd_adapter_is_target_special_symbol, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(symbols_ptr[0])), - will_return(false)); - expect(bfd_adapter_asymbol_bfd, - when(sy, is_equal_to(symbols_ptr[1])), - will_return(expect_abfd)); - expect(bfd_adapter_is_target_special_symbol, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(symbols_ptr[1])), - will_return(false)); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); -} - -static void given_a_file_with_one_line(const char *filename, const char *line) { - static bfd *expect_abfd = (bfd *) 1; - static long expect_storage = 1; - static asymbol symbols[1]; - symbols[0].name = line; - static asymbol *symbols_ptr = symbols; - static asymbol **expect_symbols = (asymbol **) &symbols_ptr; - - given_a_file_with_dynamic_symbols(filename, expect_abfd); - - expect(bfd_adapter_get_dynamic_symtab_upper_bound, - when(abfd, is_equal_to(expect_abfd)), - will_return(expect_storage)); - expect(bfd_adapter_alloc, - when(abfd, is_equal_to(expect_abfd)), - when(wanted, is_equal_to(expect_storage)), - will_return(expect_symbols)); - expect(bfd_adapter_canonicalize_dynamic_symtab, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(expect_symbols)), - will_return(1)); - expect(bfd_adapter_asymbol_bfd, - when(sy, is_equal_to(symbols_ptr)), - will_return(expect_abfd)); - expect(bfd_adapter_is_target_special_symbol, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(symbols_ptr)), - will_return(false)); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); -} - -/*======================================================================*/ -Ensure(Discoverer, should_find_no_tests_in_non_existing_file) { - expect_open_file("non-existing-file", NULL); - expect(printf_unittesting); - - CgreenVector *tests = discover_tests_in("non-existing-file", verbose); - - assert_that(tests, is_null); -} - -/*======================================================================*/ -/* Any input file that has no object: text file, pdf, etc. */ -Ensure(Discoverer, should_find_no_tests_if_file_has_no_symbol) { - const char *filename = "no-symbol"; - bfd *expect_abfd = (bfd *)1; - - expect_open_file(filename, expect_abfd); - expect(bfd_adapter_check_format, - when(abfd, is_equal_to(expect_abfd)), - when(format, is_equal_to(bfd_object))); - expect(bfd_adapter_get_file_flags, - when(abfd, is_equal_to(expect_abfd)), - will_return(0)); - expect(printf_unittesting); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); - - CgreenVector *tests = discover_tests_in(filename, verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(0)); -} - -/*======================================================================*/ -/* Any input file that has object but has no symbols */ -Ensure(Discoverer, should_find_no_tests_if_file_has_no_dynamic_symbol) { - const char *filename = "no-dynamic-symbol"; - bfd *expect_abfd = (bfd *)1; - - given_a_file_with_dynamic_symbols(filename, expect_abfd); - - expect(bfd_adapter_get_dynamic_symtab_upper_bound, - when(abfd, is_equal_to(expect_abfd)), - will_return(-1)); - expect(printf_unittesting); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); - - CgreenVector *tests = discover_tests_in(filename, verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(0)); -} - -/*======================================================================*/ -Ensure(Discoverer, should_find_no_tests_if_bdf_alloc_fail) { - const char *filename = "valid-file"; - bfd *expect_abfd = (bfd *)1; - long expect_storage = 1; - - given_a_file_with_dynamic_symbols(filename, expect_abfd); - - expect(bfd_adapter_get_dynamic_symtab_upper_bound, - when(abfd, is_equal_to(expect_abfd)), - will_return(expect_storage)); - expect(bfd_adapter_alloc, - when(abfd, is_equal_to(expect_abfd)), - when(wanted, is_equal_to(expect_storage)), - will_return(NULL)); - expect(printf_unittesting); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); - - CgreenVector *tests = discover_tests_in(filename, verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(0)); -} - -/*======================================================================*/ -Ensure(Discoverer, should_find_no_tests_if_bfd_canonicalize_dynamic_symtab_fails) { - const char *filename = "valid-file"; - bfd *expect_abfd = (bfd *) 1; - long expect_storage = 1; - asymbol **expect_symbols = (asymbol **) 2; - - given_a_file_with_dynamic_symbols(filename, expect_abfd); - - expect(bfd_adapter_get_dynamic_symtab_upper_bound, - when(abfd, is_equal_to(expect_abfd)), - will_return(expect_storage)); - expect(bfd_adapter_alloc, - when(abfd, is_equal_to(expect_abfd)), - when(wanted, is_equal_to(expect_storage)), - will_return(expect_symbols)); - expect(bfd_adapter_canonicalize_dynamic_symtab, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(expect_symbols)), - will_return(-1)); - expect(printf_unittesting); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); - - CgreenVector *tests = discover_tests_in(filename, verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(0)); -} - -/*======================================================================*/ -Ensure(Discoverer, should_find_no_tests_if_get_symbols_table_return_zero_symbol) { - const char *filename = "valid-file"; - bfd *expect_abfd = (bfd *) 1; - long expect_storage = 1; - asymbol **expect_symbols = (asymbol **) 2; - - given_a_file_with_dynamic_symbols(filename, expect_abfd); - - expect(bfd_adapter_get_dynamic_symtab_upper_bound, - when(abfd, is_equal_to(expect_abfd)), - will_return(expect_storage)); - expect(bfd_adapter_alloc, - when(abfd, is_equal_to(expect_abfd)), - when(wanted, is_equal_to(expect_storage)), - will_return(expect_symbols)); - expect(bfd_adapter_canonicalize_dynamic_symtab, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(expect_symbols)), - will_return(0)); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); - - CgreenVector *tests = discover_tests_in(filename, verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(0)); -} - -/*======================================================================*/ -Ensure(Discoverer, should_find_no_tests_if_bfd_asymbol_bfd_fails) { - const char *filename = "valid-file"; - bfd *expect_abfd = (bfd *) 1; - long expect_storage = 1; - asymbol symbols[1]; - symbols[0].name = "aline"; - asymbol *symbols_ptr = symbols; - asymbol **expect_symbols = (asymbol **) &symbols_ptr; - - given_a_file_with_dynamic_symbols(filename, expect_abfd); - - expect(bfd_adapter_get_dynamic_symtab_upper_bound, - when(abfd, is_equal_to(expect_abfd)), - will_return(expect_storage)); - expect(bfd_adapter_alloc, - when(abfd, is_equal_to(expect_abfd)), - when(wanted, is_equal_to(expect_storage)), - will_return(expect_symbols)); - expect(bfd_adapter_canonicalize_dynamic_symtab, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(expect_symbols)), - will_return(1)); - expect(bfd_adapter_asymbol_bfd, - when(sy, is_equal_to(symbols_ptr)), - will_return(NULL)); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); - - CgreenVector *tests = discover_tests_in(filename, verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(0)); -} - -/*======================================================================*/ -Ensure(Discoverer, should_find_no_tests_if_bfd_is_target_special_symbol_returns_true) { - const char *filename = "valid-file"; - bfd *expect_abfd = (bfd *) 1; - long expect_storage = 1; - asymbol symbols[1]; - symbols[0].name = "aline"; - asymbol *symbols_ptr = symbols; - asymbol **expect_symbols = (asymbol **) &symbols_ptr; - - given_a_file_with_dynamic_symbols(filename, expect_abfd); - - expect(bfd_adapter_get_dynamic_symtab_upper_bound, - when(abfd, is_equal_to(expect_abfd)), - will_return(expect_storage)); - expect(bfd_adapter_alloc, - when(abfd, is_equal_to(expect_abfd)), - when(wanted, is_equal_to(expect_storage)), - will_return(expect_symbols)); - expect(bfd_adapter_canonicalize_dynamic_symtab, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(expect_symbols)), - will_return(1)); - expect(bfd_adapter_asymbol_bfd, - when(sy, is_equal_to(symbols_ptr)), - will_return(expect_abfd)); - expect(bfd_adapter_is_target_special_symbol, - when(abfd, is_equal_to(expect_abfd)), - when(symbols, is_equal_to(symbols_ptr)), - will_return(true)); - expect(bfd_adapter_close, - when(abfd, is_equal_to(expect_abfd))); - - CgreenVector *tests = discover_tests_in(filename, verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(0)); -} -/*======================================================================*/ -Ensure(Discoverer, should_find_one_test_in_file_with_one_line_containing_testname_pattern) { - given_a_file_with_one_line("some-file", - "0000000000202160 D CgreenSpec__A_context__a_test__"); - expect(printf_unittesting); - - CgreenVector *tests = discover_tests_in("some-file", verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(1)); -} - -/*======================================================================*/ -Ensure(Discoverer, should_find_two_test_in_two_line_file_with_two_lines_containing_testname_pattern) { - given_a_file_with_two_lines("some-file", - "0000000000202160 D CgreenSpec__Context1__test1__", - "0000000000202160 D CgreenSpec__Context2__test2__"); - expect(printf_unittesting); - expect(printf_unittesting); - - CgreenVector *tests = discover_tests_in("some-file", verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(2)); -} - -/*======================================================================*/ -Ensure(Discoverer, should_find_one_test_in_two_line_file_with_one_line_containing_testname_pattern) { - given_a_file_with_two_lines("some-file", - "0000000000202160 D CgreenSpec__Discoverer__test1__", - "0000000000202160 D ID"); - expect(printf_unittesting); - - CgreenVector *tests = discover_tests_in("some-file", verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(1)); -} - -/*======================================================================*/ -Ensure(Discoverer, should_return_valid_test_item_for_a_line_containing_testname_pattern) { - given_a_file_with_one_line("some-file", - "0000000000202160 D CgreenSpec__Context1__test_1__"); - expect(printf_unittesting); - - CgreenVector *tests = discover_tests_in("some-file", verbose); - - assert_that(cgreen_vector_size(tests), is_equal_to(1)); - - TestItem *test_item = (TestItem*)cgreen_vector_get(tests, 0); - assert_that(test_item->specification_name, is_equal_to_string("CgreenSpec__Context1__test_1__")); - assert_that(test_item->context_name, is_equal_to_string("Context1")); - assert_that(test_item->test_name, is_equal_to_string("test_1")); -} - -/*======================================================================*/ -Ensure(Discoverer, can_handle_truncated_testnames) { - given_a_file_with_one_line("some-file", "0000000000202160 D CgreenSpec__Context1__a"); - expect(printf_unittesting); - - CgreenVector *tests = discover_tests_in("some-file", verbose); - - TestItem *test_item = (TestItem*)cgreen_vector_get(tests, 0); - assert_that(test_item->test_name, is_equal_to_string("a")); -}