From 8765204e0060d6071690f567194fb7da83f0e4fd Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 18:10:18 +0200 Subject: [PATCH 2/3] add unit tests for calendar parsing. --- src/kernel/jsonconf.test.c | 64 +++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/src/kernel/jsonconf.test.c b/src/kernel/jsonconf.test.c index 456a27d22..2f54eeb8d 100644 --- a/src/kernel/jsonconf.test.c +++ b/src/kernel/jsonconf.test.c @@ -13,6 +13,8 @@ #include "order.h" #include "terrain.h" +#include "move.h" +#include "calendar.h" #include "prefix.h" #include "util/language.h" @@ -75,7 +77,7 @@ static void test_settings(CuTest * tc) "\"float\" : 1.5 }}"; cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); config_set("game.id", "42"); /* should not be replaced */ config_set("game.name", "Eressea"); /* should not be replaced */ json_config(json); @@ -99,7 +101,7 @@ static void test_prefixes(CuTest * tc) "]}"; cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); json_config(json); CuAssertPtrNotNull(tc, race_prefixes); CuAssertStrEquals(tc, "snow", race_prefixes[0]); @@ -119,7 +121,7 @@ static void test_disable(CuTest * tc) "]}"; cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); CuAssertTrue(tc, skill_enabled(SK_ALCHEMY)); CuAssertTrue(tc, !keyword_disabled(K_BANNER)); CuAssertTrue(tc, !keyword_disabled(K_PAY)); @@ -135,6 +137,31 @@ static void test_disable(CuTest * tc) test_cleanup(); } +static void test_calendar(CuTest * tc) +{ + const char * data = "{\"calendar\": { " + "\"weeks\" : [ \"one\", \"two\", \"three\" ]," + "\"months\" : [" + "{ \"storm\" : 99, \"season\" : 1 }," + "{ \"storm\" : 22, \"season\" : 2 }" + "]" + "}}"; + cJSON *json = cJSON_Parse(data); + + test_setup(); + json_config(json); + CuAssertPtrNotNull(tc, storms); + CuAssertIntEquals(tc, 2, months_per_year); + CuAssertIntEquals(tc, 3, weeks_per_month); + CuAssertIntEquals(tc, 99, storms[0]); + CuAssertIntEquals(tc, 22, storms[1]); + CuAssertPtrNotNull(tc, month_season); + CuAssertIntEquals(tc, 1, month_season[0]); + CuAssertIntEquals(tc, 2, month_season[1]); + cJSON_Delete(json); + test_cleanup(); +} + static void test_races(CuTest * tc) { const char * data = "{\"races\": { \"orc\" : { " @@ -155,7 +182,7 @@ static void test_races(CuTest * tc) cJSON *json = cJSON_Parse(data); const struct race *rc; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, races); @@ -189,7 +216,7 @@ static void test_findrace(CuTest *tc) { const race *rc; CuAssertPtrNotNull(tc, json); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrEquals(tc, 0, (void *)findrace("Zwerg", lang)); @@ -211,7 +238,7 @@ static void test_items(CuTest * tc) cJSON *json = cJSON_Parse(data); const item_type * itype; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, it_find("axe")); @@ -249,7 +276,7 @@ static void test_ships(CuTest * tc) const ship_type *st; const terrain_type *ter; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, shiptypes); @@ -286,7 +313,7 @@ static void test_castles(CuTest *tc) { cJSON *json = cJSON_Parse(data); const building_type *bt; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, buildingtypes); @@ -311,7 +338,7 @@ static void test_spells(CuTest * tc) cJSON *json = cJSON_Parse(data); const spell *sp; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, find_spell("fireball")); @@ -350,7 +377,7 @@ static void test_buildings(CuTest * tc) cJSON *json = cJSON_Parse(building_data); const building_type *bt; - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, buildingtypes); @@ -400,7 +427,7 @@ static void test_buildings_default(CuTest * tc) const building_type *bt; building_type clone; - test_cleanup(); + test_setup(); bt = bt_get_or_create("house"); clone = *bt; @@ -425,7 +452,7 @@ static void test_ships_default(CuTest * tc) const ship_type *st; ship_type clone; - test_cleanup(); + test_setup(); st = st_get_or_create("hodor"); clone = *st; @@ -446,7 +473,7 @@ static void test_configs(CuTest * tc) FILE *F; cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); F = fopen("test.json", "w"); fwrite(building_data, 1, strlen(building_data), F); @@ -475,7 +502,7 @@ static void test_terrains(CuTest * tc) cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, (void *)get_terrain("plain")); @@ -511,7 +538,7 @@ static void test_directions(CuTest * tc) cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NODIRECTION, get_direction("ost", lang)); @@ -533,7 +560,7 @@ static void test_skills(CuTest * tc) cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NOSKILL, get_skill("potato", lang)); @@ -558,7 +585,7 @@ static void test_keywords(CuTest * tc) cJSON *json = cJSON_Parse(data); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NOKEYWORD, get_keyword("potato", lang)); @@ -583,7 +610,7 @@ static void test_strings(CuTest * tc) cJSON *json = cJSON_Parse(data); CuAssertPtrNotNull(tc, json); - test_cleanup(); + test_setup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, lang); CuAssertPtrEquals(tc, NULL, (void *)LOC(lang, "move")); @@ -640,6 +667,7 @@ CuSuite *get_jsonconf_suite(void) SUITE_ADD_TEST(suite, test_prefixes); SUITE_ADD_TEST(suite, test_disable); SUITE_ADD_TEST(suite, test_infinitive_from_config); + SUITE_ADD_TEST(suite, test_calendar); return suite; } From 18defdd5a23f0621e6879f5c9870e8d6142bf557 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 May 2017 20:21:49 +0200 Subject: [PATCH 3/3] remove last pieces of calendar.xml --- conf/e2/config.xml | 24 ++++++++ src/kernel/xmlreader.c | 127 ----------------------------------------- 2 files changed, 24 insertions(+), 127 deletions(-) create mode 100644 conf/e2/config.xml diff --git a/conf/e2/config.xml b/conf/e2/config.xml new file mode 100644 index 000000000..0de6bff76 --- /dev/null +++ b/conf/e2/config.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/kernel/xmlreader.c b/src/kernel/xmlreader.c index 7ba3ceb94..f595ec7a4 100644 --- a/src/kernel/xmlreader.c +++ b/src/kernel/xmlreader.c @@ -399,133 +399,6 @@ static int parse_buildings(xmlDocPtr doc) return 0; } -#if 0 -static int parse_calendar(xmlDocPtr doc) -{ - xmlXPathContextPtr xpath = xmlXPathNewContext(doc); - xmlXPathObjectPtr xpathCalendars; - xmlNodeSetPtr nsetCalendars; - - xpathCalendars = xmlXPathEvalExpression(BAD_CAST "/eressea/calendar", xpath); - nsetCalendars = xpathCalendars->nodesetval; - if (nsetCalendars != NULL && nsetCalendars->nodeNr != 0) { - int c; - for (c = 0; c != nsetCalendars->nodeNr; ++c) { - xmlNodePtr calendar = nsetCalendars->nodeTab[c]; - xmlXPathObjectPtr xpathWeeks, xpathMonths, xpathSeasons; - xmlNodeSetPtr nsetWeeks, nsetMonths, nsetSeasons; - xmlChar *propValue = xmlGetProp(calendar, BAD_CAST "name"); - xmlChar *newyear = xmlGetProp(calendar, BAD_CAST "newyear"); - xmlChar *start; - - start = xmlGetProp(calendar, BAD_CAST "start"); - if (start && config_get("game.start") == NULL) { - config_set("game.start", (const char *)start); - xmlFree(start); - } - if (propValue) { - free(agename); - agename = strdup(mkname("calendar", (const char *)propValue)); - xmlFree(propValue); - } - - xpath->node = calendar; - xpathWeeks = xmlXPathEvalExpression(BAD_CAST "week", xpath); - nsetWeeks = xpathWeeks->nodesetval; - if (nsetWeeks != NULL && nsetWeeks->nodeNr) { - int i; - - weeks_per_month = nsetWeeks->nodeNr; - free(weeknames); - free(weeknames2); - weeknames = malloc(sizeof(char *) * weeks_per_month); - weeknames2 = malloc(sizeof(char *) * weeks_per_month); - for (i = 0; i != nsetWeeks->nodeNr; ++i) { - xmlNodePtr week = nsetWeeks->nodeTab[i]; - xmlChar *propValue = xmlGetProp(week, BAD_CAST "name"); - if (propValue) { - weeknames[i] = strdup(mkname("calendar", (const char *)propValue)); - weeknames2[i] = malloc(strlen(weeknames[i]) + 3); - sprintf(weeknames2[i], "%s_d", weeknames[i]); - xmlFree(propValue); - } - } - } - xmlXPathFreeObject(xpathWeeks); - - xpathSeasons = xmlXPathEvalExpression(BAD_CAST "season", xpath); - nsetSeasons = xpathSeasons->nodesetval; - if (nsetSeasons != NULL && nsetSeasons->nodeNr) { - int i; - - seasons = nsetSeasons->nodeNr; - assert(!seasonnames); - seasonnames = malloc(sizeof(char *) * seasons); - - for (i = 0; i != nsetSeasons->nodeNr; ++i) { - xmlNodePtr season = nsetSeasons->nodeTab[i]; - xmlChar *propValue = xmlGetProp(season, BAD_CAST "name"); - if (propValue) { - seasonnames[i] = - strdup(mkname("calendar", (const char *)propValue)); - xmlFree(propValue); - } - } - } - - xpathMonths = xmlXPathEvalExpression(BAD_CAST "season/month", xpath); - nsetMonths = xpathMonths->nodesetval; - if (nsetMonths != NULL && nsetMonths->nodeNr) { - int i; - - months_per_year = nsetMonths->nodeNr; - free(monthnames); - monthnames = malloc(sizeof(char *) * months_per_year); - free(month_season); - month_season = malloc(sizeof(int) * months_per_year); - free(storms); - storms = malloc(sizeof(int) * months_per_year); - - for (i = 0; i != nsetMonths->nodeNr; ++i) { - xmlNodePtr month = nsetMonths->nodeTab[i]; - xmlChar *propValue = xmlGetProp(month, BAD_CAST "name"); - - if (propValue) { - if (newyear - && strcmp((const char *)newyear, (const char *)propValue) == 0) { - first_month = i; - xmlFree(newyear); - newyear = NULL; - } - monthnames[i] = strdup(mkname("calendar", (const char *)propValue)); - xmlFree(propValue); - } - if (nsetSeasons) { - int j; - for (j = 0; j != seasons; ++j) { - xmlNodePtr season = month->parent; - if (season == nsetSeasons->nodeTab[j]) { - month_season[i] = j; - break; - } - } - assert(j != seasons); - } - storms[i] = xml_ivalue(nsetMonths->nodeTab[i], "storm", 0); - } - } - xmlXPathFreeObject(xpathMonths); - xmlXPathFreeObject(xpathSeasons); - xmlFree(newyear); - newyear = NULL; - } - } - xmlXPathFreeObject(xpathCalendars); - xmlXPathFreeContext(xpath); - - return 0; -} -#endif static int parse_ships(xmlDocPtr doc) { xmlXPathContextPtr xpath = xmlXPathNewContext(doc);