bindings for the json export.

+ unit test (incomplete).
This commit is contained in:
Enno Rehling 2014-03-13 15:33:44 +01:00
parent e8b28725e1
commit dd3de9a1d7
9 changed files with 88 additions and 11 deletions

View File

@ -59,12 +59,6 @@ TOLUA_BINDING(eressea.pkg bind_eressea.h)
TOLUA_BINDING(settings.pkg bind_settings.h)
ENDIF()
set(TESTS
laws_test.c
economy_test.c
market_test.c
)
set (SERVER_SRC
process.pkg.c
eressea.pkg.c
@ -133,6 +127,7 @@ set(SERVER_TEST_SRC
test_eressea.c
tests.c
tests_test.c
bindings_test.c
economy_test.c
market_test.c
laws_test.c

View File

@ -1,11 +1,16 @@
#include "bind_eressea.h"
#include <platform.h>
#include "export.h"
#include <kernel/types.h>
#include <kernel/config.h>
#include <kernel/save.h>
#include <storage.h>
#include <stream.h>
#include <filestream.h>
void eressea_free_game(void) {
free_gamedata();
@ -23,3 +28,17 @@ int eressea_write_game(const char * filename) {
int eressea_read_orders(const char * filename) {
return readorders(filename);
}
int eressea_export_json(const char * filename, unsigned int flags) {
FILE *F = fopen(filename, "wt");
if (F) {
stream out = { 0 };
int err;
fstream_init(&out, F);
err = export_json(&out, flags);
fstream_done(&out);
return err;
}
perror(filename);
return -1;
}

View File

@ -8,7 +8,7 @@ void eressea_free_game(void);
int eressea_read_game(const char * filename);
int eressea_write_game(const char * filename);
int eressea_read_orders(const char * filename);
int eressea_export_json(const char * filename, unsigned int flags);
#ifdef __cplusplus
}
#endif

26
src/bindings_test.c Normal file
View File

@ -0,0 +1,26 @@
#include <platform.h>
#include <CuTest.h>
#include <stream.h>
#include <memstream.h>
#include "export.h"
#include "bind_eressea.h"
static void test_export(CuTest * tc) {
char buf[1024];
stream out = { 0 };
int err;
mstream_init(&out);
err = export_json(&out, EXPORT_REGIONS);
CuAssertIntEquals(tc, 0, err);
out.api->rewind(out.handle);
out.api->readln(out.handle, buf, sizeof(buf));
CuAssertStrEquals(tc, "{}", buf);
mstream_done(&out);
}
CuSuite *get_bindings_suite(void) {
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_export);
return suite;
}

View File

@ -5,4 +5,5 @@ module eressea {
int eressea_read_game @ read_game(const char * filename);
int eressea_write_game @ write_game(const char * filename);
int eressea_read_orders @ read_orders(const char * filename);
int eressea_export_json @ export(const char * filename, unsigned int flags);
}

View File

@ -1,6 +1,6 @@
/*
** Lua binding: eressea
** Generated automatically by tolua 5.1.3 on Sat Mar 8 10:21:00 2014.
** Generated automatically by tolua 5.1.3 on Thu Mar 13 14:40:47 2014.
*/
#include "tolua.h"
@ -131,6 +131,35 @@ static int tolua_eressea_eressea_read_orders00(lua_State* tolua_S)
#endif
}
/* function: eressea_export_json */
static int tolua_eressea_eressea_export00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isstring(tolua_S,1,0,&tolua_err) ||
!tolua_isnumber(tolua_S,2,0,&tolua_err) ||
!tolua_isnoobj(tolua_S,3,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
const char* filename = ((const char*) tolua_tostring(tolua_S,1,0));
unsigned int flags = ((unsigned int) tolua_tonumber(tolua_S,2,0));
{
int tolua_ret = (int) eressea_export_json(filename,flags);
tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);
}
}
return 1;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'export'.",&tolua_err);
return 0;
#endif
}
/* Open lib function */
LUALIB_API int luaopen_eressea (lua_State* tolua_S)
{
@ -144,6 +173,7 @@ LUALIB_API int luaopen_eressea (lua_State* tolua_S)
tolua_function(tolua_S,"read_game",tolua_eressea_eressea_read_game00);
tolua_function(tolua_S,"write_game",tolua_eressea_eressea_write_game00);
tolua_function(tolua_S,"read_orders",tolua_eressea_eressea_read_orders00);
tolua_function(tolua_S,"export",tolua_eressea_eressea_export00);
tolua_endmodule(tolua_S);
tolua_endmodule(tolua_S);
return 1;

View File

@ -11,8 +11,11 @@
#include <stream.h>
#include "cJSON.h"
void export_json(stream * out, unsigned int flags) {
#include <assert.h>
int export_json(stream * out, unsigned int flags) {
cJSON *json, *root = cJSON_CreateObject();
assert(out && out->api);
if (flags & EXPORT_REGIONS) {
region * r;
cJSON_AddItemToObject(root, "regions", json = cJSON_CreateObject());
@ -50,4 +53,5 @@ void export_json(stream * out, unsigned int flags) {
free(output);
}
cJSON_Delete(root);
return 0;
}

View File

@ -7,6 +7,6 @@
#define EXPORT_UNITS 1<<2
struct stream;
void export_json(struct stream * out, unsigned int flags);
int export_json(struct stream * out, unsigned int flags);
#endif

View File

@ -3,6 +3,7 @@
#include <util/log.h>
CuSuite *get_tests_suite(void);
CuSuite *get_bindings_suite(void);
CuSuite *get_economy_suite(void);
CuSuite *get_laws_suite(void);
CuSuite *get_market_suite(void);
@ -34,6 +35,7 @@ int RunAllTests(void)
/* self-test */
CuSuiteAddSuite(suite, get_tests_suite());
CuSuiteAddSuite(suite, get_bindings_suite());
/* util */
CuSuiteAddSuite(suite, get_base36_suite());
CuSuiteAddSuite(suite, get_bsdstring_suite());