adding a lot of test coverage

extending the binding generation to more processing functions
This commit is contained in:
Enno Rehling 2012-06-07 09:47:02 -07:00
parent 8b3d735e8c
commit dafb2fc2ab
17 changed files with 242 additions and 59 deletions

7
scripts/default.lua Normal file → Executable file
View File

@ -26,7 +26,7 @@ function nmr_check(maxnmrs)
if nmrs >= maxnmrs then if nmrs >= maxnmrs then
print("Shit. More than " .. maxnmrs .. " factions with 1 NMR (" .. nmrs .. ")") print("Shit. More than " .. maxnmrs .. " factions with 1 NMR (" .. nmrs .. ")")
write_summary() write_summary()
write_game("aborted.dat") eressea.write_game("aborted.dat")
return -1 return -1
end end
print (nmrs .. " Factions with 1 NMR") print (nmrs .. " Factions with 1 NMR")
@ -35,10 +35,7 @@ end
function open_game(turn) function open_game(turn)
file = "" .. get_turn() file = "" .. get_turn()
if read_game(file .. ".dat", "binary")~=0 then return eressea.read_game(file .. ".dat")
return read_game(file, "text")
end
return 0
end end
function write_emails(locales) function write_emails(locales)

2
scripts/init.lua Normal file → Executable file
View File

@ -8,7 +8,7 @@ function run_editor()
turn = read_turn() turn = read_turn()
set_turn(turn) set_turn(turn)
end end
read_game(turn .. ".dat") eressea.read_game(turn .. ".dat")
gmtool.editor() gmtool.editor()
end end

28
scripts/tests/bindings.lua Executable file
View File

@ -0,0 +1,28 @@
require "lunit"
local eressea = eressea
local _G = _G
module("tests.bindings", lunit.testcase)
function test_eressea()
assert_equal("function", _G.type(eressea.free_game))
assert_equal("function", _G.type(eressea.read_game))
assert_equal("function", _G.type(eressea.write_game))
assert_equal("function", _G.type(eressea.read_orders))
end
function test_process()
assert_equal("function", _G.type(eressea.process.markets))
assert_equal("function", _G.type(eressea.process.produce))
assert_equal("function", _G.type(eressea.process.make_temp))
assert_equal("function", _G.type(eressea.process.settings))
assert_equal("function", _G.type(eressea.process.set_group))
assert_equal("function", _G.type(eressea.process.set_origin))
assert_equal("function", _G.type(eressea.process.quit))
end
function test_settings()
assert_equal("function", _G.type(eressea.settings.set))
assert_equal("function", _G.type(eressea.settings.get))
end

View File

@ -912,7 +912,7 @@ function test_parser()
file:write("BENENNEN EINHEIT 'Goldene Herde'\n") file:write("BENENNEN EINHEIT 'Goldene Herde'\n")
file:close() file:close()
read_orders(filename) eressea.read_orders(filename)
process_orders() process_orders()
assert_equal("Goldene Herde", u.name) assert_equal("Goldene Herde", u.name)
end end
@ -975,7 +975,7 @@ function test_bug_1814()
file:write("; ARBEITE\n") file:write("; ARBEITE\n")
file:close() file:close()
read_orders(filename) eressea.read_orders(filename)
process_orders() process_orders()
init_reports() init_reports()
write_report(f) write_report(f)
@ -996,7 +996,7 @@ function test_bug_1679()
file:write("ARBEITEN\n") file:write("ARBEITEN\n")
file:close() file:close()
read_orders(filename) eressea.read_orders(filename)
process_orders() process_orders()
init_reports() init_reports()
write_report(f) write_report(f)
@ -1103,7 +1103,7 @@ function test_bug_1875_use_normal()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
r:set_resource("peasant", 0) r:set_resource("peasant", 0)
settings.set("rules.economy.food", "0") -- food is not free eressea.settings.set("rules.economy.food", "0") -- food is not free
local f = faction.create("noreply@eressea.de", "demon", "de") local f = faction.create("noreply@eressea.de", "demon", "de")
local u = unit.create(f, r, 1) local u = unit.create(f, r, 1)
@ -1126,7 +1126,7 @@ function test_bug_1875_use_help()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
r:set_resource("peasant", 0) r:set_resource("peasant", 0)
settings.set("rules.economy.food", "0") -- food is not free eressea.settings.set("rules.economy.food", "0") -- food is not free
local f = faction.create("noreply@eressea.de", "demon", "de") local f = faction.create("noreply@eressea.de", "demon", "de")
local u = unit.create(f, r, 1) local u = unit.create(f, r, 1)
@ -1153,7 +1153,7 @@ function test_bug_1875_use_own_first()
local r = region.create(0, 0, "plain") local r = region.create(0, 0, "plain")
r:set_resource("peasant", 0) r:set_resource("peasant", 0)
settings.set("rules.economy.food", "0") -- food is not free eressea.settings.set("rules.economy.food", "0") -- food is not free
local f = faction.create("noreply@eressea.de", "demon", "de") local f = faction.create("noreply@eressea.de", "demon", "de")
local u = unit.create(f, r, 1) local u = unit.create(f, r, 1)

