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

Commit 034fbf0

Browse files
authored
Add unit tests foundation and TextAsset unit test (#29)
* Add unit tests foundation and `TextAsset` unit test * Add missing include
1 parent 41b438b commit 034fbf0

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

.github/workflows/ci.yml

+16
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ jobs:
2121
run: cmake --build build --config=Debug
2222
- name: build release
2323
run: cmake --build build --config=Release
24+
- name: test debug
25+
run: cd build; ctest -C Debug
26+
- name: test release
27+
run: cd build; ctest -C Release
2428
linux-clang:
2529
runs-on: ubuntu-latest
2630
steps:
@@ -33,6 +37,10 @@ jobs:
3337
run: cmake --build build --config=Debug
3438
- name: build release
3539
run: cmake --build build --config=Release
40+
- name: test debug
41+
run: cd build; ctest -C Debug
42+
- name: test release
43+
run: cd build; ctest -C Release
3644
windows-vs22:
3745
runs-on: windows-latest
3846
steps:
@@ -43,6 +51,10 @@ jobs:
4351
run: cmake --build build --config=Debug
4452
- name: build release
4553
run: cmake --build build --config=Release
54+
- name: test debug
55+
run: cd build; ctest -C Debug
56+
- name: test release
57+
run: cd build; ctest -C Release
4658
windows-clang:
4759
runs-on: windows-latest
4860
steps:
@@ -55,3 +67,7 @@ jobs:
5567
run: cmake --build build --config=Debug
5668
- name: build release
5769
run: cmake --build build --config=Release
70+
- name: test debug
71+
run: cd build; ctest -C Debug
72+
- name: test release
73+
run: cd build; ctest -C Release

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ cmake_minimum_required(VERSION 3.24)
33
set(project_name Tkge)
44
project(${project_name})
55

6+
option(TKGE_BUILD_TESTS "Build Tkge unit tests" ${PROJECT_IS_TOP_LEVEL})
7+
68
set(CMAKE_CXX_STANDARD 23)
79
set(CMAKE_CXX_STANDARD_REQUIRED ON)
810
set(CMAKE_CXX_EXTENSIONS OFF)
@@ -12,3 +14,8 @@ add_subdirectory(Ext)
1214

1315
add_subdirectory(Lib)
1416
add_subdirectory(App)
17+
18+
if(TKGE_BUILD_TESTS)
19+
enable_testing()
20+
add_subdirectory(Tests)
21+
endif()

Lib/Include/Tkge/Assets/IAsset.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstddef>
55
#include <memory>
66
#include <span>
7+
#include <stdexcept>
78
#include <string>
89

910
namespace Tkge::Assets

Tests/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
project(${project_name}-Tests)
2+
3+
file(GLOB_RECURSE sources LIST_DIRECTORIES false "*.cpp")
4+
5+
if(NOT "${sources}" STREQUAL "")
6+
add_executable(${PROJECT_NAME})
7+
8+
target_link_libraries(${PROJECT_NAME} PRIVATE
9+
${project_name}::Lib
10+
klib::klib-test-main
11+
)
12+
13+
target_include_directories(${PROJECT_NAME} PRIVATE
14+
"${CMAKE_CURRENT_SOURCE_DIR}" ,
15+
../Lib/Src
16+
)
17+
18+
target_sources(${PROJECT_NAME} PRIVATE ${sources})
19+
20+
add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})
21+
endif()

Tests/TestAssetLoader.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <Tkge/Assets/TextAsset.hpp>
2+
#include <klib/unit_test.hpp>
3+
#include <filesystem>
4+
#include <fstream>
5+
6+
namespace
7+
{
8+
using namespace Tkge;
9+
10+
struct TempFile
11+
{
12+
TempFile(const TempFile&) = delete;
13+
TempFile(TempFile&&) = delete;
14+
TempFile& operator=(const TempFile&) = delete;
15+
TempFile& operator=(TempFile&&) = delete;
16+
17+
explicit TempFile(std::string path, const std::string_view text) : _path(std::move(path))
18+
{
19+
auto file = std::ofstream{_path};
20+
ASSERT(file.is_open());
21+
file << text;
22+
}
23+
24+
~TempFile() { std::filesystem::remove(_path); }
25+
26+
[[nodiscard]] const std::string& GetPath() const { return _path; }
27+
28+
private:
29+
std::string _path;
30+
};
31+
32+
TEST(TextAsset_Load)
33+
{
34+
static constexpr std::string_view Text{"Hello World"};
35+
36+
const auto file = TempFile{"Test.txt", Text};
37+
38+
auto stream = Assets::ReadonlyByteStream{file.GetPath()};
39+
EXPECT(stream.GetStreamSize() > 0);
40+
41+
auto asset = Assets::TextAsset{};
42+
EXPECT(asset.Load(std::move(stream)));
43+
44+
const auto& text = asset.Text();
45+
EXPECT(text == Text);
46+
}
47+
} // namespace

0 commit comments

Comments
 (0)