JSON configuration can read strings, with test.

This commit is contained in:
Enno Rehling 2014-06-28 10:37:40 -07:00
parent 9cf03d081e
commit 34ce9a0573
2 changed files with 50 additions and 0 deletions

View File

@ -329,6 +329,35 @@ void json_ships(cJSON *json) {
}
}
void json_locale(cJSON *json, struct locale *lang) {
cJSON *child;
if (json->type!=cJSON_Object) {
log_error_n("strings is not a json object: %d", json->type);
return;
}
for (child=json->child;child;child=child->next) {
if (child->type==cJSON_String) {
locale_setstring(lang, child->string, child->valuestring);
}
}
}
void json_strings(cJSON *json) {
cJSON *child;
if (json->type!=cJSON_Object) {
log_error_n("strings is not a json object: %d", json->type);
return;
}
for (child=json->child;child;child=child->next) {
if ((child->type==cJSON_Object)) {
struct locale *lang = get_or_create_locale(child->string);
json_locale(child, lang);
} else {
log_error_n("strings for locale `%s` are not a json object: %d", child->string, child->type);
}
}
}
static void json_direction(cJSON *json, struct locale *lang) {
cJSON *child;
if (json->type!=cJSON_Object) {
@ -476,6 +505,9 @@ void json_config(cJSON *json) {
else if (strcmp(child->string, "ships")==0) {
json_ships(child);
}
else if (strcmp(child->string, "strings")==0) {
json_strings(child);
}
else if (strcmp(child->string, "directions")==0) {
json_directions(child);
}

View File

@ -262,6 +262,23 @@ static void test_keywords(CuTest * tc)
test_cleanup();
}
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"));
}
CuSuite *get_jsonconf_suite(void)
{
CuSuite *suite = CuSuiteNew();
@ -273,6 +290,7 @@ CuSuite *get_jsonconf_suite(void)
SUITE_ADD_TEST(suite, test_buildings);
SUITE_ADD_TEST(suite, test_terrains);
SUITE_ADD_TEST(suite, test_races);
SUITE_ADD_TEST(suite, test_strings);
SUITE_ADD_TEST(suite, test_flags);
return suite;
}