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