comparison tests/js-irccd/main.cpp @ 80:98bd4d6355fe

Irccd: fix Irccd.SystemError not thrown
author David Demelier <markand@malikania.fr>
date Thu, 31 Mar 2016 12:16:00 +0200
parents f8160d515a76
children c67e734c9241
comparison
equal deleted inserted replaced
79:d0deb5835aed 80:98bd4d6355fe
47 } catch (const std::exception &ex) { 47 } catch (const std::exception &ex) {
48 FAIL() << ex.what(); 48 FAIL() << ex.what();
49 } 49 }
50 } 50 }
51 51
52 TEST(SystemError, fromJavascript)
53 {
54 duk::Context ctx;
55
56 loadJsIrccd(ctx);
57
58 try {
59 auto ret = duk::pevalString(ctx,
60 "try {"
61 " throw new Irccd.SystemError(1, 'test');"
62 "} catch (e) {"
63 " errno = e.errno;"
64 " name = e.name;"
65 " message = e.message;"
66 " v1 = (e instanceof Error);"
67 " v2 = (e instanceof Irccd.SystemError);"
68 "}"
69 );
70
71 if (ret != 0)
72 throw duk::error(ctx, -1);
73
74 ASSERT_EQ(1, duk::getGlobal<int>(ctx, "errno"));
75 ASSERT_EQ("SystemError", duk::getGlobal<std::string>(ctx, "name"));
76 ASSERT_EQ("test", duk::getGlobal<std::string>(ctx, "message"));
77 ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v1"));
78 ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v2"));
79 } catch (const std::exception &ex) {
80 FAIL() << ex.what();
81 }
82 }
83
84 TEST(SystemError, fromNative)
85 {
86 duk::Context ctx;
87
88 loadJsIrccd(ctx);
89
90 try {
91 duk::push(ctx, duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret {
92 duk::raise(ctx, SystemError{EINVAL, "hey"});
93
94 return 0;
95 }});
96
97 duk::putGlobal(ctx, "f");
98
99 auto ret = duk::pevalString(ctx,
100 "try {"
101 " f();"
102 "} catch (e) {"
103 " errno = e.errno;"
104 " name = e.name;"
105 " message = e.message;"
106 " v1 = (e instanceof Error);"
107 " v2 = (e instanceof Irccd.SystemError);"
108 "}"
109 );
110
111 if (ret != 0)
112 throw duk::error(ctx, -1);
113
114 ASSERT_EQ(EINVAL, duk::getGlobal<int>(ctx, "errno"));
115 ASSERT_EQ("SystemError", duk::getGlobal<std::string>(ctx, "name"));
116 ASSERT_EQ("hey", duk::getGlobal<std::string>(ctx, "message"));
117 ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v1"));
118 ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v2"));
119 } catch (const std::exception &ex) {
120 FAIL() << ex.what();
121 }
122 }
123
52 int main(int argc, char **argv) 124 int main(int argc, char **argv)
53 { 125 {
54 testing::InitGoogleTest(&argc, argv); 126 testing::InitGoogleTest(&argc, argv);
55 127
56 return RUN_ALL_TESTS(); 128 return RUN_ALL_TESTS();