Jelajahi Sumber

Fixed tests due to new line behaviour

- fixed tests by providing more flexibility when
handling different new line characters (tests
shouldn't care about the exact new line characters)
Patrick-Laptop 3 tahun lalu
induk
melakukan
1b66db4dd9

+ 9 - 6
test/cases/io/FileReaderTest.cpp

@@ -27,29 +27,32 @@ namespace {
   {
     ls_std::File file {TestHelper::getResourcesFolderLocation() + "simple.txt"};
     ls_std::FileReader reader {file};
-    std::string expected = "Hello!" + ls_std::NewLine::get();
+    std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
+    std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
 
     ls_std::byte* data = reader.read();
     std::string content {data, (size_t) file.getSize()};
-    ASSERT_STREQ(expected.c_str(), content.c_str());
+    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
   }
 
   TEST_F(FileReaderTest, reset)
   {
     ls_std::File file {TestHelper::getResourcesFolderLocation() + "simple.txt"};
     ls_std::FileReader reader {file};
-    std::string expected = "Hello!" + ls_std::NewLine::get();
+    std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
+    std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
 
     ls_std::byte* data = reader.read();
     std::string content {data, (size_t) file.getSize()};
-    ASSERT_STREQ(expected.c_str(), content.c_str());
+    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
 
     ls_std::File anotherFile {TestHelper::getResourcesFolderLocation() + "list_test/bla.txt"};
     reader.reset(anotherFile);
-    expected = "nothing to say!" + ls_std::NewLine::get();
+    expectedUnix = "nothing to say!" + ls_std::NewLine::getUnixNewLine();
+    expectedWindows = "nothing to say!" + ls_std::NewLine::getWindowsNewLine();
 
     data = reader.read();
     content = {data, (size_t) anotherFile.getSize()};
-    ASSERT_STREQ(expected.c_str(), content.c_str());
+    ASSERT_TRUE(content == expectedUnix || content == expectedWindows);
   }
 }

+ 10 - 8
test/cases/io/FileTest.cpp

@@ -84,8 +84,14 @@ namespace {
 
   TEST_F(FileTest, canWriteNegative)
   {
-    ls_std::File noWritableFile {TestHelper::getResourcesFolderLocation() + "no_writable.txt"};
-    ASSERT_FALSE(noWritableFile.canWrite());
+    #if defined(unix) || defined(__APPLE__)
+      ls_std::File noWritableFile {TestHelper::getResourcesFolderLocation() + "no_writable.txt"};
+      ASSERT_FALSE(noWritableFile.canWrite());
+    #endif
+    #ifdef _WIN32
+      ls_std::File noWritableFile {TestHelper::getResourcesFolderLocation() + "no_writable_windows.txt"};
+      ASSERT_FALSE(noWritableFile.canWrite());
+    #endif
   }
 
   TEST_F(FileTest, createNewFileAndRemove)
@@ -149,13 +155,9 @@ namespace {
   TEST_F(FileTest, getSize)
   {
     ls_std::File file {this->fileLocation};
+    size_t size = file.getSize();
 
-    #ifdef _WIN32
-      ASSERT_EQ(8, file.getSize());
-    #endif
-    #if defined(unix) || defined(__APPLE__)
-      ASSERT_EQ(7, file.getSize());
-    #endif
+    ASSERT_TRUE(size == 7 || size == 8); // different OS specific new lines
   }
 
   TEST_F(FileTest, isDirectory)

+ 14 - 10
test/cases/io/StorableFileTest.cpp

@@ -35,27 +35,30 @@ namespace {
   {
     ls_std::StorableFile storableFile {this->fileLocation};
     storableFile.load();
-    std::string expected = "Hello!" + ls_std::NewLine::get();
+    std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
+    std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
 
-    ASSERT_STREQ(expected.c_str(), storableFile.getContent().c_str());
+    ASSERT_TRUE(storableFile.getContent() == expectedUnix || storableFile.getContent() == expectedWindows);
   }
 
   TEST_F(StorableFileTest, reset)
   {
     ls_std::StorableFile storableFile {this->fileLocation};
     storableFile.load();
-    std::string expected = "Hello!" + ls_std::NewLine::get();
+    std::string expectedUnix = "Hello!" + ls_std::NewLine::getUnixNewLine();
+    std::string expectedWindows = "Hello!" + ls_std::NewLine::getWindowsNewLine();
 
-    ASSERT_STREQ(expected.c_str(), storableFile.getContent().c_str());
+    ASSERT_TRUE(storableFile.getContent() == expectedUnix || storableFile.getContent() == expectedWindows);
 
     // reset
 
     std::string anotherFileLocation = TestHelper::getResourcesFolderLocation() + "list_test/bla.txt";
     storableFile.reset(anotherFileLocation);
     storableFile.load();
-    expected = "nothing to say!" + ls_std::NewLine::get();
+    expectedUnix = "nothing to say!" + ls_std::NewLine::getUnixNewLine();
+    expectedWindows = "nothing to say!" + ls_std::NewLine::getWindowsNewLine();
 
-    ASSERT_STREQ(expected.c_str(), storableFile.getContent().c_str());
+    ASSERT_TRUE(storableFile.getContent() == expectedUnix || storableFile.getContent() == expectedWindows);
   }
 
   TEST_F(StorableFileTest, save)
@@ -65,14 +68,15 @@ namespace {
     file.createNewFile();
 
     ls_std::StorableFile storableFile {path};
-    std::string text = "Testing save method!" + ls_std::NewLine::get();
+    std::string textUnix = "Testing save method!" + ls_std::NewLine::getUnixNewLine();
+    std::string textWindows = "Testing save method!" + ls_std::NewLine::getWindowsNewLine();
 
     ASSERT_TRUE(storableFile.getContent().empty());
-    storableFile.setContent(text);
-    ASSERT_STREQ(text.c_str(), storableFile.getContent().c_str());
+    storableFile.setContent(textUnix);
+    ASSERT_TRUE(storableFile.getContent() == textUnix || storableFile.getContent() == textWindows);
     storableFile.save();
     storableFile.load();
-    ASSERT_STREQ(text.c_str(), storableFile.getContent().c_str());
+    ASSERT_TRUE(storableFile.getContent() == textUnix || storableFile.getContent() == textWindows);
 
     file.remove();
   }

+ 1 - 0
test/resources/no_writable_windows.txt

@@ -0,0 +1 @@
+Hello!