diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2023-08-21 16:01:59 +0200 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2023-08-21 16:01:59 +0200 |
commit | ce4f4fb3fa12bbec718da980c9cf9e937c2d3f88 (patch) | |
tree | f9234b70bf1aaee6855d420135b0a11e3eb8e419 /tests/unit/plugins/logger | |
parent | faf9d1600f5d0c1518f14eaadc8c2bad982049d6 (diff) | |
download | weechat-ce4f4fb3fa12bbec718da980c9cf9e937c2d3f88.zip |
logger: remove trailing empty line in display of backlog (closes #2002)
Diffstat (limited to 'tests/unit/plugins/logger')
-rw-r--r-- | tests/unit/plugins/logger/test-logger-backlog.cpp | 12 | ||||
-rw-r--r-- | tests/unit/plugins/logger/test-logger-tail.cpp | 232 |
2 files changed, 234 insertions, 10 deletions
diff --git a/tests/unit/plugins/logger/test-logger-backlog.cpp b/tests/unit/plugins/logger/test-logger-backlog.cpp index 7b968c204..189862d66 100644 --- a/tests/unit/plugins/logger/test-logger-backlog.cpp +++ b/tests/unit/plugins/logger/test-logger-backlog.cpp @@ -199,7 +199,10 @@ TEST(LoggerBacklog, GroupMessages) "end of line", "2023-06-04 21:15:34\t\tFirst line", "of multiline message", + "", "end of message", + "2023-06-04 21:15:37\t\tTwo lines with empty line", + "", "2023-06-04 21:15:40\t\tMessage on one line", NULL, }; @@ -207,7 +210,7 @@ TEST(LoggerBacklog, GroupMessages) POINTERS_EQUAL(NULL, logger_backlog_group_messages (NULL)); - lines = arraylist_new (3, 0, 1, + lines = arraylist_new (32, 0, 1, &test_logger_backlog_msg_cmp_cb, NULL, &test_logger_backlog_msg_free_cb, NULL); @@ -234,15 +237,18 @@ TEST(LoggerBacklog, GroupMessages) messages = logger_backlog_group_messages (lines); CHECK(messages); - LONGS_EQUAL(3, arraylist_size (messages)); + LONGS_EQUAL(4, arraylist_size (messages)); STRCMP_EQUAL("end of line", (const char *)arraylist_get (messages, 0)); STRCMP_EQUAL("2023-06-04 21:15:34\t\tFirst line\n" "of multiline message\n" + "\n" "end of message", (const char *)arraylist_get (messages, 1)); - STRCMP_EQUAL("2023-06-04 21:15:40\t\tMessage on one line", + STRCMP_EQUAL("2023-06-04 21:15:37\t\tTwo lines with empty line\n", (const char *)arraylist_get (messages, 2)); + STRCMP_EQUAL("2023-06-04 21:15:40\t\tMessage on one line", + (const char *)arraylist_get (messages, 3)); arraylist_free (messages); diff --git a/tests/unit/plugins/logger/test-logger-tail.cpp b/tests/unit/plugins/logger/test-logger-tail.cpp index 614cd1cea..6e23ea6a2 100644 --- a/tests/unit/plugins/logger/test-logger-tail.cpp +++ b/tests/unit/plugins/logger/test-logger-tail.cpp @@ -79,21 +79,24 @@ TEST(LoggerTail, LoggerTailLastEol) TEST(LoggerTail, LoggerTailFile) { - char *filename; - FILE *file; - const char *content = "line 1\nline 2\nline 3"; + const char *content_3_lines = "line 1\nline 2\nline 3\n"; + const char *content_5_lines = "line 1\nline 2\n\nline 3\n\n"; + char *filename, line[4096]; struct t_arraylist *lines; + FILE *file; + int i; + + POINTERS_EQUAL(NULL, logger_tail_file (NULL, 0)); + POINTERS_EQUAL(NULL, logger_tail_file (NULL, 1)); - /* write a test file */ + /* write a small 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); + fwrite (content_3_lines, 1, strlen (content_3_lines), 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); @@ -127,4 +130,219 @@ TEST(LoggerTail, LoggerTailFile) unlink (filename); free (filename); + + /* write a small test file, with empty lines */ + filename = string_eval_path_home ("${weechat_data_dir}/test_file.txt", + NULL, NULL, NULL); + file = fopen (filename, "w"); + fwrite (content_5_lines, 1, strlen (content_5_lines), file); + fflush (file); + fclose (file); + + POINTERS_EQUAL(NULL, logger_tail_file (filename, 0)); + + lines = logger_tail_file (filename, 1); + CHECK(lines); + LONGS_EQUAL(1, arraylist_size (lines)); + STRCMP_EQUAL("", (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 3", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("", (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("", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("line 3", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 2)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 4); + CHECK(lines); + LONGS_EQUAL(4, arraylist_size (lines)); + STRCMP_EQUAL("line 2", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("line 3", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 3)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 5); + CHECK(lines); + LONGS_EQUAL(5, 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("", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("line 3", (const char *)arraylist_get (lines, 3)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 4)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 6); + CHECK(lines); + LONGS_EQUAL(5, 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("", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("line 3", (const char *)arraylist_get (lines, 3)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 4)); + arraylist_free (lines); + + unlink (filename); + free (filename); + + /* write a bigger test file */ + filename = string_eval_path_home ("${weechat_data_dir}/test_file.txt", + NULL, NULL, NULL); + file = fopen (filename, "w"); + for (i = 0; i < 1000; i++) + { + snprintf (line, sizeof (line), "this is a test, line %d\n", i + 1); + fwrite (line, 1, strlen (line), file); + } + fflush (file); + fclose (file); + + POINTERS_EQUAL(NULL, logger_tail_file (filename, 0)); + + lines = logger_tail_file (filename, 1); + CHECK(lines); + LONGS_EQUAL(1, arraylist_size (lines)); + STRCMP_EQUAL("this is a test, line 1000", (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("this is a test, line 999", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("this is a test, line 1000", (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("this is a test, line 998", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("this is a test, line 999", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("this is a test, line 1000", (const char *)arraylist_get (lines, 2)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 4); + CHECK(lines); + LONGS_EQUAL(4, arraylist_size (lines)); + STRCMP_EQUAL("this is a test, line 997", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("this is a test, line 998", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("this is a test, line 999", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("this is a test, line 1000", (const char *)arraylist_get (lines, 3)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 1000); + CHECK(lines); + LONGS_EQUAL(1000, arraylist_size (lines)); + STRCMP_EQUAL("this is a test, line 1", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("this is a test, line 2", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("this is a test, line 3", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("this is a test, line 4", (const char *)arraylist_get (lines, 3)); + STRCMP_EQUAL("this is a test, line 998", (const char *)arraylist_get (lines, 997)); + STRCMP_EQUAL("this is a test, line 999", (const char *)arraylist_get (lines, 998)); + STRCMP_EQUAL("this is a test, line 1000", (const char *)arraylist_get (lines, 999)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 2000); + CHECK(lines); + LONGS_EQUAL(1000, arraylist_size (lines)); + STRCMP_EQUAL("this is a test, line 1", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("this is a test, line 2", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("this is a test, line 3", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("this is a test, line 4", (const char *)arraylist_get (lines, 3)); + STRCMP_EQUAL("this is a test, line 998", (const char *)arraylist_get (lines, 997)); + STRCMP_EQUAL("this is a test, line 999", (const char *)arraylist_get (lines, 998)); + STRCMP_EQUAL("this is a test, line 1000", (const char *)arraylist_get (lines, 999)); + arraylist_free (lines); + + unlink (filename); + free (filename); + + /* write a bigger test file, with empty lines */ + filename = string_eval_path_home ("${weechat_data_dir}/test_file.txt", + NULL, NULL, NULL); + file = fopen (filename, "w"); + for (i = 0; i < 1000; i++) + { + snprintf (line, sizeof (line), "this is a test, line %d\n\n", i + 1); + fwrite (line, 1, strlen (line), file); + } + fflush (file); + fclose (file); + + POINTERS_EQUAL(NULL, logger_tail_file (filename, 0)); + + lines = logger_tail_file (filename, 1); + CHECK(lines); + LONGS_EQUAL(1, arraylist_size (lines)); + STRCMP_EQUAL("", (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("this is a test, line 1000", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("", (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("", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("this is a test, line 1000", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 2)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 4); + CHECK(lines); + LONGS_EQUAL(4, arraylist_size (lines)); + STRCMP_EQUAL("this is a test, line 999", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("this is a test, line 1000", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 3)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 2000); + CHECK(lines); + LONGS_EQUAL(2000, arraylist_size (lines)); + STRCMP_EQUAL("this is a test, line 1", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("this is a test, line 2", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 3)); + STRCMP_EQUAL("this is a test, line 3", (const char *)arraylist_get (lines, 4)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 5)); + STRCMP_EQUAL("this is a test, line 998", (const char *)arraylist_get (lines, 1994)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1995)); + STRCMP_EQUAL("this is a test, line 999", (const char *)arraylist_get (lines, 1996)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1997)); + STRCMP_EQUAL("this is a test, line 1000", (const char *)arraylist_get (lines, 1998)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1999)); + arraylist_free (lines); + + lines = logger_tail_file (filename, 4000); + CHECK(lines); + LONGS_EQUAL(2000, arraylist_size (lines)); + STRCMP_EQUAL("this is a test, line 1", (const char *)arraylist_get (lines, 0)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1)); + STRCMP_EQUAL("this is a test, line 2", (const char *)arraylist_get (lines, 2)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 3)); + STRCMP_EQUAL("this is a test, line 3", (const char *)arraylist_get (lines, 4)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 5)); + STRCMP_EQUAL("this is a test, line 998", (const char *)arraylist_get (lines, 1994)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1995)); + STRCMP_EQUAL("this is a test, line 999", (const char *)arraylist_get (lines, 1996)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1997)); + STRCMP_EQUAL("this is a test, line 1000", (const char *)arraylist_get (lines, 1998)); + STRCMP_EQUAL("", (const char *)arraylist_get (lines, 1999)); + arraylist_free (lines); + + unlink (filename); + free (filename); } |