add test for keyword aliases, add keyword to strings for reverse lookup

This commit is contained in:
Enno Rehling 2014-06-16 20:41:08 -07:00
parent 0af5e96e3a
commit b40ddaeaa1
4 changed files with 11 additions and 3 deletions

View File

@ -278,11 +278,15 @@ static void json_keyword(cJSON *json, struct locale *lang) {
if (kwd!=NOKEYWORD) {
if (child->type==cJSON_String) {
init_keyword(lang, kwd, child->valuestring);
locale_setstring(lang, mkname("keyword", keywords[kwd]), child->valuestring);
}
else if (child->type==cJSON_Array) {
cJSON *entry;
for (entry=child->child;entry;entry=entry->next) {
init_keyword(lang, kwd, entry->valuestring);
if ((entry==child->child)) {
locale_setstring(lang, mkname("keyword", keywords[kwd]), entry->valuestring);
}
}
} else {
log_error_n("invalid type %d for keyword `%s`", child->type, child->string);

View File

@ -169,7 +169,7 @@ static void test_directions(CuTest * tc)
static void test_keywords(CuTest * tc)
{
const char * data = "{\"keywords\": { \"de\" : { \"move\" : \"nach\", \"study\" : \"lernen\" }}}";
const char * data = "{\"keywords\": { \"de\" : { \"move\" : \"NACH\", \"study\" : [ \"LERNEN\", \"STUDIEREN\" ] }}}";
const struct locale * lang;
cJSON *json = cJSON_Parse(data);
@ -180,9 +180,12 @@ static void test_keywords(CuTest * tc)
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();
}

View File

@ -105,7 +105,7 @@ static char* get_command(const order *ord, char *sbuffer, size_t size) {
if (kwd != NOKEYWORD) {
const struct locale *lang = ORD_LOCALE(ord);
if (size > 0) {
const char *str = (const char *)LOC(lang, keywords[kwd]);
const char *str = (const char *)LOC(lang, mkname("keyword", keywords[kwd]));
assert(str);
if (text) --size;
bytes = (int)strlcpy(bufp, str, size);

View File

@ -53,8 +53,9 @@ void init_tokens_str(const char *initstr, char *cmd)
{
if (states == NULL) {
states = malloc(sizeof(parser_state));
} else if (states->current_cmd)
} else if (states->current_cmd) {
free(states->current_cmd);
}
states->current_cmd = cmd;
states->current_token = initstr;
}