additional tests for the parser.

move more parsing code out of config.c
This commit is contained in:
Enno Rehling 2014-12-22 16:31:10 +01:00
parent c8b9044f78
commit 5ebf05f625
6 changed files with 46 additions and 17 deletions

View File

@ -794,20 +794,6 @@ parse(keyword_t kword, int(*dofun) (unit *, struct order *), bool thisorder)
} }
} }
unsigned int getuint(void)
{
char token[16];
const char *s = gettoken(token, sizeof(token));
return s ? atoip(s) : 0;
}
int getint(void)
{
char token[16];
const char * s = gettoken(token, sizeof(token));
return s ? atoi(s) : 0;
}
const struct race *findrace(const char *s, const struct locale *lang) const struct race *findrace(const char *s, const struct locale *lang)
{ {
void **tokens = get_translations(lang, UT_RACES); void **tokens = get_translations(lang, UT_RACES);

View File

@ -109,8 +109,6 @@ extern "C" {
void make_undead_unit(struct unit *); void make_undead_unit(struct unit *);
unsigned int atoip(const char *s); unsigned int atoip(const char *s);
unsigned int getuint(void);
int getint(void);
param_t findparam(const char *s, const struct locale *lang); param_t findparam(const char *s, const struct locale *lang);
param_t findparam_ex(const char *s, const struct locale * lang); param_t findparam_ex(const char *s, const struct locale * lang);

View File

@ -58,7 +58,8 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
static skill_t getskill(const struct locale *lang) static skill_t getskill(const struct locale *lang)
{ {
const char * s = getstrtoken(); char token[128];
const char * s = gettoken(token, sizeof(token));
return s ? get_skill(s, lang) : NOSKILL; return s ? get_skill(s, lang) : NOSKILL;
} }

View File

@ -217,6 +217,19 @@ const char *gettoken(char *lbuf, size_t bufsize)
return parse_token((const char **)&states->current_token, lbuf, bufsize); return parse_token((const char **)&states->current_token, lbuf, bufsize);
} }
int getint(void)
{
char token[16];
const char * s = gettoken(token, sizeof(token));
return s ? atoi(s) : 0;
}
unsigned int getuint(void)
{
int n = getint();
return (n < 0) ? 0 : n;
}
int getid(void) int getid(void)
{ {
char token[16]; char token[16];

View File

@ -23,6 +23,8 @@ extern "C" {
bool parser_end(void); bool parser_end(void);
const char *getstrtoken(void); const char *getstrtoken(void);
const char *gettoken(char *lbuf, size_t bufsize); const char *gettoken(char *lbuf, size_t bufsize);
unsigned int getuint(void);
int getint(void);
int getid(void); int getid(void);
#define getshipid() getid() #define getshipid() getid()
#define getfactionid() getid() #define getfactionid() getid()

View File

@ -15,9 +15,38 @@ static void test_gettoken(CuTest *tc) {
CuAssertStrEquals(tc, "", token); CuAssertStrEquals(tc, "", token);
} }
static void test_skip_token(CuTest *tc) {
char token[128];
init_tokens_str("HELP ONE TWO THREE");
skip_token();
CuAssertStrEquals(tc, "ONE", gettoken(token, sizeof(token)));
}
static void test_getintegers(CuTest *tc) {
init_tokens_str("ii 666 666 -42 -42");
CuAssertIntEquals(tc, 666, getid());
CuAssertIntEquals(tc, 666, getint());
CuAssertIntEquals(tc, 666, getuint());
CuAssertIntEquals(tc, -42, getint());
CuAssertIntEquals(tc, 0, getuint());
CuAssertIntEquals(tc, 0, getint());
}
static void test_getstrtoken(CuTest *tc) {
init_tokens_str("HELP ONE TWO THREE");
CuAssertStrEquals(tc, "HELP", getstrtoken());
CuAssertStrEquals(tc, "ONE", getstrtoken());
CuAssertStrEquals(tc, "TWO", getstrtoken());
CuAssertStrEquals(tc, "THREE", getstrtoken());
CuAssertPtrEquals(tc, NULL, (void *)getstrtoken());
}
CuSuite *get_parser_suite(void) CuSuite *get_parser_suite(void)
{ {
CuSuite *suite = CuSuiteNew(); CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_skip_token);
SUITE_ADD_TEST(suite, test_gettoken); SUITE_ADD_TEST(suite, test_gettoken);
SUITE_ADD_TEST(suite, test_getintegers);
SUITE_ADD_TEST(suite, test_getstrtoken);
return suite; return suite;
} }