Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Assertion Message #64

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 103 additions & 51 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,39 @@ 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}")
string(STRIP "${ARGV${I}}" LINE)
math(EXPR MOD "${I} % 2")
if(MOD EQUAL 0)
string(REPLACE "\n" "\n " LINE "${LINE}")
set(MESSAGE "${MESSAGE}\n ${LINE}")
else()
set(MESSAGE "${MESSAGE}\n${LINE}")
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 '${VARIABLE}' to be defined")
_assert_internal_format_message(
MESSAGE "expected variable:" "${VARIABLE}" "to be defined")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -36,7 +62,9 @@ endmacro()
# - VARIABLE: The variable to check.
macro(_assert_internal_assert_2_not_defined VARIABLE)
if(DEFINED "${VARIABLE}")
message(FATAL_ERROR "expected variable '${VARIABLE}' not 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 +74,9 @@ endmacro()
# - PATH: The path to check.
macro(_assert_internal_assert_2_exists PATH)
if(NOT EXISTS "${PATH}")
message(FATAL_ERROR "expected path '${PATH}' to exist")
_assert_internal_format_message(
MESSAGE "expected path:" "${PATH}" "to exist")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

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

Expand All @@ -66,7 +98,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 '${PATH}' to 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 +110,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 '${PATH}' not 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 +124,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 '${STRING_CONTENT}' to match '${REGEX}'"
)
_assert_internal_format_message(
MESSAGE "expected string:" "${STRING_CONTENT}" "to match:" "${REGEX}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -103,10 +138,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 '${STRING_CONTENT}' not to match '${REGEX}'"
)
_assert_internal_format_message(
MESSAGE "expected string:" "${STRING_CONTENT}" "not to match:" "${REGEX}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

Expand All @@ -119,10 +153,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 '${STRING1_CONTENT}' to be equal to '${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 +169,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 '${STRING1_CONTENT}' not to be equal to '${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 +185,9 @@ macro(_assert_internal_assert_1 ARG0)
# Do nothing on an empty condition.
else()
if(NOT "${ARG0}")
message(FATAL_ERROR "expected '${ARG0}' to 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 +198,9 @@ endmacro()
# - ARG0: The first argument.
macro(_assert_internal_assert_1_not ARG0)
if("${ARG0}")
message(FATAL_ERROR "expected '${ARG0}' to resolve to false")
_assert_internal_format_message(
MESSAGE "expected:" "${ARG0}" "to resolve to false")
message(FATAL_ERROR "${MESSAGE}")
endif()
endmacro()

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

Expand All @@ -218,7 +260,9 @@ macro(_assert_internal_assert_3 ARG0 ARG1 ARG2)
CALL "_assert_internal_assert_3_${OPERATOR}" "${ARG0}" "${ARG2}"
)
else()
message(FATAL_ERROR "unsupported condition: ${ARG0} ${ARG1} ${ARG2}")
_assert_internal_format_message(
MESSAGE "unsupported condition:" "${ARG0} ${ARG1} ${ARG2}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endif()
endmacro()
Expand All @@ -236,7 +280,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: 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 +297,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: ${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 +327,8 @@ function(assert)
foreach(I RANGE 4 "${STOP}")
set(ARGS "${ARGS} ${ARGV${I}}")
endforeach()
message(FATAL_ERROR "unsupported condition: ${ARGS}")
_assert_internal_format_message(MESSAGE "unsupported condition:" "${ARGS}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endfunction()

Expand Down Expand Up @@ -336,7 +385,10 @@ function(assert_message MODE EXPECTED_MESSAGE)
if(NOT MESSAGE STREQUAL EXPECTED_MESSAGE)
string(TOLOWER "${MODE}" MODE)
string(REPLACE "_" " " MODE "${MODE}")
message(FATAL_ERROR "expected ${MODE} message '${MESSAGE}' to be equal to '${EXPECTED_MESSAGE}'")
_assert_internal_format_message(
ASSERT_MESSAGE "expected ${MODE} message:" "${MESSAGE}"
"to be equal to:" "${EXPECTED_MESSAGE}")
message(FATAL_ERROR "${ASSERT_MESSAGE}")
endif()

if(DEFINED ${MODE}_MESSAGES)
Expand Down Expand Up @@ -367,28 +419,28 @@ function(assert_execute_process)
)

if(DEFINED ARG_ERROR AND RES EQUAL 0)
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected command '${ARG_COMMAND}' to fail"
)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected command:" "${COMMAND}" "to fail")
message(FATAL_ERROR "${MESSAGE}")
elseif(NOT DEFINED ARG_ERROR AND NOT RES EQUAL 0)
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected command '${ARG_COMMAND}' not to fail"
)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected command:" "${COMMAND}" "not to fail")
message(FATAL_ERROR "${MESSAGE}")
elseif(DEFINED ARG_OUTPUT AND NOT "${OUT}" MATCHES "${ARG_OUTPUT}")
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected the output of command '${ARG_COMMAND}' to match '${ARG_OUTPUT}'"
)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected the output:" "${OUT}"
"of command:" "${COMMAND}"
"to match:" "${ARG_OUTPUT}")
message(FATAL_ERROR "${MESSAGE}")
elseif(DEFINED ARG_ERROR AND NOT "${ERR}" MATCHES "${ARG_ERROR}")
string(REPLACE ";" " " ARG_COMMAND "${ARG_COMMAND}")
message(
FATAL_ERROR
"expected the error of command '${ARG_COMMAND}' to match '${ARG_ERROR}'"
)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
_assert_internal_format_message(
MESSAGE "expected the error:" "${ERR}"
"of command:" "${COMMAND}"
"to match:" "${ARG_ERROR}")
message(FATAL_ERROR "${MESSAGE}")
endif()
endfunction()
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
Loading