1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/url_pattern_index/string_splitter.h"
6 
7 #include <string>
8 #include <vector>
9 
10 #include "base/strings/string_piece.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 
13 namespace url_pattern_index {
14 
15 namespace {
IsTestSeparator(char c)16 bool IsTestSeparator(char c) {
17   return c == ' ' || c == '\t' || c == ',';
18 }
19 }
20 
TEST(StringSplitterTest,SplitWithEmptyResult)21 TEST(StringSplitterTest, SplitWithEmptyResult) {
22   const char* const kStrings[] = {
23       "", " ", "\t", ",", " \t ", ",,,,", "\t\t\t",
24   };
25 
26   for (const char* string : kStrings) {
27     auto splitter = CreateStringSplitter(string, IsTestSeparator);
28     // Explicitly verify both operator== and operator!=.
29     EXPECT_TRUE(splitter.begin() == splitter.end());
30     EXPECT_FALSE(splitter.begin() != splitter.end());
31   }
32 }
33 
TEST(StringSplitterTest,SplitOneWord)34 TEST(StringSplitterTest, SplitOneWord) {
35   const char* const kLongStrings[] = {
36       "word",     " word ",   " word",   "word ",   ",word,",
37       "\tword\t", "  word  ", "word   ", "   word", ", word, \t",
38   };
39   const char* const kShortStrings[] = {
40       "w", " w ", " w", "w ", "  w  ", "  w", "w  ", ", w, ", "w, \t",
41   };
42 
43   const char kLongWord[] = "word";
44   const char kShortWord[] = "w";
45 
46   auto expect_word = [](const char* text, const char* word) {
47     auto splitter = CreateStringSplitter(text, IsTestSeparator);
48     // Explicitly verify both operator== and operator!=.
49     EXPECT_TRUE(splitter.begin() != splitter.end());
50     EXPECT_FALSE(splitter.begin() == splitter.end());
51 
52     EXPECT_EQ(splitter.end(), ++splitter.begin());
53     EXPECT_EQ(word, *splitter.begin());
54 
55     auto iterator = splitter.begin();
56     EXPECT_EQ(splitter.begin(), iterator++);
57     EXPECT_EQ(splitter.end(), iterator);
58   };
59 
60   for (const char* string : kLongStrings)
61     expect_word(string, kLongWord);
62   for (const char* string : kShortStrings)
63     expect_word(string, kShortWord);
64 }
65 
TEST(StringSplitterTest,SplitThreeWords)66 TEST(StringSplitterTest, SplitThreeWords) {
67   const char* const kStrings[] = {
68       "one two three",     " one two three ",   "   one  two, three",
69       "one,two\t\t three", "one, two, three, ",
70   };
71   const std::vector<base::StringPiece> kResults = {
72       "one", "two", "three",
73   };
74 
75   for (const char* string : kStrings) {
76     auto splitter = CreateStringSplitter(string, IsTestSeparator);
77     std::vector<base::StringPiece> tokens(splitter.begin(), splitter.end());
78     EXPECT_EQ(kResults, tokens);
79   }
80 }
81 
82 }  // namespace url_pattern_index
83