From dafb2fc2ab76d071aee16e0b228dd18e589cec69 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 7 Jun 2012 09:47:02 -0700 Subject: [PATCH] adding a lot of test coverage extending the binding generation to more processing functions --- scripts/default.lua | 7 ++-- scripts/init.lua | 2 +- scripts/tests/bindings.lua | 28 +++++++++++++++ scripts/tests/common.lua | 12 +++---- scripts/tests/orders.lua | 72 +++++++++++++++++++++++++++++++++++++ src/bindings/bind_eressea.c | 5 +++ src/bindings/bind_eressea.h | 1 + src/bindings/bind_process.c | 61 +++++++++++++++++++++++++++++++ src/bindings/bind_process.h | 5 +++ src/bindings/bind_unit.c | 26 ++++++++++++-- src/bindings/bindings.c | 34 ------------------ src/bindings/eressea.pkg | 1 + src/bindings/process.pkg | 5 +++ src/gamecode/laws.c | 20 +++++------ src/gamecode/laws.h | 10 ++++++ src/kernel/group.c | 11 ++++++ src/kernel/group.h | 1 + 17 files changed, 242 insertions(+), 59 deletions(-) mode change 100644 => 100755 scripts/default.lua mode change 100644 => 100755 scripts/init.lua create mode 100755 scripts/tests/bindings.lua mode change 100644 => 100755 src/bindings/bind_unit.c mode change 100644 => 100755 src/gamecode/laws.c mode change 100644 => 100755 src/gamecode/laws.h mode change 100644 => 100755 src/kernel/group.c mode change 100644 => 100755 src/kernel/group.h diff --git a/scripts/default.lua b/scripts/default.lua old mode 100644 new mode 100755 index 9b5e4c8a6..6d22b4f32 --- a/scripts/default.lua +++ b/scripts/default.lua @@ -26,7 +26,7 @@ function nmr_check(maxnmrs) if nmrs >= maxnmrs then print("Shit. More than " .. maxnmrs .. " factions with 1 NMR (" .. nmrs .. ")") write_summary() - write_game("aborted.dat") + eressea.write_game("aborted.dat") return -1 end print (nmrs .. " Factions with 1 NMR") @@ -35,10 +35,7 @@ end function open_game(turn) file = "" .. get_turn() - if read_game(file .. ".dat", "binary")~=0 then - return read_game(file, "text") - end - return 0 + return eressea.read_game(file .. ".dat") end function write_emails(locales) diff --git a/scripts/init.lua b/scripts/init.lua old mode 100644 new mode 100755 index ae39c50b2..bafa3da63 --- a/scripts/init.lua +++ b/scripts/init.lua @@ -8,7 +8,7 @@ function run_editor() turn = read_turn() set_turn(turn) end - read_game(turn .. ".dat") + eressea.read_game(turn .. ".dat") gmtool.editor() end diff --git a/scripts/tests/bindings.lua b/scripts/tests/bindings.lua new file mode 100755 index 000000000..90be5e1a9 --- /dev/null +++ b/scripts/tests/bindings.lua @@ -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 diff --git a/scripts/tests/common.lua b/scripts/tests/common.lua index 95984b443..b1b69c211 100755 --- a/scripts/tests/common.lua +++ b/scripts/tests/common.lua @@ -912,7 +912,7 @@ function test_parser() file:write("BENENNEN EINHEIT 'Goldene Herde'\n") file:close() - read_orders(filename) + eressea.read_orders(filename) process_orders() assert_equal("Goldene Herde", u.name) end @@ -975,7 +975,7 @@ function test_bug_1814() file:write("; ARBEITE\n") file:close() - read_orders(filename) + eressea.read_orders(filename) process_orders() init_reports() write_report(f) @@ -996,7 +996,7 @@ function test_bug_1679() file:write("ARBEITEN\n") file:close() - read_orders(filename) + eressea.read_orders(filename) process_orders() init_reports() write_report(f) @@ -1103,7 +1103,7 @@ function test_bug_1875_use_normal() local r = region.create(0, 0, "plain") 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 u = unit.create(f, r, 1) @@ -1126,7 +1126,7 @@ function test_bug_1875_use_help() local r = region.create(0, 0, "plain") 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 u = unit.create(f, r, 1) @@ -1153,7 +1153,7 @@ function test_bug_1875_use_own_first() local r = region.create(0, 0, "plain") 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 u = unit.create(f, r, 1) diff --git a/scripts/tests/orders.lua b/scripts/tests/orders.lua index 0fe64429a..2fc1e5b77 100755 --- a/scripts/tests/orders.lua +++ b/scripts/tests/orders.lua @@ -33,3 +33,75 @@ function test_give() assert_not_equal(5, u:get_item("money")) assert_not_equal(5, u2:get_item("money")) 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 diff --git a/src/bindings/bind_eressea.c b/src/bindings/bind_eressea.c index 51c0537d3..1972bd0b1 100755 --- a/src/bindings/bind_eressea.c +++ b/src/bindings/bind_eressea.c @@ -15,5 +15,10 @@ int eressea_read_game(const char * filename) { } int eressea_write_game(const char * filename) { + remove_empty_factions(); return writegame(filename, IO_BINARY); } + +int eressea_read_orders(const char * filename) { + return readorders(filename); +} diff --git a/src/bindings/bind_eressea.h b/src/bindings/bind_eressea.h index c50683765..f8ba00d8c 100755 --- a/src/bindings/bind_eressea.h +++ b/src/bindings/bind_eressea.h @@ -7,6 +7,7 @@ extern "C" { 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); #ifdef __cplusplus } diff --git a/src/bindings/bind_process.c b/src/bindings/bind_process.c index 5a344c1c4..4b68bc7bd 100755 --- a/src/bindings/bind_process.c +++ b/src/bindings/bind_process.c @@ -2,8 +2,11 @@ #include #include +#include #include +#include #include +#include #include void process_produce(void) { @@ -16,3 +19,61 @@ void process_produce(void) { void process_markets(void) { 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(); +} diff --git a/src/bindings/bind_process.h b/src/bindings/bind_process.h index c7c7cab64..bd42c63a1 100755 --- a/src/bindings/bind_process.h +++ b/src/bindings/bind_process.h @@ -6,6 +6,11 @@ extern "C" { void process_produce(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 } diff --git a/src/bindings/bind_unit.c b/src/bindings/bind_unit.c old mode 100644 new mode 100755 index afa42389b..543a9f9d5 --- a/src/bindings/bind_unit.c +++ b/src/bindings/bind_unit.c @@ -28,6 +28,7 @@ without prior permission by the authors of Eressea. #include #include #include +#include #include #include #include @@ -141,6 +142,25 @@ int tolua_unitlist_next(lua_State * L) 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) { unit *self = (unit *) tolua_tousertype(L, 1, 0); @@ -936,9 +956,9 @@ void tolua_unit_open(lua_State * L) tolua_unit_set_name); tolua_variable(L, TOLUA_CAST "faction", &tolua_unit_get_faction, tolua_unit_set_faction); - 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_unit_set_info); + tolua_variable(L, TOLUA_CAST "id", tolua_unit_get_id, tolua_unit_set_id); + tolua_variable(L, TOLUA_CAST "group", tolua_unit_get_group, tolua_unit_set_group); + 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 "status", &tolua_unit_get_status, tolua_unit_set_status); diff --git a/src/bindings/bindings.c b/src/bindings/bindings.c index 0ec0bd9f7..ea86df4a0 100755 --- a/src/bindings/bindings.c +++ b/src/bindings/bindings.c @@ -540,12 +540,6 @@ static int tolua_write_summary(lua_State * L) return 0; } -static int tolua_free_game(lua_State * L) -{ - free_gamedata(); - return 0; -} - static int tolua_write_map(lua_State * L) { const char *filename = tolua_tostring(L, 1, 0); @@ -555,31 +549,6 @@ static int tolua_write_map(lua_State * L) 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) { 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 "regions", tolua_get_regions); 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 "read_orders", tolua_read_orders); tolua_function(L, TOLUA_CAST "process_orders", tolua_process_orders); diff --git a/src/bindings/eressea.pkg b/src/bindings/eressea.pkg index 54e699520..78740c485 100755 --- a/src/bindings/eressea.pkg +++ b/src/bindings/eressea.pkg @@ -4,4 +4,5 @@ module eressea { void eressea_free_game @ free_game(void); 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); } diff --git a/src/bindings/process.pkg b/src/bindings/process.pkg index 018d80459..662536caf 100755 --- a/src/bindings/process.pkg +++ b/src/bindings/process.pkg @@ -4,5 +4,10 @@ module eressea { module process { void process_markets @ markets(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); } } diff --git a/src/gamecode/laws.c b/src/gamecode/laws.c old mode 100644 new mode 100755 index 69e7efc45..fee370335 --- a/src/gamecode/laws.c +++ b/src/gamecode/laws.c @@ -1117,7 +1117,7 @@ static boolean EnhancedQuit(void) return value; } -static int quit_cmd(unit * u, struct order *ord) +int quit_cmd(unit * u, struct order *ord) { faction *f = u->faction; const char *passwd; @@ -1159,7 +1159,7 @@ static int quit_cmd(unit * u, struct order *ord) return 0; } -static void quit(void) +void quit(void) { faction **fptr = &factions; 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); skip_token(); @@ -2120,7 +2120,7 @@ static int banner_cmd(unit * u, struct order *ord) return 0; } -static int email_cmd(unit * u, struct order *ord) +int email_cmd(unit * u, struct order *ord) { const char *s; @@ -2142,7 +2142,7 @@ static int email_cmd(unit * u, struct order *ord) return 0; } -static int password_cmd(unit * u, struct order *ord) +int password_cmd(unit * u, struct order *ord) { char pwbuf[32]; int i; @@ -2182,7 +2182,7 @@ static int password_cmd(unit * u, struct order *ord) return 0; } -static int send_cmd(unit * u, struct order *ord) +int send_cmd(unit * u, struct order *ord) { const char *s; int option; @@ -2516,7 +2516,7 @@ static int promotion_cmd(unit * u, struct order *ord) return 0; } -static int group_cmd(unit * u, struct order *ord) +int group_cmd(unit * u, struct order *ord) { const char *s; @@ -2528,7 +2528,7 @@ static int group_cmd(unit * u, struct order *ord) return 0; } -static int origin_cmd(unit * u, struct order *ord) +int origin_cmd(unit * u, struct order *ord) { short px, py; @@ -3291,7 +3291,7 @@ int checkunitnumber(const faction * f, int add) return 0; } -static void new_units(void) +void new_units(void) { region *r; unit *u, *u2; @@ -4154,7 +4154,7 @@ void init_processor(void) p += 10; 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_PASSWORD, &password_cmd, 0, NULL); add_proc_order(p, K_SEND, &send_cmd, 0, NULL); diff --git a/src/gamecode/laws.h b/src/gamecode/laws.h old mode 100644 new mode 100755 index 2ea50b8eb..228c84b6a --- a/src/gamecode/laws.h +++ b/src/gamecode/laws.h @@ -45,6 +45,16 @@ extern "C" { extern int dropouts[2]; 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 } #endif diff --git a/src/kernel/group.c b/src/kernel/group.c old mode 100644 new mode 100755 index c61966968..a0b5821c5 --- a/src/kernel/group.c +++ b/src/kernel/group.c @@ -136,6 +136,17 @@ void free_group(group * 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) { attrib *a = NULL; diff --git a/src/kernel/group.h b/src/kernel/group.h old mode 100644 new mode 100755 index bccbf4540..14dd119cd --- a/src/kernel/group.h +++ b/src/kernel/group.h @@ -40,6 +40,7 @@ extern "C" { extern struct attrib_type at_group; /* attribute for units assigned to a group */ extern boolean join_group(struct unit *u, const char *name); 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 write_groups(struct storage *F, struct group *g);