我有一个关于如何在单元测试中“驱动”基于 flex bison 的解析器扫描仪的问题。
最终的解决方案将是一个可用的命令解析器或远程登录到目标板。我有一个使用标准输入的完全工作的 flex bison 实现。
现在我的重点是让命令解析器运行单元测试。
我希望能够向解析器(一个命令)提供一个“常量字符串”,然后测试在应用程序中调用了相应的命令(在应用程序存根中)。
我不知道如何为此设置 flex 和 bison。请在下面找到测试用例:
status_cmd_test.c:
#include "CUnit/Basic.h"
#include "cmd_stub.h"
void scan_string(const char* str);
void testSTATUS_OK(void)
{
scan_string("status\n\0\0");
CU_ASSERT(1 == status_sub_nrof_invokes())
}
摘自 cmd_lexer.l:
void scan_string(const char* str)
{
YY_BUFFER_STATE buf;
buf = yy_scan_string(str);
yylex();
yy_delete_buffer(buf);
}
cmd_parser.y 不包含任何 c 代码,只有野牛语法。
cmd_test.c 的摘录(具有 cunit 代码所在的 int main())
if (NULL == CU_add_test(suite_p, "test of status", testSTATUS_OK))
{
CU_cleanup_registry();
return CU_get_error();
}
/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
CU_cleanup_registry();
return CU_get_error();
我试图通过我不知道如何驾驶野牛( yyparse() 或类似的东西)来理解文档。
谁能给我一个提示?
/ 米凯尔