]> jfr.im git - irc/weechat/weechat.git/commitdiff
tests: add tests on logger tail functions (issue #1926)
authorSébastien Helleu <redacted>
Sun, 4 Jun 2023 19:14:20 +0000 (21:14 +0200)
committerSébastien Helleu <redacted>
Mon, 5 Jun 2023 05:29:10 +0000 (07:29 +0200)
ChangeLog.adoc
doc/en/weechat_dev.en.adoc
doc/fr/weechat_dev.fr.adoc
doc/ja/weechat_dev.ja.adoc
doc/sr/weechat_dev.sr.adoc
tests/CMakeLists.txt
tests/unit/plugins/logger/test-logger-tail.cpp [new file with mode: 0644]

index e52169737e2f872f6c9f884dafd82df7bcede7fc..605ee6ef2ce638209a1446131d9ee28d39a8ca84 100644 (file)
@@ -123,6 +123,7 @@ Tests::
   * irc: check tags in messages displayed by IRC plugin
   * irc: add tests on function irc_server_alloc_with_url
   * irc: add tests on message/notice/action/CTCP sent
+  * logger: add tests on tail functions
   * relay: add tests on "irc" protocol
 
 Build::
index 48c9cdf1bc281e6274297283f3e65d6cbd19df72..3f9fc15a8215da0162daa718bc88e10a80f7c5e9 100644 (file)
@@ -448,6 +448,7 @@ WeeChat "core" is located in following directories:
 |          logger/                    | Root of unit tests for logger plugin.
 |             test-logger.cpp         | Tests: logger.
 |             test-logger-backlog.cpp | Tests: logger backlog.
+|             test-logger-tail.cpp    | Tests: logger tail functions.
 |          trigger/                   | Root of unit tests for trigger plugin.
 |             test-trigger.cpp        | Tests: triggers.
 |             test-trigger-config.cpp | Tests: trigger configuration.
index f1cec2445253c7521b4f7b78c30ee6c06cb47d6b..e57a71f0d385e70882bea8b9cd24c3a710e42c94 100644 (file)
@@ -450,6 +450,7 @@ Le cœur de WeeChat est situé dans les répertoires suivants :
 |          logger/                    | Racine des tests unitaires pour l'extension logger.
 |             test-logger.cpp         | Tests : logger.
 |             test-logger-backlog.cpp | Tests : backlog logger.
+|             test-logger-tail.cpp    | Tests : fonctions "tail".
 |          trigger/                   | Racine des tests unitaires pour l'extension trigger.
 |             test-trigger.cpp        | Tests : triggers.
 |             test-trigger-config.cpp | Tests : configuration trigger.
index 49d37f349878fa73b1edaedddccd34c6f3522fe8..37c58cad20418be54cc7fa276322adab43ee2e0a 100644 (file)
@@ -502,6 +502,8 @@ WeeChat "core" は以下のディレクトリに配置されています:
 // TRANSLATION MISSING
 |             test-logger-backlog.cpp | Tests: logger backlog.
 // TRANSLATION MISSING
+|             test-logger-tail.cpp    | Tests: logger tail functions.
+// TRANSLATION MISSING
 |          trigger/                   | Root of unit tests for trigger plugin.
 // TRANSLATION MISSING
 |             test-trigger.cpp        | Tests: triggers.
index dfad649717731435de5992c95a019c983f13398e..c2ea1d93560fbbc3a435869e9318c01d66617a44 100644 (file)
@@ -450,6 +450,8 @@ WeeChat „језгро” се налази у следећим директо
 |          logger/                    | Корен unit тестива за logger додатак.
 |             test-logger.cpp         | Тестови: logger.
 |             test-logger-backlog.cpp | Тестови: logger заостатак.
+// TRANSLATION MISSING
+|             test-logger-tail.cpp    | Tests: logger tail functions.
 |          trigger/                   | Корен unit тестова за окидач додатак.
 |             test-trigger.cpp        | Тестови: окидачи.
 |             test-trigger-config.cpp | Тестови: конфигурација окидача.
index 092ef3a53c8d69f126d02ad54351b3761b8ae3eb..048c8a0971a7e6b83215f7ff592dc811f9d9c77f 100644 (file)
@@ -91,6 +91,7 @@ if(ENABLE_LOGGER)
   list(APPEND LIB_WEECHAT_UNIT_TESTS_PLUGINS_SRC
     unit/plugins/logger/test-logger.cpp
     unit/plugins/logger/test-logger-backlog.cpp
+    unit/plugins/logger/test-logger-tail.cpp
   )
 endif()
 
diff --git a/tests/unit/plugins/logger/test-logger-tail.cpp b/tests/unit/plugins/logger/test-logger-tail.cpp
new file mode 100644 (file)
index 0000000..c4f35f3
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * test-logger-tail.cpp - test logger tail functions
+ *
+ * Copyright (C) 2023 Sébastien Helleu <flashcode@flashtux.org>
+ *
+ * This file is part of WeeChat, the extensible chat client.
+ *
+ * WeeChat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * WeeChat is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with WeeChat.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "CppUTest/TestHarness.h"
+
+#include "tests/tests.h"
+
+extern "C"
+{
+#include <unistd.h>
+#include <stdio.h>
+#include "src/core/wee-arraylist.h"
+#include "src/core/wee-string.h"
+#include "src/plugins/logger/logger-tail.h"
+
+extern const char *logger_tail_last_eol (const char *string_start,
+                                         const char *string_ptr);
+}
+
+TEST_GROUP(LoggerTail)
+{
+};
+
+/*
+ * Tests functions:
+ *   logger_tail_last_eol
+ */
+
+TEST(LoggerTail, LoggerTailLastEol)
+{
+    const char *str = "abc\ndef\rghi";
+
+    POINTERS_EQUAL(NULL, logger_tail_last_eol (NULL, NULL));
+    POINTERS_EQUAL(NULL, logger_tail_last_eol (NULL, ""));
+    POINTERS_EQUAL(NULL, logger_tail_last_eol ("", NULL));
+    POINTERS_EQUAL(NULL, logger_tail_last_eol ("", ""));
+    POINTERS_EQUAL(NULL, logger_tail_last_eol (str + 9, str));
+
+    POINTERS_EQUAL(NULL, logger_tail_last_eol (str, str));
+    POINTERS_EQUAL(NULL, logger_tail_last_eol (str, str + 1));
+    POINTERS_EQUAL(NULL, logger_tail_last_eol (str, str + 2));
+
+    POINTERS_EQUAL(str + 3, logger_tail_last_eol (str, str + 3));
+    POINTERS_EQUAL(str + 3, logger_tail_last_eol (str, str + 4));
+    POINTERS_EQUAL(str + 3, logger_tail_last_eol (str, str + 5));
+    POINTERS_EQUAL(str + 3, logger_tail_last_eol (str, str + 6));
+
+    POINTERS_EQUAL(str + 7, logger_tail_last_eol (str, str + 7));
+    POINTERS_EQUAL(str + 7, logger_tail_last_eol (str, str + 8));
+    POINTERS_EQUAL(str + 7, logger_tail_last_eol (str, str + 9));
+    POINTERS_EQUAL(str + 7, logger_tail_last_eol (str, str + 10));
+}
+
+/*
+ * Tests functions:
+ *   logger_tail_file
+ *   logger_tail_free
+ */
+
+TEST(LoggerTail, LoggerTailFile)
+{
+    char *filename;
+    FILE *file;
+    const char *content = "line 1\nline 2\nline 3";
+    struct t_arraylist *lines;
+
+    /* write a test file */
+    filename = string_eval_path_home ("${weechat_data_dir}/test_file.txt",
+                                      NULL, NULL, NULL);
+    file = fopen (filename, "w");
+    fwrite (content, 1, strlen (content), file);
+    fflush (file);
+    fclose (file);
+
+    POINTERS_EQUAL(NULL, logger_tail_file (NULL, 0));
+    POINTERS_EQUAL(NULL, logger_tail_file (NULL, 1));
+    POINTERS_EQUAL(NULL, logger_tail_file (filename, 0));
+
+    lines = logger_tail_file (filename, 1);
+    CHECK(lines);
+    LONGS_EQUAL(1, arraylist_size (lines));
+    STRCMP_EQUAL("line 3", (const char *)arraylist_get (lines, 0));
+    arraylist_free (lines);
+
+    lines = logger_tail_file (filename, 2);
+    CHECK(lines);
+    LONGS_EQUAL(2, arraylist_size (lines));
+    STRCMP_EQUAL("line 2", (const char *)arraylist_get (lines, 0));
+    STRCMP_EQUAL("line 3", (const char *)arraylist_get (lines, 1));
+    arraylist_free (lines);
+
+    lines = logger_tail_file (filename, 3);
+    CHECK(lines);
+    LONGS_EQUAL(3, arraylist_size (lines));
+    STRCMP_EQUAL("line 1", (const char *)arraylist_get (lines, 0));
+    STRCMP_EQUAL("line 2", (const char *)arraylist_get (lines, 1));
+    STRCMP_EQUAL("line 3", (const char *)arraylist_get (lines, 2));
+    arraylist_free (lines);
+
+    lines = logger_tail_file (filename, 4);
+    CHECK(lines);
+    LONGS_EQUAL(3, arraylist_size (lines));
+    STRCMP_EQUAL("line 1", (const char *)arraylist_get (lines, 0));
+    STRCMP_EQUAL("line 2", (const char *)arraylist_get (lines, 1));
+    STRCMP_EQUAL("line 3", (const char *)arraylist_get (lines, 2));
+    arraylist_free (lines);
+
+    unlink (filename);
+    free (filename);
+}