unit tests for STUDY and TEACH commands.

This commit is contained in:
Enno Rehling 2015-07-09 13:24:21 +02:00
parent 034d1d5433
commit 27835dc4e1
8 changed files with 116 additions and 9 deletions

View File

@ -192,6 +192,7 @@ set(TESTS_SRC
skill.test.c skill.test.c
spells.test.c spells.test.c
spy.test.c spy.test.c
study.test.c
upkeep.test.c upkeep.test.c
spells/magicresistance.test.c spells/magicresistance.test.c
${ATTRIBUTES_TESTS} ${ATTRIBUTES_TESTS}

View File

@ -159,7 +159,7 @@ void process_quit(void) {
void process_study(void) { void process_study(void) {
process_cmd(K_TEACH, teach_cmd, PROC_LONG_ORDER); process_cmd(K_TEACH, teach_cmd, PROC_LONG_ORDER);
process_cmd(K_STUDY, learn_cmd, PROC_LONG_ORDER); process_cmd(K_STUDY, study_cmd, PROC_LONG_ORDER);
} }
void process_movement(void) { void process_movement(void) {

View File

@ -4489,7 +4489,7 @@ void init_processor(void)
add_proc_order(p, K_TEACH, teach_cmd, PROC_THISORDER | PROC_LONGORDER, add_proc_order(p, K_TEACH, teach_cmd, PROC_THISORDER | PROC_LONGORDER,
"Lehren"); "Lehren");
p += 10; p += 10;
add_proc_order(p, K_STUDY, learn_cmd, PROC_THISORDER | PROC_LONGORDER, add_proc_order(p, K_STUDY, study_cmd, PROC_THISORDER | PROC_LONGORDER,
"Lernen"); "Lernen");
p += 10; p += 10;

View File

@ -527,7 +527,7 @@ static double study_speedup(unit * u, skill_t s, study_rule_t rule)
return 1.0; return 1.0;
} }
int learn_cmd(unit * u, order * ord) int study_cmd(unit * u, order * ord)
{ {
region *r = u->region; region *r = u->region;
int p; int p;

View File

@ -20,13 +20,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#define H_KRNL_STUDY #define H_KRNL_STUDY
#include "skill.h" #include "skill.h"
#include <kernel/types.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
extern int teach_cmd(struct unit *u, struct order *ord); extern int teach_cmd(struct unit *u, struct order *ord);
extern int learn_cmd(struct unit *u, struct order *ord); extern int study_cmd(struct unit *u, struct order *ord);
extern magic_t getmagicskill(const struct locale *lang); extern magic_t getmagicskill(const struct locale *lang);
extern bool is_migrant(struct unit *u); extern bool is_migrant(struct unit *u);

99
src/study.test.c Normal file
View File

@ -0,0 +1,99 @@
#include <platform.h>
#include "study.h"
#include <kernel/unit.h>
#include <kernel/faction.h>
#include <kernel/order.h>
#include <util/message.h>
#include <util/language.h>
#include <util/base36.h>
#include <tests.h>
#include <assert.h>
#include <CuTest.h>
typedef struct {
unit *u;
unit *teachers[2];
} study_fixture;
static void setup_study(study_fixture *fix, skill_t sk) {
struct region * r;
struct faction *f;
struct locale *lang;
assert(fix);
test_cleanup();
test_create_world();
r = test_create_region(0, 0, 0);
f = test_create_faction(0);
lang = get_or_create_locale(locale_name(f->locale));
locale_setstring(lang, mkname("skill", skillnames[sk]), skillnames[sk]);
init_skills(lang);
fix->u = test_create_unit(f, r);
assert(fix->u);
fix->u->thisorder = create_order(K_STUDY, f->locale, "%s", skillnames[sk]);
fix->teachers[0] = test_create_unit(f, r);
assert(fix->teachers[0]);
fix->teachers[0]->thisorder = create_order(K_TEACH, f->locale, "%s", itoa36(fix->u->no));
fix->teachers[1] = test_create_unit(f, r);
assert(fix->teachers[1]);
fix->teachers[1]->thisorder = create_order(K_TEACH, f->locale, "%s", itoa36(fix->u->no));
}
static void test_study_no_teacher(CuTest *tc) {
study_fixture fix;
skill *sv;
setup_study(&fix, SK_CROSSBOW);
study_cmd(fix.u, fix.u->thisorder);
CuAssertPtrNotNull(tc, sv = unit_skill(fix.u, SK_CROSSBOW));
CuAssertIntEquals(tc, 1, sv->level);
CuAssertIntEquals(tc, 2, sv->weeks);
CuAssertPtrEquals(tc, 0, test_get_last_message(fix.u->faction->msgs));
test_cleanup();
}
static void test_study_with_teacher(CuTest *tc) {
study_fixture fix;
skill *sv;
setup_study(&fix, SK_CROSSBOW);
set_level(fix.teachers[0], SK_CROSSBOW, TEACHDIFFERENCE);
teach_cmd(fix.teachers[0], fix.teachers[0]->thisorder);
CuAssertPtrEquals(tc, 0, test_get_last_message(fix.u->faction->msgs));
study_cmd(fix.u, fix.u->thisorder);
CuAssertPtrNotNull(tc, sv = unit_skill(fix.u, SK_CROSSBOW));
CuAssertIntEquals(tc, 1, sv->level);
CuAssertIntEquals(tc, 1, sv->weeks);
test_cleanup();
}
static void test_study_with_bad_teacher(CuTest *tc) {
study_fixture fix;
skill *sv;
message *msg;
setup_study(&fix, SK_CROSSBOW);
teach_cmd(fix.teachers[0], fix.teachers[0]->thisorder);
CuAssertPtrNotNull(tc, msg = test_get_last_message(fix.u->faction->msgs));
CuAssertStrEquals(tc, "teach_asgood", test_get_messagetype(msg));
study_cmd(fix.u, fix.u->thisorder);
CuAssertPtrNotNull(tc, sv = unit_skill(fix.u, SK_CROSSBOW));
CuAssertIntEquals(tc, 1, sv->level);
CuAssertIntEquals(tc, 2, sv->weeks);
test_cleanup();
}
CuSuite *get_study_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_study_no_teacher);
SUITE_ADD_TEST(suite, test_study_with_teacher);
SUITE_ADD_TEST(suite, test_study_with_bad_teacher);
return suite;
}

View File

@ -89,6 +89,7 @@ int RunAllTests(void)
RUN_TESTS(suite, vortex); RUN_TESTS(suite, vortex);
RUN_TESTS(suite, wormhole); RUN_TESTS(suite, wormhole);
RUN_TESTS(suite, spy); RUN_TESTS(suite, spy);
RUN_TESTS(suite, study);
printf("\ntest summary: %d tests, %d failed\n", suite->count, suite->failCount); printf("\ntest summary: %d tests, %d failed\n", suite->count, suite->failCount);
log_flags = flags; log_flags = flags;

View File

@ -209,15 +209,20 @@ void test_create_world(void)
} }
message * test_get_last_message(message_list *msgs) { message * test_get_last_message(message_list *msgs) {
if (msgs) {
struct mlist *iter = msgs->begin; struct mlist *iter = msgs->begin;
while (iter->next) { while (iter->next) {
iter = iter->next; iter = iter->next;
} }
return iter->msg; return iter->msg;
}
return 0;
} }
const char * test_get_messagetype(const message *msg) { const char * test_get_messagetype(const message *msg) {
const char * name = msg->type->name; const char * name;
assert(msg);
name = msg->type->name;
if (strcmp(name, "missing_message") == 0) { if (strcmp(name, "missing_message") == 0) {
name = (const char *)msg->parameters[0].v; name = (const char *)msg->parameters[0].v;
} }