Skip to content

Commit

Permalink
refactor: ensure functions return after fail (#158)
Browse files Browse the repository at this point in the history
* refactor: ensure return on fail in `assert_execute_process` function

* refactor: ensure return on fail in `assert` function

* refactor: modify `assert` function to exit immediately if not fail
  • Loading branch information
threeal authored Jul 5, 2024
1 parent 51df3eb commit a8ff10c
Showing 1 changed file with 108 additions and 102 deletions.
210 changes: 108 additions & 102 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -66,105 +66,102 @@ endmacro()
function(assert)
cmake_parse_arguments(PARSE_ARGV 0 ARG "" "" "")
if(${ARG_UNPARSED_ARGUMENTS})
# Do nothing if the assertion succeeds.
else()
if(ARGC EQUAL 2)
if(NOT ARGV0 STREQUAL NOT)
if(ARGV0 STREQUAL DEFINED)
fail("expected variable" ARGV1 "to be defined")
elseif(ARGV0 STREQUAL EXISTS)
fail("expected path" ARGV1 "to exist")
elseif(ARGV0 STREQUAL IS_DIRECTORY)
fail("expected path" ARGV1 "to be a directory")
else()
set(ARGS "${ARGV0} ${ARGV1}")
fail("expected" ARGS "to resolve to true")
endif()
else()
set(ARGS "${ARGV0} ${ARGV1}")
fail("expected" ARGS "to resolve to true")
# Assertion passed, exit immediately.
return()
endif()

# Assertion failed, determine the fatal error message based on the given
# condition.
if(ARGC EQUAL 2)
if(NOT ARGV0 STREQUAL NOT)
if(ARGV0 STREQUAL DEFINED)
fail("expected variable" ARGV1 "to be defined")
return()
elseif(ARGV0 STREQUAL EXISTS)
fail("expected path" ARGV1 "to exist")
return()
elseif(ARGV0 STREQUAL IS_DIRECTORY)
fail("expected path" ARGV1 "to be a directory")
return()
endif()
endif()
elseif(ARGC EQUAL 3)
if(ARGV0 STREQUAL NOT)
if(ARGV1 STREQUAL DEFINED)
fail("expected variable" ARGV2 "not to be defined")
return()
elseif(ARGV1 STREQUAL EXISTS)
fail("expected path" ARGV2 "not to exist")
return()
elseif(ARGV1 STREQUAL IS_DIRECTORY)
fail("expected path" ARGV2 "not to be a directory")
return()
endif()
elseif(ARGC EQUAL 3)
if(ARGV0 STREQUAL NOT)
if(ARGV1 STREQUAL DEFINED)
fail("expected variable" ARGV2 "not to be defined")
elseif(ARGV1 STREQUAL EXISTS)
fail("expected path" ARGV2 "not to exist")
elseif(ARGV1 STREQUAL IS_DIRECTORY)
fail("expected path" ARGV2 "not to be a directory")
else()
if(ARGV1 STREQUAL MATCHES)
if(DEFINED "${ARGV0}")
fail("expected string" "${ARGV0}" "of variable" ARGV0
"to match" ARGV2)
else()
set(ARGS "${ARGV0} ${ARGV1} ${ARGV2}")
fail("expected" ARGS "to resolve to true")
fail("expected string" ARGV0 "to match" ARGV2)
endif()
else()
if(ARGV1 STREQUAL MATCHES)
if(DEFINED "${ARGV0}")
return()
elseif(ARGV1 STREQUAL STREQUAL)
if(DEFINED "${ARGV0}")
if(DEFINED "${ARGV2}")
fail("expected string" "${ARGV0}" "of variable" ARGV0
"to match" ARGV2)
"to be equal to string" "${ARGV2}" "of variable" ARGV2)
else()
fail("expected string" ARGV0 "to match" ARGV2)
fail("expected string" "${ARGV0}" "of variable" ARGV0
"to be equal to" ARGV2)
endif()
elseif(ARGV1 STREQUAL STREQUAL)
if(DEFINED "${ARGV0}")
if(DEFINED "${ARGV2}")
fail("expected string" "${ARGV0}" "of variable" ARGV0
"to be equal to string" "${ARGV2}" "of variable" ARGV2)
else()
fail("expected string" "${ARGV0}" "of variable" ARGV0
"to be equal to" ARGV2)
endif()
else()
if(DEFINED "${ARGV2}")
fail("expected string" ARGV0
"to be equal to string" "${ARGV2}" "of variable" ARGV2)
else()
if(DEFINED "${ARGV2}")
fail("expected string" ARGV0
"to be equal to string" "${ARGV2}" "of variable" ARGV2)
else()
fail("expected string" ARGV0 "to be equal to" ARGV2)
endif()
fail("expected string" ARGV0 "to be equal to" ARGV2)
endif()
else()
set(ARGS "${ARGV0} ${ARGV1} ${ARGV2}")
fail("expected" ARGS "to resolve to true")
endif()
return()
endif()
elseif(ARGC EQUAL 4)
if(ARGV0 STREQUAL NOT)
if(ARGV2 STREQUAL MATCHES)
if(DEFINED "${ARGV1}")
endif()
elseif(ARGC EQUAL 4)
if(ARGV0 STREQUAL NOT)
if(ARGV2 STREQUAL MATCHES)
if(DEFINED "${ARGV1}")
fail("expected string" "${ARGV1}" "of variable" ARGV1
"not to match" ARGV3)
else()
fail("expected string" ARGV1 "not to match" ARGV3)
endif()
return()
elseif(ARGV2 STREQUAL STREQUAL)
if(DEFINED "${ARGV1}")
if(DEFINED "${ARGV3}")
fail("expected string" "${ARGV1}" "of variable" ARGV1
"not to match" ARGV3)
"not to be equal to string" "${ARGV3}" "of variable" ARGV3)
else()
fail("expected string" ARGV1 "not to match" ARGV3)
fail("expected string" "${ARGV1}" "of variable" ARGV1
"not to be equal to" ARGV3)
endif()
elseif(ARGV2 STREQUAL STREQUAL)
if(DEFINED "${ARGV1}")
if(DEFINED "${ARGV3}")
fail("expected string" "${ARGV1}" "of variable" ARGV1
"not to be equal to string" "${ARGV3}" "of variable" ARGV3)
else()
fail("expected string" "${ARGV1}" "of variable" ARGV1
"not to be equal to" ARGV3)
endif()
else()
if(DEFINED "${ARGV3}")
fail("expected string" ARGV1
"not to be equal to string" "${ARGV3}" "of variable" ARGV3)
else()
if(DEFINED "${ARGV3}")
fail("expected string" ARGV1
"not to be equal to string" "${ARGV3}" "of variable" ARGV3)
else()
fail("expected string" ARGV1 "not to be equal to" ARGV3)
endif()
fail("expected string" ARGV1 "not to be equal to" ARGV3)
endif()
else()
set(ARGS "${ARGV0} ${ARGV1} ${ARGV2} ${ARGV3}")
fail("expected" ARGS "to resolve to true")
endif()
else()
set(ARGS "${ARGV0} ${ARGV1} ${ARGV2} ${ARGV3}")
fail("expected" ARGS "to resolve to true")
return()
endif()
else()
string(REPLACE ";" " " ARGS "${ARG_UNPARSED_ARGUMENTS}")
fail("expected" ARGS "to resolve to true")
endif()
endif()

