Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/strings/string_util.h" | 5 #include "base/strings/string_util.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <stdarg.h> | 8 #include <stdarg.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 865 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 876 EXPECT_FALSE(EndsWith(string16(), ASCIIToUTF16(".plugin"), true)); | 876 EXPECT_FALSE(EndsWith(string16(), ASCIIToUTF16(".plugin"), true)); |
| 877 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(), false)); | 877 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(), false)); |
| 878 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(), true)); | 878 EXPECT_TRUE(EndsWith(ASCIIToUTF16("Foo.plugin"), string16(), true)); |
| 879 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), | 879 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), |
| 880 ASCIIToUTF16(".plugin"), false)); | 880 ASCIIToUTF16(".plugin"), false)); |
| 881 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), ASCIIToUTF16(".plugin"), true)); | 881 EXPECT_TRUE(EndsWith(ASCIIToUTF16(".plugin"), ASCIIToUTF16(".plugin"), true)); |
| 882 EXPECT_TRUE(EndsWith(string16(), string16(), false)); | 882 EXPECT_TRUE(EndsWith(string16(), string16(), false)); |
| 883 EXPECT_TRUE(EndsWith(string16(), string16(), true)); | 883 EXPECT_TRUE(EndsWith(string16(), string16(), true)); |
| 884 } | 884 } |
| 885 | 885 |
| 886 TEST(StringUtilTest, StdStringReplaceStringPlaceholdersWithMap) { | |
| 887 std::map<std::string, std::string> subst; | |
| 888 subst.insert(std::pair<std::string, std::string>("one", "9a")); | |
| 889 subst.insert(std::pair<std::string, std::string>("b", "8b")); | |
| 890 subst.insert(std::pair<std::string, std::string>("cc", "7c")); | |
| 891 subst.insert(std::pair<std::string, std::string>("four", "6d")); | |
| 892 subst.insert( | |
| 893 std::pair<std::string, std::string>("very long with spaces", "5e")); | |
| 894 subst.insert(std::pair<std::string, std::string>("", "4f")); | |
| 895 subst.insert(std::pair<std::string, std::string>("&", "3g")); | |
| 896 subst.insert(std::pair<std::string, std::string>("$", "2h")); | |
| 897 subst.insert(std::pair<std::string, std::string>("$$", "1i")); | |
|
Dan Beam
2015/07/02 02:10:16
this test brings up a good question: what should w
dschuyler
2015/07/07 23:05:30
Could that be addressed through style review prior
| |
| 898 | |
| 899 std::string formatted = ReplaceStringPlaceholders( | |
| 900 "${one}a,${b}b,${cc}c,${four}d,${very long with " | |
| 901 "spaces}e,${}f,${&}g,${$}h,${$$}i", | |
| 902 subst); | |
| 903 | |
| 904 EXPECT_EQ(formatted, "9aa,8bb,7cc,6dd,5ee,4ff,3gg,2hh,1ii"); | |
| 905 } | |
| 906 | |
| 907 TEST(StringUtilTest, ReplaceStringPlaceholdersConsecutiveDollarSignsWithMap) { | |
| 908 std::map<std::string, std::string> subst; | |
| 909 subst.insert(std::pair<std::string, std::string>("a", "9a")); | |
| 910 subst.insert(std::pair<std::string, std::string>("b", "8b")); | |
| 911 subst.insert(std::pair<std::string, std::string>("c", "7c")); | |
| 912 EXPECT_EQ(ReplaceStringPlaceholders("$${a} $$${b} $$$${c} $$", subst), | |
| 913 "${a} $${b} $$${c} $"); | |
| 914 EXPECT_EQ(ReplaceStringPlaceholders("$$", subst), "$"); | |
| 915 EXPECT_EQ(ReplaceStringPlaceholders("$", subst), ""); | |
| 916 } | |
| 917 | |
| 886 TEST(StringUtilTest, GetStringFWithOffsets) { | 918 TEST(StringUtilTest, GetStringFWithOffsets) { |
| 887 std::vector<string16> subst; | 919 std::vector<string16> subst; |
| 888 subst.push_back(ASCIIToUTF16("1")); | 920 subst.push_back(ASCIIToUTF16("1")); |
| 889 subst.push_back(ASCIIToUTF16("2")); | 921 subst.push_back(ASCIIToUTF16("2")); |
| 890 std::vector<size_t> offsets; | 922 std::vector<size_t> offsets; |
| 891 | 923 |
| 892 ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $1. Your number is $2."), | 924 ReplaceStringPlaceholders(ASCIIToUTF16("Hello, $1. Your number is $2."), |
| 893 subst, | 925 subst, |
| 894 &offsets); | 926 &offsets); |
| 895 EXPECT_EQ(2U, offsets.size()); | 927 EXPECT_EQ(2U, offsets.size()); |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1226 const std::string live = kLive; | 1258 const std::string live = kLive; |
| 1227 std::string dead = live; | 1259 std::string dead = live; |
| 1228 strncpy(WriteInto(&dead, 5), kDead, 4); | 1260 strncpy(WriteInto(&dead, 5), kDead, 4); |
| 1229 EXPECT_EQ(kDead, dead); | 1261 EXPECT_EQ(kDead, dead); |
| 1230 EXPECT_EQ(4u, dead.size()); | 1262 EXPECT_EQ(4u, dead.size()); |
| 1231 EXPECT_EQ(kLive, live); | 1263 EXPECT_EQ(kLive, live); |
| 1232 EXPECT_EQ(4u, live.size()); | 1264 EXPECT_EQ(4u, live.size()); |
| 1233 } | 1265 } |
| 1234 | 1266 |
| 1235 } // namespace base | 1267 } // namespace base |
| OLD | NEW |