blob: 86ff8720cb42a1ca56172867e42ac1c8e5656dc5 [file]
cmake_minimum_required(VERSION 3.20)
function(_json_format_path out_var)
set(path_str "<root>")
set(segments ${ARGN})
if(segments)
set(path_str "")
foreach(seg IN LISTS segments)
if(seg MATCHES "^[0-9]+$")
string(APPEND path_str "[${seg}]")
else()
if(path_str STREQUAL "")
string(APPEND path_str "${seg}")
else()
string(APPEND path_str ".${seg}")
endif()
endif()
endforeach()
endif()
set(${out_var} "${path_str}" PARENT_SCOPE)
endfunction()
macro(_json_fail_at type actual expected path_segments)
_json_format_path(display_path ${path_segments})
set(msg "JSON Mismatch at '${display_path}'\n")
string(APPEND msg " Failure: ${type}\n")
string(APPEND msg " Actual: ${actual}\n")
string(APPEND msg " Expected: ${expected}")
set(RunCMake_TEST_FAILED "${msg}" PARENT_SCOPE)
endmacro()
function(expect_object actual_json expected_var)
if(NOT DEFINED "${expected_var}")
message(FATAL_ERROR "Test Usage Error: Variable '${expected_var}' is not defined.")
endif()
set(expected_node "${${expected_var}}")
set(path_segments ${ARGN})
if(path_segments)
string(JSON actual_node ERROR_VARIABLE err GET "${actual_json}" ${path_segments})
if(err)
_json_fail_at("Path Not Found" "<missing>" "${path_segments}" "")
return()
endif()
else()
set(actual_node "${actual_json}")
endif()
string(JSON actual_type TYPE "${actual_node}")
string(JSON expected_type TYPE "${expected_node}")
set(pattern "${expected_node}")
set(fail_type "Partial Equality Mismatch")
if(actual_type STREQUAL "ARRAY" AND expected_type STREQUAL "OBJECT")
set(pattern "[ ${expected_node} ]")
set(fail_type "Object Not Found in Array")
endif()
string(JSON is_equal PARTIAL_EQUAL "${pattern}" "${actual_node}")
if(NOT is_equal)
_json_fail_at("${fail_type}" "${actual_node}" "${expected_node}" "${path_segments}")
endif()
endfunction()
function(expect_value actual_json expected_val)
set(path_segments ${ARGN})
string(JSON actual_val ERROR_VARIABLE err GET "${actual_json}" ${path_segments})
if(err)
_json_fail_at("Path Not Found" "<missing>" "${path_segments}" "")
return()
endif()
if(NOT "${actual_val}" STREQUAL "${expected_val}")
_json_fail_at("Value Mismatch" "${actual_val}" "${expected_val}" "${path_segments}")
endif()
endfunction()
function(expect_array actual_json expected_len)
set(path_segments ${ARGN})
string(JSON actual_type ERROR_VARIABLE err TYPE "${actual_json}" ${path_segments})
if(err)
_json_fail_at("Path Not Found" "<missing>" "${path_segments}" "")
return()
endif()
if(NOT actual_type STREQUAL "ARRAY")
_json_fail_at("Type Mismatch" "${actual_type}" "ARRAY" "${path_segments}")
return()
endif()
string(JSON actual_len LENGTH "${actual_json}" ${path_segments})
if(NOT actual_len EQUAL expected_len)
_json_fail_at("Array Length" "${actual_len}" "${expected_len}" "${path_segments}")
endif()
endfunction()