Fix whitespace irregularities in code
[xapian.git] / xapian-core / tests / api_qpbackend.cc
blob96c2c61ecafc4b8459f6fb7bbf0cdfacdfd6ccc6
1 /** @file api_qpbackend.cc
2 * @brief QueryParser tests which need a backend
3 */
4 /* Copyright (c) 2009,2015 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
22 #include <config.h>
24 #include "api_qpbackend.h"
26 #include <xapian.h>
28 #include "backendmanager.h"
29 #include "testsuite.h"
30 #include "testutils.h"
32 #include "apitest.h"
34 using namespace std;
36 struct test {
37 const char *query;
38 const char *expect;
41 /// Regression test for bug#407 fixed in 1.0.17 and 1.1.3.
42 DEFINE_TESTCASE(qpsynonympartial1, synonyms) {
43 static const test test_queries[] = {
44 { "hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
45 { "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
46 { "hello world", "(hello@1 OR ((SYNONYM WILDCARD OR world) OR world@2))" },
47 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
48 { "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
49 { NULL, NULL }
51 static const test test_queries_auto[] = {
52 { "hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
53 { "~hello", "(hello@1 SYNONYM hi@1 SYNONYM howdy@1)" },
54 { "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
55 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR world@2)" },
56 { "world ~hello", "(world@1 OR (hello@2 SYNONYM hi@2 SYNONYM howdy@2))" },
57 { NULL, NULL }
59 static const test test_queries_partial_auto[] = {
60 { "hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
61 { "~hello", "((SYNONYM WILDCARD OR hello) OR hello@1)" },
62 { "hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
63 { "~hello world", "((hello@1 SYNONYM hi@1 SYNONYM howdy@1) OR ((SYNONYM WILDCARD OR world) OR world@2))" },
64 { "world ~hello", "(world@1 OR ((SYNONYM WILDCARD OR hello) OR hello@2))" },
65 { NULL, NULL }
68 Xapian::WritableDatabase db(get_writable_database());
69 db.add_synonym("hello", "hi");
70 db.add_synonym("hello", "howdy");
71 db.commit();
73 Xapian::Enquire enquire(db);
74 Xapian::QueryParser qp;
75 Xapian::Stem stemmmer("english");
76 qp.set_database(db);
77 qp.add_prefix("foo", "XFOO");
79 const test *p;
80 for (p = test_queries; p->query; ++p) {
81 string expect, parsed;
82 if (p->expect)
83 expect = p->expect;
84 else
85 expect = "parse error";
86 try {
87 unsigned f = qp.FLAG_SYNONYM | qp.FLAG_PARTIAL | qp.FLAG_DEFAULT;
88 Xapian::Query qobj = qp.parse_query(p->query, f);
89 parsed = qobj.get_description();
90 expect = string("Query(") + expect + ')';
91 } catch (const Xapian::QueryParserError &e) {
92 parsed = e.get_msg();
93 } catch (const Xapian::Error &e) {
94 parsed = e.get_description();
95 } catch (...) {
96 parsed = "Unknown exception!";
98 tout << "Query: " << p->query << '\n';
99 TEST_STRINGS_EQUAL(parsed, expect);
102 for (p = test_queries_auto; p->query; ++p) {
103 string expect, parsed;
104 if (p->expect)
105 expect = p->expect;
106 else
107 expect = "parse error";
108 try {
109 unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_DEFAULT;
110 Xapian::Query qobj = qp.parse_query(p->query, f);
111 parsed = qobj.get_description();
112 expect = string("Query(") + expect + ')';
113 } catch (const Xapian::QueryParserError &e) {
114 parsed = e.get_msg();
115 } catch (const Xapian::Error &e) {
116 parsed = e.get_description();
117 } catch (...) {
118 parsed = "Unknown exception!";
120 tout << "Query: " << p->query << '\n';
121 TEST_STRINGS_EQUAL(parsed, expect);
124 for (p = test_queries_partial_auto; p->query; ++p) {
125 string expect, parsed;
126 if (p->expect)
127 expect = p->expect;
128 else
129 expect = "parse error";
130 try {
131 unsigned f = qp.FLAG_AUTO_SYNONYMS | qp.FLAG_PARTIAL;
132 f |= qp.FLAG_DEFAULT;
133 Xapian::Query qobj = qp.parse_query(p->query, f);
134 parsed = qobj.get_description();
135 expect = string("Query(") + expect + ')';
136 } catch (const Xapian::QueryParserError &e) {
137 parsed = e.get_msg();
138 } catch (const Xapian::Error &e) {
139 parsed = e.get_description();
140 } catch (...) {
141 parsed = "Unknown exception!";
143 tout << "Query: " << p->query << '\n';
144 TEST_STRINGS_EQUAL(parsed, expect);
147 return true;