| // Copyright 2026 Google Inc. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "text_utils.h" |
| |
| #include <string> |
| |
| #include "test.h" |
| |
| TEST(TextUtilsTest, StripStringPiece) { |
| EXPECT_EQ("abc", StripStringPiece(" abc ")); |
| EXPECT_EQ("abc", StripStringPiece("abc")); |
| EXPECT_EQ("abc", StripStringPiece("\tabc")); |
| EXPECT_EQ("abc", StripStringPiece("abc\r\n")); |
| EXPECT_EQ("abc", StripStringPiece("abc\n")); |
| EXPECT_EQ("", StripStringPiece(" ")); |
| EXPECT_EQ("", StripStringPiece("")); |
| EXPECT_EQ("a", StripStringPiece(" a ")); |
| } |
| |
| TEST(TextUtilsTest, TextLineSplitter) { |
| std::string input("a\nb\nc\nwith_crlf\r\n with space \ntrailing line"); |
| TextLineSplitter splitter(input); |
| StringPiece line; |
| EXPECT_TRUE(splitter.GetNextLine(&line)); |
| EXPECT_EQ("a", line); |
| EXPECT_TRUE(splitter.GetNextLine(&line)); |
| EXPECT_EQ("b", line); |
| EXPECT_TRUE(splitter.GetNextLine(&line)); |
| EXPECT_EQ("c", line); |
| EXPECT_TRUE(splitter.GetNextLine(&line)); |
| EXPECT_EQ("with_crlf", line); |
| EXPECT_TRUE(splitter.GetNextLine(&line)); |
| EXPECT_EQ(" with space ", line); |
| EXPECT_TRUE(splitter.GetNextLine(&line)); |
| EXPECT_EQ("trailing line", line); |
| EXPECT_FALSE(splitter.GetNextLine(&line)); |
| } |
| |
| TEST(TextUtilsTest, TextFileParser) { |
| VirtualFileSystem disk_interface; |
| disk_interface.Create( |
| "test.txt", "a\nb\nc\n\r\n\nwith_crlf\r\n with space \ntrailing line"); |
| TextFileParser parser(disk_interface); |
| std::string error; |
| EXPECT_TRUE(parser.Reset("test.txt", &error)) << error; |
| EXPECT_EQ("a\nb\nc\n\r\n\nwith_crlf\r\n with space \ntrailing line", |
| parser.content()); |
| EXPECT_EQ(6, parser.lines().size()); |
| EXPECT_EQ("a", parser.lines()[0]); |
| EXPECT_EQ("b", parser.lines()[1]); |
| EXPECT_EQ("c", parser.lines()[2]); |
| EXPECT_EQ("with_crlf", parser.lines()[3]); |
| EXPECT_EQ(" with space ", parser.lines()[4]); |
| EXPECT_EQ("trailing line", parser.lines()[5]); |
| } |