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 23, 2021
1 parent 99eff50 commit 24c9fc3
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion test/compile-error-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,18 @@ set(test_names "")
# Adds a name of test into `test_names` list
# Sets `test_{}_error_expected` (where {} is test name) to 0 or 1 depending on test result
function (expect_compile name code_fragment)
cmake_parse_arguments(EXPECT_COMPILE "ERROR;FORMAT;XCHAR" "" "" ${ARGN})
cmake_parse_arguments(EXPECT_COMPILE "ERROR;FORMAT;XCHAR;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\n${code_fragment}\n#else\n${else_branch}\n#endif")
endif ()
endif ()
set(test_names_copy "${test_names}")
list(APPEND test_names_copy "${name}")
set(test_names "${test_names_copy}" PARENT_SCOPE)
Expand Down Expand Up @@ -131,5 +142,13 @@ expect_compile(udl-check "
#endif
" FORMAT)

# 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 24c9fc3

Please sign in to comment.