diff --git a/.drone.yml b/.drone.yml index 8474519..8707deb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -32,8 +32,41 @@ steps: fi exit ${has_fomatting_issues} ---- +--- +kind: pipeline +type: docker +name: unit tests +clone: + recursive: true + submodule_update_remote: true + +trigger: + branch: + - main + +steps: +- name: unit tests + image: unit_tests:latest + pull: if-not-exists + commands: + - | + set -e + + git submodule update --init --recursive + conan build . \ + -c tools.system.package_manager:mode=install \ + -s build_type=Release \ + -o enable_static_analysis=False \ + -o enable_tests=True \ + --build=missing + + for test in $(find ./build -type f -name '*_tests' -executable); do + echo "Running $test" + "$test" + done + +--- kind: pipeline type: docker name: static analysis @@ -51,5 +84,12 @@ steps: pull: if-not-exists privileged: true commands: - - git submodule update --init --recursive - - conan build . -s build_type=Release -o enable_static_analysis=True --build=missing + - | + git submodule update --init --recursive + + conan build . \ + -c tools.system.package_manager:mode=install \ + -s build_type=Release \ + -o enable_static_analysis=True \ + -o enable_tests=True \ + --build=missing diff --git a/modules/ecs/src/uuid.cpp b/modules/ecs/src/uuid.cpp index c84dd39..09a55cc 100644 --- a/modules/ecs/src/uuid.cpp +++ b/modules/ecs/src/uuid.cpp @@ -2,8 +2,10 @@ namespace lt { -auto UUID::s_engine = std::mt19937_64(std::random_device()()); -auto UUID::s_distribution = std::uniform_int_distribution {}; +std::mt19937_64 UUID::s_engine = std::mt19937_64(std::random_device()()); + +std::uniform_int_distribution + UUID::s_distribution = std::uniform_int_distribution {}; UUID::UUID(uint64_t uuid /* = -1 */): m_uuid(uuid == -1 ? s_distribution(s_engine) : uuid) { diff --git a/modules/test/include/test/test.hpp b/modules/test/include/test/test.hpp index 57d3988..7282202 100644 --- a/modules/test/include/test/test.hpp +++ b/modules/test/include/test/test.hpp @@ -26,6 +26,62 @@ concept test = requires(T test) { } // namespace concepts +namespace details { + + +class Registry +{ +public: + using Suite = void (*)(); + + static void register_suite(Suite suite) + { + instance().m_suites.emplace_back(suite); + } + + static auto run_all() -> int32_t + { + for (auto &test : instance().m_suites) + { + test(); + } + + std::cout << "_________________________[TEST RESULTS]_________________________\n"; + std::cout << "Ran " << instance().m_failed_count + instance().m_pasesed_count << " tests:\n" + << "\tpassed: " << instance().m_pasesed_count << '\n' + << "\tfailed: " << instance().m_failed_count << '\n'; + + return instance().m_failed_count; + } + + static void increment_passed_count() + { + ++instance().m_pasesed_count; + } + + static void increment_failed_count() + { + ++instance().m_failed_count; + } + +private: + Registry() = default; + + [[nodiscard]] static auto instance() -> Registry & + { + static auto registry = Registry {}; + return registry; + } + + std::vector m_suites; + + int32_t m_pasesed_count {}; + int32_t m_failed_count {}; +}; + + +} // namespace details + struct Case { auto operator=(std::invocable auto test) -> void // NOLINT @@ -40,51 +96,17 @@ struct Case { std::cout << " --> FAIL !" << '\n'; std::cout << exp.what() << "\n\n"; + details::Registry::increment_failed_count(); return; // TODO(Light): Should we run the remaining tests after a failure? } + details::Registry::increment_passed_count(); std::cout << " --> SUCCESS :D" << "\n"; } std::string_view name; }; -namespace details { - - -class Registry -{ -public: - using Suite = void (*)(); - - static void register_suite(Suite suite) - { - instance().m_suites.emplace_back(suite); - } - - static void run_all() - { - for (auto &test : instance().m_suites) - { - test(); - } - } - -private: - Registry() = default; - - [[nodiscard]] static auto instance() -> Registry & - { - static auto registry = Registry {}; - return registry; - } - - std::vector m_suites; -}; - - -} // namespace details - struct TestSuite { template diff --git a/modules/test/src/entrypoint.cpp b/modules/test/src/entrypoint.cpp index fc2a861..5404f4a 100644 --- a/modules/test/src/entrypoint.cpp +++ b/modules/test/src/entrypoint.cpp @@ -6,7 +6,7 @@ try using namespace ::lt::test; using namespace ::lt::test::details; - Registry::run_all(); + return Registry::run_all(); } catch (const std::exception &exp) { diff --git a/modules/test/src/test.tests.cpp b/modules/test/src/test.tests.cpp index 95fd8c6..6661af3 100644 --- a/modules/test/src/test.tests.cpp +++ b/modules/test/src/test.tests.cpp @@ -2,6 +2,7 @@ lt::test::Suite meta = []() { using lt::test::expect_eq; + using lt::test::expect_true; lt::test::Case { "test_1" } = [] { expect_eq(5, 5); @@ -12,7 +13,18 @@ lt::test::Suite meta = []() { }; lt::test::Case { "test_3" } = [] { - expect_eq(true, false); + auto exception_thrown = false; + + try + { + expect_eq(true, false); + } + catch (const std::exception &exp) + { + exception_thrown = true; + } + + expect_true(exception_thrown); }; lt::test::Case { "test_4" } = [] { @@ -20,6 +32,5 @@ lt::test::Suite meta = []() { }; lt::test::Case { "test_5" } = [] { - throw std::runtime_error("Uncaught std exception!"); }; }; diff --git a/modules/window/src/linux/window.cpp b/modules/window/src/linux/window.cpp index 91834de..0b9cb27 100644 --- a/modules/window/src/linux/window.cpp +++ b/modules/window/src/linux/window.cpp @@ -55,7 +55,7 @@ void lWindow::on_event(const Event &event) on_window_resize(dynamic_cast(event)); break; - default: + default: break; } } diff --git a/tools/ci/images/unit_tests/Dockerfile b/tools/ci/images/unit_tests/Dockerfile new file mode 100644 index 0000000..453253f --- /dev/null +++ b/tools/ci/images/unit_tests/Dockerfile @@ -0,0 +1,37 @@ +FROM alpine:latest + +RUN apk add --no-cache \ + bash \ + clang \ + llvm \ + cmake \ + git \ + make \ + g++ \ + python3 \ + py3-pip \ + mesa-dev \ + mesa-gl \ + pkgconf + + +RUN pip install --no-cache-dir --break-system-packages conan gitpython \ + && conan profile detect + +RUN clang --version \ + && conan --version \ + && pip --version \ + && cmake --version \ + && clang --version + + +RUN git clone 'https://git.light7734.com/light7734/light.git' --recursive \ + && cd light \ + && conan install . \ + -s build_type=Debug \ + -c tools.system.package_manager:mode=install \ + --build=missing \ + && conan install . \ + -s build_type=Release \ + -c tools.system.package_manager:mode=install \ + --build=missing