Skip to content

Commit

Permalink
Add CMake change and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebanner committed Aug 9, 2024
1 parent 78a83c2 commit 641051b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions Tests/Utilities/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(TEST_SOURCES
TestSed.cpp
TestPatch.cpp
TestUniq.cpp
TestCat.cpp
)

foreach(source IN LISTS TEST_SOURCES)
Expand Down
86 changes: 86 additions & 0 deletions Tests/Utilities/TestCat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2023, Rodrigo Tobar <[email protected]>.
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/ScopeGuard.h>
#include <AK/StringView.h>
#include <LibCore/Command.h>
#include <LibCore/File.h>
#include <LibTest/Macros.h>
#include <LibTest/TestCase.h>

static void run_cat(Vector<char const*>&& arguments, StringView standard_input, StringView expected_stdout)
{
MUST(arguments.try_insert(0, "cat"));
MUST(arguments.try_append(nullptr));
auto cat = MUST(Core::Command::create("cat"sv, arguments.data()));
MUST(cat->write(standard_input));
auto [stdout, stderr] = MUST(cat->read_all());
auto status = MUST(cat->status());
if (status != Core::Command::ProcessResult::DoneWithZeroExitCode) {
FAIL(ByteString::formatted("cat didn't exit cleanly: status: {}, stdout:{}, stderr: {}", static_cast<int>(status), StringView { stdout.bytes() }, StringView { stderr.bytes() }));
}

// // Print out stdout as raw bytes
// out("Raw stdout : "sv);
// for (auto& c : StringView { stdout.bytes() }) {
// out("{:02x} "sv, c);
// }
// out("\n"sv);
//
// // Print out stdout as raw bytes
// out("Raw expected stdout: "sv);
// for (auto& c : StringView { expected_stdout.bytes() }) {
// out("{:02x} "sv, c);
// }
// out("\n"sv);

// // Convert stdout to a stringview and print it out
// out("stdout:\n"sv);
// for (char c : StringView(stdout)) {
// if (c == '\t') {
// out("\\t"sv);
// } else if (c == '\n') {
// out("\\n"sv);
// } else {
// out("{}", StringView { &c, 1 });
// }
// }
// out("\n\n"sv);
//
// out("Expected stdout:\n"sv);
// for (char c : expected_stdout) {
// if (c == '\t') {
// out("\\t"sv);
// } else if (c == '\n') {
// out("\\n"sv);
// } else {
// out("{}", StringView { &c, 1 });
// }
// }
// out("\n"sv);

EXPECT_EQ(StringView { expected_stdout.bytes() }, StringView { stdout.bytes() });
}

TEST_CASE(show_lines)
{
run_cat({ "-n" }, "hello"sv, " 1\thello"sv);
run_cat({ "-n" }, "hello\nworld"sv, " 1\thello\n 2\tworld"sv);
run_cat({ "-n" }, "hello\n\nworld"sv, " 1\thello\n 2\t\n 3\tworld"sv);
run_cat({ "-n" }, "\nhello"sv, " 1\t\n 2\thello"sv);
run_cat({ "-n" }, "hello\n"sv, " 1\thello\n"sv);
run_cat({ "-n" }, "hello\n\n"sv, " 1\thello\n 2\t\n"sv);
}

TEST_CASE(show_only_non_blank_lines)
{
run_cat({ "-b" }, "hello"sv, " 1\thello"sv);
run_cat({ "-b" }, "hello\nworld"sv, " 1\thello\n 2\tworld"sv);
run_cat({ "-b" }, "hello\n\nworld"sv, " 1\thello\n\n 2\tworld"sv);
run_cat({ "-b" }, "\nhello"sv, "\n 1\thello"sv);
run_cat({ "-b" }, "hello\n"sv, " 1\thello\n"sv);
run_cat({ "-b" }, "hello\n\n"sv, " 1\thello\n\n"sv);
}

0 comments on commit 641051b

Please sign in to comment.