1 /*
2 * Copyright (C) 2011 Canonical Ltd
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by
17 * Mikkel Kamstrup Erlandsen <mikkel.kamstrup@canonical.com>
18 *
19 */
20
21 #include <glib.h>
22 #include <glib-object.h>
23 #include <dee.h>
24
25 typedef struct
26 {
27 DeeAnalyzer *analyzer;
28 DeeTermList *terms;
29 } Fixture;
30
31 static void setup (Fixture *fix, gconstpointer data);
32 static void text_setup (Fixture *fix, gconstpointer data);
33 static void teardown (Fixture *fix, gconstpointer data);
34
35 static void
setup(Fixture * fix,gconstpointer data)36 setup (Fixture *fix, gconstpointer data)
37 {
38 fix->analyzer = dee_analyzer_new ();
39 fix->terms = g_object_new (DEE_TYPE_TERM_LIST, NULL);
40 }
41
42 static void
text_setup(Fixture * fix,gconstpointer data)43 text_setup (Fixture *fix, gconstpointer data)
44 {
45 fix->analyzer = DEE_ANALYZER (dee_text_analyzer_new ());
46 fix->terms = g_object_new (DEE_TYPE_TERM_LIST, NULL);
47 }
48
49 static void
teardown(Fixture * fix,gconstpointer data)50 teardown (Fixture *fix, gconstpointer data)
51 {
52 g_object_unref (fix->analyzer);
53 g_object_unref (fix->terms);
54 fix->analyzer = NULL;
55 fix->terms = NULL;
56 }
57
58 static void
test_simple(Fixture * fix,gconstpointer data)59 test_simple (Fixture *fix, gconstpointer data)
60 {
61 dee_analyzer_tokenize (fix->analyzer, "tok", fix->terms);
62 g_assert_cmpint (dee_term_list_num_terms (fix->terms), ==, 1);
63 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 0), ==, "tok");
64 dee_term_list_clear (fix->terms);
65
66 dee_analyzer_tokenize (fix->analyzer, "tok kot", fix->terms);
67 g_assert_cmpint (dee_term_list_num_terms (fix->terms), ==, 1);
68 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 0), ==, "tok kot");
69 dee_term_list_clear (fix->terms);
70
71 dee_analyzer_analyze (fix->analyzer, "foobar", fix->terms, NULL);
72 g_assert_cmpint (dee_term_list_num_terms (fix->terms), ==, 1);
73 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 0), ==, "foobar");
74 }
75
76 void
_casefold(DeeTermList * in,DeeTermList * out,gpointer data)77 _casefold (DeeTermList *in, DeeTermList *out, gpointer data)
78 {
79 int i;
80 gchar *fold;
81
82 for (i = 0; i < dee_term_list_num_terms (in); i++)
83 {
84 fold = g_utf8_casefold (dee_term_list_get_term (in, i), -1);
85 dee_term_list_add_term (out, fold);
86 g_free (fold);
87 }
88 }
89
90 static void
test_term_filter1(Fixture * fix,gconstpointer data)91 test_term_filter1 (Fixture *fix, gconstpointer data)
92 {
93 dee_analyzer_analyze (fix->analyzer, "foobar", fix->terms, NULL);
94 g_assert_cmpint (dee_term_list_num_terms (fix->terms), ==, 1);
95 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 0), ==, "foobar");
96 dee_term_list_clear (fix->terms);
97
98 dee_analyzer_add_term_filter(fix->analyzer, _casefold, NULL, NULL);
99
100 dee_analyzer_analyze (fix->analyzer, "FooBar", fix->terms, NULL);
101 g_assert_cmpint (dee_term_list_num_terms (fix->terms), ==, 1);
102 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 0), ==, "foobar");
103 dee_term_list_clear (fix->terms);
104 }
105
106 void
test_text_analyzer_simple(Fixture * fix,gconstpointer data)107 test_text_analyzer_simple (Fixture *fix, gconstpointer data)
108 {
109 dee_analyzer_analyze (fix->analyzer, "foobar", fix->terms, NULL);
110 g_assert_cmpint (dee_term_list_num_terms (fix->terms), ==, 1);
111 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 0), ==, "foobar");
112 dee_term_list_clear (fix->terms);
113
114 dee_analyzer_analyze (fix->analyzer, "FooBar ", fix->terms, NULL);
115 g_assert_cmpint (dee_term_list_num_terms (fix->terms), ==, 1);
116 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 0), ==, "foobar");
117 dee_term_list_clear (fix->terms);
118
119 dee_analyzer_analyze (fix->analyzer, "foo baR", fix->terms, NULL);
120 g_assert_cmpint (dee_term_list_num_terms (fix->terms), ==, 2);
121 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 0), ==, "foo");
122 g_assert_cmpstr (dee_term_list_get_term (fix->terms, 1), ==, "bar");
123 dee_term_list_clear (fix->terms);
124 }
125
126 void
test_analyzer_create_suite(void)127 test_analyzer_create_suite (void)
128 {
129 g_test_add ("/Index/Analyzer/Simple", Fixture, 0,
130 setup, test_simple, teardown);
131
132 g_test_add ("/Index/Analyzer/TermFilter1", Fixture, 0,
133 setup, test_term_filter1, teardown);
134
135 g_test_add ("/Index/TextAnalyzer/Simple", Fixture, 0,
136 text_setup, test_text_analyzer_simple, teardown);
137 }
138