Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
|
Mark P
2016/12/23 22:38:49
Thanks for writing tests.
| |
| 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/bookmarks/browser/titled_url_match.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/strings/utf_string_conversions.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace bookmarks { | |
|
Mark P
2016/12/23 22:38:49
I don't usually see unit tests within a namespace.
mattreynolds
2017/01/04 00:58:02
I was copying bookmark_utils_unittest.cc which use
| |
| 12 | |
| 13 using MatchPositions = TitledUrlMatch::MatchPositions; | |
| 14 | |
| 15 class TitledUrlMatchTest : public testing::Test { | |
| 16 public: | |
| 17 TitledUrlMatchTest() {} | |
| 18 | |
| 19 private: | |
| 20 DISALLOW_COPY_AND_ASSIGN(TitledUrlMatchTest); | |
| 21 }; | |
| 22 | |
| 23 TEST_F(TitledUrlMatchTest, EmptyOffsetsForEmptyMatchPositions) { | |
| 24 auto offsets = TitledUrlMatch::OffsetsFromMatchPositions(MatchPositions()); | |
| 25 EXPECT_TRUE(offsets.empty()); | |
| 26 } | |
| 27 | |
| 28 TEST_F(TitledUrlMatchTest, OffsetsFromMatchPositions) { | |
| 29 MatchPositions match_positions = { | |
| 30 {1, 3}, {4, 5}, {10, 15}, {base::string16::npos, base::string16::npos} | |
|
Mark P
2016/12/23 22:38:49
I'm a little surprised to see npos in this list of
mattreynolds
2017/01/04 00:58:02
It's valid in a MatchPosition but not as an input
| |
| 31 }; | |
| 32 std::vector<size_t> expected_offsets = { | |
| 33 1, 3, 4, 5, 10, 15, base::string16::npos, base::string16::npos | |
| 34 }; | |
| 35 auto offsets = TitledUrlMatch::OffsetsFromMatchPositions(match_positions); | |
| 36 EXPECT_TRUE( | |
| 37 std::equal(offsets.begin(), offsets.end(), expected_offsets.begin())); | |
| 38 } | |
| 39 | |
| 40 TEST_F(TitledUrlMatchTest, ReplaceOffsetsInEmptyMatchPositions) { | |
| 41 auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions( | |
| 42 MatchPositions(), std::vector<size_t>()); | |
| 43 EXPECT_TRUE(match_positions.empty()); | |
| 44 } | |
| 45 | |
| 46 TEST_F(TitledUrlMatchTest, ReplaceOffsetsInMatchPositions) { | |
| 47 MatchPositions orig_match_positions = {{1, 3}, {4, 5}, {10, 15}}; | |
| 48 std::vector<size_t> offsets = {0, 2, 3, 4, 9, 14}; | |
| 49 MatchPositions expected_match_positions = {{0, 2}, {3, 4}, {9, 14}}; | |
| 50 auto match_positions = | |
| 51 TitledUrlMatch::ReplaceOffsetsInMatchPositions(orig_match_positions, | |
| 52 offsets); | |
| 53 EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(), | |
| 54 expected_match_positions.begin())); | |
| 55 } | |
| 56 | |
| 57 TEST_F(TitledUrlMatchTest, ReplaceOffsetsRemovesItemsWithNposOffsets) { | |
|
Mark P
2016/12/23 22:38:49
Can you please include one match in here that rema
mattreynolds
2017/01/04 00:58:02
Done.
| |
| 58 MatchPositions orig_match_positions = {{1, 3}, {4, 5}, {10, 15}}; | |
| 59 std::vector<size_t> offsets = { | |
| 60 0, base::string16::npos, base::string16::npos, 4, | |
| 61 base::string16::npos, base::string16::npos, | |
| 62 }; | |
| 63 auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions( | |
| 64 orig_match_positions, offsets); | |
| 65 EXPECT_TRUE(match_positions.empty()); | |
| 66 } | |
| 67 | |
| 68 TEST_F(TitledUrlMatchTest, CorrectTitleAndMatchPositions) { | |
| 69 MatchPositions match_positions = {{2, 6}, {10, 15}}; | |
| 70 base::string16 title = base::ASCIIToUTF16(" Leading whitespace"); | |
| 71 MatchPositions expected_match_positions = {{0, 4}, {8, 13}}; | |
| 72 base::string16 expected_title = base::ASCIIToUTF16("Leading whitespace"); | |
| 73 TitledUrlMatch::CorrectTitleAndMatchPositions(&title, &match_positions); | |
| 74 EXPECT_EQ(expected_title, title); | |
| 75 EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(), | |
| 76 expected_match_positions.begin())); | |
| 77 } | |
| 78 | |
| 79 } // namespace bookmarks | |
| OLD | NEW |