changeset 167:9f36f71a08c5

Irccd: unbreak test-js-elapsedtimer
author David Demelier <markand@malikania.fr>
date Wed, 25 May 2016 22:20:21 +0200
parents 2380d476f987
children 66dc46db79f7
files lib/irccd/service-module.cpp lib/irccd/service-module.hpp tests/js-elapsedtimer/empty.js tests/js-elapsedtimer/main.cpp
diffstat 3 files changed, 40 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/lib/irccd/service-module.cpp	Wed May 25 22:05:00 2016 +0200
+++ b/lib/irccd/service-module.cpp	Wed May 25 22:20:21 2016 +0200
@@ -62,6 +62,16 @@
 	m_modules.push_back(std::make_shared<UtilModule>());
 }
 
+std::shared_ptr<Module> ModuleService::get(const std::string &name) const noexcept
+{
+	auto it = find(m_modules, name);
+
+	if (it == m_modules.end())
+		return nullptr;
+
+	return *it;
+}
+
 bool ModuleService::contains(const std::string &name) const
 {
 	return find(m_modules, name) != m_modules.end();
--- a/lib/irccd/service-module.hpp	Wed May 25 22:05:00 2016 +0200
+++ b/lib/irccd/service-module.hpp	Wed May 25 22:20:21 2016 +0200
@@ -27,6 +27,8 @@
 #include <memory>
 #include <vector>
 
+#include "sysconfig.hpp"
+
 namespace irccd {
 
 class Module;
@@ -55,6 +57,14 @@
 	}
 
 	/**
+	 * Get a module.
+	 *
+	 * \param name the module name
+	 * \return the module or empty if not found
+	 */
+	IRCCD_EXPORT std::shared_ptr<Module> get(const std::string &name) const noexcept;
+
+	/**
 	 * Tells if a module exist.
 	 *
 	 * \param name the name
--- a/tests/js-elapsedtimer/main.cpp	Wed May 25 22:05:00 2016 +0200
+++ b/tests/js-elapsedtimer/main.cpp	Wed May 25 22:20:21 2016 +0200
@@ -20,38 +20,41 @@
 
 #include <thread>
 
-#include <irccd/js-irccd.hpp>
+#include <irccd/irccd.hpp>
+#include <irccd/mod-irccd.hpp>
 #include <irccd/mod-elapsed-timer.hpp>
+#include <irccd/plugin-js.hpp>
+#include <irccd/service-module.hpp>
 
 using namespace irccd;
 using namespace std::chrono_literals;
 
 class TestElapsedTimer : public testing::Test {
 protected:
-	duk::Context m_context;
+	Irccd m_irccd;
+	std::shared_ptr<JsPlugin> m_plugin;
 
 	TestElapsedTimer()
+		: m_plugin(std::make_shared<JsPlugin>("empty", SOURCEDIR "/empty.js"))
 	{
-		loadJsIrccd(m_context);
-		loadJsElapsedTimer(m_context);
+		m_irccd.moduleService().get("Irccd")->load(m_irccd, *m_plugin);
+		m_irccd.moduleService().get("Irccd.ElapsedTimer")->load(m_irccd, *m_plugin);
 	}
 };
 
 TEST_F(TestElapsedTimer, standard)
 {
 	try {
-		if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) {
-			throw duk::error(m_context, -1);
-		}
+		if (duk::pevalString(m_plugin->context(), "timer = new Irccd.ElapsedTimer();") != 0)
+			throw duk::error(m_plugin->context(), -1);
 
 		std::this_thread::sleep_for(300ms);
 
-		if (duk::pevalString(m_context, "result = timer.elapsed();") != 0) {
-			throw duk::error(m_context, -1);
-		}
+		if (duk::pevalString(m_plugin->context(), "result = timer.elapsed();") != 0)
+			throw duk::error(m_plugin->context(), -1);
 
-		ASSERT_GE(duk::getGlobal<int>(m_context, "result"), 250);
-		ASSERT_LE(duk::getGlobal<int>(m_context, "result"), 350);
+		ASSERT_GE(duk::getGlobal<int>(m_plugin->context(), "result"), 250);
+		ASSERT_LE(duk::getGlobal<int>(m_plugin->context(), "result"), 350);
 	} catch (const std::exception &ex) {
 		FAIL() << ex.what();
 	}
@@ -60,17 +63,15 @@
 TEST_F(TestElapsedTimer, reset)
 {
 	try {
-		if (duk::pevalString(m_context, "timer = new Irccd.ElapsedTimer();") != 0) {
-			throw duk::error(m_context, -1);
-		}
+		if (duk::pevalString(m_plugin->context(), "timer = new Irccd.ElapsedTimer();") != 0)
+			throw duk::error(m_plugin->context(), -1);
 
 		std::this_thread::sleep_for(300ms);
 
-		if (duk::pevalString(m_context, "timer.reset(); result = timer.elapsed();") != 0) {
-			throw duk::error(m_context, -1);
-		}
+		if (duk::pevalString(m_plugin->context(), "timer.reset(); result = timer.elapsed();") != 0)
+			throw duk::error(m_plugin->context(), -1);
 
-		ASSERT_LE(duk::getGlobal<int>(m_context, "result"), 100);
+		ASSERT_LE(duk::getGlobal<int>(m_plugin->context(), "result"), 100);
 	} catch (const std::exception &ex) {
 		FAIL() << ex.what();
 	}