cyphesis/tests/TaskFactoryTest.cpp

240 lines
5.3 KiB
C++
Raw Permalink Normal View History

2009-11-19 15:45:59 +00:00
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2011 Alistair Riddoch
2009-11-19 15:45:59 +00:00
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2011-02-15 09:30:46 +00:00
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
2009-11-19 15:45:59 +00:00
#include "server/TaskFactory.h"
#include "rulesets/Entity.h"
#include "rulesets/Task.h"
2009-11-19 15:45:59 +00:00
#include "common/TypeNode.h"
2009-11-19 15:45:59 +00:00
#include <cassert>
static bool stub_TypeNode_isTypeOf = false;
static bool stub_LocatedEntity_hasAttr = false;
2009-11-19 15:45:59 +00:00
int main()
{
{
TaskKit * ek = new TaskFactory("test1");
2009-11-19 15:45:59 +00:00
assert(ek->m_scriptFactory == 0);
assert(ek->target() == 0);
delete ek;
}
2009-11-19 15:45:59 +00:00
Entity * chr = new Entity("1", 1);
Entity * target = new Entity("2", 2);
2009-11-19 15:45:59 +00:00
// Check target will always succeed if no constraints are in place
{
TaskKit * ek = new TaskFactory("test1");
2009-11-19 15:45:59 +00:00
assert(ek->m_scriptFactory == 0);
assert(ek->target() == 0);
int c = ek->checkTarget(target);
assert(c == 0);
}
2009-11-19 15:45:59 +00:00
// Type match fails, no property set
{
TaskKit * ek = new TaskFactory("test1");
2009-11-19 15:45:59 +00:00
stub_TypeNode_isTypeOf = false;
2009-11-19 15:45:59 +00:00
ek->setTarget(new TypeNode("type1"));
target->setType(new TypeNode("type2"));
2010-03-23 01:56:13 +00:00
int c = ek->checkTarget(target);
assert(c == -1);
}
2010-03-23 01:56:13 +00:00
// Type match succeeds, no property set
{
TaskKit * ek = new TaskFactory("test1");
stub_TypeNode_isTypeOf = true;
2010-03-23 01:56:13 +00:00
ek->setTarget(new TypeNode("type1"));
target->setType(new TypeNode("type2"));
2010-03-23 01:56:13 +00:00
int c = ek->checkTarget(target);
assert(c == 0);
}
2010-03-23 01:56:13 +00:00
// property match fails, no type match
{
TaskKit * ek = new TaskFactory("test1");
2010-03-23 01:56:13 +00:00
stub_LocatedEntity_hasAttr = false;
2010-03-23 01:56:13 +00:00
ek->setRequireProperty("prop1");
2010-03-23 01:56:13 +00:00
int c = ek->checkTarget(target);
assert(c == -1);
}
2010-03-23 01:56:13 +00:00
// property match succeeds, no type match
{
TaskKit * ek = new TaskFactory("test1");
2010-03-23 01:56:13 +00:00
stub_LocatedEntity_hasAttr = true;
2010-03-23 01:56:13 +00:00
ek->setRequireProperty("prop1");
2010-03-23 01:56:13 +00:00
int c = ek->checkTarget(target);
assert(c == 0);
}
2010-03-23 01:56:13 +00:00
// property match fails, type match fails
{
TaskKit * ek = new TaskFactory("test1");
2010-03-23 01:56:13 +00:00
stub_LocatedEntity_hasAttr = false;
stub_TypeNode_isTypeOf = false;
2010-03-23 01:56:13 +00:00
ek->setTarget(new TypeNode("type1"));
target->setType(new TypeNode("type2"));
2010-03-23 01:56:13 +00:00
ek->setRequireProperty("prop1");
2010-03-23 01:56:13 +00:00
int c = ek->checkTarget(target);
assert(c == -1);
}
2010-03-23 01:56:13 +00:00
// property match succeeds, type match fails
{
TaskKit * ek = new TaskFactory("test1");
2010-03-23 01:56:13 +00:00
stub_LocatedEntity_hasAttr = true;
stub_TypeNode_isTypeOf = false;
2010-03-23 01:56:13 +00:00
ek->setTarget(new TypeNode("type1"));
target->setType(new TypeNode("type2"));
2010-03-23 01:56:13 +00:00
ek->setRequireProperty("prop1");
2010-03-23 01:56:13 +00:00
int c = ek->checkTarget(target);
assert(c == -1);
}
2010-03-23 01:56:13 +00:00
// property match fails, type match succeeds
{
TaskKit * ek = new TaskFactory("test1");
2010-03-23 01:56:13 +00:00
stub_LocatedEntity_hasAttr = false;
stub_TypeNode_isTypeOf = true;
2010-03-23 01:56:13 +00:00
ek->setTarget(new TypeNode("type1"));
target->setType(new TypeNode("type2"));
2010-03-23 01:56:13 +00:00
ek->setRequireProperty("prop1");
2010-03-23 01:56:13 +00:00
int c = ek->checkTarget(target);
assert(c == -1);
}
2010-03-23 01:56:13 +00:00
// property match fails, type match fails
{
TaskKit * ek = new TaskFactory("test1");
2010-03-23 01:56:13 +00:00
stub_LocatedEntity_hasAttr = true;
stub_TypeNode_isTypeOf = true;
2010-03-23 01:56:13 +00:00
ek->setTarget(new TypeNode("type1"));
target->setType(new TypeNode("type2"));
2010-03-23 01:56:13 +00:00
ek->setRequireProperty("prop1");
2010-03-23 01:56:13 +00:00
int c = ek->checkTarget(target);
assert(c == 0);
}
2010-03-23 01:56:13 +00:00
{
TaskKit * ek = new TaskFactory("test1");
2010-03-23 01:56:13 +00:00
Task * e = ek->newTask(*chr);
assert(e);
2010-03-23 01:56:13 +00:00
delete ek;
}
2010-03-23 01:56:13 +00:00
return 0;
2010-03-23 01:56:13 +00:00
}
// stubs
2010-03-23 01:56:13 +00:00
TaskKit::TaskKit() : m_target(0), m_scriptFactory(0)
{
}
TaskKit::~TaskKit()
{
}
void Task::initTask(const Operation & op, OpVector & res)
2010-03-23 01:56:13 +00:00
{
}
void Task::operation(const Operation & op, OpVector & res)
2010-03-23 01:56:13 +00:00
{
}
Task::Task(LocatedEntity & chr) : m_refCount(0), m_serialno(0), m_obsolete(false), m_progress(-1), m_rate(-1), m_owner(chr)
2010-03-23 01:56:13 +00:00
{
}
Task::~Task()
2010-03-23 01:56:13 +00:00
{
}
void Task::irrelevant()
2010-03-23 01:56:13 +00:00
{
}
#include "stubs/rulesets/stubEntity.h"
2010-03-23 01:56:13 +00:00
#define STUB_LocatedEntity_hasAttr
2010-03-23 01:56:13 +00:00
bool LocatedEntity::hasAttr(const std::string & name) const
{
return stub_LocatedEntity_hasAttr;
2010-03-23 01:56:13 +00:00
}
#include "stubs/rulesets/stubLocatedEntity.h"
#include "stubs/common/stubRouter.h"
2010-03-23 01:56:13 +00:00
TypeNode::TypeNode(const std::string & name) : m_name(name), m_parent(0)
2010-03-23 01:56:13 +00:00
{
}
bool TypeNode::isTypeOf(const TypeNode * base_type) const
2010-03-23 01:56:13 +00:00
{
return stub_TypeNode_isTypeOf;
2010-03-23 01:56:13 +00:00
}
#include "stubs/modules/stubLocation.h"
2010-03-23 01:56:13 +00:00