diff --git a/.gitignore b/.gitignore index 9c0382d..6828ef4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.lock-waf* /.waf* /build/ +/test/tmp/ diff --git a/test/src/TestSupport.cc b/test/src/TestSupport.cc index 4cf48ba..5947de0 100644 --- a/test/src/TestSupport.cc +++ b/test/src/TestSupport.cc @@ -1,6 +1,7 @@ #include "TestSupport.h" #include "File.h" #include +#include "gtest/gtest.h" std::shared_ptr> TestSupport::read_file(const char * filename) { @@ -22,3 +23,14 @@ std::shared_ptr> TestSupport::read_file(const char * filena return buffer; } + +void TestSupport::compare_files(const char * f1, const char * f2) +{ + auto fl1 = read_file(f1); + auto fl2 = read_file(f2); + ASSERT_EQ(fl1->size(), fl2->size()); + for (size_t i = 0; i < fl1->size(); i++) + { + ASSERT_EQ((*fl1)[i], (*fl2)[i]); + } +} diff --git a/test/src/TestSupport.h b/test/src/TestSupport.h index cd4f6c8..060b4e9 100644 --- a/test/src/TestSupport.h +++ b/test/src/TestSupport.h @@ -9,6 +9,7 @@ class TestSupport { public: static std::shared_ptr> read_file(const char * filename); + static void compare_files(const char * f1, const char * f2); }; #endif diff --git a/test/src/main.cc b/test/src/main.cc index 55fc515..dc6d270 100644 --- a/test/src/main.cc +++ b/test/src/main.cc @@ -1,9 +1,12 @@ #include "gtest/gtest.h" #include "System.h" +#include +#include int main(int argc, char * argv[]) { ::testing::InitGoogleTest(&argc, argv); System::init(); + mkdir("test/tmp", 0777); return RUN_ALL_TESTS(); } diff --git a/test/src/test_Buffer.cc b/test/src/test_Buffer.cc new file mode 100644 index 0000000..11757f3 --- /dev/null +++ b/test/src/test_Buffer.cc @@ -0,0 +1,23 @@ +#include "gtest/gtest.h" +#include "Buffer.h" +#include "TestSupport.h" +#include "Path.h" + +TEST(BufferTest, writes_an_identical_file_to_what_is_loaded_if_no_changes_were_performed) +{ + const char * files_to_test[] = { + "test/files/empty.txt", + "test/files/line_endings/cr_format.txt", + "test/files/line_endings/crlf_format.txt", + "test/files/line_endings/lf_format.txt", + "test/files/no_eol_at_eof.txt", + }; + for (auto e : files_to_test) + { + Buffer b; + ASSERT_TRUE(b.load_from_file(e)); + ASSERT_TRUE(b.write_to_file("test/tmp/f")); + ASSERT_TRUE(Path::is_file("test/tmp/f")); + TestSupport::compare_files(e, "test/tmp/f"); + } +}