mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 16:48:12 +00:00
LibCore: Add unit test for File::read_line
This commit is contained in:
parent
714a96619f
commit
9ffb3e7e30
4 changed files with 66 additions and 0 deletions
|
@ -197,6 +197,17 @@ if (BUILD_LAGOM)
|
||||||
target_link_libraries(sql_lagom Lagom)
|
target_link_libraries(sql_lagom Lagom)
|
||||||
target_link_libraries(sql_lagom stdc++)
|
target_link_libraries(sql_lagom stdc++)
|
||||||
|
|
||||||
|
add_executable(test-iodevice ../../Tests/LibCore/TestLibCoreIODevice.cpp ${LIBTEST_MAIN})
|
||||||
|
set_target_properties(test-iodevice PROPERTIES OUTPUT_NAME test-iodevice)
|
||||||
|
target_link_libraries(test-iodevice Lagom)
|
||||||
|
target_link_libraries(test-iodevice LagomTest)
|
||||||
|
target_link_libraries(test-iodevice stdc++)
|
||||||
|
add_test(
|
||||||
|
NAME test-iodevice
|
||||||
|
COMMAND test-iodevice
|
||||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../../Tests/LibCore
|
||||||
|
)
|
||||||
|
|
||||||
add_executable(wasm_lagom ../../Userland/Utilities/wasm.cpp)
|
add_executable(wasm_lagom ../../Userland/Utilities/wasm.cpp)
|
||||||
set_target_properties(wasm_lagom PROPERTIES OUTPUT_NAME wasm)
|
set_target_properties(wasm_lagom PROPERTIES OUTPUT_NAME wasm)
|
||||||
target_link_libraries(wasm_lagom Lagom)
|
target_link_libraries(wasm_lagom Lagom)
|
||||||
|
|
|
@ -2,8 +2,11 @@ set(
|
||||||
TEST_SOURCES
|
TEST_SOURCES
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreArgsParser.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreArgsParser.cpp
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreFileWatcher.cpp
|
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreFileWatcher.cpp
|
||||||
|
${CMAKE_CURRENT_SOURCE_DIR}/TestLibCoreIODevice.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
foreach(source ${TEST_SOURCES})
|
foreach(source ${TEST_SOURCES})
|
||||||
serenity_test(${source} LibCore)
|
serenity_test(${source} LibCore)
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
|
install(FILES long_lines.txt DESTINATION usr/Tests/LibCore)
|
||||||
|
|
46
Tests/LibCore/TestLibCoreIODevice.cpp
Normal file
46
Tests/LibCore/TestLibCoreIODevice.cpp
Normal file
|
@ -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]);
|
||||||
|
}
|
||||||
|
}
|
6
Tests/LibCore/long_lines.txt
Normal file
6
Tests/LibCore/long_lines.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<small>(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]].)</small>
|
||||||
|
|
||||||
|
<small>[[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|使用中文阅读本信息。]]</
|
||||||
|
<small>(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]].)</small>
|
||||||
|
|
||||||
|
<small>[[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|使用中文阅读本信息。]]</
|
Loading…
Add table
Add a link
Reference in a new issue