cyphesis/tests/Py_CreatorClientTest.cpp

167 lines
5.5 KiB
C++
Raw Permalink 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"
2010-07-27 18:05:34 +01:00
#include "client/Python_ClientAPI.h"
2018-11-03 16:43:33 +01:00
#include "rules/python/Python_API.h"
#include <cassert>
2018-07-23 16:43:25 +02:00
#include <client/CyPy_CreatorClient.h>
#include <client/ClientConnection.h>
#include <rules/simulation/Entity.h>
2018-11-03 16:43:33 +01:00
#include <rules/simulation/python/CyPy_Server.h>
#include <rules/python/CyPy_Atlas.h>
#include <rules/ai/python/CyPy_Ai.h>
#include <rules/python/CyPy_Common.h>
#include <rules/python/CyPy_Physics.h>
#include <rules/python/CyPy_Rules.h>
2018-07-23 16:43:25 +02:00
#include "external/pycxx/CXX/Objects.hxx"
2011-12-29 12:15:27 +01:00
static bool stub_make_fail = false;
static bool stub_look_fail = false;
static bool stub_lookfor_fail = false;
int main()
{
2018-10-27 13:13:45 +02:00
boost::asio::io_service io_service;
2018-11-03 16:43:33 +01:00
init_python_api({&CyPy_Server::init,
&CyPy_Rules::init,
&CyPy_Atlas::init,
&CyPy_Physics::init,
&CyPy_Common::init,
2018-12-16 23:20:33 +01:00
&CyPy_Ai::init});
extend_client_python_api();
2018-10-27 13:13:45 +02:00
auto client = new CreatorClient("1", "2", *new ClientConnection(io_service));
Ref<MemEntity> entity = new MemEntity("1", 1);
OpVector res;
client->setOwnEntity(res, entity);
2018-07-23 16:43:25 +02:00
Py::Module module("server");
2018-10-27 13:13:45 +02:00
module.setAttr("testclient", CyPy_CreatorClient::wrap(client));
2018-07-23 16:43:25 +02:00
run_python_string("import server");
2018-11-03 16:43:33 +01:00
run_python_string("import ai");
run_python_string("import rules");
2011-12-29 12:15:27 +01:00
run_python_string("import atlas");
2018-07-23 16:43:25 +02:00
expect_python_error("server.CreatorClient(1)", PyExc_RuntimeError);
expect_python_error("server.CreatorClient(\"one\")", PyExc_RuntimeError);
run_python_string("c=server.testclient");
2011-12-29 12:15:27 +01:00
run_python_string("c.as_entity()");
2018-07-23 16:43:25 +02:00
expect_python_error("c.make()", PyExc_IndexError);
expect_python_error("c.make('1')", PyExc_TypeError);
2011-12-29 12:15:27 +01:00
run_python_string("c.make(atlas.Entity('1'))");
stub_make_fail = true;
expect_python_error("c.make(atlas.Entity('1'))", PyExc_RuntimeError);
2011-12-29 12:15:27 +01:00
stub_make_fail = false;
run_python_string("c.set('1', atlas.Entity('1'))");
expect_python_error("c.set('1', 'not an entity')", PyExc_TypeError);
expect_python_error("c.set(1, atlas.Entity('1'))", PyExc_TypeError);
2011-12-29 12:15:27 +01:00
run_python_string("c.look('1')");
stub_look_fail = true;
expect_python_error("c.look('1')", PyExc_RuntimeError);
2011-12-29 12:15:27 +01:00
stub_look_fail = false;
expect_python_error("c.look(1)", PyExc_TypeError);
2011-12-29 12:15:27 +01:00
run_python_string("e=c.look('1')");
2018-07-29 21:10:22 +02:00
run_python_string("assert type(e) == server.Thing");
2011-12-29 12:15:27 +01:00
run_python_string("c.look_for(atlas.Entity('1'))");
stub_lookfor_fail = true;
run_python_string("c.look_for(atlas.Entity('1'))");
stub_lookfor_fail = false;
expect_python_error("c.look_for('1')", PyExc_TypeError);
2012-01-03 16:45:34 +00:00
run_python_string("c.send(atlas.Operation('info'))");
expect_python_error("c.send('info')", PyExc_TypeError);
2018-07-23 16:43:25 +02:00
expect_python_error("c.send()", PyExc_IndexError);
2011-12-29 12:15:27 +01:00
run_python_string("c.delete('1')");
expect_python_error("c.delete(1)", PyExc_TypeError);
2018-07-23 16:43:25 +02:00
expect_python_error("c.delete()", PyExc_IndexError);
run_python_string("assert c == server.testclient");
2011-12-29 12:15:27 +01:00
2018-11-03 16:43:33 +01:00
run_python_string("assert type(c.map) == ai.MemMap");
run_python_string("assert type(c.entity.location) == rules.Location");
2018-12-16 23:20:33 +01:00
run_python_string("assert type(c.time) == rules.WorldTime");
expect_python_error("c.foo", PyExc_AttributeError);
expect_python_error("c.foo_operation", PyExc_AttributeError);
2011-12-29 12:15:27 +01:00
run_python_string("c.foo = 1");
run_python_string("assert c.foo == 1");
2018-07-23 16:43:25 +02:00
run_python_string("c.foo = [1,2]");
expect_python_error("c.map = 1", PyExc_AttributeError);
shutdown_python_api();
return 0;
}
// stubs
#include "client/ObserverClient.h"
#include "client/CreatorClient.h"
#include "rules/simulation/Entity.h"
2011-12-29 12:15:27 +01:00
#include "common/id.h"
#include <Atlas/Objects/Operation.h>
2011-12-29 12:15:27 +01:00
#include <Atlas/Objects/RootEntity.h>
using Atlas::Objects::Entity::RootEntity;
2018-10-27 13:13:45 +02:00
#define STUB_CharacterClient_look
Ref<LocatedEntity> CharacterClient::look(const std::string & id)
{
2011-12-29 12:15:27 +01:00
if (stub_look_fail) {
2018-07-23 16:43:25 +02:00
return nullptr;
2011-12-29 12:15:27 +01:00
}
return new Entity(id, integerId(id));
}
2018-10-27 13:13:45 +02:00
#define STUB_CharacterClient_lookFor
Ref<LocatedEntity> CharacterClient::lookFor(const RootEntity & entity)
{
2011-12-29 12:15:27 +01:00
if (stub_lookfor_fail) {
2018-07-23 16:43:25 +02:00
return nullptr;
2011-12-29 12:15:27 +01:00
}
return new Entity(entity->getId(), integerId(entity->getId()));
}
2018-10-27 13:13:45 +02:00
#define STUB_CreatorClient_make
Ref<LocatedEntity> CreatorClient::make(const RootEntity & entity)
{
2011-12-29 12:15:27 +01:00
if (stub_make_fail) {
2018-07-23 16:43:25 +02:00
return nullptr;
2011-12-29 12:15:27 +01:00
}
return new Entity(entity->getId(), integerId(entity->getId()));
}
2018-10-27 13:13:45 +02:00
#include "stubs/client/stubCreatorClient.h"
#include "stubs/client/stubCharacterClient.h"
#include "stubs/client/stubObserverClient.h"
#include "stubs/client/stubBaseClient.h"
#include "stubs/client/stubClientConnection.h"