Chromium Code Reviews| Index: components/bookmarks/browser/titled_url_match_unittest.cc |
| diff --git a/components/bookmarks/browser/titled_url_match_unittest.cc b/components/bookmarks/browser/titled_url_match_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4829e6ad1e32b8478389e9964fc91b45e18c1cca |
| --- /dev/null |
| +++ b/components/bookmarks/browser/titled_url_match_unittest.cc |
| @@ -0,0 +1,79 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
|
Mark P
2016/12/23 22:38:49
Thanks for writing tests.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/bookmarks/browser/titled_url_match.h" |
| + |
| +#include "base/macros.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +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
|
| + |
| +using MatchPositions = TitledUrlMatch::MatchPositions; |
| + |
| +class TitledUrlMatchTest : public testing::Test { |
| + public: |
| + TitledUrlMatchTest() {} |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TitledUrlMatchTest); |
| +}; |
| + |
| +TEST_F(TitledUrlMatchTest, EmptyOffsetsForEmptyMatchPositions) { |
| + auto offsets = TitledUrlMatch::OffsetsFromMatchPositions(MatchPositions()); |
| + EXPECT_TRUE(offsets.empty()); |
| +} |
| + |
| +TEST_F(TitledUrlMatchTest, OffsetsFromMatchPositions) { |
| + MatchPositions match_positions = { |
| + {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
|
| + }; |
| + std::vector<size_t> expected_offsets = { |
| + 1, 3, 4, 5, 10, 15, base::string16::npos, base::string16::npos |
| + }; |
| + auto offsets = TitledUrlMatch::OffsetsFromMatchPositions(match_positions); |
| + EXPECT_TRUE( |
| + std::equal(offsets.begin(), offsets.end(), expected_offsets.begin())); |
| +} |
| + |
| +TEST_F(TitledUrlMatchTest, ReplaceOffsetsInEmptyMatchPositions) { |
| + auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions( |
| + MatchPositions(), std::vector<size_t>()); |
| + EXPECT_TRUE(match_positions.empty()); |
| +} |
| + |
| +TEST_F(TitledUrlMatchTest, ReplaceOffsetsInMatchPositions) { |
| + MatchPositions orig_match_positions = {{1, 3}, {4, 5}, {10, 15}}; |
| + std::vector<size_t> offsets = {0, 2, 3, 4, 9, 14}; |
| + MatchPositions expected_match_positions = {{0, 2}, {3, 4}, {9, 14}}; |
| + auto match_positions = |
| + TitledUrlMatch::ReplaceOffsetsInMatchPositions(orig_match_positions, |
| + offsets); |
| + EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(), |
| + expected_match_positions.begin())); |
| +} |
| + |
| +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.
|
| + MatchPositions orig_match_positions = {{1, 3}, {4, 5}, {10, 15}}; |
| + std::vector<size_t> offsets = { |
| + 0, base::string16::npos, base::string16::npos, 4, |
| + base::string16::npos, base::string16::npos, |
| + }; |
| + auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions( |
| + orig_match_positions, offsets); |
| + EXPECT_TRUE(match_positions.empty()); |
| +} |
| + |
| +TEST_F(TitledUrlMatchTest, CorrectTitleAndMatchPositions) { |
| + MatchPositions match_positions = {{2, 6}, {10, 15}}; |
| + base::string16 title = base::ASCIIToUTF16(" Leading whitespace"); |
| + MatchPositions expected_match_positions = {{0, 4}, {8, 13}}; |
| + base::string16 expected_title = base::ASCIIToUTF16("Leading whitespace"); |
| + TitledUrlMatch::CorrectTitleAndMatchPositions(&title, &match_positions); |
| + EXPECT_EQ(expected_title, title); |
| + EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(), |
| + expected_match_positions.begin())); |
| +} |
| + |
| +} // namespace bookmarks |