#include "gtest/gtest.h" #include "TextLoader.h" #include "TestSupport.h" #include #include using namespace std; TEST(TextLoaderTest, num_lines_defaults_to_0) { TextLoader tl; EXPECT_EQ(0u, tl.num_lines()); } TEST(TextLoaderTest, loading_empty_file) { TextLoader tl; auto file = TestSupport::read_file("test/files/empty.txt"); 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()); } TEST(TextLoaderTest, detects_lf_line_endings) { TextLoader tl; auto file = TestSupport::read_file("test/files/line_endings/lf_format.txt"); 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()); 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"); 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()); 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"); 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()); 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"); size_t loaded_size; tl.load_buffer(&(*file)[0], file->size(), &loaded_size); ASSERT_EQ(2u, tl.num_lines()); 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); }