2009-05-27 15:04:55 +01:00
|
|
|
// 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
|
2011-02-16 09:00:40 +00:00
|
|
|
#else
|
|
|
|
|
#define CYPHESIS_DEBUG
|
2011-02-15 09:30:46 +00:00
|
|
|
#endif
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
|
#define DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
|
2009-05-27 15:04:55 +01:00
|
|
|
#include <Python.h>
|
|
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
#include "python_testers.h"
|
|
|
|
|
|
2009-05-27 15:04:55 +01:00
|
|
|
#include "rulesets/Python_API.h"
|
2009-11-11 07:13:40 +00:00
|
|
|
#include "rulesets/Py_Message.h"
|
2009-05-27 15:04:55 +01:00
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
2009-11-11 07:13:40 +00:00
|
|
|
static PyObject * null_wrapper(PyObject * self, PyMessage * o)
|
|
|
|
|
{
|
|
|
|
|
if (!PyMessage_Check(o)) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Unknown Object type");
|
2018-01-30 16:54:49 +01:00
|
|
|
return nullptr;
|
2009-11-11 07:13:40 +00:00
|
|
|
}
|
2011-02-16 09:00:40 +00:00
|
|
|
#ifdef CYPHESIS_DEBUG
|
2018-01-30 16:54:49 +01:00
|
|
|
o->m_obj = nullptr;
|
2009-11-11 07:13:40 +00:00
|
|
|
#endif // NDEBUG
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyMethodDef sabotage_methods[] = {
|
|
|
|
|
{"null", (PyCFunction)null_wrapper, METH_O},
|
2018-01-30 16:54:49 +01:00
|
|
|
{nullptr, nullptr} /* Sentinel */
|
2009-11-11 07:13:40 +00:00
|
|
|
};
|
|
|
|
|
|
2018-05-04 19:17:55 +02:00
|
|
|
static PyObject* init_sabotage() {
|
|
|
|
|
static struct PyModuleDef def = {
|
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
|
"sabotage",
|
|
|
|
|
nullptr,
|
|
|
|
|
0,
|
|
|
|
|
sabotage_methods,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr,
|
|
|
|
|
nullptr
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return PyModule_Create(&def);
|
2009-11-11 07:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
2009-05-27 15:04:55 +01:00
|
|
|
int main()
|
|
|
|
|
{
|
2018-05-04 19:17:55 +02:00
|
|
|
PyImport_AppendInittab("sabotage", &init_sabotage);
|
2012-01-19 19:34:20 +00:00
|
|
|
init_python_api("16799987-a321-43a2-aa66-f7bc8ed8e9b2");
|
2009-05-27 15:04:55 +01:00
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("from atlas import Message");
|
|
|
|
|
run_python_string("from atlas import Operation");
|
|
|
|
|
run_python_string("from atlas import Oplist");
|
|
|
|
|
run_python_string("from atlas import Location");
|
|
|
|
|
run_python_string("from physics import Vector3D");
|
|
|
|
|
run_python_string("Message()");
|
|
|
|
|
run_python_string("Message(1)");
|
|
|
|
|
run_python_string("Message(1.1)");
|
|
|
|
|
run_python_string("Message('1')");
|
|
|
|
|
run_python_string("Message([1, 1])");
|
|
|
|
|
run_python_string("Message((1, 1))");
|
|
|
|
|
run_python_string("Message({'foo': 1})");
|
|
|
|
|
run_python_string("Message(Message(1))");
|
|
|
|
|
run_python_string("Message(Operation('get'))");
|
|
|
|
|
run_python_string("Message(Oplist(Operation('get')))");
|
|
|
|
|
run_python_string("Message(Location())");
|
2012-01-04 13:59:18 +00:00
|
|
|
expect_python_error("Message(Vector3D())", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("Message([Message(1)])");
|
2012-01-04 13:59:18 +00:00
|
|
|
expect_python_error("Message([Vector3D()])", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("Message({'foo': Message(1)})");
|
2012-01-04 13:59:18 +00:00
|
|
|
expect_python_error("Message({'foo': Vector3D()})", PyExc_TypeError);
|
|
|
|
|
expect_python_error("Message(1, 1)", PyExc_TypeError);
|
2009-05-27 15:04:55 +01:00
|
|
|
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("m=Message(1)");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("print(m.get_name())");
|
|
|
|
|
expect_python_error("print(m.foo)", PyExc_AttributeError);
|
2012-01-04 13:59:18 +00:00
|
|
|
expect_python_error("m.foo = 1", PyExc_AttributeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("m=Message({})");
|
2018-05-04 19:17:55 +02:00
|
|
|
expect_python_error("print(m.foo)", PyExc_AttributeError);
|
2012-01-04 13:59:18 +00:00
|
|
|
expect_python_error("m.foo = Vector3D()", PyExc_TypeError);
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("m.foo = 1");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("print(m.foo)");
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("m.foo = 1.1");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("print(m.foo)");
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("m.foo = '1'");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("print(m.foo)");
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("m.foo = ['1']");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("print(m.foo)");
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("m.foo = {'foo': 1}");
|
2018-05-04 19:17:55 +02:00
|
|
|
run_python_string("print(m.foo)");
|
2011-09-02 14:30:15 +01:00
|
|
|
run_python_string("m=Message(1)");
|
2011-09-02 19:04:33 +01:00
|
|
|
run_python_string("assert m == 1");
|
|
|
|
|
run_python_string("assert not m == 1.0");
|
|
|
|
|
run_python_string("assert not m == '1'");
|
|
|
|
|
run_python_string("assert not m != 1");
|
|
|
|
|
run_python_string("assert m != 1.0");
|
|
|
|
|
run_python_string("assert m != '1'");
|
|
|
|
|
run_python_string("assert m != 2");
|
2011-09-02 14:30:15 +01:00
|
|
|
run_python_string("m=Message(1.0)");
|
2011-09-02 19:04:33 +01:00
|
|
|
run_python_string("assert not m == 1");
|
|
|
|
|
run_python_string("assert m == 1.0");
|
|
|
|
|
run_python_string("assert not m == '1'");
|
|
|
|
|
run_python_string("assert m != 1");
|
|
|
|
|
run_python_string("assert not m != 1.0");
|
|
|
|
|
run_python_string("assert m != '1'");
|
|
|
|
|
run_python_string("assert m != 2.0");
|
2011-09-02 14:30:15 +01:00
|
|
|
run_python_string("m=Message('1')");
|
2011-09-02 19:04:33 +01:00
|
|
|
run_python_string("assert not m == 1");
|
|
|
|
|
run_python_string("assert not m == 1.0");
|
|
|
|
|
run_python_string("assert m == '1'");
|
|
|
|
|
run_python_string("assert m != 1");
|
|
|
|
|
run_python_string("assert m != 1.0");
|
|
|
|
|
run_python_string("assert not m != '1'");
|
|
|
|
|
run_python_string("assert m != '2'");
|
2011-09-02 14:30:15 +01:00
|
|
|
run_python_string("m=Message([])");
|
2011-09-02 19:04:33 +01:00
|
|
|
run_python_string("assert not m == 1");
|
|
|
|
|
run_python_string("assert not m == 1.0");
|
|
|
|
|
run_python_string("assert not m == '1'");
|
|
|
|
|
run_python_string("assert m != 1");
|
|
|
|
|
run_python_string("assert m != 1.0");
|
|
|
|
|
run_python_string("assert m != '1'");
|
2011-09-02 14:30:15 +01:00
|
|
|
run_python_string("m=Message({})");
|
2011-09-02 19:04:33 +01:00
|
|
|
run_python_string("assert not m == 1");
|
|
|
|
|
run_python_string("assert not m == 1.0");
|
|
|
|
|
run_python_string("assert not m == '1'");
|
|
|
|
|
run_python_string("assert m != 1");
|
|
|
|
|
run_python_string("assert m != 1.0");
|
|
|
|
|
run_python_string("assert m != '1'");
|
2009-05-27 15:04:55 +01:00
|
|
|
|
2011-02-16 09:00:40 +00:00
|
|
|
#ifdef CYPHESIS_DEBUG
|
2009-11-20 20:18:53 +00:00
|
|
|
run_python_string("import sabotage");
|
|
|
|
|
run_python_string("get_name_method=m.get_name");
|
|
|
|
|
run_python_string("sabotage.null(m)");
|
2009-11-11 07:13:40 +00:00
|
|
|
// Hit the assert checks.
|
2018-05-04 19:17:55 +02:00
|
|
|
expect_python_error("print(get_name_method())", PyExc_AssertionError);
|
|
|
|
|
expect_python_error("print(m.foo)", PyExc_AssertionError);
|
2012-01-04 13:59:18 +00:00
|
|
|
expect_python_error("m.foo = 1", PyExc_AssertionError);
|
2009-11-11 07:13:40 +00:00
|
|
|
#endif // NDEBUG
|
|
|
|
|
|
2009-05-27 15:04:55 +01:00
|
|
|
shutdown_python_api();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|