From ec78084f49ef567ca915a39ec2c2485523b2a6b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Sat, 12 Oct 2024 21:04:13 +0200 Subject: [PATCH] tests: add tests on function input_data --- doc/en/weechat_dev.en.adoc | 1 + doc/fr/weechat_dev.fr.adoc | 1 + doc/ja/weechat_dev.ja.adoc | 2 + doc/sr/weechat_dev.sr.adoc | 2 + src/core/core-input.c | 2 +- tests/CMakeLists.txt | 1 + tests/tests.cpp | 1 + tests/unit/core/test-core-input.cpp | 206 ++++++++++++++++++++++++++++ tests/unit/gui/test-gui-buffer.cpp | 1 - 9 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 tests/unit/core/test-core-input.cpp diff --git a/doc/en/weechat_dev.en.adoc b/doc/en/weechat_dev.en.adoc index a9812661c..caeadbedf 100644 --- a/doc/en/weechat_dev.en.adoc +++ b/doc/en/weechat_dev.en.adoc @@ -427,6 +427,7 @@ WeeChat "core" is located in following directories: |          test-core-hdata.cpp | Tests: hdata. |          test-core-hook.cpp | Tests: hooks. |          test-core-infolist.cpp | Tests: infolists. +|          test-core-input.cpp | Tests: input functions. |          test-core-list.cpp | Tests: lists. |          test-core-network.cpp | Tests: network functions. |          test-core-secure.cpp | Tests: secured data. diff --git a/doc/fr/weechat_dev.fr.adoc b/doc/fr/weechat_dev.fr.adoc index ef8a9cef0..d7844eb95 100644 --- a/doc/fr/weechat_dev.fr.adoc +++ b/doc/fr/weechat_dev.fr.adoc @@ -429,6 +429,7 @@ Le cœur de WeeChat est situé dans les répertoires suivants : |          test-core-hdata.cpp | Tests : hdata. |          test-core-hook.cpp | Tests : hooks. |          test-core-infolist.cpp | Tests : infolists. +|          test-core-input.cpp | Tests : fonctions d'entrée. |          test-core-list.cpp | Tests : listes. |          test-core-network.cpp | Tests : fonctions réseau. |          test-core-secure.cpp | Tests : données sécurisées. diff --git a/doc/ja/weechat_dev.ja.adoc b/doc/ja/weechat_dev.ja.adoc index f1f63d3f8..fdb691fa6 100644 --- a/doc/ja/weechat_dev.ja.adoc +++ b/doc/ja/weechat_dev.ja.adoc @@ -473,6 +473,8 @@ WeeChat "core" は以下のディレクトリに配置されています: |          test-core-hdata.cpp | テスト: hdata |          test-core-hook.cpp | テスト: フック |          test-core-infolist.cpp | テスト: インフォリスト +// TRANSLATION MISSING +|          test-core-input.cpp | Tests: input functions. |          test-core-list.cpp | テスト: リスト // TRANSLATION MISSING |          test-core-network.cpp | Tests: network functions. diff --git a/doc/sr/weechat_dev.sr.adoc b/doc/sr/weechat_dev.sr.adoc index c3ccf2de1..a415940d5 100644 --- a/doc/sr/weechat_dev.sr.adoc +++ b/doc/sr/weechat_dev.sr.adoc @@ -430,6 +430,8 @@ WeeChat „језгро” се налази у следећим директо |          test-core-hdata.cpp | Тестови: hdata. |          test-core-hook.cpp | Тестови: куке. |          test-core-infolist.cpp | Тестови: infolists. +// TRANSLATION MISSING +|          test-core-input.cpp | Tests: input functions. |          test-core-list.cpp | Тестови: листе. |          test-core-network.cpp | Тестови: мрежне функције. |          test-core-secure.cpp | Тестови: обезбеђени подаци. diff --git a/src/core/core-input.c b/src/core/core-input.c index 580c1a772..fc67a7334 100644 --- a/src/core/core-input.c +++ b/src/core/core-input.c @@ -89,7 +89,7 @@ input_exec_command (struct t_gui_buffer *buffer, const char *ptr_command_name; int rc; - if ((!string) || (!string[0])) + if (!string || (!string[0])) return WEECHAT_RC_ERROR; rc = WEECHAT_RC_OK; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a92df7481..6074e0938 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -64,6 +64,7 @@ set(LIB_WEECHAT_UNIT_TESTS_CORE_SRC unit/core/test-core-hdata.cpp unit/core/test-core-hook.cpp unit/core/test-core-infolist.cpp + unit/core/test-core-input.cpp unit/core/test-core-list.cpp unit/core/test-core-network.cpp unit/core/test-core-secure.cpp diff --git a/tests/tests.cpp b/tests/tests.cpp index 01557fb07..0581e4378 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -69,6 +69,7 @@ IMPORT_TEST_GROUP(CoreHashtable); IMPORT_TEST_GROUP(CoreHdata); IMPORT_TEST_GROUP(CoreHook); IMPORT_TEST_GROUP(CoreInfolist); +IMPORT_TEST_GROUP(CoreInput); IMPORT_TEST_GROUP(CoreList); IMPORT_TEST_GROUP(CoreNetwork); IMPORT_TEST_GROUP(CoreSecure); diff --git a/tests/unit/core/test-core-input.cpp b/tests/unit/core/test-core-input.cpp new file mode 100644 index 000000000..a138656b5 --- /dev/null +++ b/tests/unit/core/test-core-input.cpp @@ -0,0 +1,206 @@ +/* + * test-core-input.cpp - test input functions + * + * Copyright (C) 2024 Sébastien Helleu + * + * 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 . + */ + +#include "CppUTest/TestHarness.h" + +#include "tests/tests.h" +#include "tests/tests-record.h" + +#define TEST_INPUT_DATA(__rc, __buffer, __data, __commands_allowed, \ + __split_newline, __user_data) \ + record_start (); \ + LONGS_EQUAL(__rc, \ + input_data (__buffer, __data, __commands_allowed, \ + __split_newline, __user_data)); \ + record_stop (); + +extern "C" +{ +#include +#include "src/core/core-input.h" +#include "src/core/core-string.h" +#include "src/gui/gui-buffer.h" +#include "src/plugins/plugin.h" +} + +TEST_GROUP(CoreInput) +{ +}; + +/* + * Tests functions: + * input_exec_data + */ + +TEST(CoreInput, ExecData) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * input_exec_command + */ + +TEST(CoreInput, ExecCommand) +{ + /* TODO: write tests */ +} + +/* + * Test callback for buffer input. + */ + +int +test_core_input_buffer_input_cb (const void *pointer, void *data, + struct t_gui_buffer *buffer, + const char *input_data) +{ + /* make C++ compiler happy */ + (void) pointer; + (void) data; + (void) buffer; + (void) input_data; + + return WEECHAT_RC_OK; +} + +/* + * Tests functions: + * input_data + */ + +TEST(CoreInput, Data) +{ + struct t_gui_buffer *buffer; + + buffer = gui_buffer_new (NULL, "test", + &test_core_input_buffer_input_cb, NULL, NULL, + NULL, NULL, NULL); + CHECK(buffer); + gui_buffer_set (buffer, "input_get_any_user_data", "1"); + + LONGS_EQUAL(WEECHAT_RC_ERROR, input_data (NULL, NULL, NULL, 0, 0)); + LONGS_EQUAL(WEECHAT_RC_ERROR, input_data ((struct t_gui_buffer *)0x1, + NULL, NULL, 0, 0)); + LONGS_EQUAL(WEECHAT_RC_ERROR, input_data (gui_buffers, NULL, NULL, 0, 0)); + + /* on core buffer: command not found */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, gui_buffers, "/xxx", NULL, 0, 0); + RECORD_CHECK_MSG("core.weechat", "=!=", + "Unknown command \"xxx\" (type /help for help), " + "commands with similar name: -", NULL); + + /* on test buffer: command not found */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, buffer, "/xxx", NULL, 0, 0); + RECORD_CHECK_MSG("core.weechat", "=!=", + "Unknown command \"xxx\" (type /help for help), " + "commands with similar name: -", NULL); + + /* on core buffer: command not found, but user_data == 1 */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, gui_buffers, "/xxx", NULL, 0, 1); + RECORD_CHECK_MSG("core.weechat", "=!=", + "Unknown command \"xxx\" (type /help for help), " + "commands with similar name: -", NULL); + + /* on test buffer: command not found, but user_data == 1 */ + TEST_INPUT_DATA(WEECHAT_RC_OK, buffer, "/xxx", NULL, 0, 1); + RECORD_CHECK_NO_MSG(); + + /* on core buffer: empty text to buffer */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, gui_buffers, "", NULL, 0, 0); + RECORD_CHECK_MSG("core.weechat", "=!=", + "You cannot write text in this buffer", NULL); + + /* on test buffer: empty text to buffer */ + TEST_INPUT_DATA(WEECHAT_RC_OK, buffer, "", NULL, 0, 0); + RECORD_CHECK_NO_MSG(); + + /* on core buffer: text to buffer */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, gui_buffers, "test", NULL, 0, 0); + RECORD_CHECK_MSG("core.weechat", "=!=", + "You cannot write text in this buffer", NULL); + + /* on test buffer: text to buffer */ + TEST_INPUT_DATA(WEECHAT_RC_OK, buffer, "test", NULL, 0, 0); + RECORD_CHECK_NO_MSG(); + + /* on core buffer: text to buffer (with two command chars) */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, gui_buffers, "//test", NULL, 0, 0); + RECORD_CHECK_MSG("core.weechat", "=!=", + "You cannot write text in this buffer", NULL); + + /* on test buffer: text to buffer (with two command chars) */ + TEST_INPUT_DATA(WEECHAT_RC_OK, buffer, "//test", NULL, 0, 0); + RECORD_CHECK_NO_MSG(); + + /* on core buffer: valid command */ + TEST_INPUT_DATA(WEECHAT_RC_OK, gui_buffers, "/print core\n/print line2", NULL, 0, 0); + RECORD_CHECK_MSG("core.weechat", "", "core", NULL); + LONGS_EQUAL(1, record_count_messages ()); + + /* on test buffer: valid command */ + TEST_INPUT_DATA(WEECHAT_RC_OK, buffer, "/print test\n/print line2", NULL, 0, 0); + RECORD_CHECK_MSG("core.test", "", "test", NULL); + LONGS_EQUAL(1, record_count_messages ()); + + /* on core buffer: forbidden command */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, gui_buffers, "/print core\n/print line2", "*,!print", 0, 0); + RECORD_CHECK_NO_MSG(); + + /* on test buffer: forbidden command */ + TEST_INPUT_DATA(WEECHAT_RC_ERROR, buffer, "/print test\n/print line2", "*,!print", 0, 0); + RECORD_CHECK_NO_MSG(); + + /* on core buffer: valid command with split_newline */ + TEST_INPUT_DATA(WEECHAT_RC_OK, gui_buffers, "/print core\n/print line2", NULL, 1, 0); + RECORD_CHECK_MSG("core.weechat", "", "core", NULL); + RECORD_CHECK_MSG("core.weechat", "", "line2", NULL); + LONGS_EQUAL(2, record_count_messages ()); + + /* on test buffer: valid command with split_newline */ + TEST_INPUT_DATA(WEECHAT_RC_OK, buffer, "/print test\n/print line2", NULL, 1, 0); + RECORD_CHECK_MSG("core.test", "", "test", NULL); + RECORD_CHECK_MSG("core.test", "", "line2", NULL); + LONGS_EQUAL(2, record_count_messages ()); + + gui_buffer_close (buffer); +} + +/* + * Tests functions: + * input_data_timer_cb + */ + +TEST(CoreInput, DataTimerCb) +{ + /* TODO: write tests */ +} + +/* + * Tests functions: + * input_data_delayed + */ + +TEST(CoreInput, DataDelayed) +{ + /* TODO: write tests */ +} diff --git a/tests/unit/gui/test-gui-buffer.cpp b/tests/unit/gui/test-gui-buffer.cpp index 5b10dcbac..82169635f 100644 --- a/tests/unit/gui/test-gui-buffer.cpp +++ b/tests/unit/gui/test-gui-buffer.cpp @@ -468,7 +468,6 @@ TEST(GuiBuffer, ApplyConfigProperties) * Test callback for buffer input. */ - int test_buffer_input_cb (const void *pointer, void *data, struct t_gui_buffer *buffer, const char *input_data)