static void test_castles(CuTest *tc) { const char * data = "{\"buildings\": { \"castle\" : { " "\"construction\" : [" "{ \"maxsize\" : 2 }," "{ \"maxsize\" : 8 }" "]}}}"; cJSON *json = cJSON_Parse(data); const building_type *bt; test_cleanup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, buildingtypes); json_config(json); CuAssertPtrNotNull(tc, buildingtypes); bt = bt_find("castle"); CuAssertPtrNotNull(tc, bt); CuAssertPtrNotNull(tc, bt->construction); CuAssertIntEquals(tc, 2, bt->construction->maxsize); CuAssertPtrNotNull(tc, bt->construction->improvement); CuAssertIntEquals(tc, 6, bt->construction->improvement->maxsize); CuAssertPtrEquals(tc, 0, bt->construction->improvement->improvement); }
static void test_ships(CuTest * tc) { const char * data = "{\"ships\": { \"boat\" : { " "\"construction\" : { \"maxsize\" : 20, \"reqsize\" : 10, \"minskill\" : 1 }," "\"coasts\" : [ \"plain\" ]" "}}}"; cJSON *json = cJSON_Parse(data); const ship_type *st; const terrain_type *ter; test_cleanup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, shiptypes); json_config(json); CuAssertPtrNotNull(tc, shiptypes); st = st_find("boat"); CuAssertPtrNotNull(tc, st); CuAssertPtrNotNull(tc, st->construction); CuAssertIntEquals(tc, 10, st->construction->reqsize); CuAssertIntEquals(tc, 20, st->construction->maxsize); CuAssertIntEquals(tc, 1, st->construction->minskill); ter = get_terrain("plain"); CuAssertPtrNotNull(tc, ter); CuAssertPtrNotNull(tc, st->coasts); CuAssertPtrEquals(tc, (void *)ter, (void *)st->coasts[0]); CuAssertPtrEquals(tc, 0, (void *)st->coasts[1]); test_cleanup(); }
static void test_items(CuTest * tc) { const char * data = "{\"items\": { " "\"axe\" : { \"weight\" : 2}," "\"horse\" : { \"flags\" : [ \"animal\", \"big\" ], \"capacity\" : 20 }" "}}"; cJSON *json = cJSON_Parse(data); const item_type * itype; test_cleanup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, it_find("axe")); CuAssertPtrEquals(tc, 0, rt_find("axe")); CuAssertPtrEquals(tc, 0, (void *)get_resourcetype(R_HORSE)); json_config(json); itype = it_find("axe"); CuAssertPtrNotNull(tc, itype); CuAssertIntEquals(tc, 2, itype->weight); CuAssertIntEquals(tc, 0, itype->flags); itype = it_find("horse"); CuAssertPtrNotNull(tc, itype); CuAssertIntEquals(tc, 20, itype->capacity); CuAssertIntEquals(tc, ITF_ANIMAL|ITF_BIG, itype->flags); CuAssertPtrNotNull(tc, rt_find("axe")); CuAssertPtrNotNull(tc, (void *)get_resourcetype(R_HORSE)); }
static const struct race * race_with_flag(const char * name) { char data[1024]; cJSON *json; sprintf(data, "{\"races\" : { \"orc\": { \"speed\" : 1, \"flags\" : [ \"%s\"] }}}", name); json = cJSON_Parse(data); free_races(); json_config(json); return rc_find("orc"); }
static void test_strings(CuTest * tc) { const char * data = "{\"strings\": { \"de\" : { \"move\" : \"NACH\", \"study\" : \"LERNEN\" }}}"; const struct locale * lang; cJSON *json = cJSON_Parse(data); CuAssertPtrNotNull(tc, json); test_cleanup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, lang); CuAssertPtrEquals(tc, NULL, (void *)locale_string(lang, "move")); json_config(json); CuAssertStrEquals(tc, "NACH", locale_string(lang, "move")); CuAssertStrEquals(tc, "LERNEN", locale_string(lang, "study")); }
static void test_findrace(CuTest *tc) { const char * data = "{\"races\": { \"dwarf\": {} }, \"strings\": { \"de\" : { \"race::dwarf\" : \"Zwerg\" } } }"; cJSON *json = cJSON_Parse(data); const struct locale *lang; const race *rc; CuAssertPtrNotNull(tc, json); test_cleanup(); lang = get_or_create_locale("de"); CuAssertPtrEquals(tc, 0, (void *)findrace("Zwerg", lang)); json_config(json); rc = findrace("Zwerg", lang); CuAssertPtrNotNull(tc, rc); CuAssertStrEquals(tc, "dwarf", rc->_name); }
static void test_terrains(CuTest * tc) { const char * data = "{\"terrains\": { \"plain\" : { \"flags\" : [ \"land\", \"fly\", \"walk\" ] } }}"; const terrain_type *ter; cJSON *json = cJSON_Parse(data); test_cleanup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, (void *)get_terrain("plain")); json_config(json); ter = get_terrain("plain"); CuAssertPtrNotNull(tc, ter); CuAssertIntEquals(tc, ter->flags, LAND_REGION|FLY_INTO|WALK_INTO); test_cleanup(); }
static void test_spells(CuTest * tc) { const char * data = "{\"spells\": { \"fireball\" : { \"syntax\" : \"u+\" } } }"; cJSON *json = cJSON_Parse(data); const spell *sp; test_cleanup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, find_spell("fireball")); json_config(json); sp = find_spell("fireball"); CuAssertPtrNotNull(tc, sp); CuAssertStrEquals(tc, "u+", sp->syntax); test_cleanup(); CuAssertPtrEquals(tc, 0, find_spell("fireball")); }
static void json_include(cJSON *json) { cJSON *child; if (json->type != cJSON_Array) { log_error("config is not a json array: %d", json->type); return; } for (child = json->child; child; child = child->next) { FILE *F; if (json_relpath) { char name[MAX_PATH]; _snprintf(name, sizeof(name), "%s/%s", json_relpath, child->valuestring); F = fopen(name, "rt"); } else { F = fopen(child->valuestring, "rt"); } if (F) { long pos; fseek(F, 0, SEEK_END); pos = ftell(F); rewind(F); if (pos > 0) { cJSON *config; char *data; size_t sz; data = malloc(pos + 1); sz = fread(data, 1, (size_t)pos, F); data[sz] = 0; config = cJSON_Parse(data); free(data); if (config) { json_config(config); cJSON_Delete(config); } else { log_error("invalid JSON, could not parse %s", child->valuestring); } } fclose(F); } } }
static void test_races(CuTest * tc) { const char * data = "{\"races\": { \"orc\" : { " "\"damage\" : \"1d4\"," "\"magres\" : 1.0," "\"maxaura\" : 2.0," "\"regaura\" : 3.0," "\"speed\" : 4.0," "\"recruitcost\" : 1," "\"maintenance\" : 2," "\"weight\" : 3," "\"capacity\" : 4," "\"hp\" : 5," "\"ac\" : 6," "\"flags\" : [ \"npc\", \"walk\", \"undead\" ]" "}}}"; cJSON *json = cJSON_Parse(data); const struct race *rc; test_cleanup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, races); json_config(json); CuAssertPtrNotNull(tc, races); rc = rc_find("orc"); CuAssertPtrNotNull(tc, rc); CuAssertIntEquals(tc, RCF_NPC|RCF_WALK|RCF_UNDEAD, rc->flags); CuAssertStrEquals(tc, "1d4", rc->def_damage); CuAssertDblEquals(tc, 1.0, rc->magres, 0.0); CuAssertDblEquals(tc, 2.0, rc->maxaura, 0.0); CuAssertDblEquals(tc, 3.0, rc->regaura, 0.0); CuAssertDblEquals(tc, 4.0, rc->speed, 0.0); CuAssertIntEquals(tc, 1, rc->recruitcost); CuAssertIntEquals(tc, 2, rc->maintenance); CuAssertIntEquals(tc, 3, rc->weight); CuAssertIntEquals(tc, 4, rc->capacity); CuAssertIntEquals(tc, 5, rc->hitpoints); CuAssertIntEquals(tc, 6, rc->armor); test_cleanup(); }
static void test_directions(CuTest * tc) { const char * data = "{\"directions\": { \"de\" : { \"east\" : \"osten\", \"northwest\" : [ \"nw\", \"nordwest\" ], \"pause\" : \"pause\" }}}"; const struct locale * lang; cJSON *json = cJSON_Parse(data); test_cleanup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NODIRECTION, get_direction("ost", lang)); json_config(json); CuAssertIntEquals(tc, D_EAST, get_direction("ost", lang)); CuAssertIntEquals(tc, D_NORTHWEST, get_direction("nw", lang)); CuAssertIntEquals(tc, D_NORTHWEST, get_direction("nordwest", lang)); CuAssertIntEquals(tc, D_PAUSE, get_direction("pause", lang)); test_cleanup(); }
static void test_keywords(CuTest * tc) { const char * data = "{\"keywords\": { \"de\" : { \"move\" : \"NACH\", \"study\" : [ \"LERNEN\", \"STUDIEREN\" ] }}}"; const struct locale * lang; cJSON *json = cJSON_Parse(data); test_cleanup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NOKEYWORD, get_keyword("potato", lang)); json_config(json); CuAssertIntEquals(tc, K_STUDY, get_keyword("studiere", lang)); CuAssertIntEquals(tc, K_STUDY, get_keyword("lerne", lang)); CuAssertIntEquals(tc, K_MOVE, get_keyword("nach", lang)); CuAssertStrEquals(tc, "LERNEN", locale_string(lang, "keyword::study")); test_cleanup(); }
static void test_skills(CuTest * tc) { const char * data = "{\"skills\": { \"de\" : { \"alchemy\" : \"ALCHEMIE\", \"crossbow\" : [ \"ARMBRUST\", \"KREUZBOGEN\" ] }}}"; const struct locale * lang; cJSON *json = cJSON_Parse(data); test_cleanup(); lang = get_or_create_locale("de"); CuAssertPtrNotNull(tc, json); CuAssertIntEquals(tc, NOSKILL, get_skill("potato", lang)); json_config(json); CuAssertIntEquals(tc, NOSKILL, get_skill("potato", lang)); CuAssertIntEquals(tc, SK_CROSSBOW, get_skill("armbrust", lang)); CuAssertIntEquals(tc, SK_CROSSBOW, get_skill("kreuz", lang)); CuAssertIntEquals(tc, SK_ALCHEMY, get_skill("alchemie", lang)); CuAssertStrEquals(tc, "ALCHEMIE", locale_string(lang, "skill::alchemy")); CuAssertStrEquals(tc, "ARMBRUST", locale_string(lang, "skill::crossbow")); test_cleanup(); }
int config_parse(const char *json) { cJSON * conf = cJSON_Parse(json); if (conf) { json_config(conf); cJSON_Delete(conf); return 0; } else { int line; char buffer[10]; const char *xp = json, *lp, *ep = cJSON_GetErrorPtr(); for (line = 1, lp = xp; xp && xp<ep; ++line, lp = xp + 1) { xp = strchr(lp, '\n'); if (xp >= ep) break; } xp = (ep > json + 10) ? ep - 10 : json; str_strlcpy(buffer, xp, sizeof(buffer)); buffer[9] = 0; log_error("json parse error in line %d, position %d, near `%s`\n", line, ep - lp, buffer); } return 1; }
static void test_buildings(CuTest * tc) { const char * data = "{\"buildings\": { " "\"house\" : { " "\"maintenance\" : " "{ \"type\" : \"iron\", \"amount\" : 1, \"flags\" : [ \"required\", \"variable\" ] }" "," "\"construction\" : {" "\"maxsize\" : 20," "\"reqsize\" : 10," "\"minskill\" : 1," "\"materials\" : {" "\"stone\" : 2," "\"iron\" : 1" "}}}," "\"shed\" : {" "\"maintenance\" : [" "{ \"type\" : \"iron\", \"amount\" : 1 }," "{ \"type\" : \"stone\", \"amount\" : 2 }" "]}" "}}"; cJSON *json = cJSON_Parse(data); const building_type *bt; test_cleanup(); CuAssertPtrNotNull(tc, json); CuAssertPtrEquals(tc, 0, buildingtypes); json_config(json); CuAssertPtrNotNull(tc, buildingtypes); bt = bt_find("shed"); CuAssertPtrNotNull(tc, bt); CuAssertPtrNotNull(tc, bt->maintenance); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->maintenance[0].rtype); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_STONE), (void *)bt->maintenance[1].rtype); CuAssertIntEquals(tc, 1, bt->maintenance[0].number); CuAssertIntEquals(tc, 2, bt->maintenance[1].number); CuAssertIntEquals(tc, 0, bt->maintenance[2].number); bt = bt_find("house"); CuAssertPtrNotNull(tc, bt); CuAssertPtrNotNull(tc, bt->maintenance); CuAssertIntEquals(tc, 1, bt->maintenance[0].number); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->maintenance[0].rtype); CuAssertIntEquals(tc, MTF_VARIABLE|MTF_VITAL, bt->maintenance[0].flags); CuAssertIntEquals(tc, 0, bt->maintenance[1].number); CuAssertPtrNotNull(tc, bt->construction); CuAssertPtrNotNull(tc, bt->construction->materials); CuAssertIntEquals(tc, 2, bt->construction->materials[0].number); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_STONE), (void *)bt->construction->materials[0].rtype); CuAssertIntEquals(tc, 1, bt->construction->materials[1].number); CuAssertPtrEquals(tc, (void *)get_resourcetype(R_IRON), (void *)bt->construction->materials[1].rtype); CuAssertIntEquals(tc, 0, bt->construction->materials[2].number); CuAssertIntEquals(tc, 10, bt->construction->reqsize); CuAssertIntEquals(tc, 20, bt->construction->maxsize); CuAssertIntEquals(tc, 1, bt->construction->minskill); CuAssertPtrEquals(tc, 0, bt->construction->improvement); test_cleanup(); }