diff --git a/README.md b/README.md index d43132c..273b800 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,9 @@ This macro throws a fatal error message formatted from the given ``. 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 diff --git a/cmake/Assertion.cmake b/cmake/Assertion.cmake index 071dde6..0c0489f 100644 --- a/cmake/Assertion.cmake +++ b/cmake/Assertion.cmake @@ -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 ":") @@ -106,6 +121,7 @@ macro(fail FIRST_LINE) endif() endforeach() + # Throw a fatal error with the formatted message. message(FATAL_ERROR "${MESSAGE}") endmacro() @@ -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") @@ -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() diff --git a/test/assert.cmake b/test/assert.cmake index e2e15ca..65e0b56 100644 --- a/test/assert.cmake +++ b/test/assert.cmake @@ -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) @@ -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() diff --git a/test/fail.cmake b/test/fail.cmake index 8b8182d..b05d61b 100644 --- a/test/fail.cmake +++ b/test/fail.cmake @@ -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") @@ -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() @@ -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(