Skip to content

Commit

Permalink
Put in place proper mocking for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeLepape committed Dec 30, 2023
1 parent 32f8b93 commit b9198d9
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 33 deletions.
21 changes: 15 additions & 6 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
build_type: [Release, Debug]
c_compiler: [gcc, clang, cl]
include:
- os: ubuntu-latest
Expand All @@ -46,6 +46,8 @@ jobs:
fetch-depth: 0
submodules: true

- run: cmake --version

- 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
Expand All @@ -65,14 +67,21 @@ jobs:
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-DCMAKE_TOOLCHAIN_FILE=${{ github.workspace }}/vcpkg/scripts/buildsystems/vcpkg.cmake
-DCMAKE_CXX_FLAGS_DEBUG="-g -fprofile-arcs -ftest-coverage"
-DCMAKE_C_FLAGS_DEBUG="-g -fprofile-arcs -ftest-coverage"
-S ${{ github.workspace }}
- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

# - name: Test
# working-directory: ${{ steps.strings.outputs.build-output-dir }}
# # Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
# run: ctest --build-config ${{ matrix.build_type }}
- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }} -T Test -T Coverage

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
4 changes: 2 additions & 2 deletions src/action/include/elevation/action/action_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ void write(T&& statement) {

namespace result {
template <utils::Printable T>
void write(T&& result) {
utils::pause();
void write(T&& result, std::istream& in = std::cin) {
utils::pause(in);
fmt::print(fg(fmt::color::yellow) | fmt::emphasis::bold, "\n {}", result);
}
} // namespace result
Expand Down
13 changes: 8 additions & 5 deletions src/action/include/elevation/action/add_weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class AddWeapon {
std::string result_;
entity::Player& player_;
weapon::Weapon weapon_;
std::istream& in_;

public:
AddWeapon(entity::Player& player, weapon::Weapon&& weapon)
AddWeapon(entity::Player& player, weapon::Weapon&& weapon, std::istream& in)
: result_{data::action::resultAddWeapon(weapon.name)},
player_{player},
weapon_{std::move(weapon)} {}
weapon_{std::move(weapon)},
in_{in} {}

void trigger() {
if (weapon_.type != weapon::Type::noWeapon) {
Expand All @@ -26,15 +28,16 @@ class AddWeapon {
player_.weapons().insert(std::move(weapon_));

if (not_in_player_weapons) {
result::write(result_);
result::write(result_, in_);
}
}
}
};

namespace add_weapon {
void trigger(entity::Player& player, weapon::Weapon&& weapon) {
AddWeapon add_weapon{player, std::forward<weapon::Weapon>(weapon)};
void trigger(entity::Player& player, weapon::Weapon&& weapon,
std::istream& in = std::cin) {
AddWeapon add_weapon{player, std::forward<weapon::Weapon>(weapon), in};
add_weapon.trigger();
}
} // namespace add_weapon
Expand Down
11 changes: 6 additions & 5 deletions src/action/include/elevation/action/dead.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ class Dead {
private:
U result_;
const entity::Plug& plug_;
std::istream& in_;

public:
Dead(const entity::Plug& plug, const U& result)
: result_{result}, plug_{plug} {}
Dead(const entity::Plug& plug, const U& result, std::istream& in)
: result_{result}, plug_{plug}, in_{in} {}

void trigger() {
if (plug_.healthBar().dead()) {
result::write(result_);
result::write(result_, in_);
}
}
};

namespace dead {
template <utils::Printable U>
void trigger(const entity::Plug& plug, const U& result) {
Dead dead{plug, result};
void trigger(const entity::Plug& plug, const U& result, std::istream& in) {
Dead dead{plug, result, in};
dead.trigger();
}
} // namespace dead
Expand Down
13 changes: 7 additions & 6 deletions src/action/include/elevation/action/use_weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ class UseWeapon {
entity::Player& player_;
entity::Plug& plug_;
const std::string& nameWeapon_;
std::istream& in_;

public:
UseWeapon(entity::Player& player, entity::Plug& plug,
const std::string& nameWeapon)
: player_{player}, plug_{plug}, nameWeapon_{nameWeapon} {}
const std::string& nameWeapon, std::istream& in = std::cin)
: player_{player}, plug_{plug}, nameWeapon_{nameWeapon}, in_{in} {}

void trigger() {
auto weapon = ranges::find_if(player_.weapons(), [&](const auto& weapon) {
Expand All @@ -30,9 +31,9 @@ class UseWeapon {

auto result =
data::weapon::resultUseWeapon(plug_.name(), weapon->nb_damage);
result::write(result);
result::write(result, in_);

action::dead::trigger(plug_, data::action::resultDead(plug_.name()));
action::dead::trigger(plug_, data::action::resultDead(plug_.name()), in_);

// if weapon is fireArm and has no ammo, delete it
if (weapon->type == weapon::Type::fireArm and weapon->durability <= 0) {
Expand Down Expand Up @@ -67,8 +68,8 @@ class UseWeapon {

namespace use_weapon {
void trigger(entity::Player& player, entity::Plug& plug,
const std::string& nameWeapon) {
UseWeapon use_weapon{player, plug, nameWeapon};
const std::string& nameWeapon, std::istream& in = std::cin) {
UseWeapon use_weapon{player, plug, nameWeapon, in};
use_weapon.trigger();
}
} // namespace use_weapon
Expand Down
4 changes: 3 additions & 1 deletion src/utils/include/elevation/utils/pause.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <iostream>

namespace elevation::utils {
void pause();
void pause(std::istream& in = std::cin);
}
5 changes: 2 additions & 3 deletions src/utils/src/pause.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "elevation/utils/pause.h"

#include <atomic>
#include <iostream>

namespace elevation::utils {
std::atomic_bool keep_running{true};

void pause() {
void pause(std::istream& in) {
while (keep_running) {
if (std::cin.get() == 10) {
if (in.get() == 10) {
keep_running = false;
}
}
Expand Down
24 changes: 19 additions & 5 deletions test/weapon_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "elevation/weapon/weapon.h"

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <range/v3/view.hpp>

Expand All @@ -12,13 +14,19 @@
#include "elevation/entity/player.h"
#include "elevation/entity/plug.h"

using ::testing::MockFunction;
using ::testing::Return;

namespace elevation {
TEST(weapon_test, UseWeapon) {
entity::Player player("Guillaume", 0);

action::add_weapon::trigger(player, weapon::Fist());
std::istringstream s1{"\u000A\u000A"};
std::istream s2{s1.rdbuf()};

action::add_weapon::trigger(player, weapon::Fist(), s2);

action::add_weapon::trigger(player, weapon::AK47(100));
action::add_weapon::trigger(player, weapon::AK47(100), s2);

EXPECT_EQ(player.pseudo(), "Guillaume");

Expand All @@ -35,7 +43,10 @@ TEST(weapon_test, UseWeapon) {
TEST(weapon_test, Fist) {
entity::Player player("Guillaume", 0, weapon::WeaponInventory{});

action::add_weapon::trigger(player, weapon::Fist());
std::istringstream s1{"\u000A"};
std::istream s2{s1.rdbuf()};

action::add_weapon::trigger(player, weapon::Fist(), s2);

const auto& fist = *(std::cbegin(player.weapons()));

Expand All @@ -48,7 +59,10 @@ TEST(weapon_test, Fist) {
TEST(weapon_test, AK47) {
entity::Player player("Guillaume", 0, weapon::WeaponInventory{});

action::add_weapon::trigger(player, weapon::AK47(40));
std::istringstream s1{"\u000A\u000A\u000A"};
std::istream s2{s1.rdbuf()};

action::add_weapon::trigger(player, weapon::AK47(40), s2);

const auto& ak47 = *std::find_if(
std::cbegin(player.weapons()), std::cend(player.weapons()),
Expand All @@ -62,7 +76,7 @@ TEST(weapon_test, AK47) {

entity::Plug plug("Jean-Michel", 100);

action::use_weapon::trigger(player, plug, data::weapon::nameAK47);
action::use_weapon::trigger(player, plug, data::weapon::nameAK47, s2);

EXPECT_EQ(ak47.nb_damage, 150);
EXPECT_EQ(ak47.type, weapon::Type::fireArm);
Expand Down

0 comments on commit b9198d9

Please sign in to comment.