comparison tests/test-jsapi-irccd.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-irccd.c -- test Irccd 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 #include <errno.h>
20
21 #define GREATEST_USE_ABBREVS 0
22 #include <greatest.h>
23
24 #include <config.h>
25
26 #include <irccd/js-plugin.h>
27 #include <irccd/jsapi-system.h>
28 #include <irccd/plugin.h>
29
30 static struct irc_plugin *plugin;
31 static struct irc_js_plugin_data *data;
32
33 static void
34 setup(void *udata)
35 {
36 (void)udata;
37
38 plugin = irc_js_plugin_open(SOURCE "/data/example-plugin.js");
39 data = plugin->data;
40 }
41
42 static void
43 teardown(void *udata)
44 {
45 (void)udata;
46
47 irc_plugin_finish(plugin);
48
49 plugin = NULL;
50 data = NULL;
51 }
52
53 static int
54 throw(duk_context *ctx)
55 {
56 errno = EINVAL;
57 irc_jsapi_system_raise(ctx);
58
59 return 0;
60 }
61
62 GREATEST_TEST
63 basics_version(void)
64 {
65 const int ret = duk_peval_string(data->ctx,
66 "major = Irccd.version.major;"
67 "minor = Irccd.version.minor;"
68 "patch = Irccd.version.patch;"
69 );
70
71 if (ret != 0)
72 GREATEST_FAIL();
73
74 GREATEST_ASSERT(duk_get_global_string(data->ctx, "major"));
75 GREATEST_ASSERT_EQ(IRCCD_VERSION_MAJOR, duk_get_int(data->ctx, -1));
76 GREATEST_ASSERT(duk_get_global_string(data->ctx, "minor"));
77 GREATEST_ASSERT_EQ(IRCCD_VERSION_MINOR, duk_get_int(data->ctx, -1));
78 GREATEST_ASSERT(duk_get_global_string(data->ctx, "patch"));
79 GREATEST_ASSERT_EQ(IRCCD_VERSION_PATCH, duk_get_int(data->ctx, -1));
80
81 GREATEST_PASS();
82 }
83
84 GREATEST_TEST
85 basics_system_error_from_js(void)
86 {
87 const int ret = duk_peval_string(data->ctx,
88 "try {"
89 " throw new Irccd.SystemError(1, 'test');"
90 "} catch (e) {"
91 " errno = e.errno;"
92 " name = e.name;"
93 " message = e.message;"
94 " v1 = (e instanceof Error);"
95 " v2 = (e instanceof Irccd.SystemError);"
96 "}"
97 );
98
99 if (ret != 0)
100 GREATEST_FAIL();
101
102 GREATEST_ASSERT(duk_get_global_string(data->ctx, "errno"));
103 GREATEST_ASSERT_EQ(1, duk_get_int(data->ctx, -1));
104 GREATEST_ASSERT(duk_get_global_string(data->ctx, "name"));
105 GREATEST_ASSERT_STR_EQ("SystemError", duk_get_string(data->ctx, -1));
106 GREATEST_ASSERT(duk_get_global_string(data->ctx, "message"));
107 GREATEST_ASSERT_STR_EQ("test", duk_get_string(data->ctx, -1));
108 GREATEST_ASSERT(duk_get_global_string(data->ctx, "v1"));
109 GREATEST_ASSERT(duk_get_boolean(data->ctx, -1));
110 GREATEST_ASSERT(duk_get_global_string(data->ctx, "v2"));
111 GREATEST_ASSERT(duk_get_boolean(data->ctx, -1));
112
113 GREATEST_PASS();
114 }
115
116 GREATEST_TEST
117 basics_system_error_from_c(void)
118 {
119 duk_push_c_function(data->ctx, throw, 0);
120 duk_put_global_string(data->ctx, "f");
121
122 const int ret = duk_peval_string(data->ctx,
123 "try {"
124 " f();"
125 "} catch (e) {"
126 " errno = e.errno;"
127 " name = e.name;"
128 " v1 = (e instanceof Error);"
129 " v2 = (e instanceof Irccd.SystemError);"
130 "}"
131 );
132
133 if (ret != 0)
134 GREATEST_FAIL();
135
136 GREATEST_ASSERT(duk_get_global_string(data->ctx, "errno"));
137 GREATEST_ASSERT_EQ(EINVAL, duk_get_int(data->ctx, -1));
138 GREATEST_ASSERT(duk_get_global_string(data->ctx, "name"));
139 GREATEST_ASSERT_STR_EQ("SystemError", duk_get_string(data->ctx, -1));
140 GREATEST_ASSERT(duk_get_global_string(data->ctx, "v1"));
141 GREATEST_ASSERT(duk_get_boolean(data->ctx, -1));
142 GREATEST_ASSERT(duk_get_global_string(data->ctx, "v2"));
143 GREATEST_ASSERT(duk_get_boolean(data->ctx, -1));
144
145 GREATEST_PASS();
146 }
147
148 GREATEST_SUITE(suite_basics)
149 {
150 GREATEST_SET_SETUP_CB(setup, NULL);
151 GREATEST_SET_TEARDOWN_CB(teardown, NULL);
152 GREATEST_RUN_TEST(basics_version);
153 GREATEST_RUN_TEST(basics_system_error_from_js);
154 GREATEST_RUN_TEST(basics_system_error_from_c);
155 }
156
157 GREATEST_MAIN_DEFS();
158
159 int
160 main(int argc, char **argv)
161 {
162 GREATEST_MAIN_BEGIN();
163 GREATEST_RUN_SUITE(suite_basics);
164 GREATEST_MAIN_END();
165
166 return 0;
167 }