summaryrefslogtreecommitdiff
path: root/Tests/LibCore
diff options
context:
space:
mode:
authorcoderdreams <coderdreams@gmail.com>2021-05-30 12:14:16 -0300
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-06-22 18:54:40 +0430
commit9ffb3e7e3024a77a3b5688ee1d3c1b6e0d772958 (patch)
treeeaba32ef57ad951b221d06a6559b4c9ff71bb218 /Tests/LibCore
parent714a96619f1c0d617f90ae042ee8c89a54ed384d (diff)
downloadserenity-9ffb3e7e3024a77a3b5688ee1d3c1b6e0d772958.zip
LibCore: Add unit test for File::read_line
Diffstat (limited to 'Tests/LibCore')
-rw-r--r--Tests/LibCore/CMakeLists.txt3
-rw-r--r--Tests/LibCore/TestLibCoreIODevice.cpp46
-rw-r--r--Tests/LibCore/long_lines.txt6
3 files changed, 55 insertions, 0 deletions
diff --git a/Tests/LibCore/CMakeLists.txt b/Tests/LibCore/CMakeLists.txt
index 934d2982a2..fee06664c7 100644
--- a/Tests/LibCore/CMakeLists.txt
+++ b/Tests/LibCore/CMakeLists.txt
@@ -2,8 +2,11 @@ set(
TEST_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreArgsParser.cpp
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreFileWatcher.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreIODevice.cpp
)
foreach(source ${TEST_SOURCES})
serenity_test(${source} LibCore)
endforeach()
+
+install(FILES long_lines.txt DESTINATION usr/Tests/LibCore)
diff --git a/Tests/LibCore/TestLibCoreIODevice.cpp b/Tests/LibCore/TestLibCoreIODevice.cpp
new file mode 100644
index 0000000000..72ab79f93c
--- /dev/null
+++ b/Tests/LibCore/TestLibCoreIODevice.cpp
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2021, the SerenityOS developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCore/File.h>
+#include <LibTest/TestCase.h>
+#include <unistd.h>
+
+TEST_CASE(file_readline)
+{
+ auto path = "long_lines.txt";
+ auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
+ if (file_or_error.is_error()) {
+ warnln("Failed to open {}: {}", path, strerror(errno));
+ VERIFY_NOT_REACHED();
+ }
+ auto file = file_or_error.release_value();
+ auto output_path = "/tmp/output.txt";
+ auto outfile_or_error = Core::File::open(output_path, Core::OpenMode::WriteOnly);
+ auto outputfile = outfile_or_error.release_value();
+ while (file->can_read_line()) {
+ outputfile->write(file->read_line());
+ outputfile->write("\n");
+ }
+ file->close();
+ outputfile->close();
+
+ // Open files again for comparison since otherwise read_all returns empty (even when not closing the file)
+ file = Core::File::construct(path);
+ if (!file->open(Core::OpenMode::ReadOnly))
+ VERIFY_NOT_REACHED();
+ outputfile = Core::File::construct(output_path);
+ if (!outputfile->open(Core::OpenMode::ReadOnly))
+ VERIFY_NOT_REACHED();
+ auto inputData = file->read_all();
+ auto outputData = outputfile->read_all();
+ EXPECT(inputData.size() > 0);
+ EXPECT_EQ(inputData.size(), outputData.size());
+
+ // Compare char by char
+ for (size_t i = 0; i < inputData.size(); i++) {
+ EXPECT_EQ(inputData[i], outputData[i]);
+ }
+}
diff --git a/Tests/LibCore/long_lines.txt b/Tests/LibCore/long_lines.txt
new file mode 100644
index 0000000000..49232d404c
--- /dev/null
+++ b/Tests/LibCore/long_lines.txt
@@ -0,0 +1,6 @@
+&lt;small&gt;(Please consider translating this message for the benefit of your fellow Wikimedians. Please also consider translating [[m:Requests for comment/Activity levels of advanced administrative rights holders/Summary|the proposal]].)&lt;/small&gt;
+
+&lt;small&gt;[[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message|Read this message in English]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/ast|Lleer esti mensaxe n'asturianu]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/bn|বাংলায় এই বার্তাটি পড়ুন]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/ca|Llegiu aquest missatge en català]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/da|Læs denne besked på dansk]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/de|Lies diese Nachricht auf Deutsch]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/egl|Leś cal mesag' chè in Emiliàn]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/es|Leer este mensaje en español]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/fi|Lue tämä viesti suomeksi]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/fr|Lire ce message en français]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/gl|Ler esta mensaxe en galego]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/hi|हिन्दी]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/hr|Pročitajte ovu poruku na hrvatskom]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/id|Baca pesan ini dalam Bahasa Indonesia]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/it|Leggi questo messaggio in italiano]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/kn|ಈ ಸಂದೇಶವನ್ನು ಕನ್ನಡದಲ್ಲಿ ಓದಿ]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/mt|Aqra dan il-messaġġ bil-Malti]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/nb|norsk (bokmål)]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/nl|Lees dit bericht in het Nederlands]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/pl|Przeczytaj tę wiadomość po polsku]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/ro|Citiți acest mesaj în română]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/ru|Прочитать это сообщение на русском]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/so|Farriintaan ku aqri Af-Soomaali]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/sr|Pročitaj ovu poruku na srpskom (Прочитај ову поруку на српском)]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/th|อ่านข้อความนี้ในภาษาไทย]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/uk|Прочитати це повідомлення українською мовою]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/vi|Đọc thông báo bằng tiếng Việt]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/zh|使用中文阅读本信息。]]&lt;/
+&lt;small&gt;(Please consider translating this message for the benefit of your fellow Wikimedians. Please also consider translating [[m:Requests for comment/Activity levels of advanced administrative rights holders/Summary|the proposal]].)&lt;/small&gt;
+
+&lt;small&gt;[[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message|Read this message in English]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/ast|Lleer esti mensaxe n'asturianu]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/bn|বাংলায় এই বার্তাটি পড়ুন]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/ca|Llegiu aquest missatge en català]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/da|Læs denne besked på dansk]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/de|Lies diese Nachricht auf Deutsch]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/egl|Leś cal mesag' chè in Emiliàn]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/es|Leer este mensaje en español]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/fi|Lue tämä viesti suomeksi]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/fr|Lire ce message en français]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/gl|Ler esta mensaxe en galego]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/hi|हिन्दी]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/hr|Pročitajte ovu poruku na hrvatskom]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/id|Baca pesan ini dalam Bahasa Indonesia]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/it|Leggi questo messaggio in italiano]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/kn|ಈ ಸಂದೇಶವನ್ನು ಕನ್ನಡದಲ್ಲಿ ಓದಿ]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/mt|Aqra dan il-messaġġ bil-Malti]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/nb|norsk (bokmål)]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/nl|Lees dit bericht in het Nederlands]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/pl|Przeczytaj tę wiadomość po polsku]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/ro|Citiți acest mesaj în română]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/ru|Прочитать это сообщение на русском]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/so|Farriintaan ku aqri Af-Soomaali]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/sr|Pročitaj ovu poruku na srpskom (Прочитај ову поруку на српском)]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/th|อ่านข้อความนี้ในภาษาไทย]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/uk|Прочитати це повідомлення українською мовою]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/vi|Đọc thông báo bằng tiếng Việt]] / [[m:Requests for comment/Activity levels of advanced administrative rights holders/Global message/zh|使用中文阅读本信息。]]&lt;/