Change TextLoader for gap buffer approach
- for CR line endings, convert all CR to LF - for CRLF line endings, convert all CRLF to LF
This commit is contained in:
1 parent
1f3ecf950f
commit
65468bd2e5
3 files changed
+85
-63
No files matched your search
+22
-26
@@ -6,11 +6,6 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
static string line_to_string(const std::list<Span>::iterator & it)
|
||||
{
|
||||
return string((char *)it->start, it->length);
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, num_lines_defaults_to_0)
|
||||
{
|
||||
TextLoader tl;
|
||||
@@ -21,7 +16,8 @@ TEST(TextLoaderTest, loading_empty_file)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/empty.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
size_t loaded_size;
|
||||
tl.load_buffer(&(*file)[0], file->size(), &loaded_size);
|
||||
EXPECT_EQ(0u, tl.num_lines());
|
||||
EXPECT_TRUE(tl.get_eol_at_eof());
|
||||
}
|
||||
@@ -30,53 +26,53 @@ TEST(TextLoaderTest, detects_lf_line_endings)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/line_endings/lf_format.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
size_t loaded_size;
|
||||
tl.load_buffer(&(*file)[0], file->size(), &loaded_size);
|
||||
EXPECT_EQ(LineEndings::LF, tl.get_line_endings());
|
||||
ASSERT_EQ(2u, tl.num_lines());
|
||||
auto it = tl.begin();
|
||||
EXPECT_EQ("Hello.", line_to_string(it));
|
||||
it++;
|
||||
EXPECT_EQ("This file is in LF line ending format.", line_to_string(it));
|
||||
string expected = "Hello.\nThis file is in LF line ending format.\n";
|
||||
EXPECT_EQ(expected, string((char *)&(*file)[0], expected.size()));
|
||||
EXPECT_TRUE(tl.get_eol_at_eof());
|
||||
EXPECT_EQ(expected.size(), loaded_size);
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, detects_cr_line_endings)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/line_endings/cr_format.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
size_t loaded_size;
|
||||
tl.load_buffer(&(*file)[0], file->size(), &loaded_size);
|
||||
EXPECT_EQ(LineEndings::CR, tl.get_line_endings());
|
||||
ASSERT_EQ(2u, tl.num_lines());
|
||||
auto it = tl.begin();
|
||||
EXPECT_EQ("Hello.", line_to_string(it));
|
||||
it++;
|
||||
EXPECT_EQ("This file is in CR line ending format.", line_to_string(it));
|
||||
string expected = "Hello.\nThis file is in CR line ending format.\n";
|
||||
EXPECT_EQ(expected, string((char *)&(*file)[0], expected.size()));
|
||||
EXPECT_TRUE(tl.get_eol_at_eof());
|
||||
EXPECT_EQ(expected.size(), loaded_size);
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, detects_crlf_line_endings)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/line_endings/crlf_format.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
size_t loaded_size;
|
||||
tl.load_buffer(&(*file)[0], file->size(), &loaded_size);
|
||||
EXPECT_EQ(LineEndings::CRLF, tl.get_line_endings());
|
||||
ASSERT_EQ(2u, tl.num_lines());
|
||||
auto it = tl.begin();
|
||||
EXPECT_EQ("Hello.", line_to_string(it));
|
||||
it++;
|
||||
EXPECT_EQ("This file is in CRLF line ending format.", line_to_string(it));
|
||||
string expected = "Hello.\nThis file is in CRLF line ending format.\n";
|
||||
EXPECT_EQ(expected, string((char *)&(*file)[0], expected.size()));
|
||||
EXPECT_TRUE(tl.get_eol_at_eof());
|
||||
EXPECT_EQ(expected.size(), loaded_size);
|
||||
}
|
||||
|
||||
TEST(TextLoaderTest, properly_reads_files_that_do_not_end_in_a_eol_sequence)
|
||||
{
|
||||
TextLoader tl;
|
||||
auto file = TestSupport::read_file("test/files/no_eol_at_eof.txt");
|
||||
tl.load_buffer(&(*file)[0], file->size());
|
||||
size_t loaded_size;
|
||||
tl.load_buffer(&(*file)[0], file->size(), &loaded_size);
|
||||
ASSERT_EQ(2u, tl.num_lines());
|
||||
auto it = tl.begin();
|
||||
EXPECT_EQ("Line 1", line_to_string(it));
|
||||
it++;
|
||||
EXPECT_EQ("Line 2", line_to_string(it));
|
||||
string expected = "Line 1\nLine 2\n";
|
||||
EXPECT_EQ(expected, string((char *)&(*file)[0], expected.size()));
|
||||
EXPECT_FALSE(tl.get_eol_at_eof());
|
||||
EXPECT_EQ(expected.size(), loaded_size);
|
||||
}
|
||||
Reference in new issue
Block a user