Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(311)

Side by Side Diff: components/query_parser/query_parser_unittest.cc

Issue 701553002: Allow systematic prefix search in bookmarks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/memory/scoped_vector.h" 6 #include "base/memory/scoped_vector.h"
7 #include "base/strings/utf_string_conversions.h" 7 #include "base/strings/utf_string_conversions.h"
8 #include "components/query_parser/query_parser.h" 8 #include "components/query_parser/query_parser.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace query_parser { 11 namespace query_parser {
12 12
13 class QueryParserTest : public testing::Test { 13 class QueryParserTest : public testing::Test {
14 public: 14 public:
15 struct TestData { 15 struct TestData {
16 const char* input; 16 const char* input;
17 const int expected_word_count; 17 const int expected_word_count;
18 }; 18 };
19 19
20 std::string QueryToString(const std::string& query); 20 std::string QueryToString(const std::string& query);
21 21
22 protected: 22 protected:
23 QueryParser query_parser_; 23 QueryParser query_parser_;
24 }; 24 };
25 25
26 // Test helper: Convert a user query string in 8-bit (for hardcoding 26 // Test helper: Convert a user query string in 8-bit (for hardcoding
27 // convenience) to a SQLite query string. 27 // convenience) to a SQLite query string.
28 std::string QueryParserTest::QueryToString(const std::string& query) { 28 std::string QueryParserTest::QueryToString(const std::string& query) {
29 base::string16 sqlite_query; 29 base::string16 sqlite_query;
30 query_parser_.ParseQuery(base::UTF8ToUTF16(query), &sqlite_query); 30 query_parser_.ParseQuery(base::UTF8ToUTF16(query),
31 false /* always_prefix_search */,
32 &sqlite_query);
31 return base::UTF16ToUTF8(sqlite_query); 33 return base::UTF16ToUTF8(sqlite_query);
32 } 34 }
33 35
34 // Basic multi-word queries, including prefix matching. 36 // Basic multi-word queries, including prefix matching.
35 TEST_F(QueryParserTest, SimpleQueries) { 37 TEST_F(QueryParserTest, SimpleQueries) {
36 EXPECT_EQ("", QueryToString(" ")); 38 EXPECT_EQ("", QueryToString(" "));
37 EXPECT_EQ("singleword*", QueryToString("singleword")); 39 EXPECT_EQ("singleword*", QueryToString("singleword"));
38 EXPECT_EQ("spacedout*", QueryToString(" spacedout ")); 40 EXPECT_EQ("spacedout*", QueryToString(" spacedout "));
39 EXPECT_EQ("foo* bar*", QueryToString("foo bar")); 41 EXPECT_EQ("foo* bar*", QueryToString("foo bar"));
40 // Short words aren't prefix matches. For Korean Hangul 42 // Short words aren't prefix matches. For Korean Hangul
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 { "blah", 1 }, 80 { "blah", 1 },
79 { "foo \"bar baz\"", 3 }, 81 { "foo \"bar baz\"", 3 },
80 { "foo \"baz\"", 2 }, 82 { "foo \"baz\"", 2 },
81 { "foo \"bar baz\" blah", 4 }, 83 { "foo \"bar baz\" blah", 4 },
82 }; 84 };
83 85
84 for (size_t i = 0; i < arraysize(data); ++i) { 86 for (size_t i = 0; i < arraysize(data); ++i) {
85 base::string16 query_string; 87 base::string16 query_string;
86 EXPECT_EQ(data[i].expected_word_count, 88 EXPECT_EQ(data[i].expected_word_count,
87 query_parser_.ParseQuery(base::UTF8ToUTF16(data[i].input), 89 query_parser_.ParseQuery(base::UTF8ToUTF16(data[i].input),
90 false, /* always_prefix_search */
88 &query_string)); 91 &query_string));
89 } 92 }
90 } 93 }
91 94
92 TEST_F(QueryParserTest, ParseQueryNodesAndMatch) { 95 TEST_F(QueryParserTest, ParseQueryNodesAndMatch) {
93 struct TestData2 { 96 struct TestData2 {
94 const std::string query; 97 const std::string query;
95 const std::string text; 98 const std::string text;
96 const bool matches; 99 const bool matches;
97 const size_t m1_start; 100 const size_t m1_start;
(...skipping 15 matching lines...) Expand all
113 { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 }, 116 { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 },
114 { "\"foo blah\"", "foox blahx", false, 0, 0, 0, 0 }, 117 { "\"foo blah\"", "foox blahx", false, 0, 0, 0, 0 },
115 { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 }, 118 { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 },
116 { "\"foo blah\"", "\"foo blah\"", true, 1, 9, 0, 0 }, 119 { "\"foo blah\"", "\"foo blah\"", true, 1, 9, 0, 0 },
117 { "foo blah", "\"foo bar blah\"", true, 1, 4, 9, 13 }, 120 { "foo blah", "\"foo bar blah\"", true, 1, 4, 9, 13 },
118 }; 121 };
119 for (size_t i = 0; i < arraysize(data); ++i) { 122 for (size_t i = 0; i < arraysize(data); ++i) {
120 QueryParser parser; 123 QueryParser parser;
121 ScopedVector<QueryNode> query_nodes; 124 ScopedVector<QueryNode> query_nodes;
122 parser.ParseQueryNodes(base::UTF8ToUTF16(data[i].query), 125 parser.ParseQueryNodes(base::UTF8ToUTF16(data[i].query),
126 false, /* always_prefix_search */
123 &query_nodes.get()); 127 &query_nodes.get());
124 Snippet::MatchPositions match_positions; 128 Snippet::MatchPositions match_positions;
125 ASSERT_EQ(data[i].matches, 129 ASSERT_EQ(data[i].matches,
126 parser.DoesQueryMatch(base::UTF8ToUTF16(data[i].text), 130 parser.DoesQueryMatch(base::UTF8ToUTF16(data[i].text),
127 query_nodes.get(), 131 query_nodes.get(),
128 &match_positions)); 132 &match_positions));
129 size_t offset = 0; 133 size_t offset = 0;
130 if (data[i].m1_start != 0 || data[i].m1_end != 0) { 134 if (data[i].m1_start != 0 || data[i].m1_end != 0) {
131 ASSERT_TRUE(match_positions.size() >= 1); 135 ASSERT_TRUE(match_positions.size() >= 1);
132 EXPECT_EQ(data[i].m1_start, match_positions[0].first); 136 EXPECT_EQ(data[i].m1_start, match_positions[0].first);
(...skipping 17 matching lines...) Expand all
150 const size_t word_count; 154 const size_t word_count;
151 } data[] = { 155 } data[] = {
152 { "foo", "foo", "", "", 1 }, 156 { "foo", "foo", "", "", 1 },
153 { "foo bar", "foo", "bar", "", 2 }, 157 { "foo bar", "foo", "bar", "", 2 },
154 { "\"foo bar\"", "foo", "bar", "", 2 }, 158 { "\"foo bar\"", "foo", "bar", "", 2 },
155 { "\"foo bar\" a", "foo", "bar", "a", 3 }, 159 { "\"foo bar\" a", "foo", "bar", "a", 3 },
156 }; 160 };
157 for (size_t i = 0; i < arraysize(data); ++i) { 161 for (size_t i = 0; i < arraysize(data); ++i) {
158 std::vector<base::string16> results; 162 std::vector<base::string16> results;
159 QueryParser parser; 163 QueryParser parser;
160 parser.ParseQueryWords(base::UTF8ToUTF16(data[i].text), &results); 164 parser.ParseQueryWords(base::UTF8ToUTF16(data[i].text),
165 false /* always_prefix_search */,
166 &results);
161 ASSERT_EQ(data[i].word_count, results.size()); 167 ASSERT_EQ(data[i].word_count, results.size());
162 EXPECT_EQ(data[i].w1, base::UTF16ToUTF8(results[0])); 168 EXPECT_EQ(data[i].w1, base::UTF16ToUTF8(results[0]));
163 if (results.size() == 2) 169 if (results.size() == 2)
164 EXPECT_EQ(data[i].w2, base::UTF16ToUTF8(results[1])); 170 EXPECT_EQ(data[i].w2, base::UTF16ToUTF8(results[1]));
165 if (results.size() == 3) 171 if (results.size() == 3)
166 EXPECT_EQ(data[i].w3, base::UTF16ToUTF8(results[2])); 172 EXPECT_EQ(data[i].w3, base::UTF16ToUTF8(results[2]));
167 } 173 }
168 } 174 }
169 175
170 } // namespace query_parser 176 } // namespace query_parser
OLDNEW
« components/query_parser/query_parser.cc ('K') | « components/query_parser/query_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698