mirror of
https://github.com/profanity-im/profanity.git
synced 2024-12-04 14:46:46 -05:00
Autocomplte uses default functions
This commit is contained in:
parent
718229d08c
commit
eb5b04df46
3
Makefile
3
Makefile
@ -10,7 +10,7 @@ OBJS = log.o windows.o title_bar.o status_bar.o input_win.o jabber.o \
|
|||||||
contact.o preferences.o prof_autocomplete.o main.o
|
contact.o preferences.o prof_autocomplete.o main.o
|
||||||
TESTOBJS = test_contact_list.o contact_list.o contact.o \
|
TESTOBJS = test_contact_list.o contact_list.o contact.o \
|
||||||
test_util.o test_prof_history.o prof_history.o util.o \
|
test_util.o test_prof_history.o prof_history.o util.o \
|
||||||
prof_autocomplete.o
|
test_prof_autocomplete.o prof_autocomplete.o
|
||||||
|
|
||||||
profanity: $(OBJS)
|
profanity: $(OBJS)
|
||||||
$(CC) -o profanity $(OBJS) $(LIBS)
|
$(CC) -o profanity $(OBJS) $(LIBS)
|
||||||
@ -36,6 +36,7 @@ main.o: profanity.h
|
|||||||
test_contact_list.o: contact_list.h contact.h
|
test_contact_list.o: contact_list.h contact.h
|
||||||
test_util.o: util.h
|
test_util.o: util.h
|
||||||
test_prof_history.o: prof_history.h
|
test_prof_history.o: prof_history.h
|
||||||
|
test_prof_autocomplete.o: contact.h prof_autocomplete.h
|
||||||
|
|
||||||
testsuite: testsuite.h $(TESTOBJS)
|
testsuite: testsuite.h $(TESTOBJS)
|
||||||
$(CC) $(CFLAGS) $(CPPLIB) testsuite.c $(TESTOBJS) -o testsuite $(TESTLIB)
|
$(CC) $(CFLAGS) $(CPPLIB) testsuite.c $(TESTOBJS) -o testsuite $(TESTLIB)
|
||||||
|
14
contact.c
14
contact.c
@ -50,6 +50,20 @@ PContact p_contact_new(const char * const name, const char * const show,
|
|||||||
return contact;
|
return contact;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PContact p_contact_copy(PContact contact)
|
||||||
|
{
|
||||||
|
PContact copy = malloc(sizeof(struct p_contact_t));
|
||||||
|
copy->name = strdup(contact->name);
|
||||||
|
copy->show = strdup(contact->show);
|
||||||
|
|
||||||
|
if (contact->status != NULL)
|
||||||
|
copy->status = strdup(contact->status);
|
||||||
|
else
|
||||||
|
copy->status = NULL;
|
||||||
|
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
void p_contact_free(PContact contact)
|
void p_contact_free(PContact contact)
|
||||||
{
|
{
|
||||||
free(contact->name);
|
free(contact->name);
|
||||||
|
@ -27,6 +27,7 @@ typedef struct p_contact_t *PContact;
|
|||||||
|
|
||||||
PContact p_contact_new(const char * const name, const char * const show,
|
PContact p_contact_new(const char * const name, const char * const show,
|
||||||
const char * const status);
|
const char * const status);
|
||||||
|
PContact p_contact_copy(PContact contact);
|
||||||
void p_contact_free(PContact contact);
|
void p_contact_free(PContact contact);
|
||||||
const char * p_contact_name(PContact contact);
|
const char * p_contact_name(PContact contact);
|
||||||
const char * p_contact_show(PContact contact);
|
const char * p_contact_show(PContact contact);
|
||||||
|
@ -33,8 +33,8 @@ struct p_autocomplete_t {
|
|||||||
gchar *search_str;
|
gchar *search_str;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
static gchar * _search_from(PAutocomplete ac, GSList *curr, PStrFunc str_func);
|
static gchar * _search_from(PAutocomplete ac, GSList *curr, PStrFunc str_func);
|
||||||
|
static const char *_str_func_default(const char *orig);
|
||||||
|
|
||||||
PAutocomplete p_autocomplete_new(void)
|
PAutocomplete p_autocomplete_new(void)
|
||||||
{
|
{
|
||||||
@ -66,6 +66,9 @@ void p_autocomplete_reset(PAutocomplete ac)
|
|||||||
void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
|
void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
|
||||||
GDestroyNotify free_func)
|
GDestroyNotify free_func)
|
||||||
{
|
{
|
||||||
|
if (str_func == NULL)
|
||||||
|
str_func = (PStrFunc)_str_func_default;
|
||||||
|
|
||||||
if (ac->items == NULL) {
|
if (ac->items == NULL) {
|
||||||
ac->items = g_slist_append(ac->items, item);
|
ac->items = g_slist_append(ac->items, item);
|
||||||
return;
|
return;
|
||||||
@ -75,15 +78,13 @@ void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
|
|||||||
while(curr) {
|
while(curr) {
|
||||||
|
|
||||||
// insert
|
// insert
|
||||||
if ( ((str_func == NULL) && (g_strcmp0(curr->data, item) > 0)) ||
|
if (g_strcmp0(str_func(curr->data), str_func(item)) > 0) {
|
||||||
((g_strcmp0(str_func(curr->data), str_func(item)) > 0)) ) {
|
|
||||||
ac->items = g_slist_insert_before(ac->items,
|
ac->items = g_slist_insert_before(ac->items,
|
||||||
curr, item);
|
curr, item);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// update
|
// update
|
||||||
} else if ( ((str_func == NULL) && (g_strcmp0(curr->data, item) == 0)) ||
|
} else if (g_strcmp0(str_func(curr->data), str_func(item)) == 0) {
|
||||||
((g_strcmp0(str_func(curr->data), str_func(item)) == 0)) ) {
|
|
||||||
free_func(curr->data);
|
free_func(curr->data);
|
||||||
curr->data = item;
|
curr->data = item;
|
||||||
return;
|
return;
|
||||||
@ -102,10 +103,12 @@ void p_autocomplete_add(PAutocomplete ac, void *item, PStrFunc str_func,
|
|||||||
void p_autocomplete_remove(PAutocomplete ac, const char * const item,
|
void p_autocomplete_remove(PAutocomplete ac, const char * const item,
|
||||||
PStrFunc str_func, GDestroyNotify free_func)
|
PStrFunc str_func, GDestroyNotify free_func)
|
||||||
{
|
{
|
||||||
|
if (str_func == NULL)
|
||||||
|
str_func = (PStrFunc)_str_func_default;
|
||||||
|
|
||||||
// reset last found if it points to the item to be removed
|
// reset last found if it points to the item to be removed
|
||||||
if (ac->last_found != NULL)
|
if (ac->last_found != NULL)
|
||||||
if ( ((str_func == NULL) && (g_strcmp0(ac->last_found->data, item) == 0)) ||
|
if (g_strcmp0(str_func(ac->last_found->data), item) == 0)
|
||||||
((g_strcmp0(str_func(ac->last_found->data), item) == 0)) )
|
|
||||||
ac->last_found = NULL;
|
ac->last_found = NULL;
|
||||||
|
|
||||||
if (!ac->items) {
|
if (!ac->items) {
|
||||||
@ -114,8 +117,7 @@ void p_autocomplete_remove(PAutocomplete ac, const char * const item,
|
|||||||
GSList *curr = ac->items;
|
GSList *curr = ac->items;
|
||||||
|
|
||||||
while(curr) {
|
while(curr) {
|
||||||
if ( ((str_func == NULL) && (g_strcmp0(curr->data, item) == 0)) ||
|
if (g_strcmp0(str_func(curr->data), item) == 0) {
|
||||||
((g_strcmp0(str_func(curr->data), item) == 0)) ) {
|
|
||||||
void *current_item = curr->data;
|
void *current_item = curr->data;
|
||||||
ac->items = g_slist_remove(ac->items, curr->data);
|
ac->items = g_slist_remove(ac->items, curr->data);
|
||||||
free_func(current_item);
|
free_func(current_item);
|
||||||
@ -146,6 +148,9 @@ GSList * p_autocomplete_get_list(PAutocomplete ac, PCopyFunc copy_func)
|
|||||||
gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str,
|
gchar * p_autocomplete_complete(PAutocomplete ac, gchar *search_str,
|
||||||
PStrFunc str_func)
|
PStrFunc str_func)
|
||||||
{
|
{
|
||||||
|
if (str_func == NULL)
|
||||||
|
str_func = (PStrFunc)_str_func_default;
|
||||||
|
|
||||||
gchar *found = NULL;
|
gchar *found = NULL;
|
||||||
|
|
||||||
// no items to search
|
// no items to search
|
||||||
@ -184,11 +189,9 @@ static gchar * _search_from(PAutocomplete ac, GSList *curr, PStrFunc str_func)
|
|||||||
while(curr) {
|
while(curr) {
|
||||||
|
|
||||||
// match found
|
// match found
|
||||||
if ( ( (str_func == NULL) && (strncmp(curr->data, ac->search_str,
|
if (strncmp(str_func(curr->data),
|
||||||
strlen(ac->search_str)) == 0) ) ||
|
|
||||||
( (strncmp(str_func(curr->data),
|
|
||||||
ac->search_str,
|
ac->search_str,
|
||||||
strlen(ac->search_str)) == 0) ) ) {
|
strlen(ac->search_str)) == 0) {
|
||||||
gchar *result =
|
gchar *result =
|
||||||
(gchar *) malloc((strlen(str_func(curr->data)) + 1) * sizeof(gchar));
|
(gchar *) malloc((strlen(str_func(curr->data)) + 1) * sizeof(gchar));
|
||||||
|
|
||||||
@ -206,3 +209,8 @@ static gchar * _search_from(PAutocomplete ac, GSList *curr, PStrFunc str_func)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *_str_func_default(const char *orig)
|
||||||
|
{
|
||||||
|
return orig;
|
||||||
|
}
|
||||||
|
|
||||||
|
133
test_prof_autocomplete.c
Normal file
133
test_prof_autocomplete.c
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <head-unit.h>
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
#include "contact.h"
|
||||||
|
#include "prof_autocomplete.h"
|
||||||
|
|
||||||
|
static void clear_empty(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_clear(ac, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void clear_empty_with_free_func(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reset_after_create(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_reset(ac);
|
||||||
|
p_autocomplete_clear(ac, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void find_after_create(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_complete(ac, "hello", NULL);
|
||||||
|
p_autocomplete_clear(ac, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_after_create_returns_null(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_get_list(ac, NULL);
|
||||||
|
p_autocomplete_clear(ac, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void get_after_create_with_copy_func_returns_null(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);
|
||||||
|
p_autocomplete_clear(ac, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_one_and_complete(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_add(ac, "Hello", NULL, NULL);
|
||||||
|
char *result = p_autocomplete_complete(ac, "Hel", NULL);
|
||||||
|
|
||||||
|
assert_string_equals("Hello", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_one_and_complete_with_funcs(void)
|
||||||
|
{
|
||||||
|
PContact contact = p_contact_new("James", "Online", "I'm here");
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_add(ac, contact, (PStrFunc)p_contact_name,
|
||||||
|
(GDestroyNotify)p_contact_free);
|
||||||
|
char *result = p_autocomplete_complete(ac, "Jam", (PStrFunc)p_contact_name);
|
||||||
|
|
||||||
|
assert_string_equals("James", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_two_and_complete_returns_first(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_add(ac, "Hello", NULL, NULL);
|
||||||
|
p_autocomplete_add(ac, "Help", NULL, NULL);
|
||||||
|
char *result = p_autocomplete_complete(ac, "Hel", NULL);
|
||||||
|
|
||||||
|
assert_string_equals("Hello", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_two_and_complete_returns_first_with_funcs(void)
|
||||||
|
{
|
||||||
|
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
||||||
|
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
|
||||||
|
(GDestroyNotify)p_contact_free);
|
||||||
|
p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
|
||||||
|
(GDestroyNotify)p_contact_free);
|
||||||
|
char *result = p_autocomplete_complete(ac, "Jam", (PStrFunc)p_contact_name);
|
||||||
|
|
||||||
|
assert_string_equals("James", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_two_and_complete_returns_second(void)
|
||||||
|
{
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_add(ac, "Hello", NULL, NULL);
|
||||||
|
p_autocomplete_add(ac, "Help", NULL, NULL);
|
||||||
|
char *result1 = p_autocomplete_complete(ac, "Hel", NULL);
|
||||||
|
char *result2 = p_autocomplete_complete(ac, result1, NULL);
|
||||||
|
|
||||||
|
assert_string_equals("Help", result2);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_two_and_complete_returns_second_with_funcs(void)
|
||||||
|
{
|
||||||
|
PContact contact1 = p_contact_new("James", "Online", "I'm here");
|
||||||
|
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
|
||||||
|
PAutocomplete ac = p_autocomplete_new();
|
||||||
|
p_autocomplete_add(ac, contact1, (PStrFunc)p_contact_name,
|
||||||
|
(GDestroyNotify)p_contact_free);
|
||||||
|
p_autocomplete_add(ac, contact2, (PStrFunc)p_contact_name,
|
||||||
|
(GDestroyNotify)p_contact_free);
|
||||||
|
char *result1 = p_autocomplete_complete(ac, "Jam", (PStrFunc)p_contact_name);
|
||||||
|
char *result2 = p_autocomplete_complete(ac, result1, (PStrFunc)p_contact_name);
|
||||||
|
|
||||||
|
assert_string_equals("Jamie", result2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void register_prof_autocomplete_tests(void)
|
||||||
|
{
|
||||||
|
TEST_MODULE("prof_autocomplete tests");
|
||||||
|
TEST(clear_empty);
|
||||||
|
TEST(clear_empty_with_free_func);
|
||||||
|
TEST(reset_after_create);
|
||||||
|
TEST(find_after_create);
|
||||||
|
TEST(get_after_create_returns_null);
|
||||||
|
TEST(get_after_create_with_copy_func_returns_null);
|
||||||
|
TEST(add_one_and_complete);
|
||||||
|
TEST(add_one_and_complete_with_funcs);
|
||||||
|
TEST(add_two_and_complete_returns_first);
|
||||||
|
TEST(add_two_and_complete_returns_first_with_funcs);
|
||||||
|
TEST(add_two_and_complete_returns_second);
|
||||||
|
TEST(add_two_and_complete_returns_second_with_funcs);
|
||||||
|
}
|
@ -6,6 +6,7 @@ int main(void)
|
|||||||
register_prof_history_tests();
|
register_prof_history_tests();
|
||||||
register_contact_list_tests();
|
register_contact_list_tests();
|
||||||
register_util_tests();
|
register_util_tests();
|
||||||
|
register_prof_autocomplete_tests();
|
||||||
run_suite();
|
run_suite();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,6 @@
|
|||||||
void register_prof_history_tests(void);
|
void register_prof_history_tests(void);
|
||||||
void register_contact_list_tests(void);
|
void register_contact_list_tests(void);
|
||||||
void register_util_tests(void);
|
void register_util_tests(void);
|
||||||
|
void register_prof_autocomplete_tests(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user