Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discussion] Unit tests #5

Open
drdanz opened this issue Jan 29, 2019 · 2 comments
Open

[Discussion] Unit tests #5

drdanz opened this issue Jan 29, 2019 · 2 comments

Comments

@drdanz
Copy link
Member

drdanz commented Jan 29, 2019

We should definitely have unit tests in this repo.
I suggest to use Catch2, like in YARP.

Another very useful improvement for such a library would be to have a CMake module to add a unit test that should not build (for example because we added some static assert for that)
This is very important because this helps moving issue detection at build time instead of at run time.

I suggest to add such a module to YCM.

@drdanz
Copy link
Member Author

drdanz commented Jan 29, 2019

I wrote a macro to do that some times ago, it's unfinished and I don't remember if it was good or not, but it's a start, I'll just leave it here

macro(ADD_FAILING_TEST _name)

  set(${_name}_build_dir "${CMAKE_CURRENT_BINARY_DIR}/${_name}")

  file(WRITE "${${_name}_build_dir}/CMakeLists.txt"
"cmake_minimum_required(VERSION ${CMAKE_MINIMUM_REQUIRED_VERSION})
project(${_name})

project(${_name}_good)
add_executable(${_name}_good ${_name}_good.cpp)

project(${_name}_bad)
add_executable(${_name}_bad ${_name}_bad.cpp)
")

  file(WRITE "${${_name}_build_dir}/${_name}_good.cpp"
"int main()
{
    return 0;
}
")

  file(WRITE "${${_name}_build_dir}/${_name}_bad.cpp"
"int main()
{
    foo return 0;
}
")

  add_test(NAME ${_name}::setup
          COMMAND ${CMAKE_CTEST_COMMAND} --build-and-test "${${_name}_build_dir}"
                                                          "${${_name}_build_dir}/build"
                                          ${build_generator}
                                          --build-generator "${CMAKE_GENERATOR}"
                                          --build-generator-platform "${CMAKE_GENERATOR_PLATFORM}"
                                          --build-generator-toolset "${CMAKE_GENERATOR_TOOLSET}"
                                          --build-config $<CONFIGURATION>
                                          --build-makeprogram ${CMAKE_MAKE_PROGRAM}
                                          --build-noclean
                                          --build-target clean
                                          --build-options -Wno-dev
                                                          -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
                                                          -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                                                          -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
                                                          -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
                                                          -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
          WORKING_DIRECTORY "${${_name}_build_dir}")
  set_tests_properties(${_name}::setup PROPERTIES FIXTURES_SETUP ${_name}_fixture)

  add_test(NAME ${_name}::good
          COMMAND ${CMAKE_CTEST_COMMAND} --build-and-test "${${_name}_build_dir}"
                                                          "${${_name}_build_dir}/build"
                                          ${build_generator}
                                          --build-generator "${CMAKE_GENERATOR}"
                                          --build-generator-platform "${CMAKE_GENERATOR_PLATFORM}"
                                          --build-generator-toolset "${CMAKE_GENERATOR_TOOLSET}"
                                          --build-config $<CONFIGURATION>
                                          --build-makeprogram ${CMAKE_MAKE_PROGRAM}
                                          --build-noclean
                                          --build-nocmake
                                          --build-target ${_name}_good
                                          --build-options -Wno-dev
                                                          -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
                                                          -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                                                          -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
                                                          -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
                                                          -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
          WORKING_DIRECTORY "${${_name}_build_dir}")
  set_tests_properties(${_name}::good PROPERTIES FIXTURES_REQUIRED ${_name}_fixture)

  add_test(NAME ${_name}::bad
          COMMAND ${CMAKE_CTEST_COMMAND} --build-and-test "${${_name}_build_dir}"
                                                          "${${_name}_build_dir}/build"
                                          ${build_generator}
                                          --build-generator "${CMAKE_GENERATOR}"
                                          --build-generator-platform "${CMAKE_GENERATOR_PLATFORM}"
                                          --build-generator-toolset "${CMAKE_GENERATOR_TOOLSET}"
                                          --build-config $<CONFIGURATION>
                                          --build-makeprogram ${CMAKE_MAKE_PROGRAM}
                                          --build-noclean
                                          --build-nocmake
                                          --build-target ${_name}_bad
                                          --build-options -Wno-dev
                                                          -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
                                                          -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER}
                                                          -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}
                                                          -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
                                                          -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
          WORKING_DIRECTORY "${${_name}_build_dir}")
  set_tests_properties(${_name}::bad PROPERTIES WILL_FAIL 1)
  set_tests_properties(${_name}::bad PROPERTIES FIXTURES_REQUIRED ${_name}_fixture)

  add_test(NAME ${_name}::cleanup
          COMMAND ${CMAKE_COMMAND} -E remove_directory "${${_name}_build_dir}/build"
          WORKING_DIRECTORY "${${_name}_build_dir}")
  set_tests_properties(${_name}::cleanup PROPERTIES FIXTURES_CLEANUP ${_name}_fixture)

  set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${${_name}_build_dir}/build")

endmacro()

@drdanz
Copy link
Member Author

drdanz commented Jan 29, 2019

The goal of the macro was to compile something that should build and something that should not build (perhaps passing the line as argument of the macro), obviously this is unfinished, and both of them are building, because the files are exactly the same 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant