cyphesis/tests/PythonArithmeticFactoryTest.cpp

233 lines
6.4 KiB
C++
Raw Normal View History

// 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
2011-02-15 09:30:46 +00:00
#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"
2015-05-28 22:05:09 +02:00
#include <Atlas/Objects/SmartPtr.h>
#include <Atlas/Objects/Root.h>
#include <cassert>
2018-07-24 21:18:50 +02:00
#include <external/pycxx/CXX/Extensions.hxx>
2018-07-24 21:18:50 +02:00
struct TestMod : public Py::ExtensionModule<TestMod>
{
TestMod() : ExtensionModule("testmod")
{
initialize("testmod");
}
};
2018-05-04 19:17:55 +02:00
int main()
{
2018-07-24 21:18:50 +02:00
PyImport_AppendInittab("testmod", []() {
auto module = new TestMod();
return module->module().ptr();
});
Py_Initialize();
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"
2018-05-04 19:17:55 +02:00
" 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);
Ref<Entity> e = new Entity("1", 1);
as = paf.newScript(e);
assert(as != 0);
Py_Finalize();
return 0;
}
// stubs
2018-07-24 21:18:50 +02:00
#include "stubs/rulesets/python/stubCyPy_LocatedEntity.h"
ArithmeticKit::~ArithmeticKit()
{
}
ArithmeticScript::~ArithmeticScript()
{
}
2018-07-24 21:18:50 +02:00
#include "stubs/rulesets/stubPythonArithmeticScript.h"
#include "stubs/rulesets/stubScript.h"
2018-07-24 21:18:50 +02:00
#include "stubs/rulesets/stubLocation.h"
#include "stubs/rulesets/stubEntity.h"
#define STUB_LocatedEntity_makeContainer
void LocatedEntity::makeContainer()
{
if (m_contains == 0) {
m_contains = new LocatedEntitySet;
}
}
#define STUB_LocatedEntity_changeContainer
void LocatedEntity::changeContainer(LocatedEntity * new_loc)
{
2018-07-24 21:18:50 +02:00
assert(m_location.m_loc);
assert(m_location.m_loc->m_contains != nullptr);
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);
LocatedEntity* oldLoc = m_location.m_loc;
m_location.m_loc = new_loc;
m_location.m_loc->incRef();
assert(m_location.m_loc->checkRef() > 0);
onContainered(oldLoc);
}
#include "stubs/rulesets/stubLocatedEntity.h"
2015-05-28 22:05:09 +02:00
#include "stubs/common/stubRouter.h"
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());
2018-01-30 16:54:49 +01:00
if (py_class == nullptr) {
log(ERROR, String::compose("Could not find python class \"%1.%2\"",
package, type));
PyErr_Print();
2018-01-30 16:54:49 +01:00
return nullptr;
}
if (PyCallable_Check(py_class) == 0) {
log(ERROR, String::compose("Could not instance python class \"%1.%2\"",
package, type));
Py_DECREF(py_class);
2018-01-30 16:54:49 +01:00
return nullptr;
}
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);
2018-01-30 16:54:49 +01:00
return nullptr;
}
return py_class;
}
2018-07-24 21:18:50 +02:00
Py::Module Get_PyModule(const std::string & package)
{
2018-07-24 21:18:50 +02:00
Py::String package_name(package);
PyObject * module = PyImport_Import(package_name.ptr());
2018-01-30 16:54:49 +01:00
if (module == nullptr) {
log(ERROR, String::compose("Missing python module \"%1\"", package));
PyErr_Print();
2018-07-24 21:18:50 +02:00
return Py::Module(nullptr);
}
2018-07-24 21:18:50 +02:00
return Py::Module(module);
}
Py::Object Get_PyClass(const Py::Module& module,
const std::string & package,
const std::string & type)
{
auto py_class = module.getAttr(type);
if (py_class.isNull()) {
log(ERROR, String::compose("Could not find python class \"%1.%2\"",
package, type));
PyErr_Print();
return Py::Null();
}
if (!py_class.isCallable()) {
log(ERROR, String::compose("Could not instance python class \"%1.%2\"",
package, type));
return Py::Null();
}
return py_class;
}