如何在Linux下建立包含lua vm的unit test framwork

0 引言

lua是一种语法极为灵活、扩展性极强的“胶水语言”, 在使用lua/lua capi时常常会写出一些容易出错的code. 因此,有必要建立以lua vm为基础的unit test帮助程序员及早地发现bug,提高代码的质量。为此,有三件事情需要做。

1 编译配置googletest/googlemock环境

https://*.com/questions/13513905/how-to-set-up-googletest-as-a-shared-library-on-linux

2 编译配置lua5.0

2.1 download src file: https://www.lua.org/ftp/lua-5.0.3.tar.gz

2.2 make

2.3 write testing code:

#include <stdio.h>
#include <string.h>
extern "C" {///< It is very important to wrap related header files into this cracket, or you will get some error message as below.
#include "mylua/lua.h"
#include "mylua/lauxlib.h"
#include "mylua/lualib.h"
}

int main(void)
{
    char buff[256] = "print(\"hello world!\n\")";
    int error;
    lua_State* L = lua_open(); /* opens Lua */
    luaopen_base(L);           /* opens the basic library */
    luaopen_table(L);          /* opens the table library */
    luaopen_io(L);             /* opens the I/O library */
    luaopen_string(L);         /* opens the string lib. */
    luaopen_math(L);           /* opens the math lib. */

    while (fgets(buff, sizeof(buff), stdin) != NULL) {
        error = luaL_loadbuffer(L, buff, strlen(buff), "line") || lua_pcall(L, 0, 0, 0);
        if (error) {
            fprintf(stderr, "%s", lua_tostring(L, -1));
            lua_pop(L, 1); /* pop error message from the stack */
        }
    }

    lua_close(L);
    return 0;
}

如何在Linux下建立包含lua vm的unit test framwork

 2.3 use g++ to build: 

g++ test.c -I $LD_HOME/include -L $LD_HOME/lib -llua -llualib -o app

 

3 集成测试

上一篇:lua结合c/c++示例


下一篇:Lua安装及编译环境