2018-12-15 17:22:05 +08:00
|
|
|
|
#include "tkc/utils.h"
|
|
|
|
|
#include "tkc/func_call_parser.h"
|
2018-10-20 17:49:04 +08:00
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
typedef struct _parser_t {
|
|
|
|
|
func_call_parser_t base;
|
2018-12-26 09:14:02 +08:00
|
|
|
|
char name[TK_NAME_LEN + 1];
|
2018-10-20 17:49:04 +08:00
|
|
|
|
int x;
|
|
|
|
|
int y;
|
|
|
|
|
} parser_t;
|
|
|
|
|
|
|
|
|
|
static ret_t parser_on_name(func_call_parser_t* parser, const char* func_name) {
|
|
|
|
|
parser_t* p = (parser_t*)parser;
|
|
|
|
|
|
2018-12-26 09:14:02 +08:00
|
|
|
|
tk_strncpy(p->name, func_name, TK_NAME_LEN);
|
2018-10-20 17:49:04 +08:00
|
|
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ret_t parser_on_param(func_call_parser_t* parser, const char* name, const char* value) {
|
|
|
|
|
parser_t* p = (parser_t*)parser;
|
|
|
|
|
if (tk_str_eq(name, "x")) {
|
|
|
|
|
p->x = tk_atoi(value);
|
|
|
|
|
} else if (tk_str_eq(name, "y")) {
|
|
|
|
|
p->y = tk_atoi(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static ret_t parser_on_done(func_call_parser_t* parser) {
|
|
|
|
|
return RET_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void parser_test(const char* str) {
|
|
|
|
|
parser_t parser;
|
|
|
|
|
|
|
|
|
|
memset(&parser, 0x00, sizeof(parser));
|
|
|
|
|
func_call_parser_init(&(parser.base), str, strlen(str));
|
|
|
|
|
|
|
|
|
|
parser.base.on_name = parser_on_name;
|
|
|
|
|
parser.base.on_param = parser_on_param;
|
|
|
|
|
parser.base.on_done = parser_on_done;
|
|
|
|
|
|
|
|
|
|
func_call_parser_parse(&(parser.base));
|
|
|
|
|
|
|
|
|
|
ASSERT_EQ(parser.x, 10);
|
|
|
|
|
ASSERT_EQ(parser.y, 20);
|
|
|
|
|
ASSERT_EQ(tk_str_eq(parser.name, "move"), true);
|
|
|
|
|
|
|
|
|
|
func_call_parser_deinit(&parser.base);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(FuncCallParser, basic) {
|
|
|
|
|
parser_test("move(x=10, y=20)");
|
|
|
|
|
parser_test("move (x=10, y=20)");
|
|
|
|
|
parser_test("move(x = 10, y = 20 )");
|
|
|
|
|
parser_test(" move(x = 10, y = 20 )");
|
|
|
|
|
}
|
2019-03-26 18:07:30 +08:00
|
|
|
|
|
|
|
|
|
static void parser_test_object(const char* str) {
|
|
|
|
|
object_t* obj = func_call_parse(str, strlen(str));
|
|
|
|
|
|
|
|
|
|
ASSERT_STREQ(obj->name, "move");
|
|
|
|
|
ASSERT_EQ(object_get_prop_int(obj, "x", 0), 10);
|
|
|
|
|
ASSERT_EQ(object_get_prop_int(obj, "y", 0), 20);
|
|
|
|
|
|
|
|
|
|
object_unref(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(FuncCallParser, object) {
|
|
|
|
|
parser_test_object("move(x=10, y=20)");
|
|
|
|
|
parser_test_object("move (x=10, y=20)");
|
|
|
|
|
parser_test_object("move(x = 10, y = 20 )");
|
|
|
|
|
parser_test_object(" move(x = 10, y = 20 )");
|
|
|
|
|
}
|