Skip to content

Commit

Permalink
add tests for format string compile-time checks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexezeder committed Dec 27, 2021
1 parent 656434c commit c2ba8bc
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion test/compile-error-test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ set(non_error_test_content "")
# * 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})

string(MAKE_C_IDENTIFIER "${name}" test_name)

if (EXPECT_COMPILE_ERROR)
Expand Down Expand Up @@ -168,5 +167,37 @@ expect_compile(udl-check "
#endif
")

if (CMAKE_CXX_STANDARD GREATER_EQUAL 20)
# Compile-time argument type check
expect_compile(format-string-number-spec "
#ifdef FMT_HAS_CONSTEVAL
fmt::format(\"{:d}\", 42);
#endif
")
expect_compile(format-string-number-spec-error "
#ifdef FMT_HAS_CONSTEVAL
fmt::format(\"{:d}\", \"I am not a number\");
#else
#error
#endif
" ERROR)

# Compile-time argument name check
expect_compile(format-string-name "
#if defined(FMT_HAS_CONSTEVAL) && FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
using namespace fmt::literals;
fmt::print(\"{foo}\", \"foo\"_a=42);
#endif
")
expect_compile(format-string-name-error "
#if defined(FMT_HAS_CONSTEVAL) && FMT_USE_NONTYPE_TEMPLATE_PARAMETERS
using namespace fmt::literals;
fmt::print(\"{foo}\", \"bar\"_a=42);
#else
#error
#endif
" ERROR)
endif ()

# Run all tests
run_tests()

0 comments on commit c2ba8bc

Please sign in to comment.