Skip to content

Commit 9a23c25

Browse files
committed
feat: modify _assert_internal_format_message to auto insert :
1 parent 511d735 commit 9a23c25

File tree

2 files changed

+62
-62
lines changed

2 files changed

+62
-62
lines changed

cmake/Assertion.cmake

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include_guard(GLOBAL)
1111
# string. Each even line will be indented by 2 spaces. The formatted string will
1212
# then be stored in the `<out_var>` variable.
1313
function(_assert_internal_format_message OUT_VAR FIRST_LINE)
14-
set(MESSAGE "${FIRST_LINE}")
14+
set(MESSAGE "${FIRST_LINE}:")
1515
if(ARGC GREATER 2)
1616
math(EXPR STOP "${ARGC} - 1")
1717
foreach(I RANGE 2 "${STOP}")
@@ -21,7 +21,7 @@ function(_assert_internal_format_message OUT_VAR FIRST_LINE)
2121
string(REPLACE "\n" "\n " LINE "${LINE}")
2222
set(MESSAGE "${MESSAGE}\n ${LINE}")
2323
else()
24-
set(MESSAGE "${MESSAGE}\n${LINE}")
24+
set(MESSAGE "${MESSAGE}\n${LINE}:")
2525
endif()
2626
endforeach()
2727
endif()
@@ -44,68 +44,68 @@ function(assert)
4444
else()
4545
string(REPLACE ";" " " ARGS "${ARG_UNPARSED_ARGUMENTS}")
4646
_assert_internal_format_message(
47-
MESSAGE "expected:" "${ARGS}" "to resolve to true")
47+
MESSAGE "expected" "${ARGS}" "to resolve to true")
4848

