diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0bbe4f45b..ae4b6c287 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -192,6 +192,7 @@ set(TESTS_SRC skill.test.c spells.test.c spy.test.c + study.test.c upkeep.test.c spells/magicresistance.test.c ${ATTRIBUTES_TESTS} diff --git a/src/bind_process.c b/src/bind_process.c index c411e257a..2e883d38a 100755 --- a/src/bind_process.c +++ b/src/bind_process.c @@ -159,7 +159,7 @@ void process_quit(void) { void process_study(void) { 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) { diff --git a/src/laws.c b/src/laws.c index 6a18dc84e..558b95323 100755 --- a/src/laws.c +++ b/src/laws.c @@ -4489,7 +4489,7 @@ void init_processor(void) add_proc_order(p, K_TEACH, teach_cmd, PROC_THISORDER | PROC_LONGORDER, "Lehren"); 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"); p += 10; diff --git a/src/study.c b/src/study.c index ec377f0f7..dac5134d5 100644 --- a/src/study.c +++ b/src/study.c @@ -527,7 +527,7 @@ static double study_speedup(unit * u, skill_t s, study_rule_t rule) return 1.0; } -int learn_cmd(unit * u, order * ord) +int study_cmd(unit * u, order * ord) { region *r = u->region; int p; diff --git a/src/study.h b/src/study.h index d5d664fd2..d4a49ab20 100644 --- a/src/study.h +++ b/src/study.h @@ -20,13 +20,14 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. #define H_KRNL_STUDY #include "skill.h" +#include #ifdef __cplusplus extern "C" { #endif 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 bool is_migrant(struct unit *u); diff --git a/src/study.test.c b/src/study.test.c new file mode 100644 index 000000000..58614d37c --- /dev/null +++ b/src/study.test.c @@ -0,0 +1,99 @@ +#include + +#include "study.h" + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include + +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; +} diff --git a/src/test_eressea.c b/src/test_eressea.c index b5a2e2a42..e434f6c7f 100644 --- a/src/test_eressea.c +++ b/src/test_eressea.c @@ -89,6 +89,7 @@ int RunAllTests(void) RUN_TESTS(suite, vortex); RUN_TESTS(suite, wormhole); RUN_TESTS(suite, spy); + RUN_TESTS(suite, study); printf("\ntest summary: %d tests, %d failed\n", suite->count, suite->failCount); log_flags = flags; diff --git a/src/tests.c b/src/tests.c index 9c03e1e18..5a19ce9c0 100644 --- a/src/tests.c +++ b/src/tests.c @@ -209,15 +209,20 @@ void test_create_world(void) } message * test_get_last_message(message_list *msgs) { - struct mlist *iter = msgs->begin; - while (iter->next) { - iter = iter->next; + if (msgs) { + struct mlist *iter = msgs->begin; + while (iter->next) { + iter = iter->next; + } + return iter->msg; } - return iter->msg; + return 0; } 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) { name = (const char *)msg->parameters[0].v; }