add a config.reset function to clear configuration state before tests.

This commit is contained in:
Enno Rehling 2014-06-17 08:46:22 -07:00
parent 0675c50c7f
commit b6b790d7e7
8 changed files with 55 additions and 8 deletions

View File

@ -4,9 +4,27 @@
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/jsonconf.h> #include <kernel/jsonconf.h>
#include <util/log.h> #include <util/log.h>
#include <util/language.h>
#include <cJSON.h> #include <cJSON.h>
#include <string.h> #include <string.h>
#include "kernel/building.h"
#include "kernel/race.h"
#include "kernel/ship.h"
#include "kernel/spell.h"
#include "kernel/spellbook.h"
#include "kernel/terrain.h"
void config_reset(void) {
default_locale = 0;
free_locales();
free_spells();
free_buildingtypes();
free_shiptypes();
free_races();
free_spellbooks();
}
int config_parse(const char *json) int config_parse(const char *json)
{ {
cJSON * conf = cJSON_Parse(json); cJSON * conf = cJSON_Parse(json);
@ -26,8 +44,8 @@ int config_parse(const char *json)
strncpy(buffer, xp, sizeof(buffer)); strncpy(buffer, xp, sizeof(buffer));
buffer[9] = 0; buffer[9] = 0;
log_error("json parse error in line %d, position %d, near `%s`\n", line, ep-lp, buffer); log_error("json parse error in line %d, position %d, near `%s`\n", line, ep-lp, buffer);
return 1;
} }
return 1;
} }
int config_read(const char *filename) int config_read(const char *filename)

View File

@ -4,6 +4,7 @@
extern "C" { extern "C" {
#endif #endif
void config_reset(void);
int config_parse(const char *json); int config_parse(const char *json);
int config_read(const char *filename); int config_read(const char *filename);

View File

@ -2,6 +2,7 @@ $#include "bind_config.h"
module eressea { module eressea {
module config { module config {
void config_reset @ reset(void);
void config_read @ read(const char *filename); void config_read @ read(const char *filename);
void config_parse @ parse(const char *json); void config_parse @ parse(const char *json);
} }

View File

@ -25,6 +25,30 @@ static void tolua_reg_types (lua_State* tolua_S)
{ {
} }
/* function: config_reset */
static int tolua_config_eressea_config_reset00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
tolua_Error tolua_err;
if (
!tolua_isnoobj(tolua_S,1,&tolua_err)
)
goto tolua_lerror;
else
#endif
{
{
config_reset();
}
}
return 0;
#ifndef TOLUA_RELEASE
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'reset'.",&tolua_err);
return 0;
#endif
}
/* function: config_read */ /* function: config_read */
static int tolua_config_eressea_config_read00(lua_State* tolua_S) static int tolua_config_eressea_config_read00(lua_State* tolua_S)
{ {
@ -88,6 +112,7 @@ LUALIB_API int luaopen_config (lua_State* tolua_S)
tolua_beginmodule(tolua_S,"eressea"); tolua_beginmodule(tolua_S,"eressea");
tolua_module(tolua_S,"config",0); tolua_module(tolua_S,"config",0);
tolua_beginmodule(tolua_S,"config"); tolua_beginmodule(tolua_S,"config");
tolua_function(tolua_S,"reset",tolua_config_eressea_config_reset00);
tolua_function(tolua_S,"read",tolua_config_eressea_config_read00); tolua_function(tolua_S,"read",tolua_config_eressea_config_read00);
tolua_function(tolua_S,"parse",tolua_config_eressea_config_parse00); tolua_function(tolua_S,"parse",tolua_config_eressea_config_parse00);
tolua_endmodule(tolua_S); tolua_endmodule(tolua_S);

View File

@ -2927,13 +2927,6 @@ void free_gamedata(void)
free_units(); free_units();
free_regions(); free_regions();
free_borders(); free_borders();
default_locale = 0;
free_locales();
free_spells();
free_buildingtypes();
free_shiptypes();
free_races();
free_spellbooks();
for (i=0;i!=MAXLOCALES;++i) { for (i=0;i!=MAXLOCALES;++i) {
if (defaults[i]) { if (defaults[i]) {

View File

@ -60,6 +60,13 @@ void test_cleanup(void)
test_clear_resources(); test_clear_resources();
global.functions.maintenance = NULL; global.functions.maintenance = NULL;
global.functions.wage = NULL; global.functions.wage = NULL;
default_locale = 0;
free_locales();
free_spells();
free_buildingtypes();
free_shiptypes();
free_races();
free_spellbooks();
free_gamedata(); free_gamedata();
} }

View File

@ -4,6 +4,7 @@ module("tests.eressea.config", package.seeall, lunit.testcase )
function setup() function setup()
eressea.free_game() eressea.free_game()
eressea.config.reset();
end end
function test_read_race() function test_read_race()

View File

@ -40,6 +40,7 @@ function setup()
} }
}]] }]]
eressea.config.reset()
eressea.config.parse(conf) eressea.config.parse(conf)
eressea.locale.create("en") eressea.locale.create("en")
end end