Mercurial > irccd
diff 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 |
line wrap: on
line diff
--- a/tests/js-irccd/main.cpp Wed Mar 30 21:30:53 2016 +0200 +++ b/tests/js-irccd/main.cpp Thu Mar 31 12:16:00 2016 +0200 @@ -49,6 +49,78 @@ } } +TEST(SystemError, fromJavascript) +{ + duk::Context ctx; + + loadJsIrccd(ctx); + + try { + auto ret = duk::pevalString(ctx, + "try {" + " throw new Irccd.SystemError(1, 'test');" + "} catch (e) {" + " errno = e.errno;" + " name = e.name;" + " message = e.message;" + " v1 = (e instanceof Error);" + " v2 = (e instanceof Irccd.SystemError);" + "}" + ); + + if (ret != 0) + throw duk::error(ctx, -1); + + ASSERT_EQ(1, duk::getGlobal<int>(ctx, "errno")); + ASSERT_EQ("SystemError", duk::getGlobal<std::string>(ctx, "name")); + ASSERT_EQ("test", duk::getGlobal<std::string>(ctx, "message")); + ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v1")); + ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v2")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST(SystemError, fromNative) +{ + duk::Context ctx; + + loadJsIrccd(ctx); + + try { + duk::push(ctx, duk::Function{[] (duk::ContextPtr ctx) -> duk::Ret { + duk::raise(ctx, SystemError{EINVAL, "hey"}); + + return 0; + }}); + + duk::putGlobal(ctx, "f"); + + auto ret = duk::pevalString(ctx, + "try {" + " f();" + "} catch (e) {" + " errno = e.errno;" + " name = e.name;" + " message = e.message;" + " v1 = (e instanceof Error);" + " v2 = (e instanceof Irccd.SystemError);" + "}" + ); + + if (ret != 0) + throw duk::error(ctx, -1); + + ASSERT_EQ(EINVAL, duk::getGlobal<int>(ctx, "errno")); + ASSERT_EQ("SystemError", duk::getGlobal<std::string>(ctx, "name")); + ASSERT_EQ("hey", duk::getGlobal<std::string>(ctx, "message")); + ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v1")); + ASSERT_TRUE(duk::getGlobal<bool>(ctx, "v2")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv);