diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 09dc0a8..5016fc7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,10 @@ jobs: run: cmake --build build --config=Debug - name: build release run: cmake --build build --config=Release + - name: test debug + run: cd build; ctest -C Debug + - name: test release + run: cd build; ctest -C Release linux-clang: runs-on: ubuntu-latest steps: @@ -33,6 +37,10 @@ jobs: run: cmake --build build --config=Debug - name: build release run: cmake --build build --config=Release + - name: test debug + run: cd build; ctest -C Debug + - name: test release + run: cd build; ctest -C Release windows-vs22: runs-on: windows-latest steps: @@ -43,6 +51,10 @@ jobs: run: cmake --build build --config=Debug - name: build release run: cmake --build build --config=Release + - name: test debug + run: cd build; ctest -C Debug + - name: test release + run: cd build; ctest -C Release windows-clang: runs-on: windows-latest steps: @@ -55,3 +67,7 @@ jobs: run: cmake --build build --config=Debug - name: build release run: cmake --build build --config=Release + - name: test debug + run: cd build; ctest -C Debug + - name: test release + run: cd build; ctest -C Release diff --git a/CMakeLists.txt b/CMakeLists.txt index 4602219..2af0924 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.24) set(project_name Tkge) project(${project_name}) +option(TKGE_BUILD_TESTS "Build Tkge unit tests" ${PROJECT_IS_TOP_LEVEL}) + set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) @@ -12,3 +14,8 @@ add_subdirectory(Ext) add_subdirectory(Lib) add_subdirectory(App) + +if(TKGE_BUILD_TESTS) + enable_testing() + add_subdirectory(Tests) +endif() diff --git a/Lib/Include/Tkge/Assets/IAsset.hpp b/Lib/Include/Tkge/Assets/IAsset.hpp index 7eb242d..4b6b2bd 100644 --- a/Lib/Include/Tkge/Assets/IAsset.hpp +++ b/Lib/Include/Tkge/Assets/IAsset.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace Tkge::Assets diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt new file mode 100644 index 0000000..9fc6d23 --- /dev/null +++ b/Tests/CMakeLists.txt @@ -0,0 +1,21 @@ +project(${project_name}-Tests) + +file(GLOB_RECURSE sources LIST_DIRECTORIES false "*.cpp") + +if(NOT "${sources}" STREQUAL "") + add_executable(${PROJECT_NAME}) + + target_link_libraries(${PROJECT_NAME} PRIVATE + ${project_name}::Lib + klib::klib-test-main + ) + + target_include_directories(${PROJECT_NAME} PRIVATE + "${CMAKE_CURRENT_SOURCE_DIR}" , + ../Lib/Src + ) + + target_sources(${PROJECT_NAME} PRIVATE ${sources}) + + add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME}) +endif() diff --git a/Tests/TestAssetLoader.cpp b/Tests/TestAssetLoader.cpp new file mode 100644 index 0000000..7615941 --- /dev/null +++ b/Tests/TestAssetLoader.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +namespace +{ + using namespace Tkge; + + struct TempFile + { + TempFile(const TempFile&) = delete; + TempFile(TempFile&&) = delete; + TempFile& operator=(const TempFile&) = delete; + TempFile& operator=(TempFile&&) = delete; + + explicit TempFile(std::string path, const std::string_view text) : _path(std::move(path)) + { + auto file = std::ofstream{_path}; + ASSERT(file.is_open()); + file << text; + } + + ~TempFile() { std::filesystem::remove(_path); } + + [[nodiscard]] const std::string& GetPath() const { return _path; } + + private: + std::string _path; + }; + + TEST(TextAsset_Load) + { + static constexpr std::string_view Text{"Hello World"}; + + const auto file = TempFile{"Test.txt", Text}; + + auto stream = Assets::ReadonlyByteStream{file.GetPath()}; + EXPECT(stream.GetStreamSize() > 0); + + auto asset = Assets::TextAsset{}; + EXPECT(asset.Load(std::move(stream))); + + const auto& text = asset.Text(); + EXPECT(text == Text); + } +} // namespace