Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Igor Munkin <imun@tarantool.org>,
	Maxim Kokryashkin <m.kokryashkin@tarantool.org>,
	Sergey Bronnikov <sergeyb@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v2 luajit 3/6] test: introduce utils.h helper for C tests
Date: Thu, 18 May 2023 23:44:50 +0300	[thread overview]
Message-ID: <d2ce80e540b9b4635d3fb7fd6b007fa372f05b6a.1684442182.git.skaplun@tarantool.org> (raw)
In-Reply-To: <cover.1684442182.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
---
 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


  parent reply	other threads:[~2023-05-18 20:50 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-18 20:44 [Tarantool-patches] [PATCH v2 luajit 0/6] Revorking " Sergey Kaplun via Tarantool-patches
2023-05-18 20:44 ` [Tarantool-patches] [PATCH v2 luajit 1/6] test: fix setting of {DY}LD_LIBRARY_PATH variables Sergey Kaplun via Tarantool-patches
2023-05-19 11:23   ` Maxim Kokryashkin via Tarantool-patches
2023-05-22 11:03   ` Sergey Bronnikov via Tarantool-patches
2023-05-23  6:47     ` Sergey Kaplun via Tarantool-patches
2023-05-29 14:37       ` Sergey Bronnikov via Tarantool-patches
2023-05-18 20:44 ` [Tarantool-patches] [PATCH v2 luajit 2/6] test: introduce module for C tests Sergey Kaplun via Tarantool-patches
2023-05-19 11:46   ` Maxim Kokryashkin via Tarantool-patches
2023-05-22 12:33   ` Sergey Bronnikov via Tarantool-patches
2023-05-24  6:41     ` Sergey Kaplun via Tarantool-patches
2023-05-25 17:33       ` Sergey Bronnikov via Tarantool-patches
2023-05-29 10:03         ` Sergey Kaplun via Tarantool-patches
2023-05-29 14:38           ` Sergey Bronnikov via Tarantool-patches
2023-05-31 13:32             ` Sergey Kaplun via Tarantool-patches
2023-05-18 20:44 ` Sergey Kaplun via Tarantool-patches [this message]
2023-05-19 11:58   ` [Tarantool-patches] [PATCH v2 luajit 3/6] test: introduce utils.h helper " Maxim Kokryashkin via Tarantool-patches
2023-05-20  7:52     ` Sergey Kaplun via Tarantool-patches
2023-05-29 15:26   ` Sergey Bronnikov via Tarantool-patches
2023-05-18 20:44 ` [Tarantool-patches] [PATCH v2 luajit 4/6] test: rewrite misclib-getmetrics-capi test in C Sergey Kaplun via Tarantool-patches
2023-05-19 12:17   ` Maxim Kokryashkin via Tarantool-patches
2023-05-20  8:08     ` Sergey Kaplun via Tarantool-patches
2023-05-29 16:15   ` Sergey Bronnikov via Tarantool-patches
2023-05-18 20:44 ` [Tarantool-patches] [PATCH v2 luajit 5/6] test: rewrite misclib-sysprof-capi " Sergey Kaplun via Tarantool-patches
2023-05-19 13:00   ` Maxim Kokryashkin via Tarantool-patches
2023-05-20  7:28     ` Sergey Kaplun via Tarantool-patches
2023-05-18 20:44 ` [Tarantool-patches] [PATCH v2 luajit 6/6] test: rewrite lj-49-bad-lightuserdata " Sergey Kaplun via Tarantool-patches
2023-05-19 12:40   ` Maxim Kokryashkin via Tarantool-patches
2023-05-19 14:29 ` [Tarantool-patches] [PATCH v2 luajit 0/6] Revorking C tests Maxim Kokryashkin via Tarantool-patches
2023-05-20  8:38   ` Sergey Kaplun 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=d2ce80e540b9b4635d3fb7fd6b007fa372f05b6a.1684442182.git.skaplun@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=imun@tarantool.org \
    --cc=m.kokryashkin@tarantool.org \
    --cc=sergeyb@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v2 luajit 3/6] 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