View File

@ -33,3 +33,75 @@ function test_give()
assert_not_equal(5, u:get_item("money")) assert_not_equal(5, u:get_item("money"))
assert_not_equal(5, u2:get_item("money")) assert_not_equal(5, u2:get_item("money"))
end end
function test_make_temp()
u:add_order("MACHE TEMP 123 'Herpderp'")
u:add_order("// this comment will be copied")
u:add_order("ENDE")
eressea.process.make_temp()
for x in f.units do
if x.name == 'Herpderp' then u=x end
end
assert_equal('Herpderp', u.name)
assert_equal(0, u.number)
local c = 0
for o in u.orders do
assert_equal('// this comment will be copied', o)
c = c + 1
end
assert_equal(1, c)
end
function test_give_temp()
u.number = 2
u:add_order("GIB TEMP 123 1 PERSON")
u:add_order("MACHE TEMP 123 'Herpderp'")
u:add_order("ENDE")
_G.process_orders()
assert_equal(1, u.number)
for x in f.units do
if x.name == 'Herpderp' then u=x end
end
assert_equal('Herpderp', u.name)
assert_equal(1, u.number)
end
function test_process_settings()
f.options = 0
u:add_order("EMAIL herp@derp.com")
u:add_order("BANNER 'Herpderp'")
u:add_order("PASSWORT 'HerpDerp'")
u:add_order("OPTION AUSWERTUNG")
eressea.process.settings()
assert_equal("herp@derp.com", f.email)
assert_equal("Herpderp", f.info)
assert_equal("HerpDerp", f.password)
assert_equal(1, f.options)
end
function test_process_group()
u:add_order("GRUPPE herp")
eressea.process.set_group()
assert_equal('herp', u.group)
end
function test_process_origin()
u:add_order("URSPRUNG 1 2")
eressea.process.set_origin()
x, y = u.faction:get_origin()
assert_equal(1, x)
assert_equal(2, y)
end
function test_process_quit()
fno = f.id
u:add_order("STIRB '" .. u.faction.password .. "'")
assert_not_equal(nil, _G.get_faction(fno))
eressea.process.quit()
eressea.write_game('test.dat')
eressea.free_game()
eressea.read_game('test.dat')
assert_equal(nil, _G.get_faction(fno))
end

View File

@ -15,5 +15,10 @@ int eressea_read_game(const char * filename) {
} }
int eressea_write_game(const char * filename) { int eressea_write_game(const char * filename) {
remove_empty_factions();
return writegame(filename, IO_BINARY); return writegame(filename, IO_BINARY);
} }
int eressea_read_orders(const char * filename) {
return readorders(filename);
}

View File

