| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include "absl/strings/match.h" |
| 16 |
| 17 | #include "gtest/gtest.h" |
| 18 |
| 19 | namespace { |
| 20 |
| 21 | TEST(MatchTest, StartsWith) { |
| 22 | const std::string s1("123\0abc", 7); |
| 23 | const absl::string_view a("foobar"); |
| 24 | const absl::string_view b(s1); |
| 25 | const absl::string_view e; |
| 26 | EXPECT_TRUE(absl::StartsWith(a, a)); |
| 27 | EXPECT_TRUE(absl::StartsWith(a, "foo")); |
| 28 | EXPECT_TRUE(absl::StartsWith(a, e)); |
| 29 | EXPECT_TRUE(absl::StartsWith(b, s1)); |
| 30 | EXPECT_TRUE(absl::StartsWith(b, b)); |
| 31 | EXPECT_TRUE(absl::StartsWith(b, e)); |
| 32 | EXPECT_TRUE(absl::StartsWith(e, "")); |
| 33 | EXPECT_FALSE(absl::StartsWith(a, b)); |
| 34 | EXPECT_FALSE(absl::StartsWith(b, a)); |
| 35 | EXPECT_FALSE(absl::StartsWith(e, a)); |
| 36 | } |
| 37 |
| 38 | TEST(MatchTest, EndsWith) { |
| 39 | const std::string s1("123\0abc", 7); |
| 40 | const absl::string_view a("foobar"); |
| 41 | const absl::string_view b(s1); |
| 42 | const absl::string_view e; |
| 43 | EXPECT_TRUE(absl::EndsWith(a, a)); |
| 44 | EXPECT_TRUE(absl::EndsWith(a, "bar")); |
| 45 | EXPECT_TRUE(absl::EndsWith(a, e)); |
| 46 | EXPECT_TRUE(absl::EndsWith(b, s1)); |
| 47 | EXPECT_TRUE(absl::EndsWith(b, b)); |
| 48 | EXPECT_TRUE(absl::EndsWith(b, e)); |
| 49 | EXPECT_TRUE(absl::EndsWith(e, "")); |
| 50 | EXPECT_FALSE(absl::EndsWith(a, b)); |
| 51 | EXPECT_FALSE(absl::EndsWith(b, a)); |
| 52 | EXPECT_FALSE(absl::EndsWith(e, a)); |
| 53 | } |
| 54 |
| 55 | TEST(MatchTest, Contains) { |
| 56 | absl::string_view a("abcdefg"); |
| 57 | absl::string_view b("abcd"); |
| 58 | absl::string_view c("efg"); |
| 59 | absl::string_view d("gh"); |
| 60 | EXPECT_TRUE(absl::StrContains(a, a)); |
| 61 | EXPECT_TRUE(absl::StrContains(a, b)); |
| 62 | EXPECT_TRUE(absl::StrContains(a, c)); |
| 63 | EXPECT_FALSE(absl::StrContains(a, d)); |
| 64 | EXPECT_TRUE(absl::StrContains("", "")); |
| 65 | EXPECT_TRUE(absl::StrContains("abc", "")); |
| 66 | EXPECT_FALSE(absl::StrContains("", "a")); |
| 67 | } |
| 68 |
| 69 | TEST(MatchTest, ContainsChar) { |
| 70 | absl::string_view a("abcdefg"); |
| 71 | absl::string_view b("abcd"); |
| 72 | EXPECT_TRUE(absl::StrContains(a, 'a')); |
| 73 | EXPECT_TRUE(absl::StrContains(a, 'b')); |
| 74 | EXPECT_TRUE(absl::StrContains(a, 'e')); |
| 75 | EXPECT_FALSE(absl::StrContains(a, 'h')); |
| 76 |
| 77 | EXPECT_TRUE(absl::StrContains(b, 'a')); |
| 78 | EXPECT_TRUE(absl::StrContains(b, 'b')); |
| 79 | EXPECT_FALSE(absl::StrContains(b, 'e')); |
| 80 | EXPECT_FALSE(absl::StrContains(b, 'h')); |
| 81 |
| 82 | EXPECT_FALSE(absl::StrContains("", 'a')); |
| 83 | EXPECT_FALSE(absl::StrContains("", 'a')); |
| 84 | } |
| 85 |
| 86 | TEST(MatchTest, ContainsNull) { |
| 87 | const std::string s = "foo"; |
| 88 | const char* cs = "foo"; |
| 89 | const absl::string_view sv("foo"); |
| 90 | const absl::string_view sv2("foo\0bar", 4); |
| 91 | EXPECT_EQ(s, "foo"); |
| 92 | EXPECT_EQ(sv, "foo"); |
| 93 | EXPECT_NE(sv2, "foo"); |
| 94 | EXPECT_TRUE(absl::EndsWith(s, sv)); |
| 95 | EXPECT_TRUE(absl::StartsWith(cs, sv)); |
| 96 | EXPECT_TRUE(absl::StrContains(cs, sv)); |
| 97 | EXPECT_FALSE(absl::StrContains(cs, sv2)); |
| 98 | } |
| 99 |
| 100 | TEST(MatchTest, EqualsIgnoreCase) { |
| 101 | std::string text = "the"; |
| 102 | absl::string_view data(text); |
| 103 |
| 104 | EXPECT_TRUE(absl::EqualsIgnoreCase(data, "The")); |
| 105 | EXPECT_TRUE(absl::EqualsIgnoreCase(data, "THE")); |
| 106 | EXPECT_TRUE(absl::EqualsIgnoreCase(data, "the")); |
| 107 | EXPECT_FALSE(absl::EqualsIgnoreCase(data, "Quick")); |
| 108 | EXPECT_FALSE(absl::EqualsIgnoreCase(data, "then")); |
| 109 | } |
| 110 |
| 111 | TEST(MatchTest, StartsWithIgnoreCase) { |
| 112 | EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "foo")); |
| 113 | EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "Fo")); |
| 114 | EXPECT_TRUE(absl::StartsWithIgnoreCase("foo", "")); |
| 115 | EXPECT_FALSE(absl::StartsWithIgnoreCase("foo", "fooo")); |
| 116 | EXPECT_FALSE(absl::StartsWithIgnoreCase("", "fo")); |
| 117 | } |
| 118 |
| 119 | TEST(MatchTest, EndsWithIgnoreCase) { |
| 120 | EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "foo")); |
| 121 | EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "Oo")); |
| 122 | EXPECT_TRUE(absl::EndsWithIgnoreCase("foo", "")); |
| 123 | EXPECT_FALSE(absl::EndsWithIgnoreCase("foo", "fooo")); |
| 124 | EXPECT_FALSE(absl::EndsWithIgnoreCase("", "fo")); |
| 125 | } |
| 126 |
| 127 | } // namespace |
| 128 |