about summary refs log tree commit diff stats
path: root/test_prof_autocomplete.c
blob: b03d8bb8a52b89db2dfb16682a573ee410b4da21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdlib.h>
#include <string.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();
    GSList *result = p_autocomplete_get_list(ac, NULL);

    assert_is_null(result);

    p_autocomplete_clear(ac, NULL);
}

static void get_after_create_with_copy_func_returns_null(void)
{
    PAutocomplete ac = p_autocomplete_new();
    GSList *result = p_autocomplete_get_list(ac, (PCopyFunc)p_contact_copy);

    assert_is_null(result);
    
    p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
}

static void add_one_and_complete(void)
{
    char *item = strdup("Hello");
    PAutocomplete ac = p_autocomplete_new();
    p_autocomplete_add(ac, item, NULL, NULL);
    char *result = p_autocomplete_complete(ac, "Hel", NULL);

    assert_string_equals("Hello", result);

    p_autocomplete_clear(ac, (GDestroyNotify)free);
}

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);
    
    p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
}

static void add_two_and_complete_returns_first(void)
{
    char *item1 = strdup("Hello");
    char *item2 = strdup("Help");
    PAutocomplete ac = p_autocomplete_new();
    p_autocomplete_add(ac, item1, NULL, NULL);
    p_autocomplete_add(ac, item2, NULL, NULL);
    char *result = p_autocomplete_complete(ac, "Hel", NULL);

    assert_string_equals("Hello", result);
    
    p_autocomplete_clear(ac, (GDestroyNotify)free);
}

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);
    
    p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
}

static void add_two_and_complete_returns_second(void)
{
    char *item1 = strdup("Hello");
    char *item2 = strdup("Help");
    PAutocomplete ac = p_autocomplete_new();
    p_autocomplete_add(ac, item1, NULL, NULL);
    p_autocomplete_add(ac, item2, NULL, NULL);
    char *result1 = p_autocomplete_complete(ac, "Hel", NULL);
    char *result2 = p_autocomplete_complete(ac, result1, NULL);

    assert_string_equals("Help", result2);
    
    p_autocomplete_clear(ac, (GDestroyNotify)free);
}

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);
    
    p_autocomplete_clear(ac, (GDestroyNotify)p_contact_free);
}

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);
}