# Fallback to this message if it could not determine the fatal error message
# from the given condition.
string(REPLACE ";" " " ARGS "${ARG_UNPARSED_ARGUMENTS}")
fail("expected" ARGS "to resolve to true")
endfunction()

# Asserts whether a command call throws a fatal error message.
Expand Down Expand Up @@ -262,34 +259,43 @@ function(assert_execute_process)
set(ARG_COMMAND ${ARG_UNPARSED_ARGUMENTS})
endif()

if(DEFINED ARG_OUTPUT)
string(JOIN "" EXPECTED_OUTPUT ${ARG_OUTPUT})
endif()

if(DEFINED ARG_ERROR)
string(JOIN "" EXPECTED_ERROR ${ARG_ERROR})
endif()

execute_process(
COMMAND ${ARG_COMMAND}
RESULT_VARIABLE RES
OUTPUT_VARIABLE OUT
ERROR_VARIABLE ERR)

if(DEFINED ARG_ERROR AND RES EQUAL 0)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected command" COMMAND "to fail")
elseif(NOT DEFINED ARG_ERROR AND NOT RES EQUAL 0)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected command" COMMAND "not to fail with error" ERR)
elseif(DEFINED EXPECTED_OUTPUT AND NOT "${OUT}" MATCHES "${EXPECTED_OUTPUT}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected the output" OUT "of command" COMMAND
"to match" EXPECTED_OUTPUT)
elseif(DEFINED EXPECTED_ERROR AND NOT "${ERR}" MATCHES "${EXPECTED_ERROR}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected the error" ERR "of command" COMMAND
"to match" EXPECTED_ERROR)
if(DEFINED ARG_ERROR)
if(RES EQUAL 0)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected command" COMMAND "to fail")
return()
endif()
else()
if(NOT RES EQUAL 0)
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected command" COMMAND "not to fail with error" ERR)
return()
endif()
endif()

if(DEFINED ARG_OUTPUT)
string(JOIN "" EXPECTED_OUTPUT ${ARG_OUTPUT})
if(NOT "${OUT}" MATCHES "${EXPECTED_OUTPUT}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected the output" OUT "of command" COMMAND
"to match" EXPECTED_OUTPUT)
return()
endif()
endif()

if(DEFINED ARG_ERROR)
string(JOIN "" EXPECTED_ERROR ${ARG_ERROR})
if(NOT "${ERR}" MATCHES "${EXPECTED_ERROR}")
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
fail("expected the error" ERR "of command" COMMAND
"to match" EXPECTED_ERROR)
endif()
endif()
endfunction()

Expand Down

0 comments on commit a8ff10c

Please sign in to comment.