# HG changeset patch # User David Demelier # Date 1464207959 -7200 # Node ID 66dc46db79f773b3ba4949e47c1ab03624473c89 # Parent 9f36f71a08c5362cf1794ffc5ecc044350b60614 Irccd: unbreak test-js-file diff -r 9f36f71a08c5 -r 66dc46db79f7 tests/js-file/empty.js diff -r 9f36f71a08c5 -r 66dc46db79f7 tests/js-file/main.cpp --- a/tests/js-file/main.cpp Wed May 25 22:20:21 2016 +0200 +++ b/tests/js-file/main.cpp Wed May 25 22:25:59 2016 +0200 @@ -20,92 +20,81 @@ #include -#include -#include +#include +#include +#include +#include +#include using namespace irccd; -TEST(TestJsFile, functionBasename) -{ - duk::Context ctx; - - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - if (duk::pevalString(ctx, "result = Irccd.File.basename('/usr/local/etc/irccd.conf');") != 0) { - throw duk::error(ctx, -1); - } +class TestJsFile : public testing::Test { +protected: + Irccd m_irccd; + std::shared_ptr m_plugin; - ASSERT_EQ("irccd.conf", duk::getGlobal(ctx, "result")); - } catch (const std::exception &ex) { - FAIL() << ex.what(); + TestJsFile() + : m_plugin(std::make_shared("empty", SOURCEDIR "/empty.js")) + { + m_irccd.moduleService().get("Irccd")->load(m_irccd, *m_plugin); + m_irccd.moduleService().get("Irccd.File")->load(m_irccd, *m_plugin); } -} +}; -TEST(TestJsFile, functionDirname) +TEST_F(TestJsFile, functionBasename) { - duk::Context ctx; - - loadJsIrccd(ctx); - loadJsFile(ctx); + try { + if (duk::pevalString(m_plugin->context(), "result = Irccd.File.basename('/usr/local/etc/irccd.conf');") != 0) + throw duk::error(m_plugin->context(), -1); - try { - duk::pevalString(ctx, "result = Irccd.File.dirname('/usr/local/etc/irccd.conf');"); - - ASSERT_EQ("/usr/local/etc", duk::getGlobal(ctx,"result")); + ASSERT_EQ("irccd.conf", duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, functionExists) +TEST_F(TestJsFile, functionDirname) { - duk::Context ctx; + try { + duk::pevalString(m_plugin->context(), "result = Irccd.File.dirname('/usr/local/etc/irccd.conf');"); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - duk::pevalString(ctx, "result = Irccd.File.exists(directory + '/file.txt')"); - - ASSERT_TRUE(duk::getGlobal(ctx,"result")); + ASSERT_EQ("/usr/local/etc", duk::getGlobal(m_plugin->context(),"result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, functionExists2) +TEST_F(TestJsFile, functionExists) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); + duk::pevalString(m_plugin->context(), "result = Irccd.File.exists(directory + '/file.txt')"); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::pevalString(ctx, "result = Irccd.File.exists('file_which_does_not_exist.txt')"); - - ASSERT_FALSE(duk::getGlobal(ctx,"result")); + ASSERT_TRUE(duk::getGlobal(m_plugin->context(),"result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, functionRemove) +TEST_F(TestJsFile, functionExists2) +{ + try { + duk::pevalString(m_plugin->context(), "result = Irccd.File.exists('file_which_does_not_exist.txt')"); + + ASSERT_FALSE(duk::getGlobal(m_plugin->context(),"result")); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestJsFile, functionRemove) { // First create a dummy file std::ofstream("test-js-fs.remove"); - duk::Context ctx; - - loadJsIrccd(ctx); - loadJsFile(ctx); - try { - if (duk::pevalString(ctx, "Irccd.File.remove('test-js-fs.remove');") != 0) { - throw duk::error(ctx, -1); - } + if (duk::pevalString(m_plugin->context(), "Irccd.File.remove('test-js-fs.remove');") != 0) + throw duk::error(m_plugin->context(), -1); } catch (const std::exception &ex) { FAIL() << ex.what(); } @@ -115,171 +104,130 @@ ASSERT_FALSE(in.is_open()); } -TEST(TestJsFile, methodBasename) +TEST_F(TestJsFile, methodBasename) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(),"directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx,"directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/level-1/file-1.txt', 'r');" "result = f.basename();" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_EQ("file-1.txt", duk::getGlobal(ctx,"result")); + ASSERT_EQ("file-1.txt", duk::getGlobal(m_plugin->context(),"result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodBasenameClosed) +TEST_F(TestJsFile, methodBasenameClosed) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(),"directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx,"directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/level-1/file-1.txt', 'r');" "f.close();" "result = f.basename();" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_EQ("file-1.txt", duk::getGlobal(ctx,"result")); + ASSERT_EQ("file-1.txt", duk::getGlobal(m_plugin->context(),"result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodDirname) +TEST_F(TestJsFile, methodDirname) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(),"directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx,"directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/level-1/file-1.txt', 'r');" "result = f.dirname();" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_EQ(std::string{IRCCD_TESTS_DIRECTORY "/level-1"}, duk::getGlobal(ctx, "result")); + ASSERT_EQ(std::string{IRCCD_TESTS_DIRECTORY "/level-1"}, duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodDirnameClosed) +TEST_F(TestJsFile, methodDirnameClosed) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/level-1/file-1.txt', 'r');" "f.close();" "result = f.dirname();" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_EQ(std::string{IRCCD_TESTS_DIRECTORY "/level-1"}, duk::getGlobal(ctx, "result")); + ASSERT_EQ(std::string{IRCCD_TESTS_DIRECTORY "/level-1"}, duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodLines) +TEST_F(TestJsFile, methodLines) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "lines = new Irccd.File(directory + '/lines.txt', 'r').lines();" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); std::vector expected{"a", "b", "c"}; - ASSERT_EQ(expected, duk::getGlobal>(ctx, "lines")); + ASSERT_EQ(expected, duk::getGlobal>(m_plugin->context(), "lines")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodSeek1) +TEST_F(TestJsFile, methodSeek1) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/file.txt', 'r');" "f.seek(Irccd.File.SeekSet, 4);" "result = f.read(1);" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_EQ(".", duk::getGlobal(ctx, "result")); + ASSERT_EQ(".", duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodSeek1Closed) +TEST_F(TestJsFile, methodSeek1Closed) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/file.txt', 'r');" "f.close();" "f.seek(Irccd.File.SeekSet, 4);" @@ -287,55 +235,42 @@ "result = typeof (result) === \"undefined\";" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_TRUE(duk::getGlobal(ctx, "result")); + ASSERT_TRUE(duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodSeek2) +TEST_F(TestJsFile, methodSeek2) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/file.txt', 'r');" "f.seek(Irccd.File.SeekSet, 2);" "f.seek(Irccd.File.SeekCur, 2);" "result = f.read(1);" ); + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - if (ret != 0) { - throw duk::error(ctx, -1); - } - - ASSERT_EQ(".", duk::getGlobal(ctx, "result")); + ASSERT_EQ(".", duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodSeek2Closed) +TEST_F(TestJsFile, methodSeek2Closed) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/file.txt', 'r');" "f.close();" "f.seek(Irccd.File.SeekSet, 2);" @@ -344,53 +279,41 @@ "result = typeof (result) === \"undefined\";" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_TRUE(duk::getGlobal(ctx, "result")); + ASSERT_TRUE(duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodSeek3) +TEST_F(TestJsFile, methodSeek3) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/file.txt', 'r');" "f.seek(Irccd.File.SeekEnd, -2);" "result = f.read(1);" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_EQ("x", duk::getGlobal(ctx, "result")); + ASSERT_EQ("x", duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodSeek3Closed) +TEST_F(TestJsFile, methodSeek3Closed) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "f = new Irccd.File(directory + '/file.txt', 'r');" "f.close();" "f.seek(Irccd.File.SeekEnd, -2);" @@ -398,27 +321,21 @@ "result = typeof (result) === \"undefined\";" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); - ASSERT_TRUE(duk::getGlobal(ctx, "result")); + ASSERT_TRUE(duk::getGlobal(m_plugin->context(), "result")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodReadline) +TEST_F(TestJsFile, methodReadline) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "lines = [];" "f = new Irccd.File(directory + '/lines.txt', 'r');" "for (var s; s = f.readline(); ) {" @@ -426,29 +343,23 @@ "}" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); std::vector expected{"a", "b", "c"}; - ASSERT_EQ(expected, duk::getGlobal>(ctx, "lines")); + ASSERT_EQ(expected, duk::getGlobal>(m_plugin->context(), "lines")); } catch (const std::exception &ex) { FAIL() << ex.what(); } } -TEST(TestJsFile, methodReadlineClosed) +TEST_F(TestJsFile, methodReadlineClosed) { - duk::Context ctx; + try { + duk::putGlobal(m_plugin->context(), "directory", IRCCD_TESTS_DIRECTORY); - loadJsIrccd(ctx); - loadJsFile(ctx); - - try { - duk::putGlobal(ctx, "directory", IRCCD_TESTS_DIRECTORY); - - auto ret = duk::pevalString(ctx, + auto ret = duk::pevalString(m_plugin->context(), "lines = [];" "f = new Irccd.File(directory + '/lines.txt', 'r');" "f.close();" @@ -457,13 +368,12 @@ "}" ); - if (ret != 0) { - throw duk::error(ctx, -1); - } + if (ret != 0) + throw duk::error(m_plugin->context(), -1); std::vector expected; - ASSERT_EQ(expected, duk::getGlobal>(ctx, "lines")); + ASSERT_EQ(expected, duk::getGlobal>(m_plugin->context(), "lines")); } catch (const std::exception &ex) { FAIL() << ex.what(); }