Skip to content

Commit

Permalink
test status display
Browse files Browse the repository at this point in the history
  • Loading branch information
oir committed Jul 20, 2024
1 parent 80cc76f commit 6cc1a42
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
5 changes: 3 additions & 2 deletions barkeep/barkeep.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ struct AnimationConfig {

/// interval in which the animation is refreshed
std::variant<Duration, double> interval = Duration{0.};
bool no_tty = false; ///< no-tty mode if true (no \r, slower default refresh)
bool show = true; ///< show the animation immediately after construction
bool no_tty =
false; ///< no-tty mode if true (no `\r`, slower default refresh)
bool show = true; ///< show the animation immediately after construction
};

Duration as_duration(std::variant<Duration, double> interval) {
Expand Down
57 changes: 53 additions & 4 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ bool startswith(const std::string& s, const std::string& prefix) {
}

auto check_and_get_parts(const std::string& s, bool no_tty = false) {
static const std::string crcl = "\r\033[K";
static const std::string crcl = "\r\033[K"; // carriage return clear line
if (not no_tty) { REQUIRE(startswith(s, crcl)); }
REQUIRE(s.back() == '\n');

Expand All @@ -67,8 +67,24 @@ void check_anim(const std::vector<std::string>& parts,
}
}

void check_status(const std::vector<std::string>& parts,
const std::vector<std::string>& messages,
const std::vector<std::string>& stills) {
size_t msg_i = 0;
for (size_t i = 0; i < parts.size() - 1; i++) {
size_t j = i % stills.size();
auto& part = parts[i];
auto msg = messages.at(msg_i);
if (part != (msg + " " + stills[j] + " ")) {
msg_i++;
msg = messages.at(msg_i);
}
CHECK(part == (msg + " " + stills[j] + " "));
}
}

TEST_CASE("Animation default", "[anim]") {
auto anim = Animation({.show=false});
auto anim = Animation({.show = false});
anim.done();
}

Expand Down Expand Up @@ -116,14 +132,41 @@ TEST_CASE("Animation custom", "[anim]") {
check_anim(parts, "Working", sty);
}

TEST_CASE("Status", "[status]") {
std::stringstream out;

auto sty = GENERATE(Ellipsis, Clock, Moon, Earth, Bar, Bounce);
auto no_tty = GENERATE(true, false);
auto interval = GENERATE(as<std::variant<Duration, double>>(), 100ms, 0.1);

auto stat = Status({
.out = &out,
.message = "Working",
.style = sty,
.interval = interval,
.no_tty = no_tty,
});

std::this_thread::sleep_for(0.5s);
stat.message("Still working");
std::this_thread::sleep_for(0.5s);
stat.message("Done");
stat.done();

auto parts = check_and_get_parts(out.str(), no_tty);
check_status(parts,
{"Working", "Still working", "Done"},
animation_stills_[size_t(sty)].first);
}

using ProgressTypeList =
std::tuple<size_t, std::atomic<size_t>, int, unsigned, float, double>;

TEMPLATE_LIST_TEST_CASE("Counter default", "[counter]", ProgressTypeList) {
using ValueType = value_t<TestType>;
TestType amount{GENERATE(as<ValueType>(), 0, 3)};

auto ctr = Counter(&amount, {.show=false});
auto ctr = Counter(&amount, {.show = false});
ctr.done();
}

Expand Down Expand Up @@ -265,6 +308,12 @@ Animation factory_helper<Animation>(bool /*speedy*/) {
return Animation({.out = &hide, .show = false});
}

template <>
Status factory_helper<Status>(bool /*speedy*/) {
static std::stringstream hide;
return Status({.out = &hide, .show = false});
}

template <>
Counter<> factory_helper<Counter<>>(bool speedy) {
static size_t progress;
Expand Down Expand Up @@ -301,7 +350,7 @@ Composite factory_helper<Composite>(bool speedy) {
}

using DisplayTypes =
std::tuple<Animation, Counter<>, ProgressBar<float>, Composite>;
std::tuple<Animation, Status, Counter<>, ProgressBar<float>, Composite>;

TEMPLATE_LIST_TEST_CASE("Error cases", "[edges]", DisplayTypes) {
auto orig = factory_helper<TestType>(GENERATE(true, false));
Expand Down

0 comments on commit 6cc1a42

Please sign in to comment.