From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 609D4376362; Wed, 15 Mar 2023 19:16:21 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 609D4376362 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1678896981; bh=FiPqMdvg705wxi4ox1mqd5RFrP4BILr8YnWDLOqcXQk=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=g2J33biySfx6raAZ+bfbCED1B16mwuhS9jFxB5+ME9iye6VKOOyWcdqqF67CyANA8 naSe4GZvJoAkuPrXaKFWAnkoky1YQsA8ThXe9QGyF2Jf2dFyegkv3bvYn4S8Zp+d1M UI2/g0lJGMyoxZcvVUQmzcEY2hhqCEI595GCNV5Y= Received: from smtpng1.i.mail.ru (smtpng1.i.mail.ru [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id A349B376365 for ; Wed, 15 Mar 2023 19:14:52 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org A349B376365 Received: by smtpng1.m.smailru.net with esmtpa (envelope-from ) id 1pcTm7-0003ZV-Ng; Wed, 15 Mar 2023 19:14:52 +0300 To: Igor Munkin , Maxim Kokryashkin Date: Wed, 15 Mar 2023 19:11:03 +0300 Message-Id: <347ba509b444328b515dbf7463426401283bf217.1678895861.git.skaplun@tarantool.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9BCEC41593EBD8357A3BFCFA0FC8EDD65B542913A4AA2B57D182A05F538085040D27376F5D4C1B52C1D218680C70378C5C49CEBF21CCB794D9CB57BCE56496B57 X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7DB84ED444C624799EA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637C21C2889FA5309238638F802B75D45FF36EB9D2243A4F8B5A6FCA7DBDB1FC311F39EFFDF887939037866D6147AF826D8836B80CC50EA324ACB6BB62254016D19117882F4460429724CE54428C33FAD305F5C1EE8F4F765FC59F0E22272DBA2DBA471835C12D1D9774AD6D5ED66289B52BA9C0B312567BB23117882F4460429728776938767073520437C869540D2AB0FF04B652EEC242312D2E47CDBA5A96583BA9C0B312567BB231DD303D21008E29813377AFFFEAFD269A417C69337E82CC2E827F84554CEF50127C277FBC8AE2E8BA83251EDC214901ED5E8D9A59859A8B6B1CFA6D474D4A6A4089D37D7C0E48F6C5571747095F342E88FB05168BE4CE3AF X-B7AD71C0: 4965CFDFE0519134C1FE400A9E48C5401DD40DE57556AFB266D16FC5F53507A1816E0A2A8F779BBED8D40077074E805C66D16FC5F53507A117535B0CF9F6D0C3EE9D5CB6078CC77CCA7DBF51DC25A365EE319BF62A11F40A X-C8649E89: 4E36BF7865823D7055A7F0CF078B5EC49A30900B95165D34CA9B3B5DE5D383E6AD710EE8704207D4BEA53F5036A60B7745034A5ED0E1E4955AA1C604DAD6A1301D7E09C32AA3244C8EE22F356EC5C63C41DC616352CF43D67C0C08F7987826B9927AC6DF5659F194 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu530nj6fImhcD4MUrOEAnl0W826KZ9Q+tr5ycPtXkTV4k65bRjmOUUP8cvGozZ33TWg5HZplvhhXbhDGzqmQDTd6OAevLeAnq3Ra9uf7zvY2zzsIhlcp/Y7m53TZgf2aB4JOg4gkr2biojOXwBDQf4j7MEEIEXJYbzRA== X-DA7885C5: 5ABF57B33C4484DD992EB771DD7126D0FCDDF4F3A66E3FBA525F3F9A009509BE262E2D401490A4A0DB037EFA58388B346E8BC1A9835FDE71 X-Mailru-Sender: 689FA8AB762F73933AF1F914F131DBF5CF336CA814896E5C55EE27FE14CC66A60FBE9A32752B8C9C2AA642CC12EC09F1FB559BB5D741EB962F61BD320559CF1EFD657A8799238ED55FEEDEB644C299C0ED14614B50AE0675 X-Mras: Ok Subject: [Tarantool-patches] [PATCH v1 luajit 3/5] test: introduce utils.h helper for C tests X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Sergey Kaplun via Tarantool-patches Reply-To: Sergey Kaplun Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" 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 +#include + +#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 . + * 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