iceoryx icon indicating copy to clipboard operation
iceoryx copied to clipboard

Testing framework for code which is expected to not compile

Open elBoberido opened this issue 3 years ago • 1 comments

Brief feature description

We need to write tests for code which we expect to not compile.

Detailed information

We use the type system to prevent some errors, e.g. the NewType pattern which can be used to wrap an integer and add only the required operations like being comparable but not addable. While this makes the software more robust since the compiler will reject invalid code, we can not easily test if the compiler detects the invalid usage like add something to a type which does not support it, because our tests would not compile if it works like expected.

Before we implement something, we should check if there is already framework which we can use.

Proposal for own implementation with:

  • in the Setup() of Fixture, a CMakeLists.txt and main.cpp is created in e.g. ./compile_test
  • for the CMakeLists.txt there should be templates for iceoryx_utils, iceoryx_posh and iceoryx_dds, e.g. with find_package(iceoryx_utils REQUIRED)
  • each test overwrites the main.cpp and then calls cmake to compile the file
    • keep in mind to use CMAKE_PREFIX_PATH
  • the value for CMAKE_PREFIX_PATH must be passed to the test executable as as parameter, e.g. utils_modultests --library-path ../install/prefix

Alternatively using Boost.Build for compile-fail tests: Quoting from https://akrzemi1.wordpress.com/2016/05/10/diagnosable-validity/


A practical way to check if a given construct fails to compile is to do it from outside C++: prepare a small test program with erroneous construct, compile it, and test if compiler reports compilation failure. This is how “negative” unit tests work with Boost.Build. For an example, see this negative test form Boost.Optional library: optional_test_fail_convert_from_null.cpp. In configuration file it is annotated as compile-fail, meaning that test passes only if compilation fails.


Once it is implemented, use it for:

  • Create compile failure tests for all classes which have deleted c'tors/assignment operators (comes from #202)

elBoberido avatar Jul 29 '20 14:07 elBoberido