38

特定のものがビルドに失敗していることを確認するとよい場合があります。たとえば、次のようになります。

// Next line should fail to compile: can't convert const iterator to iterator.
my_new_container_type::iterator it = my_new_container_type::const_iterator();

これらのタイプのものを CMake/CTest に組み込むことは可能ですか? 私はでこのようなものを探していますCMakeLists.txt:

add_build_failure_executable(
    test_iterator_conversion_build_failure
    iterator_conversion_build_failure.cpp)
add_build_failure_test(
    test_iterator_conversion_build_failure
    test_iterator_conversion_build_failure)

(もちろん、私の知る限り、これらの特定の CMake ディレクティブは存在しません。)

4

2 に答える 2

42

あなたが説明したように、これは多かれ少なかれ行うことができます。コンパイルに失敗するターゲットを追加してcmake --buildから、ターゲットのビルドを試みるために呼び出すテストを追加できます。あとは、test プロパティWILL_FAILを true に設定するだけです。

したがって、次の内容を含む「will_fail.cpp」という名前のファイルにテストがあるとします。

#if defined TEST1
non-compiling code for test 1
#elif defined TEST2
non-compiling code for test 2
#endif

次に、CMakeLists.txt に次のようなものを含めることができます。

cmake_minimum_required(VERSION 3.0)
project(Example)

include(CTest)

# Add a couple of failing-to-compile targets
add_executable(will_fail will_fail.cpp)
add_executable(will_fail_again will_fail.cpp)
# Avoid building these targets normally
set_target_properties(will_fail will_fail_again PROPERTIES
                      EXCLUDE_FROM_ALL TRUE
                      EXCLUDE_FROM_DEFAULT_BUILD TRUE)
# Provide a PP definition to target the appropriate part of
# "will_fail.cpp", or provide separate files per test.
target_compile_definitions(will_fail PRIVATE TEST1)
target_compile_definitions(will_fail_again PRIVATE TEST2)

# Add the tests.  These invoke "cmake --build ..." which is a
# cross-platform way of building the given target.
add_test(NAME Test1
         COMMAND ${CMAKE_COMMAND} --build . --target will_fail --config $<CONFIGURATION>
         WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
add_test(NAME Test2
         COMMAND ${CMAKE_COMMAND} --build . --target will_fail_again --config $<CONFIGURATION>
         WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
# Expect these tests to fail (i.e. cmake --build should return
# a non-zero value)
set_tests_properties(Test1 Test2 PROPERTIES WILL_FAIL TRUE)

これらをたくさん書く必要がある場合は、明らかにこれらすべてを関数またはマクロにラップできます。

于 2015-05-12T12:56:52.093 に答える