(vibe) Added testframework
This commit is contained in:
151
douwco_hivemind/include/Testing/Mocks.hpp
Normal file
151
douwco_hivemind/include/Testing/Mocks.hpp
Normal file
@@ -0,0 +1,151 @@
|
||||
#ifndef DOUWCO_HIVEMIND_TESTING_MOCKS_HPP
|
||||
#define DOUWCO_HIVEMIND_TESTING_MOCKS_HPP
|
||||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <cmath>
|
||||
#include <Screeps/Creep.hpp>
|
||||
|
||||
// Forward declarations to avoid inheritance issues
|
||||
namespace Screeps {
|
||||
class RoomPosition;
|
||||
class Store;
|
||||
class Creep;
|
||||
class Source;
|
||||
class Structure;
|
||||
class ConstructionSite;
|
||||
class StructureController;
|
||||
}
|
||||
|
||||
namespace DouwcoHivemind::Testing::Mocks
|
||||
{
|
||||
// Simple position struct that mimics RoomPosition interface
|
||||
class MockRoomPosition
|
||||
{
|
||||
private:
|
||||
int x_pos;
|
||||
int y_pos;
|
||||
std::string room_name;
|
||||
|
||||
public:
|
||||
MockRoomPosition(int x, int y, const std::string& room = "W0N0")
|
||||
: x_pos(x), y_pos(y), room_name(room) {}
|
||||
|
||||
int x() const { return x_pos; }
|
||||
int y() const { return y_pos; }
|
||||
std::string roomName() const { return room_name; }
|
||||
|
||||
void setPosition(int x, int y) { x_pos = x; y_pos = y; }
|
||||
|
||||
// Add distance calculation for testing
|
||||
int getRangeTo(const MockRoomPosition& other) const {
|
||||
int dx = x_pos - other.x_pos;
|
||||
int dy = y_pos - other.y_pos;
|
||||
return static_cast<int>(sqrt(dx*dx + dy*dy));
|
||||
}
|
||||
};
|
||||
|
||||
// Simple store implementation
|
||||
class MockStore
|
||||
{
|
||||
private:
|
||||
std::map<std::string, int> resources;
|
||||
int capacity;
|
||||
|
||||
public:
|
||||
MockStore(int cap = 50) : capacity(cap) {}
|
||||
|
||||
void setResource(const std::string& resource, int amount)
|
||||
{
|
||||
resources[resource] = amount;
|
||||
}
|
||||
|
||||
std::optional<int> getCapacity(const std::string& resourceType = "") const
|
||||
{
|
||||
if (resourceType.empty() || resourceType == "energy")
|
||||
return capacity;
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::optional<int> getUsedCapacity(const std::string& resourceType = "") const
|
||||
{
|
||||
if (resourceType.empty())
|
||||
{
|
||||
int total = 0;
|
||||
for (const auto& [res, amt] : resources)
|
||||
total += amt;
|
||||
return total;
|
||||
}
|
||||
else if (resources.find(resourceType) != resources.end())
|
||||
{
|
||||
return resources.at(resourceType);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::optional<int> getFreeCapacity(const std::string& resourceType = "") const
|
||||
{
|
||||
auto used = getUsedCapacity(resourceType);
|
||||
auto cap = getCapacity(resourceType);
|
||||
if (used.has_value() && cap.has_value())
|
||||
return cap.value() - used.value();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void addResource(const std::string& resource, int amount)
|
||||
{
|
||||
resources[resource] += amount;
|
||||
if (resources[resource] > capacity)
|
||||
resources[resource] = capacity;
|
||||
else if (resources[resource] < 0)
|
||||
resources[resource] = 0;
|
||||
}
|
||||
};
|
||||
|
||||
// Simple creep mock that implements the interface needed for testing
|
||||
class MockCreep
|
||||
{
|
||||
private:
|
||||
std::string creep_name;
|
||||
MockRoomPosition position;
|
||||
MockStore store;
|
||||
JSON memory;
|
||||
|
||||
public:
|
||||
MockCreep(const std::string& name = "TestCreep")
|
||||
: creep_name(name), position(10, 10), store(50) {}
|
||||
|
||||
std::string name() const { return creep_name; }
|
||||
const MockRoomPosition& pos() const { return position; }
|
||||
const MockStore& getStore() const { return store; }
|
||||
JSON getMemory() const { return memory; }
|
||||
|
||||
void setMemory(const JSON& mem) { memory = mem; }
|
||||
void setPosition(int x, int y) { position.setPosition(x, y); }
|
||||
void setEnergy(int amount) { store.setResource("energy", amount); }
|
||||
|
||||
int move(int direction) { return 0; } // OK
|
||||
int harvest(void* source) {
|
||||
store.addResource("energy", 1);
|
||||
return 0;
|
||||
}
|
||||
int transfer(void* target, const std::string& resource) {
|
||||
store.addResource(resource, -1);
|
||||
return 0;
|
||||
}
|
||||
int build(void* site) { return 0; }
|
||||
int repair(void* structure) { return 0; }
|
||||
int upgradeController(void* controller) { return 0; }
|
||||
|
||||
void say(const std::string& message) {}
|
||||
};
|
||||
|
||||
// Forward declarations for other mocks
|
||||
class MockSource;
|
||||
class MockStructure;
|
||||
class MockRoom;
|
||||
class MockGame;
|
||||
}
|
||||
|
||||
#endif // DOUWCO_HIVEMIND_TESTING_MOCKS_HPP
|
||||
Reference in New Issue
Block a user