titled_url_match_unittest.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/bookmarks/browser/titled_url_match.h"
  5. #include "base/strings/utf_string_conversions.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace bookmarks {
  8. using MatchPositions = TitledUrlMatch::MatchPositions;
  9. TEST(TitledUrlMatchTest, EmptyOffsetsForEmptyMatchPositions) {
  10. auto offsets = TitledUrlMatch::OffsetsFromMatchPositions(MatchPositions());
  11. EXPECT_TRUE(offsets.empty());
  12. }
  13. TEST(TitledUrlMatchTest, OffsetsFromMatchPositions) {
  14. MatchPositions match_positions = {{1, 3}, {4, 5}, {10, 15}};
  15. std::vector<size_t> expected_offsets = {1, 3, 4, 5, 10, 15};
  16. auto offsets = TitledUrlMatch::OffsetsFromMatchPositions(match_positions);
  17. EXPECT_TRUE(
  18. std::equal(offsets.begin(), offsets.end(), expected_offsets.begin()));
  19. }
  20. TEST(TitledUrlMatchTest, ReplaceOffsetsInEmptyMatchPositions) {
  21. auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions(
  22. MatchPositions(), std::vector<size_t>());
  23. EXPECT_TRUE(match_positions.empty());
  24. }
  25. TEST(TitledUrlMatchTest, ReplaceOffsetsInMatchPositions) {
  26. MatchPositions orig_match_positions = {{1, 3}, {4, 5}, {10, 15}};
  27. std::vector<size_t> offsets = {0, 2, 3, 4, 9, 14};
  28. MatchPositions expected_match_positions = {{0, 2}, {3, 4}, {9, 14}};
  29. auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions(
  30. orig_match_positions, offsets);
  31. EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(),
  32. expected_match_positions.begin()));
  33. }
  34. TEST(TitledUrlMatchTest, ReplaceOffsetsRemovesItemsWithNposOffsets) {
  35. MatchPositions orig_match_positions = {{1, 3}, {4, 5}, {10, 15}, {17, 20}};
  36. std::vector<size_t> offsets = {0,
  37. std::u16string::npos,
  38. std::u16string::npos,
  39. 4,
  40. std::u16string::npos,
  41. std::u16string::npos,
  42. 17,
  43. 20};
  44. MatchPositions expected_match_positions = {{17, 20}};
  45. auto match_positions = TitledUrlMatch::ReplaceOffsetsInMatchPositions(
  46. orig_match_positions, offsets);
  47. EXPECT_TRUE(std::equal(match_positions.begin(), match_positions.end(),
  48. expected_match_positions.begin()));
  49. }
  50. }