Skip to content

Commit

Permalink
Fix and re-enable unit tests (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
oddbookworm authored Nov 7, 2024
1 parent e3b8dfe commit 1fb7a11
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 21 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/linux-gtest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Run GTest Linux

env:
SDL_VIDEODRIVER: "dummy"
SDL_AUDIODRIVER: "dummy"

on:
push:
branches: [ "main", "**dev" ]
pull_request:
jobs:
linux-gtest:
runs-on: ${{ matrix.os }}
strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false
# Set up a matrix to run the following 2 configurations:
# 1. <Linux, latest GCC compiler toolchain on the default runner image, default generator>
# 2. <Linux, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest]
c_compiler: [gcc, clang]
include:
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
steps:
- uses: actions/checkout@v4
- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
- name: Update packages
run: sudo apt-get update && sudo apt-get upgrade -y
- name: Install SDL2 dependency
run: sudo apt-get install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev libsdl2-ttf-dev libgtest-dev -y
- uses: actions/setup-python@v5
- name: Install meson
run: python3 -m pip install meson ninja
- name: Configure meson
run: meson setup build -Dbuild_tests=true
- name: Build test executable
run: cd build && meson compile -j$(nproc)
- name: Run Tests
run: cd build && ./krakentests --gtest_shuffle --gtest_color=true
84 changes: 84 additions & 0 deletions .github/workflows/macos-gtest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
name: Run GTest MacOS

env:
SDL_VIDEODRIVER: "dummy"
SDL_AUDIODRIVER: "dummy"

on:
push:
branches: [ "main", "**dev" ]
pull_request:

jobs:
macos-gtest:
env:
MAC_ARCH: ${{ matrix.macarch }}
runs-on: ${{ matrix.os }}
strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false
# Set up a matrix to run the following 2 configurations:
# 1. <MacOS 13, x86_64, latest GCC compier toolchain on the default runner image, default generator>
# 2. <MacOS 13, arm64, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <MacOS 13, x86_64, latest Clang compiler toolchain on the default runner image, default generator>
# 4. <MacOS 13, x86_64, latest Clang compiler toolchain on the default runner image, default generator>
# 5. <MacOS 14, x86_64, latest GCC compiler toolchain on the default runner image, default generator>
# 6. <MacOS 14, arm64, latest GCC compiler toolchain on the default runner image, default generator>
# 7. <MacOS 14, x86_64, latest Clang compiler toolchain on the default runner image, default generator>
# 8. <MacOS 14, x86_64, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [macos-13, macos-14]
c_compiler: [gcc, clang]
macarch: [x86_64, arm64]
include:
- os: macos-13
macarch: x86_64
c_compiler: gcc
cpp_compiler: g++
- os: macos-13
macarch: arm64
c_compiler: gcc
cpp_compiler: g++
- os: macos-13
macarch: x86_64
c_compiler: clang
cpp_compiler: clang++
- os: macos-13
macarch: arm64
c_compiler: clang
cpp_compiler: clang++
- os: macos-14
macarch: x86_64
c_compiler: gcc
cpp_compiler: g++
- os: macos-14
macarch: arm64
c_compiler: gcc
cpp_compiler: g++
- os: macos-14
macarch: x86_64
c_compiler: clang
cpp_compiler: clang++
- os: macos-14
macarch: arm64
c_compiler: clang
cpp_compiler: clang++
steps:
- uses: actions/checkout@v4
- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"
- uses: actions/setup-python@v5
- name: Install meson
run: python3 -m pip install meson ninja --break-system-packages
- name: Configure meson
run: meson setup build -Dbuild_tests=true
- name: Build test executable
run: cd build && meson compile -j$(sysctl -n hw.ncpu)
- name: Run Tests
run: cd build && ./krakentests --gtest_shuffle --gtest_color=true
32 changes: 29 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ incs = include_directories('include')

deps = [
dependency('sdl2'),
dependency('sdl2_image'),
dependency('sdl2_ttf'),
dependency('sdl2_mixer'),
dependency('SDL2_image'),
dependency('SDL2_ttf'),
dependency('SDL2_mixer'),
]

# Declare kraken engine lib and dependency
Expand All @@ -39,3 +39,29 @@ if get_option('build_example')
link_with : libkraken,
)
endif

if get_option('build_tests')
gtest_proj = subproject('gtest')

gtest_dep = gtest_proj.get_variable('gtest_dep')
gmock_dep = gtest_proj.get_variable('gmock_dep')
gtest_main_dep = gtest_proj.get_variable('gtest_main_dep')
gtest_dep = dependency('gtest', main : true, required : false)
gmock_dep = dependency('gmock', main : true, required : false)
gtest_main_dep = dependency('gtest_main', main : true, required : false)

gtest = shared_library('gtest', install : true)
gmock = shared_library('gmock', install: true)
gtest_main = shared_library('gtest_main', install: true)


subdir('tests')
executable(
'krakentests',
sources : test_src,
include_directories : [incs, include_directories('tests')],
dependencies : deps + [kraken_engine_dep, gtest_dep, gtest_main_dep],
win_subsystem : 'console',
link_with : [libkraken, gtest, gtest_main]
)
endif
7 changes: 7 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ option(
type : 'boolean',
value : false,
description : 'Build example demo. Off by default.',
)

