Skip to content

Commit

Permalink
refactor: add function to format assertion message
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed May 30, 2024
1 parent b394252 commit 25ed273
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 30 deletions.
108 changes: 78 additions & 30 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,38 @@ function(_assert_internal_get_content VARIABLE OUTPUT_VARIABLE)
endif()
endfunction()

# Formats an assertion message with indentation on even lines.
#
# Arguments:
# - OUT_VAR: The output variable that holds the formatted message.
# - FIRST_LINE: The first line of the message.
# - ARGN: The rest of the lines of the message.
function(_assert_internal_format_message OUT_VAR FIRST_LINE)
set(MESSAGE "${FIRST_LINE}")
if(ARGC GREATER 2)
math(EXPR STOP "${ARGC} - 1")
foreach(I RANGE 2 "${STOP}")
math(EXPR MOD "${I} % 2")
if(MOD EQUAL 0)
string(REPLACE "\n" "\n " INDENTED_LINE "${ARGV${I}}")
set(MESSAGE "${MESSAGE}\n ${INDENTED_LINE}")
else()
set(MESSAGE "${MESSAGE}\n${ARGV${I}}")
endif()
endforeach()
endif()
set("${OUT_VAR}" "${MESSAGE}" PARENT_SCOPE)
endfunction()

# Asserts whether the given variable is defined.
#
# Arguments:
# - VARIABLE: The variable to check.
macro(_assert_internal_assert_2_defined VARIABLE)
if(NOT DEFINED "${VARIABLE}")
message(FATAL_ERROR "expected variable:\n ${VARIABLE}\nto be defined")
_assert_internal_format_message(
MESSAGE "expected variable:" "${VARIABLE}" "to be defined")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -36,7 +61,9 @@ endmacro()
# - VARIABLE: The variable to check.
macro(_assert_internal_assert_2_not_defined VARIABLE)
if(DEFINED "${VARIABLE}")
message(FATAL_ERROR "expected variable:\n ${VARIABLE}\nnot to be defined")
_assert_internal_format_message(
MESSAGE "expected variable:" "${VARIABLE}" "not to be defined")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -46,7 +73,9 @@ endmacro()
# - PATH: The path to check.
macro(_assert_internal_assert_2_exists PATH)
if(NOT EXISTS "${PATH}")
message(FATAL_ERROR "expected path:\n ${PATH}\nto exist")
_assert_internal_format_message(
MESSAGE "expected path:" "${PATH}" "to exist")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -56,7 +85,9 @@ endmacro()
# - PATH: The path to check.
macro(_assert_internal_assert_2_not_exists PATH)
if(EXISTS "${PATH}")
message(FATAL_ERROR "expected path:\n ${PATH}\nnot to exist")
_assert_internal_format_message(
MESSAGE "expected path:" "${PATH}" "not to exist")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -66,7 +97,9 @@ endmacro()
# - PATH: The path to check.
macro(_assert_internal_assert_2_is_directory PATH)
if(NOT IS_DIRECTORY "${PATH}")
message(FATAL_ERROR "expected path:\n ${PATH}\nto be a directory")
_assert_internal_format_message(
MESSAGE "expected path:" "${PATH}" "to be a directory")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -76,7 +109,9 @@ endmacro()
# - PATH: The path to check.
macro(_assert_internal_assert_2_not_is_directory PATH)
if(IS_DIRECTORY "${PATH}")
message(FATAL_ERROR "expected path:\n ${PATH}\nnot to be a directory")
_assert_internal_format_message(
MESSAGE "expected path:" "${PATH}" "not to be a directory")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -88,10 +123,9 @@ endmacro()
macro(_assert_internal_assert_3_matches STRING REGEX)
_assert_internal_get_content("${STRING}" STRING_CONTENT)
if(NOT "${STRING_CONTENT}" MATCHES "${REGEX}")
message(
FATAL_ERROR
"expected string:\n ${STRING_CONTENT}\nto match:\n ${REGEX}"
)
_assert_internal_format_message(
MESSAGE "expected string:" "${STRING_CONTENT}" "to match:" "${REGEX}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -103,10 +137,9 @@ endmacro()
macro(_assert_internal_assert_3_not_matches STRING REGEX)
_assert_internal_get_content("${STRING}" STRING_CONTENT)
if("${STRING_CONTENT}" MATCHES "${REGEX}")
message(
FATAL_ERROR
"expected string:\n ${STRING_CONTENT}\nnot to match:\n ${REGEX}"
)
_assert_internal_format_message(
MESSAGE "expected string:" "${STRING_CONTENT}" "not to match:" "${REGEX}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -119,10 +152,10 @@ macro(_assert_internal_assert_3_strequal STRING1 STRING2)
_assert_internal_get_content("${STRING1}" STRING1_CONTENT)
_assert_internal_get_content("${STRING2}" STRING2_CONTENT)
if(NOT "${STRING1_CONTENT}" STREQUAL "${STRING2_CONTENT}")
message(
FATAL_ERROR
"expected string:\n ${STRING1_CONTENT}\nto be equal to:\n ${STRING2_CONTENT}"
)
_assert_internal_format_message(
MESSAGE "expected string:" "${STRING1_CONTENT}"
"to be equal to:" "${STRING2_CONTENT}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -135,10 +168,10 @@ macro(_assert_internal_assert_3_not_strequal STRING1 STRING2)
_assert_internal_get_content("${STRING1}" STRING1_CONTENT)
_assert_internal_get_content("${STRING2}" STRING2_CONTENT)
if("${STRING1_CONTENT}" STREQUAL "${STRING2_CONTENT}")
message(
FATAL_ERROR
"expected string:\n ${STRING1_CONTENT}\nnot to be equal to:\n ${STRING2_CONTENT}"
)
_assert_internal_format_message(
MESSAGE "expected string:" "${STRING1_CONTENT}"
"not to be equal to:" "${STRING2_CONTENT}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -151,7 +184,9 @@ macro(_assert_internal_assert_1 ARG0)
# Do nothing on an empty condition.
else()
if(NOT "${ARG0}")
message(FATAL_ERROR "expected:\n ${ARG0}\nto resolve to true")
_assert_internal_format_message(
MESSAGE "expected:" "${ARG0}" "to resolve to true")
message(FATAL_ERROR "${MESSAGE}")
endif()
endif()
endmacro()
Expand All @@ -162,7 +197,9 @@ endmacro()
# - ARG0: The first argument.
macro(_assert_internal_assert_1_not ARG0)
if("${ARG0}")
message(FATAL_ERROR "expected:\n ${ARG0}\nto resolve to false")
_assert_internal_format_message(
MESSAGE "expected:" "${ARG0}" "to resolve to false")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -181,7 +218,9 @@ macro(_assert_internal_assert_2 ARG0 ARG1)
CALL "_assert_internal_assert_2_${OPERATOR}" "${ARG1}"
)
else()
message(FATAL_ERROR "unsupported condition:\n ${ARG0} ${ARG1}")
_assert_internal_format_message(
MESSAGE "unsupported condition:" "${ARG0} ${ARG1}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endif()
endmacro()
Expand All @@ -198,7 +237,9 @@ macro(_assert_internal_assert_2_not ARG0 ARG1)
CALL "_assert_internal_assert_2_not_${OPERATOR}" "${ARG1}"
)
else()
message(FATAL_ERROR "unsupported condition:\n NOT ${ARG0} ${ARG1}")
_assert_internal_format_message(
MESSAGE "unsupported condition:" "NOT ${ARG0} ${ARG1}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -218,7 +259,9 @@ macro(_assert_internal_assert_3 ARG0 ARG1 ARG2)
CALL "_assert_internal_assert_3_${OPERATOR}" "${ARG0}" "${ARG2}"
)
else()
message(FATAL_ERROR "unsupported condition:\n ${ARG0} ${ARG1} ${ARG2}")
_assert_internal_format_message(
MESSAGE "unsupported condition:" "${ARG0} ${ARG1} ${ARG2}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endif()
endmacro()
Expand All @@ -236,7 +279,9 @@ macro(_assert_internal_assert_3_not ARG0 ARG1 ARG2)
CALL "_assert_internal_assert_3_not_${OPERATOR}" "${ARG0}" "${ARG2}"
)
else()
message(FATAL_ERROR "unsupported condition:\n NOT ${ARG0} ${ARG1} ${ARG2}")
_assert_internal_format_message(
MESSAGE "unsupported condition:" "NOT ${ARG0} ${ARG1} ${ARG2}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -251,7 +296,9 @@ macro(_assert_internal_assert_4 ARG0 ARG1 ARG2 ARG3)
if("${ARG0}" STREQUAL NOT)
_assert_internal_assert_3_not("${ARG1}" "${ARG2}" "${ARG3}")
else()
message(FATAL_ERROR "unsupported condition:\n ${ARG0} ${ARG1} ${ARG2} ${ARG3}")
_assert_internal_format_message(
MESSAGE "unsupported condition:" "${ARG0} ${ARG1} ${ARG2} ${ARG3}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand Down Expand Up @@ -279,7 +326,8 @@ function(assert)
foreach(I RANGE 4 "${STOP}")
set(ARGS "${ARGS} ${ARGV${I}}")
endforeach()
message(FATAL_ERROR "unsupported condition:\n ${ARGS}")
_assert_internal_format_message(MESSAGE "unsupported condition:" "${ARGS}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endfunction()

Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ endfunction()
add_cmake_test(
cmake/InternalTest.cmake
"Variable content retrieval"
"Assertion message formatting"
)

add_cmake_test(
Expand Down
15 changes: 15 additions & 0 deletions test/cmake/InternalTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,19 @@ function("Variable content retrieval")
assert(CONTENT STREQUAL "some other value")
endfunction()

function("Assertion message formatting")
_assert_internal_format_message(
MESSAGE "first line" "second line" "third line\nthird line" "fourth line\nfourth line")

string(
JOIN "\n" EXPECTED_MESSAGE
"first line"
" second line"
"third line"
"third line"
" fourth line"
" fourth line")
assert(MESSAGE STREQUAL EXPECTED_MESSAGE)
endfunction()

cmake_language(CALL "${TEST_COMMAND}")

0 comments on commit 25ed273

Please sign in to comment.