0
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-07-26 12:14:28 -04:00
Steffen Jaeckel c0da36c48d Rage-cleanup.
While trying to get the unit tests working again I stumbled over all those
things that I thought could be better^TM.

Now we also know "TODO: why does this make the test fail?" - because
the unit tests are brittle AF ... and we have to init the subsystems
we use in the test, otherwise the cleanup will fail...

BTW. you can now also only run a single test ... or a pattern or so ...
you'd have to read how `cmocka_set_test_filter()` works exactly.

Signed-off-by: Steffen Jaeckel <s@jaeckel.eu>
2025-03-10 11:45:15 +01:00

156 lines
2.9 KiB
C

#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <cmocka.h>
#include <glib.h>
#include <stdio.h>
#include <unistd.h>
#include "common.h"
#include "helpers.h"
#include "config/preferences.h"
#include "xmpp/chat_session.h"
#include "command/cmd_defs.h"
void prof_shutdown(void);
void
create_config_dir(void** state)
{
setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1);
if (!create_dir("./tests/files/xdg_config_home/profanity")) {
assert_true(FALSE);
}
}
void
remove_config_dir(void** state)
{
rmdir("./tests/files/xdg_config_home/profanity");
rmdir("./tests/files/xdg_config_home");
}
void
create_data_dir(void** state)
{
setenv("XDG_DATA_HOME", "./tests/files/xdg_data_home", 1);
if (!create_dir("./tests/files/xdg_data_home/profanity")) {
assert_true(FALSE);
}
}
void
remove_data_dir(void** state)
{
rmdir("./tests/files/xdg_data_home/profanity");
rmdir("./tests/files/xdg_data_home");
}
int
load_preferences(void** state)
{
create_config_dir(state);
FILE* f = fopen("./tests/files/xdg_config_home/profanity/profrc", "ab+");
if (f) {
prefs_load(NULL);
} else {
return 1;
}
fclose(f);
cmd_init();
chat_sessions_init();
return 0;
}
int
close_preferences(void** state)
{
chat_sessions_clear();
prefs_close();
remove("./tests/files/xdg_config_home/profanity/profrc");
remove_config_dir(state);
rmdir("./tests/files");
prof_shutdown();
return 0;
}
int
init_chat_sessions(void** state)
{
load_preferences(NULL);
chat_sessions_init();
return 0;
}
int
close_chat_sessions(void** state)
{
chat_sessions_clear();
close_preferences(NULL);
prof_shutdown();
return 0;
}
int
utf8_pos_to_col(char* str, int utf8_pos)
{
int col = 0;
int i = 0;
for (i = 0; i < utf8_pos; i++) {
col++;
gchar* ch = g_utf8_offset_to_pointer(str, i);
gunichar uni = g_utf8_get_char(ch);
if (g_unichar_iswide(uni)) {
col++;
}
}
return col;
}
static GCompareFunc cmp_func;
void
glist_set_cmp(GCompareFunc func)
{
cmp_func = func;
}
int
glist_contents_equal(const void* actual, const void* expected)
{
GList* ac = (GList*)actual;
GList* ex = (GList*)expected;
GList* p = ex;
printf("\nExpected\n");
while (ex) {
printf("\n\n%s\n", (char*)p->data);
ex = g_list_next(ex);
}
printf("\n\n");
p = ac;
printf("\nActual\n");
while (ac) {
printf("\n\n%s\n", (char*)p->data);
ac = g_list_next(ac);
}
printf("\n\n");
if (g_list_length(ex) != g_list_length(ac)) {
return 0;
}
GList* ex_curr = ex;
while (ex_curr != NULL) {
if (g_list_find_custom(ac, ex_curr->data, cmp_func) == NULL) {
return 0;
}
ex_curr = g_list_next(ex_curr);
}
return 1;
}