Skip to content

Commit

Permalink
feat: allow fail function to expand twice (#191)
Browse files Browse the repository at this point in the history
* feat: modify `fail` to support expanding vars twice

* test: minimize string equality assertion tests
  • Loading branch information
threeal authored Jul 9, 2024
1 parent 0f66746 commit 78ca271
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 111 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ This macro throws a fatal error message formatted from the given `<lines>`.

It formats the message by concatenating all the lines into a single message. If
one of the lines is a variable, it will be expanded and indented by two spaces
before being concatenated with the other lines.
before being concatenated with the other lines. If the expanded variable is
another variable, it will format both the name and the value of the other
variable.

#### Example

Expand Down
93 changes: 30 additions & 63 deletions cmake/Assertion.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,34 @@ endfunction()
#
# It formats the message by concatenating all the lines into a single message.
# If one of the lines is a variable, it will be expanded and indented by two
# spaces before being concatenated with the other lines.
# spaces before being concatenated with the other lines. If the expanded
# variable is another variable, it will format both the name and the value of
# the other variable.
macro(fail FIRST_LINE)
if(DEFINED "${FIRST_LINE}")
set(MESSAGE "${${FIRST_LINE}}")
set(LINES)
foreach(LINE IN ITEMS "${FIRST_LINE}" ${ARGN})
# Expand variable if it is defined and contains another variable.
if(DEFINED "${LINE}" AND DEFINED "${${LINE}}")
list(APPEND LINES "${${LINE}}" "of variable" "${LINE}")
else()
list(APPEND LINES "${LINE}")
endif()
endforeach()

# Format the first line.
list(POP_FRONT LINES LINE)
if(DEFINED "${LINE}")
set(MESSAGE "${${LINE}}")
set(PREV_IS_STRING FALSE)
set(INDENT_VAR FALSE)
else()
set(MESSAGE "${FIRST_LINE}")
set(MESSAGE "${LINE}")
set(PREV_IS_STRING TRUE)
set(INDENT_VAR TRUE)
endif()

foreach(LINE IN ITEMS ${ARGN})
# Format the consecutive lines.
foreach(LINE IN ITEMS ${LINES})
if(DEFINED "${LINE}")
if(PREV_IS_STRING)
string(APPEND MESSAGE ":")
Expand All @@ -106,6 +121,7 @@ macro(fail FIRST_LINE)
endif()
endforeach()

# Throw a fatal error with the formatted message.
message(FATAL_ERROR "${MESSAGE}")
endmacro()

Expand Down Expand Up @@ -185,6 +201,9 @@ function(assert)
fail("expected test" ARGV2 "not to exist")
return()
elseif(ARGV1 STREQUAL "DEFINED")
# Unset this to prevent the value from being formatted.
unset("${ARGV2}")

fail("expected variable" ARGV2 "not to be defined")
return()
elseif(ARGV1 STREQUAL "EXISTS")
Expand All @@ -211,78 +230,26 @@ function(assert)
endif()
else()
if(ARGV1 STREQUAL "IN_LIST")
if(DEFINED "${ARGV0}")
fail("expected string" "${ARGV0}" "of variable" ARGV0
"to exist in" "${ARGV2}" "of variable" ARGV2)
else()
fail("expected string" ARGV0
"to exist in" "${ARGV2}" "of variable" ARGV2)
endif()
fail("expected string" ARGV0 "to exist in" ARGV2)
return()
elseif(ARGV1 STREQUAL "MATCHES")
if(DEFINED "${ARGV0}")
fail("expected string" "${ARGV0}" "of variable" ARGV0
"to match" ARGV2)
else()
fail("expected string" ARGV0 "to match" ARGV2)
endif()
fail("expected string" ARGV0 "to match" ARGV2)
return()
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()
fail("expected string" ARGV0 "to be equal to" ARGV2)
endif()
endif()
fail("expected string" ARGV0 "to be equal to" ARGV2)
return()
endif()
endif()
elseif(ARGC EQUAL 4)
if(ARGV0 STREQUAL "NOT")
if(ARGV2 STREQUAL "IN_LIST")
if(DEFINED "${ARGV1}")
fail("expected string" "${ARGV1}" "of variable" ARGV1
"not to exist in" "${ARGV3}" "of variable" ARGV3)
else()
fail("expected string" ARGV1
"not to exist in" "${ARGV3}" "of variable" ARGV3)
endif()
fail("expected string" ARGV1 "not to exist in" ARGV3)
return()
elseif(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()
fail("expected string" ARGV1 "not to match" ARGV3)
return()
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()
fail("expected string" ARGV1 "not to be equal to" ARGV3)
endif()
endif()
fail("expected string" ARGV1 "not to be equal to" ARGV3)
return()
endif()
endif()
Expand Down
46 changes: 2 additions & 44 deletions test/assert.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -417,48 +417,6 @@ section("string equality condition assertions")
endsection()
endsection()

section("given a string and a variable")
section("it should assert string equality conditions")
assert("some string" STREQUAL STRING_VAR)
assert(NOT "some string" STREQUAL OTHER_STRING_VAR)
endsection()

section("it should fail to assert string equality conditions")
assert_fatal_error(
CALL assert NOT "some string" STREQUAL STRING_VAR
MESSAGE "expected string:\n some string\n"
"not to be equal to string:\n some string\n"
"of variable:\n STRING_VAR")

assert_fatal_error(
CALL assert "some string" STREQUAL OTHER_STRING_VAR
MESSAGE "expected string:\n some string\n"
"to be equal to string:\n some other string\n"
"of variable:\n OTHER_STRING_VAR")
endsection()
endsection()

section("given a variable and a string")
section("it should assert string equality conditions")
assert(STRING_VAR STREQUAL "some string")
assert(NOT STRING_VAR STREQUAL "some other string")
endsection()

section("it should fail to assert string equality conditions")
assert_fatal_error(
CALL assert NOT STRING_VAR STREQUAL "some string"
MESSAGE "expected string:\n some string\n"
"of variable:\n STRING_VAR\n"
"not to be equal to:\n some string")

assert_fatal_error(
CALL assert STRING_VAR STREQUAL "some other string"
MESSAGE "expected string:\n some string\n"
"of variable:\n STRING_VAR\n"
"to be equal to:\n some other string")
endsection()
endsection()

section("given variables")
section("it should assert string equality conditions")
assert(STRING_VAR STREQUAL STRING_VAR)
Expand All @@ -470,14 +428,14 @@ section("string equality condition assertions")
CALL assert NOT STRING_VAR STREQUAL STRING_VAR
MESSAGE "expected string:\n some string\n"
"of variable:\n STRING_VAR\n"
"not to be equal to string:\n some string\n"
"not to be equal to:\n some string\n"
"of variable:\n STRING_VAR")

assert_fatal_error(
CALL assert STRING_VAR STREQUAL OTHER_STRING_VAR
MESSAGE "expected string:\n some string\n"
"of variable:\n STRING_VAR\n"
"to be equal to string:\n some other string\n"
"to be equal to:\n some other string\n"
"of variable:\n OTHER_STRING_VAR")
endsection()
endsection()
Expand Down
30 changes: 27 additions & 3 deletions test/fail.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ endsection()

set(SINGLE "single line variable")
set(MULTIPLE "multiple\nlines\nvariable")
set(CONTAINS_SINGLE SINGLE)

section("given variables")
section("it should fail with a formatted fatal error message")
Expand All @@ -27,9 +28,18 @@ section("given variables")
CALL fail MULTIPLE
MESSAGE "multiple\nlines\nvariable")

