cyphesis/tests/Py_MessageTest.cpp

168 lines
5.8 KiB
C++
Raw Permalink Normal View History

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
#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>
#include "python_testers.h"
2009-05-27 15:04:55 +01:00
#include "rulesets/Python_API.h"
#include "rulesets/Py_Message.h"
2009-05-27 15:04:55 +01:00
#include <cassert>
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;
}
#ifdef CYPHESIS_DEBUG
2018-01-30 16:54:49 +01:00
o->m_obj = nullptr;
#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 */
};
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-05-27 15:04:55 +01:00
int main()
{
2018-05-04 19:17:55 +02:00
PyImport_AppendInittab("sabotage", &init_sabotage);
init_python_api("16799987-a321-43a2-aa66-f7bc8ed8e9b2");
2009-05-27 15:04:55 +01: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);
run_python_string("Message([Message(1)])");
2012-01-04 13:59:18 +00:00
expect_python_error("Message([Vector3D()])", PyExc_TypeError);
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
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);
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);
run_python_string("m.foo = 1");
2018-05-04 19:17:55 +02:00
run_python_string("print(m.foo)");
run_python_string("m.foo = 1.1");
2018-05-04 19:17:55 +02:00
run_python_string("print(m.foo)");
run_python_string("m.foo = '1'");
2018-05-04 19:17:55 +02:00
run_python_string("print(m.foo)");
run_python_string("m.foo = ['1']");
2018-05-04 19:17:55 +02:00
run_python_string("print(m.foo)");
run_python_string("m.foo = {'foo': 1}");
2018-05-04 19:17:55 +02:00
run_python_string("print(m.foo)");
run_python_string("m=Message(1)");
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");
run_python_string("m=Message(1.0)");
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");
run_python_string("m=Message('1')");
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'");
run_python_string("m=Message([])");
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'");
run_python_string("m=Message({})");
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
#ifdef CYPHESIS_DEBUG
run_python_string("import sabotage");
run_python_string("get_name_method=m.get_name");
run_python_string("sabotage.null(m)");
// 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);
#endif // NDEBUG
2009-05-27 15:04:55 +01:00
shutdown_python_api();
return 0;
}