jes/test/src/test_TextLoader.cc

83 lines
2.4 KiB
C++

#include "gtest/gtest.h"
#include "TextLoader.h"
#include "TestSupport.h"
#include <vector>
#include <string>
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;
EXPECT_EQ(0u, tl.num_lines());
}
TEST(TextLoaderTest, loading_empty_file)
{
TextLoader tl;
auto file = TestSupport::read_file("test/files/empty.txt");
tl.load_buffer(&(*file)[0], file->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");
tl.load_buffer(&(*file)[0], file->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));
EXPECT_TRUE(tl.get_eol_at_eof());
}
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());
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));
EXPECT_TRUE(tl.get_eol_at_eof());
}
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());
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));
EXPECT_TRUE(tl.get_eol_at_eof());
}
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());
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));
EXPECT_FALSE(tl.get_eol_at_eof());
}