Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Add unit tests foundation and TextAsset unit test #29

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -12,3 +14,8 @@ add_subdirectory(Ext)

add_subdirectory(Lib)
add_subdirectory(App)

if(TKGE_BUILD_TESTS)
enable_testing()
add_subdirectory(Tests)
endif()
1 change: 1 addition & 0 deletions Lib/Include/Tkge/Assets/IAsset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cstddef>
#include <memory>
#include <span>
#include <stdexcept>
#include <string>

namespace Tkge::Assets
Expand Down
21 changes: 21 additions & 0 deletions Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
47 changes: 47 additions & 0 deletions Tests/TestAssetLoader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <Tkge/Assets/TextAsset.hpp>
#include <klib/unit_test.hpp>
#include <filesystem>
#include <fstream>

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