assert_fatal_error(
CALL fail CONTAINS_SINGLE
MESSAGE "single line variable\nof variable:\n SINGLE")

assert_fatal_error(
CALL fail SINGLE MULTIPLE
MESSAGE "single line variable\nmultiple\nlines\nvariable")

assert_fatal_error(
CALL fail SINGLE MULTIPLE CONTAINS_SINGLE
MESSAGE "single line variable\nmultiple\nlines\nvariable\n"
"single line variable\nof variable:\n SINGLE")
endsection()
endsection()

Expand All @@ -52,13 +62,27 @@ section("given strings and variables")
MESSAGE "multiple\nlines\nvariable\nmultiple\nlines\nstring")

assert_fatal_error(
CALL fail "single line string" "multiple\nlines\nstring" SINGLE MULTIPLE
CALL fail "single line string" CONTAINS_SINGLE
MESSAGE "single line string:\n single line variable\n"
"of variable:\n SINGLE")

assert_fatal_error(
CALL fail CONTAINS_SINGLE "single line string"
MESSAGE "single line variable\nof variable:\n SINGLE\n"
"single line string")

assert_fatal_error(
CALL fail "single line string" "multiple\nlines\nstring"
SINGLE MULTIPLE CONTAINS_SINGLE
MESSAGE "single line string\nmultiple\nlines\nstring:\n"
" single line variable\n multiple\n lines\n variable")
" single line variable\n multiple\n lines\n variable\n"
" single line variable\nof variable:\n SINGLE")

assert_fatal_error(
CALL fail SINGLE MULTIPLE "single line string" "multiple\nlines\nstring"
CALL fail SINGLE MULTIPLE CONTAINS_SINGLE
"single line string" "multiple\nlines\nstring"
MESSAGE "single line variable\nmultiple\nlines\nvariable\n"
"single line variable\nof variable:\n SINGLE\n"
"single line string\nmultiple\nlines\nstring")

assert_fatal_error(
Expand Down

0 comments on commit 78ca271

Please sign in to comment.