Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-expr-syntax.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright(C) 2013 Kouhei Sutou <kou@clear-code.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 #include <groonga.h>
21 
22 #include <gcutter.h>
23 
24 #include "../lib/grn-assertions.h"
25 
26 static gchar *tmp_directory;
27 static gchar *path;
28 static grn_ctx context;
29 static grn_obj *database;
30 static grn_obj escaped_query;
31 
32 void data_escape_query(void);
33 void test_escape_query(gconstpointer data);
34 
35 void
37 {
38  tmp_directory = g_build_filename(grn_test_get_tmp_dir(),
39  "test-expr-syntax",
40  NULL);
41 }
42 
43 void
45 {
46  g_free(tmp_directory);
47 }
48 
49 void
50 cut_setup(void)
51 {
52  cut_remove_path(tmp_directory, NULL);
53  g_mkdir_with_parents(tmp_directory, 0700);
54  path = g_build_filename(tmp_directory, "text-expr-syntax", NULL);
55  grn_ctx_init(&context, 0);
56  database = grn_db_create(&context, path, NULL);
57 
58  GRN_TEXT_INIT(&escaped_query, 0);
59 }
60 
61 void
63 {
64  grn_obj_close(&context, &escaped_query);
65  grn_obj_close(&context, database);
66  grn_ctx_fin(&context);
67  cut_remove_path(tmp_directory, NULL);
68  g_free(path);
69 }
70 
71 void
73 {
74 #define ADD_DATUM(label, \
75  expected, query) \
76  gcut_add_datum(label, \
77  "expected", G_TYPE_STRING, expected, \
78  "query", G_TYPE_STRING, query, \
79  NULL)
80 
81  ADD_DATUM("+", "a\\+b", "a+b");
82  ADD_DATUM("-", "a\\-b", "a-b");
83  ADD_DATUM(">", "a\\>b", "a>b");
84  ADD_DATUM("<", "a\\<b", "a<b");
85  ADD_DATUM("~", "a\\~b", "a~b");
86  ADD_DATUM("*", "a\\*b", "a*b");
87  ADD_DATUM("(", "a\\(b", "a(b");
88  ADD_DATUM(")", "a\\)b", "a)b");
89  ADD_DATUM("\"", "a\\\"b", "a\"b");
90  ADD_DATUM("\\", "a\\\\b", "a\\b");
91  ADD_DATUM(":", "a\\:b", "a:b");
92 
93 #undef ADD_DATUM
94 }
95 
96 void
97 test_escape_query(gconstpointer data)
98 {
99  const gchar *expected;
100  const gchar *query;
101 
102  query = gcut_data_get_string(data, "query");
104  query, -1,
105  &escaped_query));
106 
107  expected = gcut_data_get_string(data, "expected");
108  GRN_TEXT_PUTC(&context, &escaped_query, '\0');
109  cut_assert_equal_string(expected, GRN_TEXT_VALUE(&escaped_query));
110 }