summaryrefslogtreecommitdiff
path: root/src/tests/elua/elua_lib.c
blob: 0d670e56d8bc7397930ab8f5df5487662a14bfda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include<Eina.h>
#include "Elua.h"
#include "elua_suite.h"

START_TEST(elua_api)
{
    Elua_State *st;
    lua_State *lst;
    char buf[] = "tmpXXXXXX";
    FILE *f;
    int fd;
    char *cargv[2];
    char arg1[] = "test";
    char arg2[] = "lualian";
    int quit = 0;
    cargv[0] = arg1;
    cargv[1] = arg2;

    fail_if(!elua_init());

    st = elua_state_new("test");
    fail_if(!st);

    /* 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);
    fail_if(strcmp(elua_state_core_dir_get(st), "foo"));
    fail_if(strcmp(elua_state_mod_dir_get(st), "bar"));
    fail_if(strcmp(elua_state_apps_dir_get(st), "baz"));
    unsetenv("ELUA_CORE_DIR");
    unsetenv("ELUA_MODULES_DIR");
    unsetenv("ELUA_APPS_DIR");

    /* now fill it properly */
    elua_state_dirs_set(st, ELUA_CORE_DIR, ELUA_MODULES_DIR, ELUA_APPS_DIR);
    fail_if(strcmp(elua_state_core_dir_get(st), ELUA_CORE_DIR));
    fail_if(strcmp(elua_state_mod_dir_get(st), ELUA_MODULES_DIR));
    fail_if(strcmp(elua_state_apps_dir_get(st), ELUA_APPS_DIR));

    /* needed for later setup, but untestable alone */
    elua_state_include_path_add(st, ELUA_BINDINGS_DIR);

    lst = elua_state_lua_state_get(st);
    fail_if(!lst);

    fail_if(elua_state_from_lua_state_get(lst) != st);

    fail_if(strcmp(elua_state_prog_name_get(st), "test"));

    fail_if(!elua_state_modules_setup(st));
    fail_if(!elua_state_i18n_setup(st));
    fail_if(!elua_state_io_setup(st));

    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);

    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, "lualian"));
    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);

    fd = mkstemp(buf);
    fail_if(fd < 0);
    f = fdopen(fd, "w");
    fail_if(!f);
    fprintf(f, "return 5\n");
    fclose(f);
    fail_if(elua_util_file_run(st, buf));
    fail_if(lua_tointeger(lst, -1) != 5);
    lua_pop(lst, 1);
    fail_if(remove(buf));

    /* halfassed testing here, but not possible otherwise */
    fail_if(elua_util_error_report(st, "foo", 0));
    lua_pushliteral(lst, "msg");
    fail_if(!elua_util_error_report(st, "foo", 5));
    fail_if(lua_gettop(lst) > 0);

    fail_if(elua_util_script_run(st, 2, cargv, 1, &quit));
    fail_if(quit != 1);

    f = fopen(buf, "w");
    fail_if(!f);
    fprintf(f, "return false");
    fclose(f);
    cargv[1] = buf;
    fail_if(elua_util_script_run(st, 2, cargv, 1, &quit));
    fail_if(quit != 0);
    fail_if(remove(buf));

    elua_state_free(st);

    elua_shutdown();
}
END_TEST

void elua_lib_test(TCase *tc)
{
   tcase_add_test(tc, elua_api);
}