cyphesis/tests/PythonArithmeticFactorytest.cpp
Alistair Riddoch 71cbcdcfc1 Modify externalOperation to include Link
The Link reference provided is the object allowing direct communication
back to the external source of the operation. This allows us to handle
things more cleanly in some cases.
2012-12-04 20:39:35 +00:00

486 lines
10 KiB
C++

// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2009 Alistair Riddoch
//
// 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
// $Id$
#ifdef NDEBUG
#undef NDEBUG
#endif
#ifndef DEBUG
#define DEBUG
#endif
#include <Python.h>
#include "python_testers.h"
#include "rulesets/Entity.h"
#include "rulesets/PythonArithmeticFactory.h"
#include "rulesets/PythonArithmeticScript.h"
#include "rulesets/Python_API.h"
#include "rulesets/Script.h"
#include "common/log.h"
#include "common/compose.hpp"
#include <cassert>
static PyMethodDef no_methods[] = {
{NULL, NULL} /* Sentinel */
};
static bool stub_wrapEntity_fail = false;
int main()
{
Py_Initialize();
PyObject * testmod = Py_InitModule("testmod", no_methods);
assert(testmod != 0);
run_python_string("import testmod");
run_python_string("class TestArithmeticScript(object):\n"
" def __init__(self,entity=None):\n"
" self.foo=1\n"
" self.bar=1.1\n"
" self.baz=None\n"
" self.qux='1'\n"
);
run_python_string("class FailArithmeticScript(object):\n"
" def __init__(self):\n"
" raise AssertionError, 'deliberate'\n"
);
run_python_string("testmod.TestArithmeticScript=TestArithmeticScript");
run_python_string("testmod.FailArithmeticScript=FailArithmeticScript");
{
PythonArithmeticFactory paf("badmod", "TestArithmeticScript");
assert(paf.setup() != 0);
ArithmeticScript * as = paf.newScript(0);
assert(as == 0);
}
{
PythonArithmeticFactory paf("testmod", "BadArithmeticScriptClass");
assert(paf.setup() != 0);
ArithmeticScript * as = paf.newScript(0);
assert(as == 0);
}
{
PythonArithmeticFactory paf("testmod", "FailArithmeticScript");
assert(paf.setup() == 0);
ArithmeticScript * as = paf.newScript(0);
assert(as != 0);
}
PythonArithmeticFactory paf("testmod", "TestArithmeticScript");
assert(paf.setup() == 0);
ArithmeticScript * as = paf.newScript(0);
assert(as != 0);
Entity * e = new Entity("1", 1);
as = paf.newScript(e);
assert(as != 0);
stub_wrapEntity_fail = true;
as = paf.newScript(e);
assert(as == 0);
Py_Finalize();
return 0;
}
// stubs
ArithmeticKit::~ArithmeticKit()
{
}
ArithmeticScript::~ArithmeticScript()
{
}
PythonArithmeticScript::PythonArithmeticScript(PyObject * script) :
m_script(script)
{
}
PythonArithmeticScript::~PythonArithmeticScript()
{
}
int PythonArithmeticScript::attribute(const std::string & name, float & val)
{
return 0;
}
void PythonArithmeticScript::set(const std::string & name, const float & val)
{
}
PyObject * wrapEntity(LocatedEntity * le)
{
if (stub_wrapEntity_fail) {
return 0;
} else {
return PyInt_FromLong(1L);
}
}
Script::Script()
{
}
/// \brief Script destructor
Script::~Script()
{
}
bool Script::operation(const std::string & opname,
const Atlas::Objects::Operation::RootOperation & op,
OpVector & res)
{
return false;
}
void Script::hook(const std::string & function, LocatedEntity * entity)
{
}
Location::Location() : m_loc(0)
{
}
Location::Location(LocatedEntity * rf, const Point3D & pos)
{
}
void Location::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
Entity::Entity(const std::string & id, long intId) :
LocatedEntity(id, intId), m_motion(0), m_flags(0)
{
}
Entity::~Entity()
{
}
void Entity::destroy()
{
destroyed.emit();
}
void Entity::ActuateOperation(const Operation &, OpVector &)
{
}
void Entity::AppearanceOperation(const Operation &, OpVector &)
{
}
void Entity::AttackOperation(const Operation &, OpVector &)
{
}
void Entity::CombineOperation(const Operation &, OpVector &)
{
}
void Entity::CreateOperation(const Operation &, OpVector &)
{
}
void Entity::DeleteOperation(const Operation &, OpVector &)
{
}
void Entity::DisappearanceOperation(const Operation &, OpVector &)
{
}
void Entity::DivideOperation(const Operation &, OpVector &)
{
}
void Entity::EatOperation(const Operation &, OpVector &)
{
}
void Entity::GetOperation(const Operation &, OpVector &)
{
}
void Entity::InfoOperation(const Operation &, OpVector &)
{
}
void Entity::ImaginaryOperation(const Operation &, OpVector &)
{
}
void Entity::LookOperation(const Operation &, OpVector &)
{
}
void Entity::MoveOperation(const Operation &, OpVector &)
{
}
void Entity::NourishOperation(const Operation &, OpVector &)
{
}
void Entity::SetOperation(const Operation &, OpVector &)
{
}
void Entity::SightOperation(const Operation &, OpVector &)
{
}
void Entity::SoundOperation(const Operation &, OpVector &)
{
}
void Entity::TalkOperation(const Operation &, OpVector &)
{
}
void Entity::TickOperation(const Operation &, OpVector &)
{
}
void Entity::TouchOperation(const Operation &, OpVector &)
{
}
void Entity::UpdateOperation(const Operation &, OpVector &)
{
}
void Entity::UseOperation(const Operation &, OpVector &)
{
}
void Entity::WieldOperation(const Operation &, OpVector &)
{
}
void Entity::externalOperation(const Operation & op, Link &)
{
}
void Entity::operation(const Operation & op, OpVector & res)
{
}
void Entity::addToMessage(Atlas::Message::MapType & omap) const
{
}
void Entity::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
PropertyBase * Entity::setAttr(const std::string & name,
const Atlas::Message::Element & attr)
{
return 0;
}
const PropertyBase * Entity::getProperty(const std::string & name) const
{
return 0;
}
PropertyBase * Entity::modProperty(const std::string & name)
{
return 0;
}
void Entity::onContainered()
{
}
void Entity::onUpdated()
{
}
Domain * Entity::getMovementDomain()
{
return 0;
}
LocatedEntity::LocatedEntity(const std::string & id, long intId) :
Router(id, intId),
m_refCount(0), m_seq(0),
m_script(0), m_type(0), m_contains(0)
{
}
LocatedEntity::~LocatedEntity()
{
}
bool LocatedEntity::hasAttr(const std::string & name) const
{
return false;
}
int LocatedEntity::getAttr(const std::string & name,
Atlas::Message::Element & attr) const
{
return -1;
}
int LocatedEntity::getAttrType(const std::string & name,
Atlas::Message::Element & attr,
int type) const
{
return -1;
}
PropertyBase * LocatedEntity::setAttr(const std::string & name,
const Atlas::Message::Element & attr)
{
return 0;
}
const PropertyBase * LocatedEntity::getProperty(const std::string & name) const
{
return 0;
}
void LocatedEntity::onContainered()
{
}
void LocatedEntity::onUpdated()
{
}
void LocatedEntity::makeContainer()
{
if (m_contains == 0) {
m_contains = new LocatedEntitySet;
}
}
void LocatedEntity::changeContainer(LocatedEntity * new_loc)
{
assert(m_location.m_loc != 0);
assert(m_location.m_loc->m_contains != 0);
m_location.m_loc->m_contains->erase(this);
if (m_location.m_loc->m_contains->empty()) {
m_location.m_loc->onUpdated();
}
new_loc->makeContainer();
bool was_empty = new_loc->m_contains->empty();
new_loc->m_contains->insert(this);
if (was_empty) {
new_loc->onUpdated();
}
assert(m_location.m_loc->checkRef() > 0);
m_location.m_loc->decRef();
m_location.m_loc = new_loc;
m_location.m_loc->incRef();
assert(m_location.m_loc->checkRef() > 0);
onContainered();
}
void LocatedEntity::merge(const Atlas::Message::MapType & ent)
{
}
Router::Router(const std::string & id, long intId) : m_id(id),
m_intId(intId)
{
}
Router::~Router()
{
}
void Router::addToMessage(Atlas::Message::MapType & omap) const
{
}
void Router::addToEntity(const Atlas::Objects::Entity::RootEntity & ent) const
{
}
void Router::error(const Operation & op,
const std::string & errstring,
OpVector & res,
const std::string & to) const
{
}
void log(LogLevel lvl, const std::string & msg)
{
}
PyObject * Get_PyClass(PyObject * module,
const std::string & package,
const std::string & type)
{
PyObject * py_class = PyObject_GetAttrString(module, (char *)type.c_str());
if (py_class == NULL) {
log(ERROR, String::compose("Could not find python class \"%1.%2\"",
package, type));
PyErr_Print();
return NULL;
}
if (PyCallable_Check(py_class) == 0) {
log(ERROR, String::compose("Could not instance python class \"%1.%2\"",
package, type));
Py_DECREF(py_class);
return NULL;
}
if (PyType_Check(py_class) == 0) {
log(ERROR, String::compose("PyCallable_Check returned true, "
"but PyType_Check returned false \"%1.%2\"",
package, type));
Py_DECREF(py_class);
return NULL;
}
return py_class;
}
PyObject * Get_PyModule(const std::string & package)
{
PyObject * package_name = PyString_FromString((char *)package.c_str());
PyObject * module = PyImport_Import(package_name);
Py_DECREF(package_name);
if (module == NULL) {
log(ERROR, String::compose("Missing python module \"%1\"", package));
PyErr_Print();
}
return module;
}