[Tarantool-patches] [PATCH v2 luajit 3/6] test: introduce utils.h helper for C tests
Sergey Bronnikov
sergeyb at tarantool.org
Mon May 29 18:26:15 MSK 2023
Hi, Sergey!
Thanks for the patch! See my comments below.
On 5/18/23 23:44, Sergey Kaplun wrote:
> This header contains generic init and close for tests and helpers for
probably "generic init and close *functions*"?
> loading auxiliary Lua script with functions to run inside a C test.
>
> It will be properly used in the next commit.
>
> Part of tarantool/tarantool#7900
> ---
> test/tarantool-c-tests/utils.h | 63 ++++++++++++++++++++++++++++++++++
> 1 file changed, 63 insertions(+)
> create mode 100644 test/tarantool-c-tests/utils.h
>
> diff --git a/test/tarantool-c-tests/utils.h b/test/tarantool-c-tests/utils.h
> new file mode 100644
> index 00000000..cf668006
> --- /dev/null
> +++ b/test/tarantool-c-tests/utils.h
> @@ -0,0 +1,63 @@
> +#include <limits.h>
> +#include <string.h>
> +
> +#include "lauxlib.h"
> +#include "lua.h"
> +#include "luajit.h"
> +#include "lualib.h"
> +
> +#include "test.h"
> +
> +/* Generic init for our tests. */
> +static lua_State *utils_lua_init(void)
> +{
> + lua_State *L = luaL_newstate();
> + if (!L)
> + bail_out("Can't init Lua state");
> + /* Stop collector during library initialization. */
> + lua_gc(L, LUA_GCSTOP, 0);
> + luaL_openlibs(L);
> + lua_gc(L, LUA_GCRESTART, -1);
> + return L;
> +}
> +
> +/* Generic close for our tests. */
> +static void utils_lua_close(lua_State *L)
> +{
> + lua_close(L);
> +}
> +
> +/*
> + * Load the pair to the test file <filename-script.lua>.
> + * Each file should return the table with functions (named the
> + * same as the corresponding unit tests) to call in unit tests.
> + * Push the table with those functions on the Lua stack.
> + */
> +#define utils_load_aux_script(L) do { \
> + /* \
> + * Format script name. \
> + * `__ABS_FILENAME__` is set via CMake. \
> + */ \
> + char script[PATH_MAX] = __ABS_FILENAME__; \
Why do you need to pass user script to a test library from a build system?
Can we pass it in a user function?
my_test.c:
char script_path[PATH_MAX] = __TEST_LUA_SCRIPT_ABS_PATH__;
utils_load_aux_script(L, script_path)
BTW filename cannot be absolute, path could be.
> + char *file_suffix = strstr(script, ".test.c"); \
> + strcpy(file_suffix, "-script.lua"); \
> + \
> + if (luaL_dofile((L), script) != LUA_OK) { \
> + test_comment("Can't load %s: '%s'", script, \
> + lua_tostring((L), -1)); \
> + bail_out("Can't load auxiliary script"); \
> + } \
> + \
> + if (!lua_istable((L), -1)) \
> + bail_out("Returned value from script is not a table"); \
> +} while (0)
> +
> +/*
> + * Accept a table on top of the Lua stack which containing the
> + * function named as the unit test we currently executing.
> + */
> +#define utils_get_aux_lfunc(L) do { \
> + lua_getfield((L), -1, __func__); \
> + if (!lua_isfunction((L), -1)) \
> + bail_out("Can't get auxiliary test function"); \
> +} while (0)
More information about the Tarantool-patches
mailing list