changeset 1003:bbb3d3075ec2

tests: fix all
author David Demelier <markand@malikania.fr>
date Sun, 14 Feb 2021 10:11:03 +0100
parents ec5461750efd
children 3ea3361f0fc7
files extern/libcompat/CMakeLists.txt extern/libduktape/CMakeLists.txt irccd/dl-plugin.c tests/CMakeLists.txt tests/data/example-dl-plugin.c tests/test-dl-plugin.c tests/test-event.c tests/test-jsapi-irccd.c
diffstat 8 files changed, 79 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/extern/libcompat/CMakeLists.txt	Sat Feb 13 22:15:16 2021 +0100
+++ b/extern/libcompat/CMakeLists.txt	Sun Feb 14 10:11:03 2021 +0100
@@ -96,7 +96,7 @@
 		$<BUILD_INTERFACE:${libirccd-compat_BINARY_DIR}>
 		$<BUILD_INTERFACE:${libirccd-compat_BINARY_DIR}/irccd>
 )
-set_target_properties(libirccd-compat PROPERTIES PREFIX "")
+set_target_properties(libirccd-compat PROPERTIES PREFIX "" FOLDER extern)
 
 # Remove useless C4996 warning about "deprecated" functions from MSVC.
 if (CMAKE_C_COMPILER_ID MATCHES MSVC)
--- a/extern/libduktape/CMakeLists.txt	Sat Feb 13 22:15:16 2021 +0100
+++ b/extern/libduktape/CMakeLists.txt	Sun Feb 14 10:11:03 2021 +0100
@@ -30,4 +30,4 @@
 	target_compile_definitions(libirccd-duktape PUBLIC DUK_F_DLL_BUILD)
 endif ()
 
-set_target_properties(libirccd-duktape PROPERTIES PREFIX "")
+set_target_properties(libirccd-duktape PROPERTIES PREFIX "" FOLDER extern)
--- a/irccd/dl-plugin.c	Sat Feb 13 22:15:16 2021 +0100
+++ b/irccd/dl-plugin.c	Sun Feb 14 10:11:03 2021 +0100
@@ -182,7 +182,6 @@
 		dlclose(self->handle);
 
 	free(self);
-	memset(self, 0, sizeof (*self));
 }
 
 static struct self *
--- a/tests/CMakeLists.txt	Sat Feb 13 22:15:16 2021 +0100
+++ b/tests/CMakeLists.txt	Sun Feb 14 10:11:03 2021 +0100
@@ -22,8 +22,8 @@
 	TESTS
 	test-bot
 	test-channel
-	#test-event
-	#test-dl-plugin
+	test-event
+	test-dl-plugin
 	test-log
 	test-rule
 	test-subst
@@ -52,6 +52,19 @@
 	)
 endif ()
 
