diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d1391537f..1defee8d1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/bind_eressea.c b/src/bind_eressea.c index e6ff82c2d..057c68e8e 100755 --- a/src/bind_eressea.c +++ b/src/bind_eressea.c @@ -1,11 +1,16 @@ #include "bind_eressea.h" #include + +#include "export.h" + #include #include #include -#include +#include +#include + 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; +} diff --git a/src/bind_eressea.h b/src/bind_eressea.h index f8ba00d8c..282e97bd9 100755 --- a/src/bind_eressea.h +++ b/src/bind_eressea.h @@ -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 diff --git a/src/bindings_test.c b/src/bindings_test.c new file mode 100644 index 000000000..f9f535f5a --- /dev/null +++ b/src/bindings_test.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#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; +} diff --git a/src/eressea.pkg b/src/eressea.pkg index 361edafc5..8b42cfbc6 100755 --- a/src/eressea.pkg +++ b/src/eressea.pkg @@ -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); } diff --git a/src/eressea.pkg.c b/src/eressea.pkg.c index b71418e97..cebc27274 100644 --- a/src/eressea.pkg.c +++ b/src/eressea.pkg.c @@ -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; diff --git a/src/export.c b/src/export.c index 2a3e7ea21..42b80da7f 100644 --- a/src/export.c +++ b/src/export.c @@ -11,8 +11,11 @@ #include #include "cJSON.h" -void export_json(stream * out, unsigned int flags) { +#include + +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; } diff --git a/src/export.h b/src/export.h index 1fa23f5c5..5e04379ea 100644 --- a/src/export.h +++ b/src/export.h @@ -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 diff --git a/src/test_eressea.c b/src/test_eressea.c index 2a56c087e..38030e820 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -3,6 +3,7 @@ #include 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());