cyphesis/tests/Py_ObserverClientTest.cpp

158 lines
4.8 KiB
C++
Raw Permalink Normal View History

2009-11-26 01:37:47 +00: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
#endif
#ifndef DEBUG
#define DEBUG
#endif
2009-11-26 01:37:47 +00:00
#include <Python.h>
#include "python_testers.h"
2010-07-27 18:05:34 +01:00
#include "client/Python_ClientAPI.h"
2018-10-27 13:13:45 +02:00
#include "client/ObserverClient.h"
#include "client/CyPy_ObserverClient.h"
2010-07-27 18:05:34 +01:00
2018-11-03 16:43:33 +01:00
#include "rules/python/Python_API.h"
2009-11-26 01:37:47 +00:00
#include <cassert>
2018-11-03 16:43:33 +01:00
#include <rules/simulation/python/CyPy_Server.h>
#include <rules/python/CyPy_Atlas.h>
2009-11-26 01:37:47 +00:00
2011-12-29 17:50:27 +00:00
static bool stub_setup_fail = false;
static bool stub_createCharacter_fail = false;
static bool stub_send_wait_fail = false;
static int stub_send_wait_results = 0;
static bool stub_wait_fail = false;
2009-11-26 01:37:47 +00:00
int main()
{
2018-10-27 13:13:45 +02:00
boost::asio::io_service io_service;
2018-12-16 23:20:33 +01:00
init_python_api({&CyPy_Server::init, &CyPy_Atlas::init});
2009-11-26 01:37:47 +00:00
extend_client_python_api();
2018-10-27 13:13:45 +02:00
auto client = new ObserverClient(io_service);
Py::Module module("server");
module.setAttr("testclient", CyPy_ObserverClient::wrap(client));
2011-12-29 17:50:27 +00:00
run_python_string("import atlas");
2009-11-26 01:37:47 +00:00
run_python_string("import server");
2011-12-29 17:50:27 +00:00
run_python_string("import types");
2018-10-27 13:13:45 +02:00
run_python_string("o=server.testclient");
2011-12-29 17:50:27 +00:00
run_python_string("o.setup()");
2018-07-23 16:43:25 +02:00
expect_python_error("o.setup('bob')", PyExc_IndexError);
2011-12-29 17:50:27 +00:00
run_python_string("o.setup('bob', 'jim')");
run_python_string("o.setup('bob', 'jim', 'settler')");
stub_setup_fail = true;
expect_python_error("o.setup('bob', 'jim', 'settler')",
PyExc_RuntimeError);
2011-12-29 17:50:27 +00:00
stub_setup_fail = false;
run_python_string("o.create_avatar('settler')");
expect_python_error("o.create_avatar(1)", PyExc_TypeError);
2011-12-29 17:50:27 +00:00
run_python_string("o.run()");
2018-07-23 16:43:25 +02:00
expect_python_error("o.send()", PyExc_IndexError);
expect_python_error("o.send('get')", PyExc_TypeError);
2011-12-29 17:50:27 +00:00
run_python_string("o.send(atlas.Operation('get'))");
2018-07-23 16:43:25 +02:00
expect_python_error("o.send_wait()", PyExc_IndexError);
expect_python_error("o.send_wait('get')", PyExc_TypeError);
2011-12-29 17:50:27 +00:00
run_python_string("o.send_wait(atlas.Operation('get'))");
stub_send_wait_results = 1;
run_python_string("assert type(o.send_wait(atlas.Operation('get'))) == atlas.Operation");
stub_send_wait_results = 2;
run_python_string("assert type(o.send_wait(atlas.Operation('get'))) == atlas.Oplist");
run_python_string("assert len(o.send_wait(atlas.Operation('get'))) == 2");
stub_send_wait_fail = true;
// FIXME This really should fail
// expect_python_error("o.send_wait(atlas.Operation('get'))",
// PyExc_AssertionError);
2011-12-29 17:50:27 +00:00
run_python_string("o.wait()");
stub_wait_fail = true;
expect_python_error("o.wait()", PyExc_RuntimeError);
2018-05-04 19:17:55 +02:00
run_python_string("assert type(o.id) == str");
2011-12-29 17:50:27 +00:00
run_python_string("o.character");
run_python_string("o.server = 'foo'");
expect_python_error("o.server = 23", PyExc_TypeError);
2011-12-29 17:50:27 +00:00
2009-11-26 01:37:47 +00:00
shutdown_python_api();
return 0;
}
// stubs
#include "client/ObserverClient.h"
#include "client/CreatorClient.h"
#include <Atlas/Objects/Operation.h>
using Atlas::Objects::Entity::RootEntity;
2018-10-27 13:13:45 +02:00
#include "stubs/client/stubCharacterClient.h"
#include "stubs/client/stubCreatorClient.h"
2018-10-27 13:13:45 +02:00
#define STUB_ObserverClient_setup
int ObserverClient::setup(const std::string & account , const std::string & password , const std::string & avatar )
{
2011-12-29 17:50:27 +00:00
if (stub_setup_fail) {
return -1;
}
return 0;
}
2018-10-27 13:13:45 +02:00
#include "stubs/client/stubObserverClient.h"
2018-10-27 13:13:45 +02:00
#define STUB_BaseClient_createCharacter
CreatorClient * BaseClient::createCharacter(const std::string & type)
{
2011-12-29 17:50:27 +00:00
if (stub_createCharacter_fail) {
return 0;
}
2018-10-27 13:13:45 +02:00
return new CreatorClient("1", "2", m_connection);
}
2018-10-27 13:13:45 +02:00
#include "stubs/client/stubBaseClient.h"
2018-10-27 13:13:45 +02:00
#define STUB_ClientConnection_wait
int ClientConnection::wait()
{
2011-12-29 17:50:27 +00:00
if (stub_wait_fail) {
return -1;
}
return 0;
}
2018-10-27 13:13:45 +02:00
#define STUB_ClientConnection_sendAndWaitReply
int ClientConnection::sendAndWaitReply(const Operation & op, OpVector & res)
{
2011-12-29 17:50:27 +00:00
if (stub_send_wait_fail) {
return -1;
}
for (int i = 0; i < stub_send_wait_results; ++i) {
res.push_back(Atlas::Objects::Operation::Info());
}
return 0;
}
2018-10-27 13:13:45 +02:00
#include "stubs/client/stubClientConnection.h"