#########################################################
# Unittests on internal classes of the library

SET(TESTNAME "Test_CPP_Bindings")

file(GLOB GTEST_SRC_FILES "${CMAKE_SOURCE_DIR}/Libraries/googletest/Source/*cc")

set(SRCS_UNITTEST
	./Source/AllTests.cpp
	./Source/Attachments.cpp
	./Source/BaseMaterialGroup.cpp
	./Source/BeamLattice.cpp
	./Source/BeamSets.cpp
	./Source/BuildItems.cpp
	./Source/ColorGroup.cpp
	./Source/CompositeMaterials.cpp
	./Source/EncryptionMethods.cpp
	./Source/ImplicitNodes.test.cpp
	./Source/MeshObject.cpp
	./Source/MetaData.cpp
	./Source/MetaDataGroup.cpp
	./Source/MergeModels.cpp
	./Source/Model.cpp
	./Source/MultiProperties.cpp
	./Source/Object.cpp
	./Source/Outbox.cpp
	./Source/ProductionExtension.cpp
	./Source/ProgressCallback.cpp
	./Source/Properties.cpp
	./Source/Reader.cpp
	./Source/SecureContent.cpp
	./Source/Slice.cpp
	./Source/SliceStack.cpp
	./Source/UnitTest_Utilities.cpp
	./Source/UnitTest_EncryptionUtils.cpp
	./Source/Writer.cpp
	./Source/TextureProperty.cpp
	./Source/TextureResources.cpp
	./Source/UnitTest_Utilities.cpp
	./Source/v093.cpp
	./Source/Volumetric.cpp
	./Source/ZipBuffer.cpp
	./Source/Wrapper.cpp
	./Source/Writer.cpp
	./Source/TriangleSets.cpp
)

set(CMAKE_CURRENT_BINARY_DIR ${CMAKE_BINARY_DIR})
add_executable(${TESTNAME} ${SRCS_UNITTEST} ${GTEST_SRC_FILES})

set(STARTUPPROJECT ${TESTNAME})

target_include_directories(${TESTNAME} PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}/Include
	${CMAKE_SOURCE_DIR}/Libraries/googletest/Include
	${CMAKE_CURRENT_SOURCE_DIR}/../../Libraries/libressl/include
	${CMAKE_CURRENT_SOURCE_DIR_AUTOGENERATED}/Bindings/Cpp
	)

if (USE_INCLUDED_ZLIB)
    file(GLOB TEST_ZLIB_FILES "${CMAKE_SOURCE_DIR}/Libraries/zlib/Source/*.c")
    target_sources(${TESTNAME} PRIVATE ${TEST_ZLIB_FILES})
    target_include_directories(${TESTNAME} PRIVATE ${CMAKE_SOURCE_DIR}/Libraries/zlib/Include)
else()
    find_package(ZLIB REQUIRED)
    target_link_libraries(${TESTNAME} PRIVATE ZLIB::ZLIB)
endif()

# pthreads Needed for googletest	
if (LINUX)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(${TESTNAME} PRIVATE Threads::Threads)
endif()

if (USE_INCLUDED_SSL)
	target_include_directories(${TESTNAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../Libraries/libressl/include)
	target_link_libraries(${TESTNAME} PRIVATE ${PROJECT_NAME} ssl crypto)
else()
	find_package(OpenSSL REQUIRED)
	target_include_directories(${TESTNAME} PRIVATE ${OPENSSL_INCLUDE_DIR})
	target_link_libraries(${TESTNAME} PRIVATE ${PROJECT_NAME} ${OPENSSL_LIBRARIES})
endif()

if (WIN32)
target_link_libraries(${TESTNAME} PRIVATE ws2_32)
endif()


set_target_properties(${TESTNAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/")

# Enable fine-grained GoogleTest discovery instead of one monolithic test entry
include(GoogleTest)
gtest_discover_tests(
	${TESTNAME}
	WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
	DISCOVERY_TIMEOUT 60
	# Avoid re-running discovery on every build unless sources change
	PROPERTIES VS_TEST_CATEGORY "CPP_Bindings"
)

find_program(VALGRIND "valgrind")
if(VALGRIND)
        add_custom_target(lib3mf_memcheck
                COMMAND "${VALGRIND}" --tool=memcheck --leak-check=full --error-exitcode=1 --suppressions=${CMAKE_SOURCE_DIR}/Tests/memcheck/conditional.valgrind.supp  $<TARGET_FILE:${TESTNAME}>)
endif()

set(STARTUPPROJECT ${STARTUPPROJECT} PARENT_SCOPE)
