From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org> To: Igor Munkin <imun@tarantool.org>, Maxim Kokryashkin <m.kokryashkin@tarantool.org> Cc: tarantool-patches@dev.tarantool.org Subject: [Tarantool-patches] [PATCH v1 luajit 3/5] test: introduce utils.h helper for C tests Date: Wed, 15 Mar 2023 19:11:03 +0300 [thread overview] Message-ID: <347ba509b444328b515dbf7463426401283bf217.1678895861.git.skaplun@tarantool.org> (raw) In-Reply-To: <cover.1678895861.git.skaplun@tarantool.org> This header contains generic init and close for tests and helpers for 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 --- Those introduced utils macros assume that we have the table on the top of the Lua stack. So, after each test the stack should be stayed "untouched". But if we push helper function or some value of the Lua stack and them assertion fails, we exit from function without stack cleaning. So we should try to avoid such situation whenever appropriate, but don't be fanatical at this point -- if some assertion fails, we are in troubles anyway. Don't use __FILE__ here, because it may contain relative path to the file depending on CMake version or -fmacro-prefix-map= flag. 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__; \ + 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) -- 2.34.1
next prev parent reply other threads:[~2023-03-15 16:16 UTC|newest] Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-03-15 16:11 [Tarantool-patches] [PATCH v1 luajit 0/5] reworking " Sergey Kaplun via Tarantool-patches 2023-03-15 16:11 ` [Tarantool-patches] [PATCH v1 luajit 1/5] test: fix setting of {DY}LD_LIBRARY_PATH variables Sergey Kaplun via Tarantool-patches 2023-03-20 13:54 ` Maxim Kokryashkin via Tarantool-patches 2023-03-15 16:11 ` [Tarantool-patches] [PATCH v1 luajit 2/5] test: introduce module for C tests Sergey Kaplun via Tarantool-patches 2023-03-20 15:17 ` Maxim Kokryashkin via Tarantool-patches 2023-03-15 16:11 ` Sergey Kaplun via Tarantool-patches [this message] 2023-03-20 15:21 ` [Tarantool-patches] [PATCH v1 luajit 3/5] test: introduce utils.h helper " Maxim Kokryashkin via Tarantool-patches 2023-03-15 16:11 ` [Tarantool-patches] [PATCH v1 luajit 4/5] test: rewrite misclib-getmetrics-capi test in C Sergey Kaplun via Tarantool-patches 2023-03-22 0:07 ` Maxim Kokryashkin via Tarantool-patches 2023-03-15 16:11 ` [Tarantool-patches] [PATCH v1 luajit 5/5] test: rewrite misclib-sysprof-capi " Sergey Kaplun via Tarantool-patches 2023-03-20 16:24 ` Maxim Kokryashkin via Tarantool-patches 2023-03-20 13:50 ` [Tarantool-patches] [PATCH v1 luajit 0/5] reworking C tests Maxim Kokryashkin via Tarantool-patches
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=347ba509b444328b515dbf7463426401283bf217.1678895861.git.skaplun@tarantool.org \ --to=tarantool-patches@dev.tarantool.org \ --cc=imun@tarantool.org \ --cc=m.kokryashkin@tarantool.org \ --cc=skaplun@tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH v1 luajit 3/5] test: introduce utils.h helper for C tests' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox