/*
 * SPDX-FileCopyrightText: 2023-2026 Sébastien Helleu <flashcode@flashtux.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

/* Test alias functions */

#include "CppUTest/TestHarness.h"

#include "tests.h"

extern "C"
{
#include "src/plugins/alias/alias.h"

extern char *alias_replace_args (const char *alias_args, const char *user_args);
}

TEST_GROUP(Alias)
{
};

/*
 * Test functions:
 *   alias_valid
 */

TEST(Alias, Valid)
{
    struct t_alias *alias;

    alias = alias_new ("test_alias", "/mute", NULL);

    LONGS_EQUAL(0, alias_valid (NULL));
    LONGS_EQUAL(0, alias_valid ((struct t_alias *)0x1));
    LONGS_EQUAL(0, alias_valid (alias + 1));

    LONGS_EQUAL(1, alias_valid (alias));

    alias_free (alias);
}

/*
 * Test functions:
 *   alias_search
 */

TEST(Alias, Search)
{
    struct t_alias *alias;

    alias = alias_new ("test_alias", "/mute", NULL);

    POINTERS_EQUAL(NULL, alias_search (NULL));
    POINTERS_EQUAL(NULL, alias_search (""));
    POINTERS_EQUAL(NULL, alias_search ("does_not_exist"));

    POINTERS_EQUAL(alias, alias_search ("test_alias"));

    alias_free (alias);
}

/*
 * Test functions:
 *   alias_string_add_word_range
 *   alias_string_add_arguments
 *   alias_replace_args
 */

TEST(Alias, ReplaceArgs)
{
    char *str;

    STRCMP_EQUAL(NULL, alias_replace_args (NULL, NULL));
    STRCMP_EQUAL(NULL, alias_replace_args (NULL, ""));
    STRCMP_EQUAL(NULL, alias_replace_args ("", NULL));

    WEE_TEST_STR("", alias_replace_args ("", ""));
    WEE_TEST_STR("", alias_replace_args ("", "abc def"));
    WEE_TEST_STR("/test", alias_replace_args ("/test", ""));
    WEE_TEST_STR("/test $1", alias_replace_args ("/test \\$1", "abc def"));

    /* Arguments by index: $n */
    WEE_TEST_STR("/test $0", alias_replace_args ("/test $0", ""));
    WEE_TEST_STR("/test $0", alias_replace_args ("/test $0", "abc def"));
    WEE_TEST_STR("/test ", alias_replace_args ("/test $1", ""));
    WEE_TEST_STR("/test ", alias_replace_args ("/test $9", "abc def"));
    WEE_TEST_STR("/test abc", alias_replace_args ("/test $1", "abc def"));
    WEE_TEST_STR("/test def abc", alias_replace_args ("/test $2 $1", "abc def"));
    WEE_TEST_STR("/test def abc", alias_replace_args ("/test $2 $1", "abc def"));

    /* Arguments from 1 to m: $-m */
    WEE_TEST_STR("/test $-0", alias_replace_args ("/test $-0", ""));
    WEE_TEST_STR("/test $-0", alias_replace_args ("/test $-0", "abc def"));
    WEE_TEST_STR("/test ", alias_replace_args ("/test $-1", ""));
    WEE_TEST_STR("/test abc", alias_replace_args ("/test $-1", "abc def"));
    WEE_TEST_STR("/test abc def", alias_replace_args ("/test $-2", "abc def"));
    WEE_TEST_STR("/test abc def", alias_replace_args ("/test $-3", "abc def"));
    WEE_TEST_STR("/test abc def", alias_replace_args ("/test $-9", "abc def"));

    /* Arguments from n to last: $n- */
    WEE_TEST_STR("/test $0-", alias_replace_args ("/test $0-", ""));
    WEE_TEST_STR("/test $0-", alias_replace_args ("/test $0-", "abc def"));
    WEE_TEST_STR("/test ", alias_replace_args ("/test $1-", ""));
    WEE_TEST_STR("/test abc def", alias_replace_args ("/test $1-", "abc def"));
    WEE_TEST_STR("/test def", alias_replace_args ("/test $2-", "abc def"));
    WEE_TEST_STR("/test ", alias_replace_args ("/test $3-", "abc def"));
    WEE_TEST_STR("/test ", alias_replace_args ("/test $9-", "abc def"));

    /* Arguments from n to m: $n-m */
    WEE_TEST_STR("/test $0-0", alias_replace_args ("/test $0-0", ""));
    WEE_TEST_STR("/test $0-0", alias_replace_args ("/test $0-0", "abc def"));
    WEE_TEST_STR("/test $0-1", alias_replace_args ("/test $0-1", ""));
    WEE_TEST_STR("/test $0-1", alias_replace_args ("/test $0-1", "abc def"));
    WEE_TEST_STR("/test ", alias_replace_args ("/test $1-1", ""));
    WEE_TEST_STR("/test abc", alias_replace_args ("/test $1-1", "abc def"));
    WEE_TEST_STR("/test abc def", alias_replace_args ("/test $1-2", "abc def"));
    WEE_TEST_STR("/test def", alias_replace_args ("/test $2-2", "abc def"));
    WEE_TEST_STR("/test def", alias_replace_args ("/test $2-3", "abc def"));
    WEE_TEST_STR("/test def", alias_replace_args ("/test $2-9", "abc def"));

    /* All arguments: $* */
    WEE_TEST_STR("/test ", alias_replace_args ("/test $*", ""));
    WEE_TEST_STR("/test abc \"def\"", alias_replace_args ("/test $*", "abc \"def\""));

    /* All arguments with double quotes escaped: $& */
    WEE_TEST_STR("/test ", alias_replace_args ("/test $&", ""));
    WEE_TEST_STR("/test abc \\\"def\\\"", alias_replace_args ("/test $&", "abc \"def\""));

    /* Last argument: $~ */
    WEE_TEST_STR("/test ", alias_replace_args ("/test $~", ""));
    WEE_TEST_STR("/test abc", alias_replace_args ("/test $~", "abc"));
    WEE_TEST_STR("/test def", alias_replace_args ("/test $~", "abc def"));

    /* Multiple arguments */
    WEE_TEST_STR("/test def abc 'ghi jkl'",
                 alias_replace_args("/test $2 $1 '$3-'", "abc def ghi jkl"));
}

/*
 * Test functions:
 *   alias_run_command
 */

TEST(Alias, RunCommand)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_cb
 */

TEST(Alias, Cb)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_hook_command
 */

TEST(Alias, HookCommand)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_find_pos
 */

TEST(Alias, FindPos)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_insert
 */

TEST(Alias, Insert)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_remove_from_list
 */

TEST(Alias, RemoveFromList)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_rename
 */

TEST(Alias, Rename)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_free
 */

TEST(Alias, Free)
{
    /* Test free of NULL alias. */
    alias_free (NULL);
}

/*
 * Test functions:
 *   alias_free_all
 */

TEST(Alias, FreeAll)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_update_completion
 */

TEST(Alias, UpdateCompletion)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_name_valid
 */

TEST(Alias, NameValid)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_new
 */

TEST(Alias, New)
{
    /* TODO: write tests */
}

/*
 * Test functions:
 *   alias_add_to_infolist
 */

TEST(Alias, AddToInfolist)
{
    /* TODO: write tests */
}