+add_library(example-dl-plugin MODULE ${tests_SOURCE_DIR}/data/example-dl-plugin.c)
+target_link_libraries(example-dl-plugin libirccd)
+
+if (APPLE)
+	set_target_properties(example-dl-plugin PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
+endif ()
+
+set_target_properties(example-dl-plugin
+	PROPERTIES
+		FOLDER tests/test-dl-plugin
+		PREFIX ""
+)
+
 foreach (t ${TESTS})
 	add_executable(${t} ${t}.c)
 	add_test(${t} ${t})
@@ -60,11 +73,12 @@
 	target_compile_definitions(
 		${t}
 		PRIVATE
+			EXAMPLE_DL_PLUGIN="$<TARGET_FILE:example-dl-plugin>"
 			IRCCD_EXECUTABLE="$<TARGET_FILE:irccd>"
 			CMAKE_SOURCE_DIR="${CMAKE_SOURCE_DIR}"
 			# TODO: change those names.
 			BINARY="${tests_BINARY_DIR}"
 			SOURCE="${tests_SOURCE_DIR}"
 	)
-	add_dependencies(${t} irccd)
+	add_dependencies(${t} irccd example-dl-plugin)
 endforeach ()
--- a/tests/data/example-dl-plugin.c	Sat Feb 13 22:15:16 2021 +0100
+++ b/tests/data/example-dl-plugin.c	Sun Feb 14 10:11:03 2021 +0100
@@ -18,8 +18,8 @@
 
 #include <string.h>
 
-#include <irccd/dl-plugin.h>
 #include <irccd/event.h>
+#include <irccd/server.h>
 #include <irccd/util.h>
 
 struct kw {
@@ -84,77 +84,77 @@
 	return NULL;
 }
 
-IRC_DL_EXPORT void
+void
 example_dl_plugin_set_option(const char *key, const char *value)
 {
 	set(options, IRC_UTIL_SIZE(options), key, value);
 }
 
-IRC_DL_EXPORT const char *
+const char *
 example_dl_plugin_get_option(const char *key)
 {
 	return get(options, IRC_UTIL_SIZE(options), key);
 }
 
-IRC_DL_EXPORT const char **
+const char **
 example_dl_plugin_get_options(void)
 {
 	return options_list;
 }
 
-IRC_DL_EXPORT void
+void
 example_dl_plugin_set_template(const char *key, const char *value)
 {
 	set(templates, IRC_UTIL_SIZE(templates), key, value);
 }
 
-IRC_DL_EXPORT const char *
+const char *
 example_dl_plugin_get_template(const char *key)
 {
 	return get(templates, IRC_UTIL_SIZE(templates), key);
 }
 
-IRC_DL_EXPORT const char **
+const char **
 example_dl_plugin_get_templates(void)
 {
 	return templates_list;
 }
 
-IRC_DL_EXPORT void
+void
 example_dl_plugin_set_path(const char *key, const char *value)
 {
 	set(paths, IRC_UTIL_SIZE(paths), key, value);
 }
 
-IRC_DL_EXPORT const char *
+const char *
 example_dl_plugin_get_path(const char *key)
 {
 	return get(paths, IRC_UTIL_SIZE(paths), key);
 }
 
-IRC_DL_EXPORT const char **
+const char **
 example_dl_plugin_get_paths(void)
 {
 	return paths_list;
 }
 
-IRC_DL_EXPORT void
+void
 example_dl_plugin_event(const struct irc_event *ev)
 {
-	(void)ev;
+	irc_server_send(ev->server, "EVENT");
 }
 
-IRC_DL_EXPORT void
+void
 example_dl_plugin_load(void)
 {
 }
 
-IRC_DL_EXPORT void
+void
 example_dl_plugin_reload(void)
 {
 }
 
-IRC_DL_EXPORT void
+void
 example_dl_plugin_unload(void)
 {
 }
--- a/tests/test-dl-plugin.c	Sat Feb 13 22:15:16 2021 +0100
+++ b/tests/test-dl-plugin.c	Sun Feb 14 10:11:03 2021 +0100
@@ -16,14 +16,18 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <err.h>
+
 #define GREATEST_USE_ABBREVS 0
 #include <greatest.h>
 
+#include <irccd/compat.h>
 #include <irccd/dl-plugin.h>
 #include <irccd/event.h>
 #include <irccd/plugin.h>
+#include <irccd/server.h>
 
-static struct irc_plugin plugin;
+static struct irc_plugin *plugin;
 
 static void
 setup(void *udata)
@@ -31,8 +35,8 @@
 	(void)udata;
 
 	/* TODO: No idea how to stop greatest from here. */
-	if (!irc_dl_plugin_open(&plugin, SOURCEDIR "/tests/example-dl-plugin" IRC_DL_EXT))
-		exit(1);
+	if ((plugin = dl_plugin_open("example", EXAMPLE_DL_PLUGIN)) == NULL)
+		err(1, "dlopen");
 }
 
 static void
