comparison tests/test-jsapi-system.c @ 963:371e1cc2c697

tests: add 80% of the Javascript API
author David Demelier <markand@malikania.fr>
date Thu, 28 Jan 2021 14:20:58 +0100
parents
children a518664b20a0
comparison
equal deleted inserted replaced
962:63208f5bb0f6 963:371e1cc2c697
1 /*
2 * test-jsapi-system.c -- test Irccd.System API
3 *
4 * Copyright (c) 2013-2021 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #define GREATEST_USE_ABBREVS 0
20 #include <greatest.h>
21
22 // TODO: irccd/
23 #include <config.h>
24
25 #include <irccd/js-plugin.h>
26 #include <irccd/plugin.h>
27
28 static struct irc_plugin *plugin;
29 static struct irc_js_plugin_data *data;
30
31 static void
32 setup(void *udata)
33 {
34 (void)udata;
35
36 plugin = irc_js_plugin_open(SOURCE "/data/example-plugin.js");
37 data = plugin->data;
38 }
39
40 static void
41 teardown(void *udata)
42 {
43 (void)udata;
44
45 irc_plugin_finish(plugin);
46
47 plugin = NULL;
48 data = NULL;
49 }
50
51 GREATEST_TEST
52 basics_popen(void)
53 {
54 int ret = duk_peval_string(data->ctx,
55 "f = Irccd.System.popen(\"" IRCCD_EXECUTABLE " version\", \"r\");"
56 "r = f.readline();"
57 );
58
59 if (ret != 0)
60 GREATEST_FAIL();
61
62 GREATEST_ASSERT(duk_get_global_string(data->ctx, "r"));
63 GREATEST_ASSERT_STR_EQ(IRCCD_VERSION, duk_get_string(data->ctx, -1));
64
65 GREATEST_PASS();
66 }
67
68 GREATEST_TEST
69 basics_sleep(void)
70 {
71 time_t start, now;
72
73 start = time(NULL);
74
75 if (duk_peval_string(data->ctx, "Irccd.System.sleep(2)") != 0)
76 GREATEST_FAIL();
77
78 now = time(NULL);
79
80 GREATEST_ASSERT_IN_RANGE(2000LL, difftime(now, start) * 1000LL, 100LL);
81
82 GREATEST_PASS();
83 }
84
85 GREATEST_TEST
86 basics_usleep(void)
87 {
88 time_t start, now;
89
90 start = time(NULL);
91
92 if (duk_peval_string(data->ctx, "Irccd.System.usleep(2000000)") != 0)
93 GREATEST_FAIL();
94
95 now = time(NULL);
96
97 GREATEST_ASSERT_IN_RANGE(2000LL, difftime(now, start) * 1000LL, 100LL);
98
99 GREATEST_PASS();
100 }
101
102 GREATEST_SUITE(suite_basics)
103 {
104 GREATEST_SET_SETUP_CB(setup, NULL);
105 GREATEST_SET_TEARDOWN_CB(teardown, NULL);
106 GREATEST_RUN_TEST(basics_popen);
107 GREATEST_RUN_TEST(basics_sleep);
108 GREATEST_RUN_TEST(basics_usleep);
109 }
110
111 GREATEST_MAIN_DEFS();
112
113 int
114 main(int argc, char **argv)
115 {
116 GREATEST_MAIN_BEGIN();
117 GREATEST_RUN_SUITE(suite_basics);
118 GREATEST_MAIN_END();
119
120 return 0;
121 }