diff --git a/.gitignore b/.gitignore index a3bdea5..14e65ac 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ CMakeCache.txt CMakeFiles/ cmake_install.cmake dist/ -build/ +build*/ # User-specific files diff --git a/CMakeLists.txt b/CMakeLists.txt index a85e348..6c04f72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,8 +8,6 @@ project( ) option(SIMPLEINI_USE_SYSTEM_GTEST "Use system GoogleTest dependency" OFF) -option(SIMPLEINI_BUILD_TESTS "Build tests" ON) -option(SIMPLEINI_BUILD_EXAMPLES "Build examples" ON) # disable in-source builds if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) @@ -17,14 +15,10 @@ if(CMAKE_SOURCE_DIR STREQUAL CMAKE_BINARY_DIR) endif() set(EXPORT_NAMESPACE "${PROJECT_NAME}::") -set(SIMPLEINI_HEADERS SimpleIni.h) - -# ConvertUTF files are only needed on Windows for SI_CONVERT_WIN32 -if(WIN32) - list(APPEND SIMPLEINI_HEADERS ConvertUTF.h) - set(SIMPLEINI_SOURCES ConvertUTF.c) -endif() +# SimpleIni is a header-only library. The ConvertUTF.c/h files in this repository +# are only required if users define SI_CONVERT_GENERIC. In that case, users must +# manually copy and compile ConvertUTF.c into their own projects. add_library(${PROJECT_NAME} INTERFACE) add_library(${EXPORT_NAMESPACE}${PROJECT_NAME} ALIAS ${PROJECT_NAME}) target_include_directories(${PROJECT_NAME} INTERFACE @@ -32,14 +26,6 @@ target_include_directories(${PROJECT_NAME} INTERFACE $ ) -# On Windows, add ConvertUTF.c as a source file -if(WIN32) - target_sources(${PROJECT_NAME} INTERFACE - $ - $ - ) -endif() - include(GNUInstallDirs) include(CMakePackageConfigHelpers) @@ -49,28 +35,21 @@ write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Conf ) configure_package_config_file(${PROJECT_NAME}Config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake - INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME} + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} ) -install(FILES ${SIMPLEINI_HEADERS} +install(FILES SimpleIni.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) -# Install ConvertUTF.c on Windows (needed for SI_CONVERT_WIN32) -if(WIN32) - install(FILES ConvertUTF.c - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} - ) -endif() - install(TARGETS ${PROJECT_NAME} EXPORT SimpleIniTargets ) install(EXPORT SimpleIniTargets FILE SimpleIniTargets.cmake - NAMESPACE SimpleIni:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/SimpleIni + NAMESPACE ${EXPORT_NAMESPACE} + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} ) install(FILES @@ -79,8 +58,8 @@ install(FILES DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} ) -# only build tests when top level and testing enabled -if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND SIMPLEINI_BUILD_TESTS) +# only build tests when top level +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) include(CTest) if(BUILD_TESTING) add_subdirectory(tests) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 27762d9..b24747d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -35,6 +35,8 @@ set(TEST_SOURCES ts-edgecases.cpp ts-multiline.cpp ts-casesensitivity.cpp + ts-generic.cpp + ${CMAKE_SOURCE_DIR}/ConvertUTF.c ) # ts-wchar.cpp uses wchar_t which is primarily for Windows diff --git a/tests/ts-generic.cpp b/tests/ts-generic.cpp new file mode 100644 index 0000000..ecdf7cc --- /dev/null +++ b/tests/ts-generic.cpp @@ -0,0 +1,138 @@ +#define SI_CONVERT_GENERIC +#include "../SimpleIni.h" +#include "gtest/gtest.h" + +// Test SI_CONVERT_GENERIC with char interface +class TestGenericChar : public ::testing::Test { +protected: + void SetUp() override; +protected: + CSimpleIniA ini; +}; + +void TestGenericChar::SetUp() { + ini.SetUnicode(); + SI_Error err = ini.LoadFile("tests.ini"); + ASSERT_EQ(err, SI_OK); +} + +TEST_F(TestGenericChar, TestSectionAKeyAValA) { + const char* result = ini.GetValue("section1", "key1"); + ASSERT_STREQ(result, "value1"); +} + +TEST_F(TestGenericChar, TestSectionAKeyAValU) { + const char tesuto2[] = u8"テスト2"; + const char* result = ini.GetValue("section2", "test2"); + ASSERT_STREQ(result, tesuto2); +} + +TEST_F(TestGenericChar, TestSectionAKeyUValA) { + const char tesuto[] = u8"テスト"; + const char* result = ini.GetValue("section2", tesuto); + ASSERT_STREQ(result, "test"); +} + +TEST_F(TestGenericChar, TestSectionAKeyUValU) { + const char tesuto2[] = u8"テスト2"; + const char tesutoni[] = u8"テスト二"; + const char* result = ini.GetValue("section2", tesuto2); + ASSERT_STREQ(result, tesutoni); +} + +TEST_F(TestGenericChar, TestSectionUKeyAValA) { + const char kensa[] = u8"検査"; + const char* result = ini.GetValue(kensa, "key2"); + ASSERT_STREQ(result, "value2"); +} + +TEST_F(TestGenericChar, TestSectionUKeyAValU) { + const char kensa[] = u8"検査"; + const char tesuto2[] = u8"テスト2"; + const char* result = ini.GetValue(kensa, "test2"); + ASSERT_STREQ(result, tesuto2); +} + +TEST_F(TestGenericChar, TestSectionUKeyUValA) { + const char kensa[] = u8"検査"; + const char tesuto[] = u8"テスト"; + const char* result = ini.GetValue(kensa, tesuto); + ASSERT_STREQ(result, "test"); +} + +TEST_F(TestGenericChar, TestSectionUKeyUValU) { + const char kensa[] = u8"検査"; + const char tesuto2[] = u8"テスト2"; + const char tesutoni[] = u8"テスト二"; + const char* result = ini.GetValue(kensa, tesuto2); + ASSERT_STREQ(result, tesutoni); +} + +#ifdef _WIN32 +// Test SI_CONVERT_GENERIC with wchar_t interface (Windows only) +// On non-Windows platforms, wchar_t support with SI_CONVERT_GENERIC doesn't work the same way +class TestGenericWide : public ::testing::Test { +protected: + void SetUp() override; +protected: + CSimpleIniW ini; +}; + +void TestGenericWide::SetUp() { + ini.SetUnicode(); + SI_Error err = ini.LoadFile("tests.ini"); + ASSERT_EQ(err, SI_OK); +} + +TEST_F(TestGenericWide, TestSectionAKeyAValA) { + const wchar_t* result = ini.GetValue(L"section1", L"key1"); + ASSERT_STREQ(result, L"value1"); +} + +TEST_F(TestGenericWide, TestSectionAKeyAValU) { + const wchar_t tesuto2[] = L"テスト2"; + const wchar_t* result = ini.GetValue(L"section2", L"test2"); + ASSERT_STREQ(result, tesuto2); +} + +TEST_F(TestGenericWide, TestSectionAKeyUValA) { + const wchar_t tesuto[] = L"テスト"; + const wchar_t* result = ini.GetValue(L"section2", tesuto); + ASSERT_STREQ(result, L"test"); +} + +TEST_F(TestGenericWide, TestSectionAKeyUValU) { + const wchar_t tesuto2[] = L"テスト2"; + const wchar_t tesutoni[] = L"テスト二"; + const wchar_t* result = ini.GetValue(L"section2", tesuto2); + ASSERT_STREQ(result, tesutoni); +} + +TEST_F(TestGenericWide, TestSectionUKeyAValA) { + const wchar_t kensa[] = L"検査"; + const wchar_t* result = ini.GetValue(kensa, L"key2"); + ASSERT_STREQ(result, L"value2"); +} + +TEST_F(TestGenericWide, TestSectionUKeyAValU) { + const wchar_t kensa[] = L"検査"; + const wchar_t tesuto2[] = L"テスト2"; + const wchar_t* result = ini.GetValue(kensa, L"test2"); + ASSERT_STREQ(result, tesuto2); +} + +TEST_F(TestGenericWide, TestSectionUKeyUValA) { + const wchar_t kensa[] = L"検査"; + const wchar_t tesuto[] = L"テスト"; + const wchar_t* result = ini.GetValue(kensa, tesuto); + ASSERT_STREQ(result, L"test"); +} + +TEST_F(TestGenericWide, TestSectionUKeyUValU) { + const wchar_t kensa[] = L"検査"; + const wchar_t tesuto2[] = L"テスト2"; + const wchar_t tesutoni[] = L"テスト二"; + const wchar_t* result = ini.GetValue(kensa, tesuto2); + ASSERT_STREQ(result, tesutoni); +} +#endif // _WIN32