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

Convert Mock Message Function to Behave Within a Scope #22

Merged
merged 1 commit into from
May 5, 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
56 changes: 31 additions & 25 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -105,35 +105,41 @@ function(assert_not_strequal STR1 STR2)
endif()
endfunction()

# Mocks the 'message' function.
# Begins a scope for mocking the 'message' function.
#
# If enabled, this function will mock the 'message' function by modifying its
# behavior to store the message into a list variable instead of printing it to
# the log.
# This function begins a scope for mocking the 'message' function by modifying
# its behavior to store the message into a list variable instead of printing it
# to the log.
#
# Arguments:
# - ENABLED: Whether to mock the 'message' function or not.
function(mock_message ENABLED)
if("${ENABLED}")
set_property(GLOBAL PROPERTY message_mocked "${ENABLED}")

macro(message MODE MESSAGE)
get_property(ENABLED GLOBAL PROPERTY message_mocked)
if("${ENABLED}")
list(APPEND ${MODE}_MESSAGES "${MESSAGE}")
set(${MODE}_MESSAGES "${${MODE}_MESSAGES}" PARENT_SCOPE)
if("${MODE}" STREQUAL FATAL_ERROR)
return()
endif()
else()
_message("${MODE}" "${MESSAGE}")
# Use the 'end_mock_message' function to end the scope for mocking the
# 'message' function, reverting it to the original behavior.
function(mock_message)
set_property(GLOBAL PROPERTY message_mocked ON)

macro(message MODE MESSAGE)
get_property(ENABLED GLOBAL PROPERTY message_mocked)
if("${ENABLED}")
list(APPEND ${MODE}_MESSAGES "${MESSAGE}")
set(${MODE}_MESSAGES "${${MODE}_MESSAGES}" PARENT_SCOPE)
if("${MODE}" STREQUAL FATAL_ERROR)
return()
endif()
endmacro()
else()
_message("${MODE}" "${MESSAGE}")
endif()
endmacro()

function(mock_message)
set_property(GLOBAL PROPERTY message_mocked ON)
endfunction()
endfunction()

function(mock_message ENABLED)
set_property(GLOBAL PROPERTY message_mocked "${ENABLED}")
endfunction()
endif()
# Ends a scope for mocking the 'message' function.
#
# This function ends the scope for mocking the 'message' function, reverting it
# to the original behavior.
function(end_mock_message)
set_property(GLOBAL PROPERTY message_mocked OFF)
endfunction()

# Asserts whether the 'message' function was called with the expected
Expand Down
96 changes: 42 additions & 54 deletions test/cmake/AssertionTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,37 @@ include(Assertion)
function(test_assert_a_true_condition)
assert_true(TRUE)

mock_message(ON)
assert_false(TRUE)

mock_message(OFF)
mock_message()
assert_false(TRUE)
end_mock_message()
assert_message(FATAL_ERROR "expected the condition to be false")
endfunction()

function(test_assert_a_false_condition)
assert_false(FALSE)

mock_message(ON)
assert_true(FALSE)

mock_message(OFF)
mock_message()
assert_true(FALSE)
end_mock_message()
assert_message(FATAL_ERROR "expected the condition to be true")
endfunction()

function(test_assert_a_defined_variable)
set(SOME_VARIABLE "some value")
assert_defined(SOME_VARIABLE)

mock_message(ON)
assert_not_defined(SOME_VARIABLE)

mock_message(OFF)
mock_message()
assert_not_defined(SOME_VARIABLE)
end_mock_message()
assert_message(FATAL_ERROR "expected variable 'SOME_VARIABLE' not to be defined")
endfunction()

function(test_assert_an_undefined_variable)
assert_not_defined(SOME_VARIABLE)

mock_message(ON)
assert_defined(SOME_VARIABLE)

mock_message(OFF)
mock_message()
assert_defined(SOME_VARIABLE)
end_mock_message()
assert_message(FATAL_ERROR "expected variable 'SOME_VARIABLE' to be defined")
endfunction()

Expand All @@ -48,16 +44,14 @@ function(test_assert_a_file_path)

assert_exists(some-file)

mock_message(ON)
assert_not_exists(some-file)

mock_message(OFF)
mock_message()
assert_not_exists(some-file)
end_mock_message()
assert_message(FATAL_ERROR "expected path 'some-file' not to exist")

mock_message(ON)
assert_directory(some-file)

mock_message(OFF)
mock_message()
assert_directory(some-file)
end_mock_message()
assert_message(FATAL_ERROR "expected path 'some-file' to be a directory")

assert_not_directory(some-file)
Expand All @@ -68,49 +62,44 @@ function(test_assert_a_directory_path)

assert_exists(some-directory)

mock_message(ON)
assert_not_exists(some-directory)

mock_message(OFF)
mock_message()
assert_not_exists(some-directory)
end_mock_message()
assert_message(FATAL_ERROR "expected path 'some-directory' not to exist")

assert_directory(some-directory)

mock_message(ON)
assert_not_directory(some-directory)

mock_message(OFF)
mock_message()
assert_not_directory(some-directory)
end_mock_message()
assert_message(FATAL_ERROR "expected path 'some-directory' not to be a directory")
endfunction()

function(test_assert_a_non_existing_path)
file(REMOVE some-non-existing-file)
assert_not_exists(some-non-existing-file)

mock_message(ON)
assert_exists(some-non-existing-file)

mock_message(OFF)
mock_message()
assert_exists(some-non-existing-file)
end_mock_message()
assert_message(FATAL_ERROR "expected path 'some-non-existing-file' to exist")
endfunction()

function(test_assert_equal_strings)
assert_strequal("some string" "some string")

mock_message(ON)
assert_not_strequal("some string" "some string")

mock_message(OFF)
mock_message()
assert_not_strequal("some string" "some string")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' not to be equal to 'some string'")
endfunction()

function(test_assert_unequal_strings)
assert_not_strequal("some string" "some other string")

mock_message(ON)
assert_strequal("some string" "some other string")

mock_message(OFF)
mock_message()
assert_strequal("some string" "some other string")
end_mock_message()
assert_message(FATAL_ERROR "expected string 'some string' to be equal to 'some other string'")
endfunction()

Expand All @@ -123,10 +112,10 @@ function(call_sample_messages)
endfunction()

function(test_mock_message)
mock_message(ON)
call_sample_messages()
mock_message()
call_sample_messages()
end_mock_message()

mock_message(OFF)
assert_defined(WARNING_MESSAGES)
assert_strequal("${WARNING_MESSAGES}" "some warning message;some other warning message")

Expand All @@ -138,19 +127,18 @@ function(test_mock_message)
endfunction()

function(test_assert_messages)
mock_message(ON)
call_sample_messages()
mock_message()
call_sample_messages()
end_mock_message()

mock_message(OFF)
assert_message(WARNING "some warning message")
assert_message(WARNING "some other warning message")
assert_message(ERROR "some error message")
assert_message(FATAL_ERROR "some fatal error message")

mock_message(ON)
assert_message(ERROR "some other error message")

mock_message(OFF)
mock_message()
assert_message(ERROR "some other error message")
end_mock_message()
assert_message(FATAL_ERROR "expected error message '' to be equal to 'some other error message'")
endfunction()

Expand Down