Mercurial > irccd
annotate tests/test-jsapi-chrono.c @ 1089:16e66403ea66
plugins: substitute IRCCD_VERSION in Javascript plugins
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 21 Jul 2021 15:33:17 +0200 |
parents | 8f8ce47aba8a |
children | f06e9761cc90 |
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-chrono.c -- test Irccd.Chrono 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 #include <unistd.h> |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
20 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
21 #define GREATEST_USE_ABBREVS 0 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
22 #include <greatest.h> |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
23 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
24 #include <irccd/js-plugin.h> |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
25 #include <irccd/plugin.h> |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
26 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
27 static struct irc_plugin *plugin; |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
28 static duk_context *ctx; |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
29 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
30 static void |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
31 setup(void *udata) |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
32 { |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
33 (void)udata; |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
34 |
1037
8f8ce47aba8a
make: switch to GNU make
David Demelier <markand@malikania.fr>
parents:
985
diff
changeset
|
35 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
|
36 ctx = js_plugin_get_context(plugin); |
963
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 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
39 static void |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
40 teardown(void *udata) |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
41 { |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
42 (void)udata; |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
43 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
44 irc_plugin_finish(plugin); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
45 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
46 plugin = NULL; |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
47 ctx = NULL; |
963
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 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
50 GREATEST_TEST |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
51 basics_simple(void) |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
52 { |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
53 if (duk_peval_string(ctx, "timer = new Irccd.Chrono();") != 0) |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
54 GREATEST_FAIL(); |
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 sleep(1); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
57 |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
58 if (duk_peval_string(ctx, "result = timer.elapsed;") != 0) |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
59 GREATEST_FAIL(); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
60 |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
61 duk_get_global_string(ctx, "result"); |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
62 |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
63 GREATEST_ASSERT_IN_RANGE(1000U, duk_get_uint(ctx, -1), 100); |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
64 GREATEST_PASS(); |
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 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
67 GREATEST_TEST |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
68 basics_reset(void) |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
69 { |
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 * Create a timer and wait for it to accumulate some time. Then use |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
72 * start to reset its value and wait for 1s. The elapsed time must not |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
73 * be greater than 1s. |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
74 */ |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
75 if (duk_peval_string(ctx, "timer = new Irccd.Chrono()") != 0) |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
76 GREATEST_FAIL(); |
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 sleep(1); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
79 |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
80 if (duk_peval_string(ctx, "timer.reset();") != 0) |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
81 GREATEST_FAIL(); |
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 sleep(1); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
84 |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
85 if (duk_peval_string(ctx, "result = timer.elapsed") != 0) |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
86 GREATEST_FAIL(); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
87 |
965
a518664b20a0
irccd: move javascript API from library to frontend
David Demelier <markand@malikania.fr>
parents:
963
diff
changeset
|
88 duk_get_global_string(ctx, "result"); |
963
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 GREATEST_ASSERT_IN_RANGE(1000U, duk_get_uint(ctx, -1), 100); |
963
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
91 GREATEST_PASS(); |
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 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
94 GREATEST_SUITE(suite_basics) |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
95 { |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
96 GREATEST_SET_SETUP_CB(setup, NULL); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
97 GREATEST_SET_TEARDOWN_CB(teardown, NULL); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
98 GREATEST_RUN_TEST(basics_simple); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
99 GREATEST_RUN_TEST(basics_reset); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
100 } |
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_MAIN_DEFS(); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
103 |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
104 int |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
105 main(int argc, char **argv) |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
106 { |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
107 GREATEST_MAIN_BEGIN(); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
108 GREATEST_RUN_SUITE(suite_basics); |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
109 GREATEST_MAIN_END(); |
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 return 0; |
371e1cc2c697
tests: add 80% of the Javascript API
David Demelier <markand@malikania.fr>
parents:
diff
changeset
|
112 } |