4949
if(ARGC EQUAL 2)
5050
if(NOT ARGV0 STREQUAL NOT)
5151
if(ARGV0 STREQUAL DEFINED)
5252
_assert_internal_format_message(
53-
MESSAGE "expected variable:" "${ARGV1}" "to be defined")
53+
MESSAGE "expected variable" "${ARGV1}" "to be defined")
5454
elseif(ARGV0 STREQUAL EXISTS)
5555
_assert_internal_format_message(
56-
MESSAGE "expected path:" "${ARGV1}" "to exist")
56+
MESSAGE "expected path" "${ARGV1}" "to exist")
5757
elseif(ARGV0 STREQUAL IS_DIRECTORY)
5858
_assert_internal_format_message(
59-
MESSAGE "expected path:" "${ARGV1}" "to be a directory")
59+
MESSAGE "expected path" "${ARGV1}" "to be a directory")
6060
endif()
6161
endif()
6262
elseif(ARGC EQUAL 3)
6363
if(ARGV0 STREQUAL NOT)
6464
if(ARGV1 STREQUAL DEFINED)
6565
_assert_internal_format_message(
66-
MESSAGE "expected variable:" "${ARGV2}" "not to be defined")
66+
MESSAGE "expected variable" "${ARGV2}" "not to be defined")
6767
elseif(ARGV1 STREQUAL EXISTS)
6868
_assert_internal_format_message(
69-
MESSAGE "expected path:" "${ARGV2}" "not to exist")
69+
MESSAGE "expected path" "${ARGV2}" "not to exist")
7070
elseif(ARGV1 STREQUAL IS_DIRECTORY)
7171
_assert_internal_format_message(
72-
MESSAGE "expected path:" "${ARGV2}" "not to be a directory")
72+
MESSAGE "expected path" "${ARGV2}" "not to be a directory")
7373
endif()
7474
else()
7575
if(ARGV1 STREQUAL MATCHES)
7676
if(DEFINED "${ARGV0}")
7777
_assert_internal_format_message(
78-
MESSAGE "expected string:" "${${ARGV0}}"
79-
"of variable:" "${ARGV0}"
80-
"to match:" "${ARGV2}")
78+
MESSAGE "expected string" "${${ARGV0}}"
79+
"of variable" "${ARGV0}"
80+
"to match" "${ARGV2}")
8181
else()
8282
_assert_internal_format_message(
83-
MESSAGE "expected string:" "${ARGV0}" "to match:" "${ARGV2}")
83+
MESSAGE "expected string" "${ARGV0}" "to match" "${ARGV2}")
8484
endif()
8585
elseif(ARGV1 STREQUAL STREQUAL)
8686
if(DEFINED "${ARGV0}")
8787
if(DEFINED "${ARGV2}")
8888
_assert_internal_format_message(
89-
MESSAGE "expected string:" "${${ARGV0}}"
90-
"of variable:" "${ARGV0}"
91-
"to be equal to string:" "${${ARGV2}}"
92-
"of variable:" "${ARGV2}")
89+
MESSAGE "expected string" "${${ARGV0}}"
90+
"of variable" "${ARGV0}"
91+
"to be equal to string" "${${ARGV2}}"
92+
"of variable" "${ARGV2}")
9393
else()
9494
_assert_internal_format_message(
95-
MESSAGE "expected string:" "${${ARGV0}}"
96-
"of variable:" "${ARGV0}"
97-
"to be equal to:" "${ARGV2}")
95+
MESSAGE "expected string" "${${ARGV0}}"
96+
"of variable" "${ARGV0}"
97+
"to be equal to" "${ARGV2}")
9898
endif()
9999
else()
100100
if(DEFINED "${ARGV2}")
101101
_assert_internal_format_message(
102-
MESSAGE "expected string:" "${ARGV0}"
103-
"to be equal to string:" "${${ARGV2}}"
104-
"of variable:" "${ARGV2}")
102+
MESSAGE "expected string" "${ARGV0}"
103+
"to be equal to string" "${${ARGV2}}"
104+
"of variable" "${ARGV2}")
105105
else()
106106
_assert_internal_format_message(
107-
MESSAGE "expected string:" "${ARGV0}"
108-
"to be equal to:" "${ARGV2}")
107+
MESSAGE "expected string" "${ARGV0}"
108+
"to be equal to" "${ARGV2}")
109109
endif()
110110
endif()
111111
endif()
@@ -115,37 +115,37 @@ function(assert)
115115
if(ARGV2 STREQUAL MATCHES)
116116
if(DEFINED "${ARGV1}")
117117
_assert_internal_format_message(
118-
MESSAGE "expected string:" "${${ARGV1}}"
119-
"of variable:" "${ARGV1}"
120-
"not to match:" "${ARGV3}")
118+
MESSAGE "expected string" "${${ARGV1}}"
119+
"of variable" "${ARGV1}"
120+
"not to match" "${ARGV3}")
121121
else()
122122
_assert_internal_format_message(
123-
MESSAGE "expected string:" "${ARGV1}" "not to match:" "${ARGV3}")
123+
MESSAGE "expected string" "${ARGV1}" "not to match" "${ARGV3}")
124124
endif()
125125
elseif(ARGV2 STREQUAL STREQUAL)
126126
if(DEFINED "${ARGV1}")
127127
if(DEFINED "${ARGV3}")
128128
_assert_internal_format_message(
129-
MESSAGE "expected string:" "${${ARGV1}}"
130-
"of variable:" "${ARGV1}"
131-
"not to be equal to string:" "${${ARGV3}}"
132-
"of variable:" "${ARGV3}")
129+
MESSAGE "expected string" "${${ARGV1}}"
130+
"of variable" "${ARGV1}"
131+
"not to be equal to string" "${${ARGV3}}"
132+
"of variable" "${ARGV3}")
133133
else()
134134
_assert_internal_format_message(
135-
MESSAGE "expected string:" "${${ARGV1}}"
136-
"of variable:" "${ARGV1}"
137-
"not to be equal to:" "${ARGV3}")
135+
MESSAGE "expected string" "${${ARGV1}}"
136+
"of variable" "${ARGV1}"
137+
"not to be equal to" "${ARGV3}")
138138
endif()
139139
else()
140140
if(DEFINED "${ARGV3}")
141141
_assert_internal_format_message(
142-
MESSAGE "expected string:" "${ARGV1}"
143-
"not to be equal to string:" "${${ARGV3}}"
144-
"of variable:" "${ARGV3}")
142+
MESSAGE "expected string" "${ARGV1}"
143+
"not to be equal to string" "${${ARGV3}}"
144+
"of variable" "${ARGV3}")
145145
else()
146146
_assert_internal_format_message(
147-
MESSAGE "expected string:" "${ARGV1}"
148-
"not to be equal to:" "${ARGV3}")
147+
MESSAGE "expected string" "${ARGV1}"
148+
"not to be equal to" "${ARGV3}")
149149
endif()
150150
endif()
151151
endif()
@@ -179,8 +179,8 @@ function(assert_fatal_error)
179179
if(NOT "${ARG_UNPARSED_ARGUMENTS}" MATCHES "${EXPECTED_MESSAGE}")
180180
_assert_internal_format_message(
181181
ASSERT_MESSAGE
182-
"expected fatal error message:" "${ARG_UNPARSED_ARGUMENTS}"
183-
"to match:" "${EXPECTED_MESSAGE}")
182+
"expected fatal error message" "${ARG_UNPARSED_ARGUMENTS}"
183+
"to match" "${EXPECTED_MESSAGE}")
184184
message(FATAL_ERROR "${ASSERT_MESSAGE}")
185185
endif()
186186
else()
@@ -226,27 +226,27 @@ function(assert_execute_process)
226226
if(DEFINED ARG_ERROR AND RES EQUAL 0)
227227
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
228228
_assert_internal_format_message(
229-
MESSAGE "expected command:" "${COMMAND}" "to fail")
229+
MESSAGE "expected command" "${COMMAND}" "to fail")
230230
message(FATAL_ERROR "${MESSAGE}")
231231
elseif(NOT DEFINED ARG_ERROR AND NOT RES EQUAL 0)
232232
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
233233
_assert_internal_format_message(
234-
MESSAGE "expected command:" "${COMMAND}"
235-
"not to fail with error:" "${ERR}")
234+
MESSAGE "expected command" "${COMMAND}"
235+
"not to fail with error" "${ERR}")
236236
message(FATAL_ERROR "${MESSAGE}")
237237
elseif(DEFINED ARG_OUTPUT AND NOT "${OUT}" MATCHES "${ARG_OUTPUT}")
238238
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
239239
_assert_internal_format_message(
240-
MESSAGE "expected the output:" "${OUT}"
241-
"of command:" "${COMMAND}"
242-
"to match:" "${ARG_OUTPUT}")
240+
MESSAGE "expected the output" "${OUT}"
241+
"of command" "${COMMAND}"
242+
"to match" "${ARG_OUTPUT}")
243243
message(FATAL_ERROR "${MESSAGE}")
244244
elseif(DEFINED ARG_ERROR AND NOT "${ERR}" MATCHES "${ARG_ERROR}")
245245
string(REPLACE ";" " " COMMAND "${ARG_COMMAND}")
246246
_assert_internal_format_message(
247-
MESSAGE "expected the error:" "${ERR}"
248-
"of command:" "${COMMAND}"
249-
"to match:" "${ARG_ERROR}")
247+
MESSAGE "expected the error" "${ERR}"
248+
"of command" "${COMMAND}"
249+
"to match" "${ARG_ERROR}")
250250
message(FATAL_ERROR "${MESSAGE}")
251251
endif()
252252
endfunction()

test/InternalFormatMessage.cmake

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ cmake_minimum_required(VERSION 3.5)
33
find_package(Assertion REQUIRED PATHS ${CMAKE_CURRENT_LIST_DIR}/../cmake)
44

55
_assert_internal_format_message(
6-
MESSAGE "first line" "second line" "third line\nthird line" "fourth line\nfourth line")
6+
MESSAGE "first line" "second line"
7+
"third line\nthird line" "fourth line\nfourth line")
78

8-
string(
9-
JOIN "\n" EXPECTED_MESSAGE
10-
"first line"
11-
" second line"
12-
"third line"
13-
"third line"
14-
" fourth line"
15-
" fourth line")
16-
assert(MESSAGE STREQUAL EXPECTED_MESSAGE)
9+
assert_fatal_error(
10+
CALL message FATAL_ERROR "${MESSAGE}"
11+
MESSAGE "first line:\n"
12+
" second line\n"
13+
"third line\n"
14+
"third line:\n"
15+
" fourth line\n"
16+
" fourth line")

0 commit comments

Comments
 (0)