@@ -40,22 +44,22 @@
 {
 	(void)udata;
 
-	irc_plugin_finish(&plugin);
+	irc_plugin_finish(plugin);
 }
 
 GREATEST_TEST
 options_set_get(void)
 {
-	irc_plugin_set_option(&plugin, "option-1", "new-value-1");
-	GREATEST_ASSERT_STR_EQ("new-value-1", irc_plugin_get_option(&plugin, "option-1"));
-	GREATEST_ASSERT(!irc_plugin_get_option(&plugin, "not-found"));
+	irc_plugin_set_option(plugin, "option-1", "new-value-1");
+	GREATEST_ASSERT_STR_EQ("new-value-1", irc_plugin_get_option(plugin, "option-1"));
+	GREATEST_ASSERT(!irc_plugin_get_option(plugin, "not-found"));
 	GREATEST_PASS();
 }
 
 GREATEST_TEST
 options_list(void)
 {
-	const char **options = irc_plugin_get_options(&plugin);
+	const char **options = irc_plugin_get_options(plugin);
 
 	GREATEST_ASSERT_STR_EQ("option-1", options[0]);
 	GREATEST_ASSERT(!options[1]);
@@ -73,16 +77,16 @@
 GREATEST_TEST
 paths_set_get(void)
 {
-	irc_plugin_set_path(&plugin, "path-1", "new-value-1");
-	GREATEST_ASSERT_STR_EQ("new-value-1", irc_plugin_get_path(&plugin, "path-1"));
-	GREATEST_ASSERT(!irc_plugin_get_path(&plugin, "not-found"));
+	irc_plugin_set_path(plugin, "path-1", "new-value-1");
+	GREATEST_ASSERT_STR_EQ("new-value-1", irc_plugin_get_path(plugin, "path-1"));
+	GREATEST_ASSERT(!irc_plugin_get_path(plugin, "not-found"));
 	GREATEST_PASS();
 }
 
 GREATEST_TEST
 paths_list(void)
 {
-	const char **paths = irc_plugin_get_paths(&plugin);
+	const char **paths = irc_plugin_get_paths(plugin);
 
 	GREATEST_ASSERT_STR_EQ("path-1", paths[0]);
 	GREATEST_ASSERT(!paths[1]);
@@ -100,16 +104,16 @@
 GREATEST_TEST
 templates_set_get(void)
 {
-	irc_plugin_set_template(&plugin, "template-1", "new-value-1");
-	GREATEST_ASSERT_STR_EQ("new-value-1", irc_plugin_get_template(&plugin, "template-1"));
-	GREATEST_ASSERT(!irc_plugin_get_template(&plugin, "not-found"));
+	irc_plugin_set_template(plugin, "template-1", "new-value-1");
+	GREATEST_ASSERT_STR_EQ("new-value-1", irc_plugin_get_template(plugin, "template-1"));
+	GREATEST_ASSERT(!irc_plugin_get_template(plugin, "not-found"));
 	GREATEST_PASS();
 }
 
 GREATEST_TEST
 templates_list(void)
 {
-	const char **templates = irc_plugin_get_templates(&plugin);
+	const char **templates = irc_plugin_get_templates(plugin);
 
 	GREATEST_ASSERT_STR_EQ("template-1", templates[0]);
 	GREATEST_ASSERT(!templates[1]);
@@ -127,12 +131,18 @@
 GREATEST_TEST
 calls_simple(void)
 {
-	struct irc_event ev = {0};
+	struct irc_server server = {
+		.state = IRC_SERVER_STATE_CONNECTED
+	};
+	struct irc_event ev = {
+		.server = &server
+	};
 
-	irc_plugin_load(&plugin);
-	irc_plugin_unload(&plugin);
-	irc_plugin_reload(&plugin);
-	irc_plugin_handle(&plugin, &ev);
+	irc_plugin_load(plugin);
+	irc_plugin_unload(plugin);
+	irc_plugin_reload(plugin);
+	irc_plugin_handle(plugin, &ev);
+	GREATEST_ASSERT_STR_EQ("EVENT\r\n", server.conn.out);
 
 	GREATEST_PASS();
 }
--- a/tests/test-event.c	Sat Feb 13 22:15:16 2021 +0100
+++ b/tests/test-event.c	Sun Feb 14 10:11:03 2021 +0100
@@ -19,17 +19,19 @@
 #define GREATEST_USE_ABBREVS 0
 #include <greatest.h>
 
+#include <irccd/conn.h>
 #include <irccd/event.h>
-#include <irccd/server.h>
 
 GREATEST_TEST
 basics_parse_simple(void)
 {
 	/* This is a TOPIC message. */
-	char str[] = ":malikania.fr 332 boris #test :Welcome to #test :: a testing channel";
-	struct irc_event_msg msg;
+	struct irc_conn conn = {
+		.in = ":malikania.fr 332 boris #test :Welcome to #test :: a testing channel\r\n"
+	};
+	struct irc_conn_msg msg = {0};
 
-	irc_event_parse(&msg, str);
+	irc_conn_poll(&conn, &msg);
 
 	GREATEST_ASSERT_STR_EQ("malikania.fr", msg.prefix);
 	GREATEST_ASSERT_STR_EQ("332", msg.cmd);
@@ -44,10 +46,12 @@
 basics_parse_noprefix(void)
 {
 	/* Ping messages usually don't have a prefix. */
-	char str[] = "PING :malikania.fr";
-	struct irc_event_msg msg;
+	struct irc_conn conn = {
+		.in = "PING :malikania.fr\r\n"
+	};
+	struct irc_conn_msg msg = {0};
 
-	irc_event_parse(&msg, str);
+	irc_conn_poll(&conn, &msg);
 
 	GREATEST_ASSERT(!msg.prefix);
 	GREATEST_ASSERT_STR_EQ("PING", msg.cmd);
--- a/tests/test-jsapi-irccd.c	Sat Feb 13 22:15:16 2021 +0100
+++ b/tests/test-jsapi-irccd.c	Sun Feb 14 10:11:03 2021 +0100
@@ -63,9 +63,9 @@
 basics_version(void)
 {
 	const int ret = duk_peval_string(ctx,
-		"major = Irccd.version.major;"
-		"minor = Irccd.version.minor;"
-		"patch = Irccd.version.patch;"
+		"major = Irccd.Version.Major;"
+		"minor = Irccd.Version.Minor;"
+		"patch = Irccd.Version.Match;"
 	);
 
 	if (ret != 0)