1 #include <setjmp.h>
2 #include <stdarg.h>
3 #include <stddef.h>
4 #include <stdlib.h>
5 #include <cmocka.h>
6 #include <glib.h>
7 #include <stdio.h>
8 #include <unistd.h>
9
10 #include "common.h"
11 #include "helpers.h"
12 #include "config/preferences.h"
13 #include "xmpp/chat_session.h"
14
15 void
create_config_dir(void ** state)16 create_config_dir(void** state)
17 {
18 setenv("XDG_CONFIG_HOME", "./tests/files/xdg_config_home", 1);
19 if (!mkdir_recursive("./tests/files/xdg_config_home/profanity")) {
20 assert_true(FALSE);
21 }
22 }
23
24 void
remove_config_dir(void ** state)25 remove_config_dir(void** state)
26 {
27 rmdir("./tests/files/xdg_config_home/profanity");
28 rmdir("./tests/files/xdg_config_home");
29 }
30
31 void
create_data_dir(void ** state)32 create_data_dir(void** state)
33 {
34 setenv("XDG_DATA_HOME", "./tests/files/xdg_data_home", 1);
35 if (!mkdir_recursive("./tests/files/xdg_data_home/profanity")) {
36 assert_true(FALSE);
37 }
38 }
39
40 void
remove_data_dir(void ** state)41 remove_data_dir(void** state)
42 {
43 rmdir("./tests/files/xdg_data_home/profanity");
44 rmdir("./tests/files/xdg_data_home");
45 }
46
47 void
load_preferences(void ** state)48 load_preferences(void** state)
49 {
50 create_config_dir(state);
51 FILE* f = fopen("./tests/files/xdg_config_home/profanity/profrc", "ab+");
52 if (f) {
53 prefs_load(NULL);
54 }
55 fclose(f);
56 }
57
58 void
close_preferences(void ** state)59 close_preferences(void** state)
60 {
61 prefs_close();
62 remove("./tests/files/xdg_config_home/profanity/profrc");
63 remove_config_dir(state);
64 rmdir("./tests/files");
65 }
66
67 void
init_chat_sessions(void ** state)68 init_chat_sessions(void** state)
69 {
70 load_preferences(NULL);
71 chat_sessions_init();
72 }
73
74 void
close_chat_sessions(void ** state)75 close_chat_sessions(void** state)
76 {
77 chat_sessions_clear();
78 close_preferences(NULL);
79 }
80
81 int
utf8_pos_to_col(char * str,int utf8_pos)82 utf8_pos_to_col(char* str, int utf8_pos)
83 {
84 int col = 0;
85
86 int i = 0;
87 for (i = 0; i < utf8_pos; i++) {
88 col++;
89 gchar* ch = g_utf8_offset_to_pointer(str, i);
90 gunichar uni = g_utf8_get_char(ch);
91 if (g_unichar_iswide(uni)) {
92 col++;
93 }
94 }
95
96 return col;
97 }
98
99 static GCompareFunc cmp_func;
100
101 void
glist_set_cmp(GCompareFunc func)102 glist_set_cmp(GCompareFunc func)
103 {
104 cmp_func = func;
105 }
106
107 int
glist_contents_equal(const void * actual,const void * expected)108 glist_contents_equal(const void* actual, const void* expected)
109 {
110 GList* ac = (GList*)actual;
111 GList* ex = (GList*)expected;
112
113 GList* p = ex;
114 printf("\nExpected\n");
115 while (ex) {
116 printf("\n\n%s\n", (char*)p->data);
117 ex = g_list_next(ex);
118 }
119 printf("\n\n");
120 p = ac;
121 printf("\nActual\n");
122 while (ac) {
123 printf("\n\n%s\n", (char*)p->data);
124 ac = g_list_next(ac);
125 }
126 printf("\n\n");
127
128 if (g_list_length(ex) != g_list_length(ac)) {
129 return 0;
130 }
131
132 GList* ex_curr = ex;
133 while (ex_curr != NULL) {
134 if (g_list_find_custom(ac, ex_curr->data, cmp_func) == NULL) {
135 return 0;
136 }
137 ex_curr = g_list_next(ex_curr);
138 }
139
140 return 1;
141 }
142