diff --git a/src/core/TextLoader.h b/src/core/TextLoader.h index 9cdb977..b0a5405 100644 --- a/src/core/TextLoader.h +++ b/src/core/TextLoader.h @@ -33,10 +33,11 @@ public: auto begin() { return m_lines->begin(); } auto end() { return m_lines->end(); } -protected: typedef std::pair LineIndexPair; typedef std::list LineIndexPairList; typedef std::shared_ptr LineIndexPairListRef; + +protected: int m_line_endings; LineIndexPairListRef m_lines; }; diff --git a/test/files/line_endings/cr_format.txt b/test/files/line_endings/cr_format.txt new file mode 100644 index 0000000..2810e5c --- /dev/null +++ b/test/files/line_endings/cr_format.txt @@ -0,0 +1 @@ +Hello. This file is in CR line ending format. \ No newline at end of file diff --git a/test/files/line_endings/crlf_format.txt b/test/files/line_endings/crlf_format.txt new file mode 100644 index 0000000..e317dca --- /dev/null +++ b/test/files/line_endings/crlf_format.txt @@ -0,0 +1,2 @@ +Hello. +This file is in CRLF line ending format. diff --git a/test/files/line_endings/dos_format.txt b/test/files/line_endings/dos_format.txt deleted file mode 100644 index 844dd2a..0000000 --- a/test/files/line_endings/dos_format.txt +++ /dev/null @@ -1,2 +0,0 @@ -Hello. -This file is in DOS line ending format. diff --git a/test/files/line_endings/lf_format.txt b/test/files/line_endings/lf_format.txt new file mode 100644 index 0000000..db101d0 --- /dev/null +++ b/test/files/line_endings/lf_format.txt @@ -0,0 +1,2 @@ +Hello. +This file is in LF line ending format. diff --git a/test/files/line_endings/mac_format.txt b/test/files/line_endings/mac_format.txt deleted file mode 100644 index 99a41d8..0000000 --- a/test/files/line_endings/mac_format.txt +++ /dev/null @@ -1 +0,0 @@ -Hello. This file is in MAC line ending format. \ No newline at end of file diff --git a/test/files/line_endings/unix_format.txt b/test/files/line_endings/unix_format.txt deleted file mode 100644 index 96326ac..0000000 --- a/test/files/line_endings/unix_format.txt +++ /dev/null @@ -1,2 +0,0 @@ -Hello. -This file is in UNIX line ending format. diff --git a/test/src/TestSupport.cc b/test/src/TestSupport.cc new file mode 100644 index 0000000..4cf48ba --- /dev/null +++ b/test/src/TestSupport.cc @@ -0,0 +1,24 @@ +#include "TestSupport.h" +#include "File.h" +#include + +std::shared_ptr> TestSupport::read_file(const char * filename) +{ + File file; + if (!file.open(filename)) + { + fprintf(stderr, "Could not open %s\n", filename); + return nullptr; + } + + size_t size = file.get_size(); + auto buffer = std::make_shared>(size, 0u); + + if (!file.read(&(*buffer)[0], size)) + { + fprintf(stderr, "Failed to read file %s\n", filename); + return nullptr; + } + + return buffer; +} diff --git a/test/src/TestSupport.h b/test/src/TestSupport.h new file mode 100644 index 0000000..cd4f6c8 --- /dev/null +++ b/test/src/TestSupport.h @@ -0,0 +1,14 @@ +#ifndef TESTSUPPORT_H +#define TESTSUPPORT_H + +#include +#include +#include + +class TestSupport +{ +public: + static std::shared_ptr> read_file(const char * filename); +}; + +#endif diff --git a/test/src/test_FileLoader.cc b/test/src/test_FileLoader.cc deleted file mode 100644 index efc8161..0000000 --- a/test/src/test_FileLoader.cc +++ /dev/null @@ -1,53 +0,0 @@ -#if 0 -#include "gtest/gtest.h" -#include "FileLoader.h" - -TEST(FileLoaderTest, num_lines_defaults_to_0) -{ - FileLoader fr; - EXPECT_EQ(0u, fr.num_lines()); -} - -TEST(FileLoaderTest, load_returns_false_for_nonexistent_file) -{ - FileLoader fr; - EXPECT_FALSE(fr.load("this/file/does/not/exist.txt")); -} - -TEST(FileLoaderTest, loading_empty_file) -{ - FileLoader fr; - EXPECT_TRUE(fr.load("test/files/empty.txt")); - EXPECT_EQ(0u, fr.num_lines()); -} - -TEST(FileLoaderTest, reads_lf_format_file) -{ - FileLoader fr; - EXPECT_TRUE(fr.load("test/files/line_endings/unix_format.txt")); - EXPECT_EQ(FileLoader::LINE_ENDING_LF, fr.get_line_endings()); - EXPECT_EQ(2u, fr.num_lines()); -// EXPECT_EQ("Hello.", fr.get_line(0)->to_s()); -// EXPECT_EQ("This file is in UNIX line ending format.", fr.get_line(1)->to_s()); -} - -TEST(FileLoaderTest, reads_cr_format_file) -{ - FileLoader fr; - EXPECT_TRUE(fr.load("test/files/line_endings/mac_format.txt")); - EXPECT_EQ(FileLoader::LINE_ENDING_CR, fr.get_line_endings()); - EXPECT_EQ(2u, fr.num_lines()); -// EXPECT_EQ("Hello.", fr.get_line(0)->to_s()); -// EXPECT_EQ("This file is in MAC line ending format.", fr.get_line(1)->to_s()); -} - -TEST(FileLoaderTest, reads_crlf_format_file) -{ - FileLoader fr; - EXPECT_TRUE(fr.load("test/files/line_endings/dos_format.txt")); - EXPECT_EQ(FileLoader::LINE_ENDING_CRLF, fr.get_line_endings()); - EXPECT_EQ(2u, fr.num_lines()); -// EXPECT_EQ("Hello.", fr.get_line(0)->to_s()); -// EXPECT_EQ("This file is in DOS line ending format.", fr.get_line(1)->to_s()); -} -#endif diff --git a/test/src/test_TextLoader.cc b/test/src/test_TextLoader.cc new file mode 100644 index 0000000..092f82c --- /dev/null +++ b/test/src/test_TextLoader.cc @@ -0,0 +1,65 @@ +#include "gtest/gtest.h" +#include "TextLoader.h" +#include "TestSupport.h" +#include +#include + +using namespace std; + +static string line_to_string(const TextLoader::LineIndexPairList::iterator & it) +{ + return string((char *)it->first, it->second); +} + +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()); +} + +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(TextLoader::LINE_ENDING_LF, tl.get_line_endings()); + EXPECT_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)); +} + +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(TextLoader::LINE_ENDING_CR, tl.get_line_endings()); + EXPECT_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)); +} + +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(TextLoader::LINE_ENDING_CRLF, tl.get_line_endings()); + EXPECT_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)); +}