Skip to content

Commit

Permalink
tests: yaml: add test for generator expressions
Browse files Browse the repository at this point in the history
Add tests to the YAML suite for generator expressions. The test checks
that generator expressions are handled as expected at different stages:

 - immediately after yaml_save(), the file must contain only non-genex
   entries;
 - at a later time, after the target has been built, the file must
   contain all the expanded genex values.

Signed-off-by: Luca Burelli <[email protected]>
  • Loading branch information
pillo79 committed Jan 7, 2025
1 parent b45b169 commit 0600b1c
Showing 1 changed file with 114 additions and 1 deletion.
115 changes: 114 additions & 1 deletion tests/cmake/yaml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,108 @@ function(test_setting_empty_list)
endforeach()
endfunction()

function(test_setting_genexes)
set(file ${CMAKE_BINARY_DIR}/test_setting_genexes.yaml)
yaml_create(FILE ${file}
NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
)

set(new_value "fixed string")
yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
KEY cmake test set key-string
VALUE ${new_value}
)

set_property(TARGET app PROPERTY expanding_str "expanded genex")
set(new_value "$<TARGET_PROPERTY:app,expanding_str>")
yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
KEY cmake test set key-string-genex
GENEX VALUE ${new_value}
)

# create the list by appending in several steps to test conversion
# from JSON list to genex stringified list
yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
KEY cmake test set key-list-string-genex
LIST "A"
)
set_property(TARGET app PROPERTY expanding_list "list" "of")
yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
KEY cmake test set key-list-string-genex
APPEND GENEX LIST "new" "$<TARGET_PROPERTY:app,expanding_list>"
)
yaml_set(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
KEY cmake test set key-list-string-genex
APPEND LIST "strings"
)

yaml_save(NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create TARGET app)

# Read-back the yaml immediately and verify the genex values are NOT present
# (genexes are expanded at generation time)
yaml_load(FILE ${file}
NAME ${CMAKE_CURRENT_FUNCTION}_readback
)

yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string)
set(expected "fixed string")

test_assert(TEST ${expected} STREQUAL ${readback}
COMMENT "yaml key value does not match expectation."
)

yaml_length(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string-genex)
set(expected cmake-test-set-key-list-string-genex-NOTFOUND)

test_assert(TEST ${expected} STREQUAL ${readback}
COMMENT "Expected -NOTFOUND, but something was found."
)

yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string-genex)
set(expected cmake-test-set-key-string-genex-NOTFOUND)

test_assert(TEST ${expected} STREQUAL ${readback}
COMMENT "Expected -NOTFOUND, but something was found."
)
endfunction()

function(test_verify_genexes)
set(file ${CMAKE_BINARY_DIR}/test_setting_genexes.yaml)
yaml_load(FILE ${file}
NAME ${CMAKE_CURRENT_FUNCTION}_readback
)

yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string)
set(expected "fixed string")

test_assert(TEST ${expected} STREQUAL ${readback}
COMMENT "yaml key value does not match expectation."
)

set(expected "expanded genex")
yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-string-genex)

test_assert(TEST ${expected} STREQUAL ${readback}
COMMENT "new yaml value does not match readback value."
)

set(expected "A" "new" "list" "of" "strings")
list(LENGTH expected exp_len)

yaml_get(readback NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string-genex)
yaml_length(act_len NAME ${CMAKE_CURRENT_FUNCTION}_readback KEY cmake test set key-list-string-genex)

test_assert(TEST ${exp_len} EQUAL ${act_len}
COMMENT "yaml list length does not match expectation."
)

foreach(a e IN ZIP_LISTS readback expected)
test_assert(TEST "${e}" STREQUAL "${a}"
COMMENT "list values mismatch."
)
endforeach()
endfunction()

function(test_set_remove_int)
yaml_create(FILE ${CMAKE_BINARY_DIR}/${CMAKE_CURRENT_FUNCTION}_test_create.yaml
NAME ${CMAKE_CURRENT_FUNCTION}_yaml-create
Expand Down Expand Up @@ -465,9 +567,20 @@ test_set_remove_int()

test_fail_missing_filename()

# Generator expressions cannot be tested immediately, since they are expanded
# at project generation time. The script mode re-run is delayed until after
# the associated target has been built, and the verification is performed at
# that time.
if(NOT CMAKE_SCRIPT_MODE_FILE)
# create a file containing genexes
test_setting_genexes()

# Spawn a new CMake instance to re-run the whole test suite in script mode
execute_process(
# and verify the genex values once the associated target is built.
add_custom_command(TARGET app POST_BUILD
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_LIST_FILE}
)
else()
# verify the contents of the created genex file
test_verify_genexes()
endif()

0 comments on commit 0600b1c

Please sign in to comment.