Skip to content

Commit

Permalink
add test for format string compile-time check
Browse files Browse the repository at this point in the history
  • Loading branch information
alexezeder committed Dec 26, 2021
1 parent a127c09 commit 4b4ad87
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion test/compile-error-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ set(non_error_test_content "")
# For non-error tests (we expect them to compile successfully):
# * adds a code segment as separate function to `non_error_test_content`
function (expect_compile name code_fragment)
cmake_parse_arguments(EXPECT_COMPILE "ERROR" "" "" ${ARGN})
cmake_parse_arguments(EXPECT_COMPILE "ERROR;FORMAT_STRING_CHECK" "" "" ${ARGN})
if (EXPECT_COMPILE_FORMAT_STRING_CHECK)
if (CMAKE_CXX_STANDARD LESS 20)
return()
else ()
set(else_branch "")
if (EXPECT_COMPILE_ERROR)
set(else_branch "#error")
endif ()
set(code_fragment "
#ifdef FMT_HAS_CONSTEVAL
${code_fragment}
#else
${else_branch}
#endif")
endif ()
endif ()

string(MAKE_C_IDENTIFIER "${name}" test_name)

Expand Down Expand Up @@ -169,5 +185,13 @@ expect_compile(udl-check "
#endif
")

# Compile-time argument type check
expect_compile(format-string-number-spec "
fmt::format(\"{:d}\", 42);
" FORMAT_STRING_CHECK)
expect_compile(format-string-number-spec-error "
fmt::format(\"{:d}\", \"I am not a number\");
" FORMAT_STRING_CHECK ERROR)

# Run all tests
run_tests()

0 comments on commit 4b4ad87

Please sign in to comment.