|
| 1 | +// Copyright 2024 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://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 "elide_middle.h" |
| 16 | + |
| 17 | +#include "test.h" |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +std::string ElideMiddle(const std::string& str, size_t width) { |
| 22 | + std::string result = str; |
| 23 | + ElideMiddleInPlace(result, width); |
| 24 | + return result; |
| 25 | +} |
| 26 | + |
| 27 | +} // namespace |
| 28 | + |
| 29 | + |
| 30 | +TEST(ElideMiddle, NothingToElide) { |
| 31 | + std::string input = "Nothing to elide in this short string."; |
| 32 | + EXPECT_EQ(input, ElideMiddle(input, 80)); |
| 33 | + EXPECT_EQ(input, ElideMiddle(input, 38)); |
| 34 | + EXPECT_EQ("", ElideMiddle(input, 0)); |
| 35 | + EXPECT_EQ(".", ElideMiddle(input, 1)); |
| 36 | + EXPECT_EQ("..", ElideMiddle(input, 2)); |
| 37 | + EXPECT_EQ("...", ElideMiddle(input, 3)); |
| 38 | +} |
| 39 | + |
| 40 | +TEST(ElideMiddle, ElideInTheMiddle) { |
| 41 | + std::string input = "01234567890123456789"; |
| 42 | + EXPECT_EQ("...9", ElideMiddle(input, 4)); |
| 43 | + EXPECT_EQ("0...9", ElideMiddle(input, 5)); |
| 44 | + EXPECT_EQ("012...789", ElideMiddle(input, 9)); |
| 45 | + EXPECT_EQ("012...6789", ElideMiddle(input, 10)); |
| 46 | + EXPECT_EQ("0123...6789", ElideMiddle(input, 11)); |
| 47 | + EXPECT_EQ("01234567...23456789", ElideMiddle(input, 19)); |
| 48 | + EXPECT_EQ("01234567890123456789", ElideMiddle(input, 20)); |
| 49 | +} |
| 50 | + |
| 51 | +// A few ANSI escape sequences. These macros make the following |
| 52 | +// test easier to read and understand. |
| 53 | +#define MAGENTA "\x1B[0;35m" |
| 54 | +#define NOTHING "\33[m" |
| 55 | +#define RED "\x1b[1;31m" |
| 56 | +#define RESET "\x1b[0m" |
| 57 | + |
| 58 | +TEST(ElideMiddle, ElideAnsiEscapeCodes) { |
| 59 | + std::string input = "012345" MAGENTA "67890123456789"; |
| 60 | + EXPECT_EQ("012..." MAGENTA "6789", ElideMiddle(input, 10)); |
| 61 | + EXPECT_EQ("012345" MAGENTA "67...23456789", ElideMiddle(input, 19)); |
| 62 | + |
| 63 | + EXPECT_EQ("Nothing " NOTHING " string.", |
| 64 | + ElideMiddle("Nothing " NOTHING " string.", 18)); |
| 65 | + EXPECT_EQ("0" NOTHING "12...6789", |
| 66 | + ElideMiddle("0" NOTHING "1234567890123456789", 10)); |
| 67 | + |
| 68 | + input = "abcd" RED "efg" RESET "hlkmnopqrstuvwxyz"; |
| 69 | + EXPECT_EQ("", ElideMiddle(input, 0)); |
| 70 | + EXPECT_EQ(".", ElideMiddle(input, 1)); |
| 71 | + EXPECT_EQ("..", ElideMiddle(input, 2)); |
| 72 | + EXPECT_EQ("...", ElideMiddle(input, 3)); |
| 73 | + EXPECT_EQ("..." RED RESET "z", ElideMiddle(input, 4)); |
| 74 | + EXPECT_EQ("a..." RED RESET "z", ElideMiddle(input, 5)); |
| 75 | + EXPECT_EQ("a..." RED RESET "yz", ElideMiddle(input, 6)); |
| 76 | + EXPECT_EQ("ab..." RED RESET "yz", ElideMiddle(input, 7)); |
| 77 | + EXPECT_EQ("ab..." RED RESET "xyz", ElideMiddle(input, 8)); |
| 78 | + EXPECT_EQ("abc..." RED RESET "xyz", ElideMiddle(input, 9)); |
| 79 | + EXPECT_EQ("abc..." RED RESET "wxyz", ElideMiddle(input, 10)); |
| 80 | + EXPECT_EQ("abcd" RED "..." RESET "wxyz", ElideMiddle(input, 11)); |
| 81 | + EXPECT_EQ("abcd" RED "..." RESET "vwxyz", ElideMiddle(input, 12)); |
| 82 | + |
| 83 | + EXPECT_EQ("abcd" RED "ef..." RESET "uvwxyz", ElideMiddle(input, 15)); |
| 84 | + EXPECT_EQ("abcd" RED "ef..." RESET "tuvwxyz", ElideMiddle(input, 16)); |
| 85 | + EXPECT_EQ("abcd" RED "efg" RESET "...tuvwxyz", ElideMiddle(input, 17)); |
| 86 | + EXPECT_EQ("abcd" RED "efg" RESET "...stuvwxyz", ElideMiddle(input, 18)); |
| 87 | + EXPECT_EQ("abcd" RED "efg" RESET "h...stuvwxyz", ElideMiddle(input, 19)); |
| 88 | + |
| 89 | + input = "abcdef" RED "A" RESET "BC"; |
| 90 | + EXPECT_EQ("..." RED RESET "C", ElideMiddle(input, 4)); |
| 91 | + EXPECT_EQ("a..." RED RESET "C", ElideMiddle(input, 5)); |
| 92 | + EXPECT_EQ("a..." RED RESET "BC", ElideMiddle(input, 6)); |
| 93 | + EXPECT_EQ("ab..." RED RESET "BC", ElideMiddle(input, 7)); |
| 94 | + EXPECT_EQ("ab..." RED "A" RESET "BC", ElideMiddle(input, 8)); |
| 95 | + EXPECT_EQ("abcdef" RED "A" RESET "BC", ElideMiddle(input, 9)); |
| 96 | +} |
| 97 | + |
| 98 | +#undef RESET |
| 99 | +#undef RED |
| 100 | +#undef NOTHING |
| 101 | +#undef MAGENTA |
0 commit comments