@ -7,6 +7,7 @@ extern "C" {
void eressea_free_game(void); void eressea_free_game(void);
int eressea_read_game(const char * filename); int eressea_read_game(const char * filename);
int eressea_write_game(const char * filename); int eressea_write_game(const char * filename);
int eressea_read_orders(const char * filename);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -2,8 +2,11 @@
#include <platform.h> #include <platform.h>
#include <kernel/types.h> #include <kernel/types.h>
#include <kernel/order.h>
#include <kernel/region.h> #include <kernel/region.h>
#include <kernel/unit.h>
#include <gamecode/economy.h> #include <gamecode/economy.h>
#include <gamecode/laws.h>
#include <gamecode/market.h> #include <gamecode/market.h>
void process_produce(void) { void process_produce(void) {
@ -16,3 +19,61 @@ void process_produce(void) {
void process_markets(void) { void process_markets(void) {
do_markets(); do_markets();
} }
void process_make_temp(void) {
new_units();
}
void process_settings(void) {
region * r;
for (r=regions; r; r=r->next) {
unit * u;
for (u=r->units; u; u=u->next) {
order * ord;
for (ord=u->orders; ord; ord=ord->next) {
keyword_t kwd = get_keyword(ord);
if (kwd==K_BANNER) {
banner_cmd(u, ord);
}
else if (kwd==K_EMAIL) {
email_cmd(u, ord);
}
else if (kwd==K_SEND) {
send_cmd(u, ord);
}
else if (kwd==K_PASSWORD) {
password_cmd(u, ord);
}
}
}
}
}
static void process_cmd(keyword_t kwd, int (*callback)(unit *, order *))
{
region * r;
for (r=regions; r; r=r->next) {
unit * u;
for (u=r->units; u; u=u->next) {
order * ord;
for (ord=u->orders; ord; ord=ord->next) {
if (kwd == get_keyword(ord)) {
callback(u, ord);
}
}
}
}
}
void process_group(void) {
process_cmd(K_GROUP, group_cmd);
}
void process_origin(void) {
process_cmd(K_URSPRUNG, origin_cmd);
}
void process_quit(void) {
process_cmd(K_QUIT, quit_cmd);
quit();
}

View File

@ -6,6 +6,11 @@ extern "C" {
void process_produce(void); void process_produce(void);
void process_markets(void); void process_markets(void);
void process_make_temp(void);
void process_settings(void);
void process_group(void);
void process_origin(void);
void process_quit(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

26
src/bindings/bind_unit.c Normal file → Executable file
View File

@ -28,6 +28,7 @@ without prior permission by the authors of Eressea.
#include <kernel/building.h> #include <kernel/building.h>
#include <kernel/config.h> #include <kernel/config.h>
#include <kernel/faction.h> #include <kernel/faction.h>
#include <kernel/group.h>
#include <kernel/item.h> #include <kernel/item.h>
#include <kernel/magic.h> #include <kernel/magic.h>
#include <kernel/message.h> #include <kernel/message.h>
@ -141,6 +142,25 @@ int tolua_unitlist_next(lua_State * L)
return 0; /* no more values to return */ return 0; /* no more values to return */
} }
static int tolua_unit_get_group(lua_State * L)
{
unit *u = (unit *) tolua_tousertype(L, 1, 0);
group *g = get_group(u);
if (g) {
tolua_pushstring(L, g->name);
return 1;
}
return 0;
}
static int tolua_unit_set_group(lua_State * L)
{
unit *self = (unit *) tolua_tousertype(L, 1, 0);
int result = join_group(self, tolua_tostring(L, 2, 0));
tolua_pushnumber(L, result);
return 1;
}
static int tolua_unit_get_name(lua_State * L) static int tolua_unit_get_name(lua_State * L)
{ {
unit *self = (unit *) tolua_tousertype(L, 1, 0); unit *self = (unit *) tolua_tousertype(L, 1, 0);
@ -936,9 +956,9 @@ void tolua_unit_open(lua_State * L)
tolua_unit_set_name); tolua_unit_set_name);
tolua_variable(L, TOLUA_CAST "faction", &tolua_unit_get_faction, tolua_variable(L, TOLUA_CAST "faction", &tolua_unit_get_faction,
tolua_unit_set_faction); tolua_unit_set_faction);
tolua_variable(L, TOLUA_CAST "id", &tolua_unit_get_id, tolua_unit_set_id); tolua_variable(L, TOLUA_CAST "id", tolua_unit_get_id, tolua_unit_set_id);
tolua_variable(L, TOLUA_CAST "info", &tolua_unit_get_info, tolua_variable(L, TOLUA_CAST "group", tolua_unit_get_group, tolua_unit_set_group);
tolua_unit_set_info); tolua_variable(L, TOLUA_CAST "info", tolua_unit_get_info, tolua_unit_set_info);
tolua_variable(L, TOLUA_CAST "hp", &tolua_unit_get_hp, tolua_unit_set_hp); tolua_variable(L, TOLUA_CAST "hp", &tolua_unit_get_hp, tolua_unit_set_hp);
tolua_variable(L, TOLUA_CAST "status", &tolua_unit_get_status, tolua_variable(L, TOLUA_CAST "status", &tolua_unit_get_status,
tolua_unit_set_status); tolua_unit_set_status);

View File

@ -540,12 +540,6 @@ static int tolua_write_summary(lua_State * L)
return 0; return 0;
} }
static int tolua_free_game(lua_State * L)
{
free_gamedata();
return 0;
}
static int tolua_write_map(lua_State * L) static int tolua_write_map(lua_State * L)
{ {
const char *filename = tolua_tostring(L, 1, 0); const char *filename = tolua_tostring(L, 1, 0);
@ -555,31 +549,6 @@ static int tolua_write_map(lua_State * L)
return 0; return 0;
} }
static int tolua_write_game(lua_State * L)
{
const char *filename = tolua_tostring(L, 1, 0);
const char *mode = tolua_tostring(L, 2, 0);
int result, m = IO_BINARY;
if (mode && strcmp(mode, "text") == 0)
m = IO_TEXT;
remove_empty_factions();
result = writegame(filename, m);
tolua_pushnumber(L, (lua_Number) result);
return 1;
}
static int tolua_read_game(lua_State * L)
{
const char *filename = tolua_tostring(L, 1, 0);
const char *mode = tolua_tostring(L, 2, 0);
int rv, m = IO_BINARY;
if (mode && strcmp(mode, "text") == 0)
m = IO_TEXT;
rv = readgame(filename, m, false);
tolua_pushnumber(L, (lua_Number) rv);
return 1;
}
static int tolua_read_turn(lua_State * L) static int tolua_read_turn(lua_State * L)
{ {
int cturn = current_turn(); int cturn = current_turn();
@ -1208,9 +1177,6 @@ int tolua_bindings_open(lua_State * L)
tolua_function(L, TOLUA_CAST "factions", tolua_get_factions); tolua_function(L, TOLUA_CAST "factions", tolua_get_factions);
tolua_function(L, TOLUA_CAST "regions", tolua_get_regions); tolua_function(L, TOLUA_CAST "regions", tolua_get_regions);
tolua_function(L, TOLUA_CAST "read_turn", tolua_read_turn); tolua_function(L, TOLUA_CAST "read_turn", tolua_read_turn);
tolua_function(L, TOLUA_CAST "read_game", tolua_read_game);
tolua_function(L, TOLUA_CAST "write_game", tolua_write_game);
tolua_function(L, TOLUA_CAST "free_game", tolua_free_game);
tolua_function(L, TOLUA_CAST "write_map", &tolua_write_map); tolua_function(L, TOLUA_CAST "write_map", &tolua_write_map);
tolua_function(L, TOLUA_CAST "read_orders", tolua_read_orders); tolua_function(L, TOLUA_CAST "read_orders", tolua_read_orders);
tolua_function(L, TOLUA_CAST "process_orders", tolua_process_orders); tolua_function(L, TOLUA_CAST "process_orders", tolua_process_orders);

View File

@ -4,4 +4,5 @@ module eressea {
void eressea_free_game @ free_game(void); void eressea_free_game @ free_game(void);
int eressea_read_game @ read_game(const char * filename); int eressea_read_game @ read_game(const char * filename);
int eressea_write_game @ write_game(const char * filename); int eressea_write_game @ write_game(const char * filename);
int eressea_read_orders @ read_orders(const char * filename);
} }

View File

@ -4,5 +4,10 @@ module eressea {
module process { module process {
void process_markets @ markets(void); void process_markets @ markets(void);
void process_produce @ produce(void); void process_produce @ produce(void);
void process_make_temp @ make_temp(void);
void process_settings @ settings(void);
void process_group @ set_group(void);
void process_origin @ set_origin(void);
void process_quit @ quit(void);
} }
} }

20
src/gamecode/laws.c Normal file → Executable file
View File

@ -1117,7 +1117,7 @@ static boolean EnhancedQuit(void)
return value; return value;
} }
static int quit_cmd(unit * u, struct order *ord) int quit_cmd(unit * u, struct order *ord)
{ {
faction *f = u->faction; faction *f = u->faction;
const char *passwd; const char *passwd;
@ -1159,7 +1159,7 @@ static int quit_cmd(unit * u, struct order *ord)
return 0; return 0;
} }
static void quit(void) void quit(void)
{ {
faction **fptr = &factions; faction **fptr = &factions;
while (*fptr) { while (*fptr) {
@ -2107,7 +2107,7 @@ static int mail_cmd(unit * u, struct order *ord)
/* ------------------------------------------------------------- */ /* ------------------------------------------------------------- */
static int banner_cmd(unit * u, struct order *ord) int banner_cmd(unit * u, struct order *ord)
{ {
init_tokens(ord); init_tokens(ord);
skip_token(); skip_token();
@ -2120,7 +2120,7 @@ static int banner_cmd(unit * u, struct order *ord)
return 0; return 0;
} }
static int email_cmd(unit * u, struct order *ord) int email_cmd(unit * u, struct order *ord)
{ {
const char *s; const char *s;
@ -2142,7 +2142,7 @@ static int email_cmd(unit * u, struct order *ord)
return 0; return 0;
} }
static int password_cmd(unit * u, struct order *ord) int password_cmd(unit * u, struct order *ord)
{ {
char pwbuf[32]; char pwbuf[32];
int i; int i;
@ -2182,7 +2182,7 @@ static int password_cmd(unit * u, struct order *ord)
return 0; return 0;
} }
static int send_cmd(unit * u, struct order *ord) int send_cmd(unit * u, struct order *ord)
{ {
const char *s; const char *s;
int option; int option;
@ -2516,7 +2516,7 @@ static int promotion_cmd(unit * u, struct order *ord)
return 0; return 0;
} }
static int group_cmd(unit * u, struct order *ord) int group_cmd(unit * u, struct order *ord)
{ {
const char *s; const char *s;
@ -2528,7 +2528,7 @@ static int group_cmd(unit * u, struct order *ord)
return 0; return 0;
} }
static int origin_cmd(unit * u, struct order *ord) int origin_cmd(unit * u, struct order *ord)
{ {
short px, py; short px, py;
@ -3291,7 +3291,7 @@ int checkunitnumber(const faction * f, int add)
return 0; return 0;
} }
static void new_units(void) void new_units(void)
{ {
region *r; region *r;
unit *u, *u2; unit *u, *u2;
@ -4154,7 +4154,7 @@ void init_processor(void)
p += 10; p += 10;
add_proc_unit(p, &setdefaults, "Default-Befehle"); add_proc_unit(p, &setdefaults, "Default-Befehle");
add_proc_order(p, K_BANNER, &banner_cmd, 0, NULL); add_proc_order(p, K_BANNER, banner_cmd, 0, NULL);
add_proc_order(p, K_EMAIL, &email_cmd, 0, NULL); add_proc_order(p, K_EMAIL, &email_cmd, 0, NULL);
add_proc_order(p, K_PASSWORD, &password_cmd, 0, NULL); add_proc_order(p, K_PASSWORD, &password_cmd, 0, NULL);
add_proc_order(p, K_SEND, &send_cmd, 0, NULL); add_proc_order(p, K_SEND, &send_cmd, 0, NULL);

10
src/gamecode/laws.h Normal file → Executable file
View File

@ -45,6 +45,16 @@ extern "C" {
extern int dropouts[2]; extern int dropouts[2];
extern int *age; extern int *age;
extern void new_units(void);
extern void quit(void);
extern int password_cmd(struct unit *u, struct order *ord);
extern int banner_cmd(struct unit *u, struct order *ord);
extern int email_cmd(struct unit *u, struct order *ord);
extern int send_cmd(struct unit *u, struct order *ord);
extern int origin_cmd(struct unit *u, struct order *ord);
extern int group_cmd(struct unit *u, struct order *ord);
extern int quit_cmd(struct unit *u, struct order *ord);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

11
src/kernel/group.c Normal file → Executable file
View File

@ -136,6 +136,17 @@ void free_group(group * g)
free(g); free(g);
} }
group * get_group(const struct unit *u)
{
if (fval(u, UFL_GROUP)) {
attrib * a = a_find(u->attribs, &at_group);
if (a) {
return (group *) a->data.v;
}
}
return 0;
}
void set_group(struct unit *u, struct group *g) void set_group(struct unit *u, struct group *g)
{ {
attrib *a = NULL; attrib *a = NULL;

1
src/kernel/group.h Normal file → Executable file
View File

@ -40,6 +40,7 @@ extern "C" {
extern struct attrib_type at_group; /* attribute for units assigned to a group */ extern struct attrib_type at_group; /* attribute for units assigned to a group */
extern boolean join_group(struct unit *u, const char *name); extern boolean join_group(struct unit *u, const char *name);
extern void set_group(struct unit *u, struct group *g); extern void set_group(struct unit *u, struct group *g);
extern struct group * get_group(const struct unit *u);
extern void free_group(struct group *g); extern void free_group(struct group *g);
extern void write_groups(struct storage *F, struct group *g); extern void write_groups(struct storage *F, struct group *g);