Commits

Anonymous committed 9412666

TOOLS: Add unit test for ReplaceAll().

Patch contributed by pmezard.

  • Participants
  • Parent commits bc42c91
  • Branches default

Comments (0)

Files changed (3)

File tools/cpp/src/cpp-build/generate_geocoding_data.cc

 }
 
 // Returns a copy of input where all occurences of pattern are replaced with
-// value.
+// value. If pattern is empty, input is returned unchanged.
 string ReplaceAll(const string& input, const string& pattern,
                   const string& value) {
+  if (pattern.size() == 0) {
+    return input;
+  }
   string replaced;
   std::back_insert_iterator<string> output = std::back_inserter(replaced);
   string::const_iterator begin = input.begin(), end = begin;

File tools/cpp/src/cpp-build/generate_geocoding_data.h

 
 string MakeStringLiteral(const string& s);
 
+string ReplaceAll(const string& input, const string& pattern,
+                  const string& value);
+
 int Main(int argc, const char* argv[]);
 
 }  // namespace phonenumbers

File tools/cpp/test/cpp-build/generate_geocoding_data_test.cc

             MakeStringLiteral("Op\xc3\xa9ra"));
 }
 
+TEST(GenerateGeocodingDataTest, TestReplaceAll) {
+  EXPECT_EQ("", ReplaceAll("", "$input$", "cc"));
+  EXPECT_EQ("accb", ReplaceAll("a$input$b", "$input$", "cc"));
+  EXPECT_EQ("ab", ReplaceAll("a$input$b", "$input$", ""));
+  EXPECT_EQ("ab", ReplaceAll("ab", "", "cc"));
+  EXPECT_EQ("acdc", ReplaceAll("a$input$d$input$", "$input$", "c"));
+}
+
 }  // namespace phonenumbers
 }  // namespace i18n