ladybird/Tests/Utilities/TestSed.cpp
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

55 lines
1.8 KiB
C++

/*
* Copyright (c) 2023, Rodrigo Tobar <rtobarc@gmail.com>.
*
* 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_sed(Vector<char const*>&& arguments, StringView standard_input, StringView expected_stdout)
{
MUST(arguments.try_insert(0, "sed"));
MUST(arguments.try_append(nullptr));
auto sed = MUST(Core::Command::create("sed"sv, arguments.data()));
MUST(sed->write(standard_input));
auto [stdout, stderr] = MUST(sed->read_all());
auto status = MUST(sed->status());
if (status != Core::Command::ProcessResult::DoneWithZeroExitCode) {
FAIL(ByteString::formatted("sed didn't exit cleanly: status: {}, stdout:{}, stderr: {}", static_cast<int>(status), StringView { stdout.bytes() }, StringView { stderr.bytes() }));
}
EXPECT_EQ(StringView { expected_stdout.bytes() }, StringView { stdout.bytes() });
}
TEST_CASE(print_lineno)
{
run_sed({ "=", "-n" }, "hi"sv, "1\n"sv);
run_sed({ "=", "-n" }, "hi\n"sv, "1\n"sv);
run_sed({ "=", "-n" }, "hi\nho"sv, "1\n2\n"sv);
run_sed({ "=", "-n" }, "hi\nho\n"sv, "1\n2\n"sv);
}
TEST_CASE(s)
{
run_sed({ "s/a/b/g" }, "aa\n"sv, "bb\n"sv);
run_sed({ "s/././g" }, "aa\n"sv, "..\n"sv);
run_sed({ "s/a/b/p" }, "a\n"sv, "b\nb\n"sv);
run_sed({ "s/a/b/p", "-n" }, "a\n"sv, "b\n"sv);
run_sed({ "1s/a/b/" }, "a\na"sv, "b\na\n"sv);
run_sed({ "1s/a/b/p", "-n" }, "a\na"sv, "b\n"sv);
}
TEST_CASE(hold_space)
{
run_sed({ "1h; 2x; 2p", "-n" }, "hi\nbye"sv, "hi\n"sv);
}
TEST_CASE(complex)
{
run_sed({ "h; x; s/./*/gp; x; h; p; x; s/./*/gp", "-n" }, "hello serenity"sv, "**************\nhello serenity\n**************\n"sv);
}