Skip to content

Commit

Permalink
libdnf5-cli unit tests: Use only public API methods
Browse files Browse the repository at this point in the history
The libdnf5-cli progressbar unit tests unnecessarily used the protected
method `DownloadProgressBar::to_stream`. The libdnf5-cli library
publicly provides the `<<` operator (API) for `DownloadProgressBar`.
This operator does the same thing and internally uses the `to_stream`
method.
  • Loading branch information
jrohel committed Jan 8, 2025
1 parent f6b851a commit 6e03840
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 20 deletions.
13 changes: 2 additions & 11 deletions test/libdnf5-cli/test_progressbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "test_progressbar.hpp"

#include "../shared/private_accessor.hpp"
#include "../shared/utils.hpp"

#include <libdnf5-cli/progressbar/download_progress_bar.hpp>
Expand All @@ -29,14 +28,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

CPPUNIT_TEST_SUITE_REGISTRATION(ProgressbarTest);

namespace {

// Allows accessing private methods
create_private_getter_template;
create_getter(to_stream, &libdnf5::cli::progressbar::DownloadProgressBar::to_stream);

} //namespace

void ProgressbarTest::setUp() {
// MultiProgressBar behaves differently depending on interactivity
setenv("DNF5_FORCE_INTERACTIVE", "0", 1);
Expand All @@ -58,13 +49,13 @@ void ProgressbarTest::test_download_progress_bar() {
auto download_progress_bar_raw = download_progress_bar.get();

std::ostringstream oss;
(*download_progress_bar.*get(to_stream{}))(oss);
oss << *download_progress_bar;
Pattern expected = "";
ASSERT_MATCHES(expected, oss.str());

download_progress_bar_raw->set_ticks(10);
download_progress_bar_raw->set_state(libdnf5::cli::progressbar::ProgressBarState::SUCCESS);
(*download_progress_bar.*get(to_stream{}))(oss);
oss << *download_progress_bar;

expected = "\\[0/0\\] test 100% | ????? ??B\\/s | 10.0 B | ???????";
ASSERT_MATCHES(expected, oss.str());
Expand Down
14 changes: 5 additions & 9 deletions test/libdnf5-cli/test_progressbar_interactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

#include "test_progressbar_interactive.hpp"

#include "../shared/private_accessor.hpp"
#include "../shared/utils.hpp"
#include "utils/string.hpp"

Expand Down Expand Up @@ -101,10 +100,6 @@ std::string perform_control_sequences(std::string target) {
return libdnf5::utils::string::join(output, "\n");
}

// Allows accessing private methods
create_private_getter_template;
create_getter(to_stream, &libdnf5::cli::progressbar::DownloadProgressBar::to_stream);

} //namespace

void ProgressbarInteractiveTest::setUp() {
Expand Down Expand Up @@ -156,14 +151,15 @@ void ProgressbarInteractiveTest::test_download_progress_bar() {
auto download_progress_bar_raw = download_progress_bar.get();

std::ostringstream oss;
(*download_progress_bar.*get(to_stream{}))(oss);
oss << *download_progress_bar;

Pattern expected = "\\[0/0\\] test 40% | ????? ??B\\/s | 4.0 B | ???????";
ASSERT_MATCHES(expected, oss.str());

download_progress_bar_raw->set_ticks(10);
download_progress_bar_raw->set_state(libdnf5::cli::progressbar::ProgressBarState::SUCCESS);
oss.str("");
(*download_progress_bar.*get(to_stream{}))(oss);
oss << *download_progress_bar;

expected = "\\[0/0\\] test 100% | ????? ??B\\/s | 10.0 B | ???????";
ASSERT_MATCHES(expected, oss.str());
Expand All @@ -180,7 +176,7 @@ void ProgressbarInteractiveTest::test_download_progress_bar_with_messages() {
download_progress_bar->add_message(libdnf5::cli::progressbar::MessageType::INFO, "test もで 諤奯ゞ");

std::ostringstream oss;
(*download_progress_bar.*get(to_stream{}))(oss);
oss << *download_progress_bar;
Pattern expected =
"\\[0/0\\] test 40% | ????? ??B\\/s | 4.0 B | ???????\n"
">>> test message1 \n"
Expand All @@ -193,7 +189,7 @@ void ProgressbarInteractiveTest::test_download_progress_bar_with_messages() {
download_progress_bar->pop_message();

oss.str("");
(*download_progress_bar.*get(to_stream{}))(oss);
oss << *download_progress_bar;
expected = "\\[0/0\\] test 40% | ????? ??B\\/s | 4.0 B | ???????";
ASSERT_MATCHES(expected, oss.str());
}
Expand Down

0 comments on commit 6e03840

Please sign in to comment.