2015-04-16 02:55:27 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2019-02-21 06:04:31 -08:00
|
|
|
#include <unistd.h>
|
2015-04-16 02:55:27 -07:00
|
|
|
|
2016-02-04 05:57:14 -08:00
|
|
|
#include <Eina.h>
|
|
|
|
#include <Elua.h>
|
|
|
|
|
2015-04-16 02:55:27 -07:00
|
|
|
#include "elua_suite.h"
|
|
|
|
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_START_TEST(elua_api)
|
2015-04-16 02:55:27 -07:00
|
|
|
{
|
2015-04-16 06:02:21 -07:00
|
|
|
Elua_State *st;
|
|
|
|
lua_State *lst;
|
2015-04-16 08:06:48 -07:00
|
|
|
char buf[] = "tmpXXXXXX";
|
|
|
|
FILE *f;
|
|
|
|
int fd;
|
2015-04-16 08:20:55 -07:00
|
|
|
char *cargv[2];
|
|
|
|
char arg1[] = "test";
|
2015-05-07 03:56:59 -07:00
|
|
|
char arg2[] = "test";
|
2015-04-16 08:20:55 -07:00
|
|
|
int quit = 0;
|
|
|
|
cargv[0] = arg1;
|
|
|
|
cargv[1] = arg2;
|
2015-06-09 06:01:25 -07:00
|
|
|
char *spath = NULL;
|
2015-04-16 06:02:21 -07:00
|
|
|
|
|
|
|
st = elua_state_new("test");
|
2015-04-16 05:45:08 -07:00
|
|
|
fail_if(!st);
|
|
|
|
|
2015-04-16 07:08:02 -07:00
|
|
|
/* test env vars */
|
|
|
|
setenv("ELUA_CORE_DIR", "foo", 1);
|
|
|
|
setenv("ELUA_MODULES_DIR", "bar", 1);
|
|
|
|
setenv("ELUA_APPS_DIR", "baz", 1);
|
|
|
|
elua_state_dirs_fill(st, EINA_FALSE);
|
2015-06-09 06:01:25 -07:00
|
|
|
|
|
|
|
spath = eina_file_path_sanitize("foo");
|
|
|
|
fail_if(strcmp(elua_state_core_dir_get(st), spath));
|
|
|
|
free(spath);
|
|
|
|
spath = eina_file_path_sanitize("bar");
|
|
|
|
fail_if(strcmp(elua_state_mod_dir_get(st), spath));
|
|
|
|
free(spath);
|
|
|
|
spath = eina_file_path_sanitize("baz");
|
|
|
|
fail_if(strcmp(elua_state_apps_dir_get(st), spath));
|
|
|
|
free(spath);
|
2015-04-16 07:38:28 -07:00
|
|
|
unsetenv("ELUA_CORE_DIR");
|
|
|
|
unsetenv("ELUA_MODULES_DIR");
|
|
|
|
unsetenv("ELUA_APPS_DIR");
|
2015-04-16 07:08:02 -07:00
|
|
|
|
|
|
|
/* now fill it properly */
|
2015-04-16 06:58:42 -07:00
|
|
|
elua_state_dirs_set(st, ELUA_CORE_DIR, ELUA_MODULES_DIR, ELUA_APPS_DIR);
|
2015-06-09 06:01:25 -07:00
|
|
|
spath = eina_file_path_sanitize(ELUA_CORE_DIR);
|
|
|
|
fail_if(strcmp(elua_state_core_dir_get(st), spath));
|
|
|
|
free(spath);
|
|
|
|
spath = eina_file_path_sanitize(ELUA_MODULES_DIR);
|
|
|
|
fail_if(strcmp(elua_state_mod_dir_get(st), spath));
|
|
|
|
free(spath);
|
|
|
|
spath = eina_file_path_sanitize(ELUA_APPS_DIR);
|
|
|
|
fail_if(strcmp(elua_state_apps_dir_get(st), spath));
|
|
|
|
free(spath);
|
2015-04-16 06:58:42 -07:00
|
|
|
|
2015-04-16 07:23:33 -07:00
|
|
|
/* needed for later setup, but untestable alone */
|
|
|
|
elua_state_include_path_add(st, ELUA_BINDINGS_DIR);
|
|
|
|
|
2015-04-16 06:02:21 -07:00
|
|
|
lst = elua_state_lua_state_get(st);
|
|
|
|
fail_if(!lst);
|
|
|
|
|
|
|
|
fail_if(elua_state_from_lua_state_get(lst) != st);
|
|
|
|
|
2015-04-16 06:08:28 -07:00
|
|
|
fail_if(strcmp(elua_state_prog_name_get(st), "test"));
|
|
|
|
|
2015-04-23 08:13:27 -07:00
|
|
|
fail_if(!elua_state_setup(st));
|
2015-04-16 07:11:43 -07:00
|
|
|
|
2015-04-16 07:16:15 -07:00
|
|
|
fail_if(!elua_state_require_ref_push(st));
|
|
|
|
fail_if(lua_type(lst, -1) != LUA_TFUNCTION);
|
|
|
|
lua_pop(lst, 1);
|
|
|
|
|
|
|
|
fail_if(!elua_state_appload_ref_push(st));
|
|
|
|
fail_if(lua_type(lst, -1) != LUA_TFUNCTION);
|
|
|
|
lua_pop(lst, 1);
|
|
|
|
|
2015-04-16 08:06:48 -07:00
|
|
|
fd = mkstemp(buf);
|
|
|
|
fail_if(fd < 0);
|
2015-12-03 01:52:56 -08:00
|
|
|
f = fdopen(fd, "wb");
|
2015-04-16 08:06:48 -07:00
|
|
|
fail_if(!f);
|
|
|
|
fprintf(f, "return 5\n");
|
|
|
|
fclose(f);
|
2015-04-24 08:01:24 -07:00
|
|
|
fail_if(!elua_util_file_run(st, buf));
|
2015-04-16 08:06:48 -07:00
|
|
|
fail_if(lua_tointeger(lst, -1) != 5);
|
|
|
|
lua_pop(lst, 1);
|
|
|
|
fail_if(remove(buf));
|
|
|
|
|
2015-04-16 07:33:10 -07:00
|
|
|
/* halfassed testing here, but not possible otherwise */
|
2015-04-23 08:13:27 -07:00
|
|
|
fail_if(elua_util_error_report(st, 0));
|
2015-04-16 07:33:10 -07:00
|
|
|
lua_pushliteral(lst, "msg");
|
2015-04-23 08:13:27 -07:00
|
|
|
fail_if(!elua_util_error_report(st, 5));
|
2015-04-16 07:33:10 -07:00
|
|
|
fail_if(lua_gettop(lst) > 0);
|
2015-04-16 07:23:33 -07:00
|
|
|
|
2019-02-27 09:38:43 -08:00
|
|
|
f = fopen(buf, "wb");
|
|
|
|
fail_if(!f);
|
|
|
|
fprintf(f, "return true");
|
|
|
|
fclose(f);
|
|
|
|
cargv[1] = buf;
|
2015-04-24 08:01:24 -07:00
|
|
|
fail_if(!elua_util_script_run(st, 2, cargv, 1, &quit));
|
2015-04-16 08:20:55 -07:00
|
|
|
fail_if(quit != 1);
|
|
|
|
|
2015-12-03 00:32:39 -08:00
|
|
|
f = fopen(buf, "wb");
|
2015-04-16 08:20:55 -07:00
|
|
|
fail_if(!f);
|
|
|
|
fprintf(f, "return false");
|
|
|
|
fclose(f);
|
2015-04-24 08:01:24 -07:00
|
|
|
fail_if(!elua_util_script_run(st, 2, cargv, 1, &quit));
|
2015-04-16 08:20:55 -07:00
|
|
|
fail_if(quit != 0);
|
|
|
|
fail_if(remove(buf));
|
|
|
|
|
2019-02-27 09:38:43 -08:00
|
|
|
/* elua API here tries accessing files by relative path,
|
|
|
|
* prevent any unintentional file accesses in cwd
|
|
|
|
*/
|
|
|
|
fail_if(chdir(TESTS_SRC_DIR));
|
|
|
|
|
|
|
|
fail_if(!elua_util_require(st, "util"));
|
|
|
|
fail_if(!elua_util_string_run(st, "return 1337", "foo"));
|
|
|
|
fail_if(elua_util_string_run(st, "foo bar", "foo")); /* invalid code */
|
|
|
|
fail_if(elua_util_app_load(st, "test"));
|
|
|
|
fail_if(lua_type(lst, -1) != LUA_TFUNCTION);
|
|
|
|
lua_pop(lst, 1);
|
|
|
|
fail_if(!elua_util_app_load(st, "non_existent_app"));
|
|
|
|
fail_if(lua_type(lst, -1) != LUA_TSTRING);
|
|
|
|
lua_pop(lst, 1);
|
|
|
|
fail_if(elua_io_loadfile(st, ELUA_CORE_DIR "/util.lua"));
|
|
|
|
fail_if(lua_type(lst, -1) != LUA_TFUNCTION);
|
|
|
|
lua_pop(lst, 1);
|
|
|
|
fail_if(!elua_io_loadfile(st, ELUA_CORE_DIR "/non_existent_file.lua"));
|
|
|
|
fail_if(lua_type(lst, -1) != LUA_TSTRING);
|
|
|
|
lua_pop(lst, 1);
|
|
|
|
|
2015-04-16 05:45:08 -07:00
|
|
|
elua_state_free(st);
|
2015-04-16 02:55:27 -07:00
|
|
|
}
|
2018-03-26 11:19:28 -07:00
|
|
|
EFL_END_TEST
|
2015-04-16 02:55:27 -07:00
|
|
|
|
|
|
|
void elua_lib_test(TCase *tc)
|
|
|
|
{
|
2015-04-16 05:45:08 -07:00
|
|
|
tcase_add_test(tc, elua_api);
|
2015-04-16 02:55:27 -07:00
|
|
|
}
|
|
|
|
|