# ====================  Define your project name (edit) ========================
set(project_name "iotmi_data_model_handler")

# =====================  Create your mock here  (edit)  ========================

# list the files to mock here
list(APPEND mock_list
    ${IOTMI_EMBEDDED_C_TOP_DIR}/components/mqtt_adapter/source/include/iotmi_mqtt_adapter.h
)

# list the directories your mocks need
list(APPEND mock_include_list
    ${IOTMI_EMBEDDED_C_TOP_DIR}/platform/include
    ${IOTMI_EMBEDDED_C_TOP_DIR}/components/mqtt_adapter/source/include
    ${IOTMI_EMBEDDED_C_TOP_DIR}/commonDependencies/core_mqtt/coreMQTT/source/include
    ${IOTMI_EMBEDDED_C_TOP_DIR}/commonDependencies/core_mqtt/coreMQTT/source/interface
)

#list the definitions of your mocks to control what to be included
list(APPEND mock_define_list)

# =====================  Embed test cases  ========================

# Define test case file path
set(MQTT_TEST_CASES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/test_cases/mqtt_tests.json")

# Make CMake re-run configure_file if the JSON file changes
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${MQTT_TEST_CASES_FILE}")

# Read the JSON test case file
file(READ "${MQTT_TEST_CASES_FILE}" EMBEDDED_MQTT_TESTS_JSON)

# Escape newlines for C string (ESCAPE_QUOTES will handle the quotes)
string(REPLACE "\n" "\\n" EMBEDDED_MQTT_TESTS_JSON "${EMBEDDED_MQTT_TESTS_JSON}")

# Configure the header file with the embedded JSON
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/embedded_test_cases.h.in"
    "${CMAKE_CURRENT_BINARY_DIR}/embedded_test_cases.h"
    @ONLY
    ESCAPE_QUOTES  # Use CMake's built-in quote escaping
)

# ================= Create the library under test here (edit) ==================

# list the files you would like to test here
list(APPEND real_source_files
    ${IOTMI_EMBEDDED_C_TOP_DIR}/src/iotmi_device_data_model_handler.c
    ${IOTMI_EMBEDDED_C_TOP_DIR}/src/iotmi_device_agent.c
    ${IOTMI_EMBEDDED_C_TOP_DIR}/src/iotmi_device_message_builders.c
    ${IOTMI_EMBEDDED_C_TOP_DIR}/src/iotmi_string_utils.c
    # We need to include cJSON here because we wanted to disable optimization
    # when compiling cJSON. So we could not use the built cJSON library directly.
    ${CJSON_SOURCE_DIR}/cJSON.c
)
# list the directories the module under test includes
list(APPEND real_include_directories
    ${IOTMI_EMBEDDED_C_TOP_DIR}/src/private
    ${IOTMI_EMBEDDED_C_TOP_DIR}/include
    ${IOTMI_EMBEDDED_C_TOP_DIR}/components/sdk_log/source/include/logging
    ${IOTMI_EMBEDDED_C_TOP_DIR}/components/sdk_version/source/generated_include
    "$<TARGET_PROPERTY:cjson,INTERFACE_INCLUDE_DIRECTORIES>"
)

# list the read library link libraries
list(APPEND real_link_libraries iotmi::pal)

# =====================  Create UnitTest Code here (edit)  =====================

# list the directories your test needs to include
list(APPEND test_include_directories 
    .
    ${CMAKE_CURRENT_BINARY_DIR}  # Include directory for generated embedded_test_cases.h
)

# Add compiler flags for backtrace support and debug symbols
add_compile_options(-O0 -fno-omit-frame-pointer -fno-inline -g)

# This is necesary for backtrace_symbols() to show function names
add_link_options(-rdynamic)

# =============================  (end edit)  ===================================

set(mock_name "${project_name}_mock")
set(real_name "${project_name}_real")

create_mock_list(${mock_name}
                "${mock_list}"
                "${CMAKE_CURRENT_SOURCE_DIR}/cmock_config.yml"
                "${mock_include_list}"
                "${mock_define_list}"
)

create_real_library(${real_name}
                    "${real_source_files}"
                    "${real_include_directories}"
                    ""
)

target_compile_definitions(${real_name}
  PRIVATE
  NDEBUG
  LIBRARY_LOG_LEVEL=LOG_NONE
  # UNCOMMENT below line helps to print static function names in the
  # stacktrace. This is useful when finding the occurrence of the
  # malloc.
  # static=
)

target_link_libraries( ${real_name}
    PUBLIC
        ${real_link_libraries}
    PRIVATE
        ${mock_name}
)

# Helper library for common test functions
add_library(${project_name}_test_helpers STATIC
    iotmi_data_model_handler_test_helpers.c
)

target_include_directories(${project_name}_test_helpers PUBLIC
    .
    ${real_include_directories}
)

target_link_libraries(${project_name}_test_helpers
    PUBLIC
        ${real_name}
        ${mock_name}
)

list(APPEND ${project_name}_utest_link_list ${mock_name} ${real_name} ${project_name}_test_helpers)
list(APPEND ${project_name}_utest_dep_list ${real_name} ${project_name}_test_helpers)

# Function tests
set(utest_name "${project_name}_function_utest")
set(utest_source "iotmi_data_model_handler_function_tests.c")
create_test(${utest_name}
            ${utest_source}
            "${${project_name}_utest_link_list}"
            "${${project_name}_utest_dep_list}"
            "${test_include_directories}"
)

# Set output directory for function tests
set_target_properties(${utest_name} PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/tests/unit-test")

# MQTT tests
set(utest_name "${project_name}_mqtt_utest")
set(utest_source "iotmi_data_model_handler_mqtt_tests.c")

create_test(${utest_name}
            ${utest_source}
            "${${project_name}_utest_link_list}"
            "${${project_name}_utest_dep_list}"
            "${test_include_directories}"
)

# Set output directory for MQTT tests
set_target_properties(${utest_name} PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/tests/unit-test")

file(GLOB_RECURSE FORMAT_PROJECT_CXX_SOURCE_FILES *.c *.h )
set( FORMAT_PROJECT_NAME "iotmi_data_model_handler_unit_test" )
include( ${IOTMI_EMBEDDED_C_TOP_DIR}/cmake/clang-cxx-dev-tools.cmake )
