| OLD | NEW |
| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "base/memory/scoped_vector.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/query_parser/query_parser.h" | 9 #include "components/query_parser/query_parser.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 11 |
| 13 namespace query_parser { | 12 namespace query_parser { |
| 14 | 13 |
| 15 class QueryParserTest : public testing::Test { | 14 class QueryParserTest : public testing::Test { |
| 16 public: | 15 public: |
| 17 struct TestData { | 16 struct TestData { |
| 18 const char* input; | 17 const char* input; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 { "foo blah", "blah", false, 0, 0, 0, 0 }, | 115 { "foo blah", "blah", false, 0, 0, 0, 0 }, |
| 117 { "foo blah", "blahx foobar", true, 0, 4, 6, 9 }, | 116 { "foo blah", "blahx foobar", true, 0, 4, 6, 9 }, |
| 118 { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 }, | 117 { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 }, |
| 119 { "\"foo blah\"", "foox blahx", false, 0, 0, 0, 0 }, | 118 { "\"foo blah\"", "foox blahx", false, 0, 0, 0, 0 }, |
| 120 { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 }, | 119 { "\"foo blah\"", "foo blah", true, 0, 8, 0, 0 }, |
| 121 { "\"foo blah\"", "\"foo blah\"", true, 1, 9, 0, 0 }, | 120 { "\"foo blah\"", "\"foo blah\"", true, 1, 9, 0, 0 }, |
| 122 { "foo blah", "\"foo bar blah\"", true, 1, 4, 9, 13 }, | 121 { "foo blah", "\"foo bar blah\"", true, 1, 4, 9, 13 }, |
| 123 }; | 122 }; |
| 124 for (size_t i = 0; i < arraysize(data); ++i) { | 123 for (size_t i = 0; i < arraysize(data); ++i) { |
| 125 QueryParser parser; | 124 QueryParser parser; |
| 126 ScopedVector<QueryNode> query_nodes; | 125 query_parser::QueryNodeVector query_nodes; |
| 127 parser.ParseQueryNodes(base::UTF8ToUTF16(data[i].query), | 126 parser.ParseQueryNodes(base::UTF8ToUTF16(data[i].query), |
| 128 MatchingAlgorithm::DEFAULT, | 127 MatchingAlgorithm::DEFAULT, &query_nodes); |
| 129 &query_nodes.get()); | |
| 130 Snippet::MatchPositions match_positions; | 128 Snippet::MatchPositions match_positions; |
| 131 ASSERT_EQ(data[i].matches, | 129 ASSERT_EQ(data[i].matches, |
| 132 parser.DoesQueryMatch(base::UTF8ToUTF16(data[i].text), | 130 parser.DoesQueryMatch(base::UTF8ToUTF16(data[i].text), |
| 133 query_nodes.get(), | 131 query_nodes, &match_positions)); |
| 134 &match_positions)); | |
| 135 size_t offset = 0; | 132 size_t offset = 0; |
| 136 if (data[i].m1_start != 0 || data[i].m1_end != 0) { | 133 if (data[i].m1_start != 0 || data[i].m1_end != 0) { |
| 137 ASSERT_TRUE(match_positions.size() >= 1); | 134 ASSERT_TRUE(match_positions.size() >= 1); |
| 138 EXPECT_EQ(data[i].m1_start, match_positions[0].first); | 135 EXPECT_EQ(data[i].m1_start, match_positions[0].first); |
| 139 EXPECT_EQ(data[i].m1_end, match_positions[0].second); | 136 EXPECT_EQ(data[i].m1_end, match_positions[0].second); |
| 140 offset++; | 137 offset++; |
| 141 } | 138 } |
| 142 if (data[i].m2_start != 0 || data[i].m2_end != 0) { | 139 if (data[i].m2_start != 0 || data[i].m2_end != 0) { |
| 143 ASSERT_TRUE(match_positions.size() == 1 + offset); | 140 ASSERT_TRUE(match_positions.size() == 1 + offset); |
| 144 EXPECT_EQ(data[i].m2_start, match_positions[offset].first); | 141 EXPECT_EQ(data[i].m2_start, match_positions[offset].first); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 169 ASSERT_EQ(data[i].word_count, results.size()); | 166 ASSERT_EQ(data[i].word_count, results.size()); |
| 170 EXPECT_EQ(data[i].w1, base::UTF16ToUTF8(results[0])); | 167 EXPECT_EQ(data[i].w1, base::UTF16ToUTF8(results[0])); |
| 171 if (results.size() == 2) | 168 if (results.size() == 2) |
| 172 EXPECT_EQ(data[i].w2, base::UTF16ToUTF8(results[1])); | 169 EXPECT_EQ(data[i].w2, base::UTF16ToUTF8(results[1])); |
| 173 if (results.size() == 3) | 170 if (results.size() == 3) |
| 174 EXPECT_EQ(data[i].w3, base::UTF16ToUTF8(results[2])); | 171 EXPECT_EQ(data[i].w3, base::UTF16ToUTF8(results[2])); |
| 175 } | 172 } |
| 176 } | 173 } |
| 177 | 174 |
| 178 } // namespace query_parser | 175 } // namespace query_parser |
| OLD | NEW |