option(
'build_tests',
type : 'boolean',
value : false,
description : 'Build test suite. Off by default.',
)
16 changes: 16 additions & 0 deletions subprojects/gtest.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[wrap-file]
directory = googletest-1.15.0
source_url = https://github.com/google/googletest/archive/refs/tags/v1.15.0.tar.gz
source_filename = gtest-1.15.0.tar.gz
source_hash = 7315acb6bf10e99f332c8a43f00d5fbb1ee6ca48c52f6b936991b216c586aaad
patch_filename = gtest_1.15.0-1_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/gtest_1.15.0-1/get_patch
patch_hash = 5f8e484c48fdc1029c7fd08807bd2615f8c9d16f90df6d81984f4f292752c925
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/gtest_1.15.0-1/gtest-1.15.0.tar.gz
wrapdb_version = 1.15.0-1

[provide]
gtest = gtest_dep
gtest_main = gtest_main_dep
gmock = gmock_dep
gmock_main = gmock_main_dep
22 changes: 22 additions & 0 deletions tests/TestBase.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "gtest/gtest.h"

#include "ErrorLogger.hpp"

namespace
{

class TestBase : public ::testing::Test
{
public:
TestBase() { kn::ErrorLogger::setConsoleOnly(); }
virtual ~TestBase() {}

protected:
virtual void SetUp() {}

virtual void TearDown() {}
};

} // namespace
5 changes: 5 additions & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test_src = [
'tests/test_Math.cpp',
'tests/test_Overflow.cpp',
'tests/test_Window.cpp'
]
28 changes: 18 additions & 10 deletions tests/test_Math.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
#include <gtest/gtest.h>

#include "Math.hpp"
#include "TestBase.hpp"

using kn::math::Vec2;

namespace
{

class MathTest : public ::testing::Test
class MathTest : public TestBase
{
public:
MathTest() : TestBase() {}
virtual ~MathTest() override = default;

protected:
MathTest() {}
~MathTest() override = default;
virtual void SetUp() override {}
virtual void TearDown() override {}

void SetUp() override {}
void TearDown() override {}
static const double DOUBLE_INF;
static const float FLOAT_INF;
};

const double MathTest::DOUBLE_INF = std::numeric_limits<double>::infinity();
const float MathTest::FLOAT_INF = std::numeric_limits<float> ::infinity();

TEST_F(MathTest, Vec2DefaultConstructor)
{
Vec2 vec;
Expand Down Expand Up @@ -99,7 +107,7 @@ TEST_F(MathTest, GetLength)
{
Vec2 first = Vec2();
Vec2 second = Vec2(3.0, 4.0);
Vec2 third = Vec2(INFINITY, INFINITY);
Vec2 third = Vec2(FLOAT_INF, FLOAT_INF);
Vec2 fourth = Vec2(4.0, 3.0);

EXPECT_EQ(first.getLength(), 0.0);
Expand All @@ -112,15 +120,15 @@ TEST_F(MathTest, Normalize)
{
Vec2 first = Vec2();
Vec2 second = Vec2(3.0, 4.0);
Vec2 third = Vec2(INFINITY, INFINITY);
Vec2 third = Vec2(DOUBLE_INF, DOUBLE_INF);

EXPECT_EQ(first.normalize(), false);
EXPECT_EQ(first, Vec2::ZERO());
EXPECT_EQ(first, Vec2());
EXPECT_EQ(second.normalize(), true);
EXPECT_EQ(second, Vec2(0.6, 0.8));
EXPECT_EQ(third.normalize(), false);
EXPECT_EQ(third.x, std::numeric_limits<double>::infinity());
EXPECT_EQ(third.y, std::numeric_limits<double>::infinity());
EXPECT_EQ(third.x, DOUBLE_INF);
EXPECT_EQ(third.y, DOUBLE_INF);
}

} // namespace
8 changes: 5 additions & 3 deletions tests/test_Overflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@

#include "Constants.hpp"
#include "Overflow.hpp"
#include "TestBase.hpp"

namespace
{
using kn::overflow::isProductValid;
using kn::overflow::isSumValid;

class DoubleOverflowTest : public ::testing::Test
class DoubleOverflowTest : public TestBase
{
protected:
DoubleOverflowTest() : doubleMax(std::numeric_limits<double>::max()) {}
DoubleOverflowTest() : TestBase(), doubleMax(std::numeric_limits<double>::max()) {}

virtual ~DoubleOverflowTest() {}

Expand Down Expand Up @@ -144,14 +145,15 @@ TEST_F(UnsignedFourByteIntTest, SumOfTwoFifthsMax)

class UnsignedEightByteIntTest : public ::testing::Test
{
protected:
public:
UnsignedEightByteIntTest()
: max(std::numeric_limits<uint64_t>::max()), min(std::numeric_limits<uint64_t>::min())
{
}

virtual ~UnsignedEightByteIntTest() {}

protected:
virtual void SetUp() {}

virtual void TearDown() {}
Expand Down
12 changes: 7 additions & 5 deletions tests/test_Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@

#include <gtest/gtest.h>

#include "ErrorLogger.hpp"
#include "Window.hpp"
#include "TestBase.hpp"

namespace
{

class WindowTest : public ::testing::Test
class WindowTest : public TestBase
{
protected:
WindowTest() : m_size(320, 240), m_scale(2) { kn::ErrorLogger::setConsoleOnly(); }
public:
WindowTest() : TestBase(), m_size(320, 240), m_scale(2), m_title("Kraken Window") {}

virtual ~WindowTest() {}

virtual void SetUp() { kn::window::init(m_size, m_scale); }
protected:
virtual void SetUp() { kn::window::init(m_size, m_title, m_scale); }

virtual void TearDown() { kn::window::quit(); }

kn::math::Vec2 m_size;
int m_scale;
std::string m_title;
};

TEST_F(WindowTest, WindowInit)
Expand Down

0 comments on commit 1fb7a11

Please sign in to comment.