4a6ade44 |
struct test {
const char *str;
int is;
};
static struct test kw_test[] = {
{"new",1},
{"eval",0},
{"function",1},
{"eval1",0},
{"ne",0}
};
static struct test op_test[] = {
{"-",1},
{"---",0}
};
START_TEST (test_keywords)
{
const struct keyword *kw = in_word_set(kw_test[_i].str, strlen(kw_test[_i].str));
if(kw_test[_i].is) {
fail_unless(kw && !strcmp(kw->name, kw_test[_i].str), "keyword mismatch");
} else {
fail_unless(!kw, "non-keyword detected as keyword");
}
}
END_TEST
START_TEST (test_operators)
{
const struct operator *op = in_op_set(op_test[_i].str, strlen(op_test[_i].str));
if(op_test[_i].is)
fail_unless(op && !strcmp(op->name, op_test[_i].str), "operator mismatch");
else
fail_unless(!op, "non-operator detected as operator");
}
END_TEST
START_TEST (test_token_string)
{
char str[] = "test";
yystype tok;
memset(&tok, 0, sizeof(tok));
TOKEN_SET(&tok, string, str);
fail_unless(TOKEN_GET(&tok, string) == str, "token string get/set");
fail_unless(TOKEN_GET(&tok, cstring) == str, "token string->cstring");
fail_unless(TOKEN_GET(&tok, scope) == NULL, "token string->scope");
fail_unless(TOKEN_GET(&tok, ival) == -1, "token string->ival");
}
END_TEST
START_TEST (test_token_cstring)
{
const char *str = "test";
yystype tok;
memset(&tok, 0, sizeof(tok));
TOKEN_SET(&tok, cstring, str);
fail_unless(TOKEN_GET(&tok, string) == NULL, "token cstring->string");
fail_unless(TOKEN_GET(&tok, cstring) == str, "token string->cstring");
fail_unless(TOKEN_GET(&tok, scope) == NULL, "token string->scope");
fail_unless(TOKEN_GET(&tok, ival) == -1, "token string->ival");
}
END_TEST
START_TEST (test_token_scope)
{
struct scope *sc = (struct scope*)0xdeadbeef;
yystype tok;
memset(&tok, 0, sizeof(tok));
TOKEN_SET(&tok, scope, sc);
fail_unless(TOKEN_GET(&tok, string) == NULL, "token scope->string");
fail_unless(TOKEN_GET(&tok, cstring) == NULL, "token scope->cstring");
fail_unless(TOKEN_GET(&tok, scope) == sc, "token scope->scope");
fail_unless(TOKEN_GET(&tok, ival) == -1, "token scope->ival");
}
END_TEST
START_TEST (test_token_ival)
{
int val = 0x1234567;
yystype tok;
memset(&tok, 0, sizeof(tok));
TOKEN_SET(&tok, ival, val);
fail_unless(TOKEN_GET(&tok, string) == NULL, "token ival->string");
fail_unless(TOKEN_GET(&tok, cstring) == NULL, "token ival->cstring");
fail_unless(TOKEN_GET(&tok, scope) == NULL, "token ival->scope");
fail_unless(TOKEN_GET(&tok, dval) == -1, "token ival->dval");
fail_unless(TOKEN_GET(&tok, ival) == val, "token ival->ival");
}
END_TEST
START_TEST (test_token_dval)
{
int val = 0.12345;
yystype tok;
memset(&tok, 0, sizeof(tok));
TOKEN_SET(&tok, dval, val);
fail_unless(TOKEN_GET(&tok, string) == NULL, "token dval->string");
fail_unless(TOKEN_GET(&tok, cstring) == NULL, "token dval->cstring");
fail_unless(TOKEN_GET(&tok, scope) == NULL, "token dval->scope");
fail_unless(TOKEN_GET(&tok, dval) == val, "token dval->dval");
fail_unless(TOKEN_GET(&tok, ival) == -1, "token dval->ival");
}
END_TEST
START_TEST (test_init_destroy)
{
struct parser_state *state = cli_js_init();
fail_unless(!!state, "cli_js_init()");
cli_js_destroy(state);
}
END_TEST
START_TEST (test_init_parse_destroy)
{
const char buf[] = "function (p) { return \"anonymous\";}";
struct parser_state *state = cli_js_init();
fail_unless(!!state, "cli_js_init()");
cli_js_process_buffer(state, buf, strlen(buf));
cli_js_process_buffer(state, buf, strlen(buf));
cli_js_parse_done(state);
cli_js_destroy(state);
}
END_TEST
|