cyphesis/tests/Py_WorldTimetest.cpp

95 lines
2.6 KiB
C++
Raw Permalink Normal View History

2009-05-28 02:18:41 +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
// $Id$
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-28 02:18:41 +01:00
#include <Python.h>
#include "python_testers.h"
2009-05-28 02:18:41 +01:00
#include "rulesets/Python_API.h"
#include "rulesets/Py_WorldTime.h"
2009-05-28 02:18:41 +01:00
#include <cassert>
static PyObject * null_wrapper(PyObject * self, PyWorldTime * o)
{
if (!PyWorldTime_Check(o)) {
PyErr_SetString(PyExc_TypeError, "Unknown Object type");
return Py_True;
}
#ifdef CYPHESIS_DEBUG
o->time = NULL;
#endif // NDEBUG
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef sabotage_methods[] = {
{"null", (PyCFunction)null_wrapper, METH_O},
{NULL, NULL} /* Sentinel */
};
static void setup_test_functions()
{
PyObject * sabotage = Py_InitModule("sabotage", sabotage_methods);
assert(sabotage != 0);
}
2009-05-28 02:18:41 +01:00
int main()
{
init_python_api("cc4b05b9-4ff9-4127-85b0-300298d16c3c");
2009-05-28 02:18:41 +01:00
setup_test_functions();
PyWorldTime * world_time = newPyWorldTime();
assert(world_time != 0);
run_python_string("from server import WorldTime");
expect_python_error("WorldTime()", PyExc_TypeError);
run_python_string("WorldTime(23)");
// FIXME This started failing with Python 2.7
// run_python_string("WorldTime(23.1)");
2009-05-28 02:18:41 +01:00
run_python_string("w=WorldTime(23)");
run_python_string("w.season");
expect_python_error("w.foo", PyExc_AttributeError);
run_python_string("w.is_now('morning')");
expect_python_error("w.is_now(1)", PyExc_TypeError);
run_python_string("w.seconds()");
2009-05-28 02:18:41 +01:00
#ifdef CYPHESIS_DEBUG
run_python_string("import sabotage");
run_python_string("sabotage.null(w)");
// Hit the assert checks.
expect_python_error("w.is_now('morning')", PyExc_AssertionError);
expect_python_error("w.seconds()", PyExc_AssertionError);
#endif // NDEBUG
2009-05-28 02:18:41 +01:00
shutdown_python_api();
return 0;
}