Skip to content

Commit

Permalink
dird: renamed FormatMulticolumnPrompts() to FormatPrompts()
Browse files Browse the repository at this point in the history
`FormatPrompts` instead of `FormatMulticolumnPrompts` makes
more sense since the function does multicolumn only on certain
conditions
Changed test names accordingly
  • Loading branch information
alaaeddineelamri committed Mar 18, 2022
1 parent 45704dd commit 9d15218
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 50 deletions.
18 changes: 8 additions & 10 deletions core/src/dird/ua_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2001-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2021 Bareos GmbH & Co. KG
Copyright (C) 2013-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -1093,14 +1093,13 @@ void AddPrompt(UaContext* ua, std::string&& prompt)
AddPrompt(ua, p.c_str());
}


/**
* Formats the prompts of a UaContext to be displayed in a multicolumn output
* when possible
*/
std::string FormatMulticolumnPrompts(const UaContext* ua,
const int window_width,
const int min_lines_threshold)
std::string FormatPrompts(const UaContext* ua,
const int window_width,
const int min_lines_threshold)
{
unsigned int max_prompt_length = 1;

Expand Down Expand Up @@ -1134,7 +1133,6 @@ std::string FormatMulticolumnPrompts(const UaContext* ua,
std::vector<std::vector<char>> formatted_prompts_container;

std::string output{};
int index = 0;

if (ua->num_prompts > min_lines_threshold
&& window_width > max_formatted_prompt_length * 2) {
Expand All @@ -1147,6 +1145,8 @@ std::string FormatMulticolumnPrompts(const UaContext* ua,

formatted_prompts_container.push_back(formatted_prompt);
}

int index = 0;
for (int i = 0; i < number_output_lines; i++) {
index = i;
while (static_cast<size_t>(index) < formatted_prompts_container.size()) {
Expand Down Expand Up @@ -1204,8 +1204,7 @@ int DoPrompt(UaContext* ua,
if (ua->batch) {
// First print the choices he wanted to make
ua->SendMsg(ua->prompt[0]);
ua->SendMsg(FormatMulticolumnPrompts(ua, window_width, min_lines_threshold)
.c_str());
ua->SendMsg(FormatPrompts(ua, window_width, min_lines_threshold).c_str());

// Now print error message
ua->SendMsg(_("Your request has multiple choices for \"%s\". Selection is "
Expand All @@ -1225,8 +1224,7 @@ int DoPrompt(UaContext* ua,
ua->SendMsg("%s", ua->prompt[i]);
}
} else {
ua->SendMsg(FormatMulticolumnPrompts(ua, window_width, min_lines_threshold)
.c_str());
ua->SendMsg(FormatPrompts(ua, window_width, min_lines_threshold).c_str());
}


Expand Down
8 changes: 4 additions & 4 deletions core/src/dird/ua_select.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2021 Bareos GmbH & Co. KG
Copyright (C) 2018-2022 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -59,9 +59,9 @@ bool SelectClientDbr(UaContext* ua, ClientDbRecord* cr);
void StartPrompt(UaContext* ua, const char* msg);
void AddPrompt(UaContext* ua, const char* prompt);
void AddPrompt(UaContext* ua, std::string&& prompt);
std::string FormatMulticolumnPrompts(const UaContext* ua,
int window_width,
int min_lines_threshold);
std::string FormatPrompts(const UaContext* ua,
int window_width,
int min_lines_threshold);
int DoPrompt(UaContext* ua,
const char* automsg,
const char* msg,
Expand Down
61 changes: 25 additions & 36 deletions core/src/tests/multicolumn_prompts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace directordaemon {
bool DoReloadConfig() { return false; }
} // namespace directordaemon

class MulticolumPrompts : public ::testing::Test {
class PromptsFormatting : public ::testing::Test {
protected:
void SetUp() override { ua = new_ua_context(&jcr); }

Expand All @@ -53,13 +53,12 @@ class MulticolumPrompts : public ::testing::Test {
UaContext* ua{};
};

TEST_F(MulticolumPrompts, ReturnsNothingOnAnEmptyList)
TEST_F(PromptsFormatting, ReturnsNothingOnAnEmptyList)
{
const char* list[] = {nullptr};
PopulateUaWithPrompts(ua, list);

std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(
Expand All @@ -68,13 +67,12 @@ TEST_F(MulticolumPrompts, ReturnsNothingOnAnEmptyList)
/* clang-format on */
}

TEST_F(MulticolumPrompts, ReturnsSingleElementWhenOnlyOnePromptIsAvailable)
TEST_F(PromptsFormatting, ReturnsSingleElementWhenOnlyOnePromptIsAvailable)
{
const char* list[] = {_("bareos1"), nullptr};
PopulateUaWithPrompts(ua, list);

std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(
Expand All @@ -83,7 +81,7 @@ TEST_F(MulticolumPrompts, ReturnsSingleElementWhenOnlyOnePromptIsAvailable)
/* clang-format on */
}

TEST_F(MulticolumPrompts, Formatting10Elements_StandardWidthNoThreshold)
TEST_F(PromptsFormatting, Formatting10Elements_StandardWidthNoThreshold)
{
const char* list[] = {_("bareos1"), _("bareos2"), _("bareos3"), _("bareos4"),
_("bareos5"), _("bareos6"), _("bareos7"), _("bareos8"),
Expand All @@ -92,8 +90,7 @@ TEST_F(MulticolumPrompts, Formatting10Elements_StandardWidthNoThreshold)
PopulateUaWithPrompts(ua, list);

lines_threshold = 0;
std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(
Expand All @@ -104,7 +101,7 @@ TEST_F(MulticolumPrompts, Formatting10Elements_StandardWidthNoThreshold)
/* clang-format on */
}

TEST_F(MulticolumPrompts, Formatting15Elements_StandardWidthNoThreshold)
TEST_F(PromptsFormatting, Formatting15Elements_StandardWidthNoThreshold)
{
const char* list[] = {
_("bareos1"), _("bareos2"), _("bareos3"), _("bareos4"), _("bareos5"),
Expand All @@ -116,8 +113,7 @@ TEST_F(MulticolumPrompts, Formatting15Elements_StandardWidthNoThreshold)
PopulateUaWithPrompts(ua, list);

lines_threshold = 0;
std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);
/* clang-format off */
EXPECT_STREQ(
output.c_str(),
Expand All @@ -127,7 +123,7 @@ TEST_F(MulticolumPrompts, Formatting15Elements_StandardWidthNoThreshold)
/* clang-format on */
}

TEST_F(MulticolumPrompts, Formatting16Elements_StandardWidthNoThreshold)
TEST_F(PromptsFormatting, Formatting16Elements_StandardWidthNoThreshold)
{
const char* list[] = {
_("bareos1"), _("bareos2"), _("bareos3"), _("bareos4"), _("bareos5"),
Expand All @@ -138,8 +134,7 @@ TEST_F(MulticolumPrompts, Formatting16Elements_StandardWidthNoThreshold)
PopulateUaWithPrompts(ua, list);

lines_threshold = 0;
std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(
Expand All @@ -150,7 +145,7 @@ TEST_F(MulticolumPrompts, Formatting16Elements_StandardWidthNoThreshold)
/* clang-format on */
}

TEST_F(MulticolumPrompts, Formatting21Elements_StandardWidthNoThreshold)
TEST_F(PromptsFormatting, Formatting21Elements_StandardWidthNoThreshold)
{
const char* list[] = {
_("bareos1"), _("bareos2"), _("bareos3"), _("bareos4"), _("bareos5"),
Expand All @@ -162,8 +157,7 @@ TEST_F(MulticolumPrompts, Formatting21Elements_StandardWidthNoThreshold)
PopulateUaWithPrompts(ua, list);

lines_threshold = 0;
std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(
Expand All @@ -175,15 +169,14 @@ TEST_F(MulticolumPrompts, Formatting21Elements_StandardWidthNoThreshold)
/* clang-format on */
}

TEST_F(MulticolumPrompts,
TEST_F(PromptsFormatting,
NoMulticolumnformattingWhenNumberOfElementsLessThanThreshold)
{
const char* list[] = {_("List last 20 Jobs run"), _("Cancel"), nullptr};

PopulateUaWithPrompts(ua, list);

std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(
Expand All @@ -193,7 +186,7 @@ TEST_F(MulticolumPrompts,
/* clang-format on */
}

TEST_F(MulticolumPrompts, FormatsForVeryLargeWidth)
TEST_F(PromptsFormatting, FormatsForVeryLargeWidth)
{
const char* list[]
= {_("List last 20 Jobs run"),
Expand All @@ -215,8 +208,7 @@ TEST_F(MulticolumPrompts, FormatsForVeryLargeWidth)

window_width = 5000;
lines_threshold = 10;
std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(
Expand All @@ -237,7 +229,7 @@ TEST_F(MulticolumPrompts, FormatsForVeryLargeWidth)
/* clang-format on */
}

TEST_F(MulticolumPrompts, Format15Elements_SmallWidth10LineThreshold)
TEST_F(PromptsFormatting, Format15Elements_SmallWidth10LineThreshold)
{
const char* list[] = {
_("bareos1"), _("bareos2"), _("bareos3"), _("bareos4"), _("bareos5"),
Expand All @@ -250,8 +242,7 @@ TEST_F(MulticolumPrompts, Format15Elements_SmallWidth10LineThreshold)

window_width = 60;
lines_threshold = 10;
std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(output.c_str(),
Expand All @@ -262,7 +253,7 @@ TEST_F(MulticolumPrompts, Format15Elements_SmallWidth10LineThreshold)
/* clang-format on */
}

TEST_F(MulticolumPrompts, Formatting_NoWidth)
TEST_F(PromptsFormatting, Formatting_NoWidth)
{
const char* list[]
= {_("bareos1"), _("bareos2"), _("bareos3"), _("bareos4"),
Expand All @@ -272,8 +263,7 @@ TEST_F(MulticolumPrompts, Formatting_NoWidth)
PopulateUaWithPrompts(ua, list);

window_width = 0;
std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(output.c_str(),
Expand All @@ -291,16 +281,15 @@ TEST_F(MulticolumPrompts, Formatting_NoWidth)
/* clang-format on */
}

TEST_F(MulticolumPrompts,
TEST_F(PromptsFormatting,
FormatPromptsContainingSpacesAndRegularPrompts_StandartWidthNoThreshold)
{
const char* list[] = {_(""), _("Listsaved"), _("Cancel"), nullptr};

PopulateUaWithPrompts(ua, list);

lines_threshold = 0;
std::string output
= FormatMulticolumnPrompts(ua, window_width, lines_threshold);
std::string output = FormatPrompts(ua, window_width, lines_threshold);

/* clang-format off */
EXPECT_STREQ(output.c_str(),
Expand All @@ -310,14 +299,14 @@ TEST_F(MulticolumPrompts,
/* clang-format on */
}

TEST_F(MulticolumPrompts,
TEST_F(PromptsFormatting,
FormatPromptsContainingOnlySpacesPrompts_StandartWidthNoThreshold)
{
const char* list[] = {_(""), _(" "), _(" "), nullptr};

PopulateUaWithPrompts(ua, list);

std::string output = FormatMulticolumnPrompts(ua, 80, 20);
std::string output = FormatPrompts(ua, 80, 20);

/* clang-format off */
EXPECT_STREQ(output.c_str(),
Expand Down

0 comments on commit 9d15218

Please sign in to comment.