Mercurial > irccd
annotate tests/js-irccd/main.cpp @ 95:1125d90b3b44
Misc: switch to .hpp, #477
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 20 Apr 2016 19:45:00 +0200 |
parents | c67e734c9241 |
children | 1ed760f6e0c6 |
rev | line source |
---|---|
0 | 1 /* |
2 * main.cpp -- test Irccd API | |
3 * | |
4 * Copyright (c) 2013-2016 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 <gtest/gtest.h> | |
20 | |
95
1125d90b3b44
Misc: switch to .hpp, #477
David Demelier <markand@malikania.fr>
parents:
84
diff
changeset
|
21 #include <irccd/sysconfig.hpp> |
0 | 22 |
95
1125d90b3b44
Misc: switch to .hpp, #477
David Demelier <markand@malikania.fr>
parents:
84
diff
changeset
|
23 #include <irccd/js-irccd.hpp> |
1125d90b3b44
Misc: switch to .hpp, #477
David Demelier <markand@malikania.fr>
parents:
84
diff
changeset
|
24 #include <irccd/logger.hpp> |
0 | 25 |
26 using namespace irccd; | |
27 | |
28 TEST(TestJsIrccd, version) | |
29 { | |
75
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
30 duk::Context ctx; |
0 | 31 |
32 loadJsIrccd(ctx); | |
33 | |
34 try { | |
75
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
35 auto ret = duk::pevalString(ctx, |
0 | 36 "major = Irccd.version.major;" |
37 "minor = Irccd.version.minor;" | |
38 "patch = Irccd.version.patch;" | |
75
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
39 ); |
0 | 40 |
75
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
41 if (ret != 0) |
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
42 throw duk::error(ctx, -1); |
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
43 |
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
44 ASSERT_EQ(IRCCD_VERSION_MAJOR, duk::getGlobal<int>(ctx, "major")); |
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
45 ASSERT_EQ(IRCCD_VERSION_MINOR, duk::getGlobal<int>(ctx, "minor")); |
f8160d515a76
Irccd: rework a lot the JavaScript library
David Demelier <markand@malikania.fr>
parents:
0
diff
changeset
|
46 ASSERT_EQ(IRCCD_VERSION_PATCH, duk::getGlobal<int>(ctx, "patch")); |
0 | 47 } catch (const std::exception &ex) { |
48 FAIL() << ex.what(); | |
49 } | |
50 } | |
51 | |
80
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
52 TEST(SystemError, fromJavascript) |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
53 { |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
54 duk::Context ctx; |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
55 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
56 loadJsIrccd(ctx); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
57 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
58 try { |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
59 auto ret = duk::pevalString(ctx, |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
60 "try {" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
61 " throw new Irccd.SystemError(1, 'test');" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
62 "} catch (e) {" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
63 " errno = e.errno;" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
64 " name = e.name;" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
65 " message = e.message;" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
66 " v1 = (e instanceof Error);" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
67 " v2 = (e instanceof Irccd.SystemError);" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
68 "}" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
69 ); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
70 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
71 if (ret != 0) |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
72 throw duk::error(ctx, -1); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
73 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
74 ASSERT_EQ(1, duk::getGlobal<int>(ctx, "errno")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
75 ASSERT_EQ("SystemError", duk::getGlobal<std::string>(ctx, "name")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
76 ASSERT_EQ("test", duk::getGlobal<std::string>(ctx, "message")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
77 ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v1")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
78 ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v2")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
79 } catch (const std::exception &ex) { |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
80 FAIL() << ex.what(); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
81 } |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
82 } |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
83 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
84 TEST(SystemError, fromNative) |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
85 { |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
86 duk::Context ctx; |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
87 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
88 loadJsIrccd(ctx); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
89 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
90 try { |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
91 duk::push(ctx, duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
92 duk::raise(ctx, SystemError{EINVAL, "hey"}); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
93 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
94 return 0; |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
95 }}); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
96 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
97 duk::putGlobal(ctx, "f"); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
98 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
99 auto ret = duk::pevalString(ctx, |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
100 "try {" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
101 " f();" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
102 "} catch (e) {" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
103 " errno = e.errno;" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
104 " name = e.name;" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
105 " message = e.message;" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
106 " v1 = (e instanceof Error);" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
107 " v2 = (e instanceof Irccd.SystemError);" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
108 "}" |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
109 ); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
110 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
111 if (ret != 0) |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
112 throw duk::error(ctx, -1); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
113 |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
114 ASSERT_EQ(EINVAL, duk::getGlobal<int>(ctx, "errno")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
115 ASSERT_EQ("SystemError", duk::getGlobal<std::string>(ctx, "name")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
116 ASSERT_EQ("hey", duk::getGlobal<std::string>(ctx, "message")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
117 ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v1")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
118 ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v2")); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
119 } catch (const std::exception &ex) { |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
120 FAIL() << ex.what(); |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
121 } |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
122 } |
98bd4d6355fe
Irccd: fix Irccd.SystemError not thrown
David Demelier <markand@malikania.fr>
parents:
75
diff
changeset
|
123 |
0 | 124 int main(int argc, char **argv) |
125 { | |
126 testing::InitGoogleTest(&argc, argv); | |
127 | |
128 return RUN_ALL_TESTS(); | |
129 } |