File: ProgressBarTest.cpp

package info (click to toggle)
cryfs 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,412 kB
  • sloc: cpp: 150,187; asm: 10,493; python: 1,455; javascript: 65; sh: 50; makefile: 17; xml: 7
file content (66 lines) | stat: -rw-r--r-- 1,554 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <cpp-utils/io/ProgressBar.h>
#include <gmock/gmock.h>

using cpputils::ProgressBar;
using std::make_shared;

class MockConsole final: public cpputils::Console {
public:
    void EXPECT_OUTPUT(const char* expected) {
        EXPECT_EQ(expected, _output);
        _output = "";
    }

    void print(const std::string& text) override {
        _output += text;
    }

    unsigned int ask(const std::string&, const std::vector<std::string>&) override {
        EXPECT_TRUE(false);
        return 0;
    }

    bool askYesNo(const std::string&, bool) override {
        EXPECT_TRUE(false);
        return false;
    }

    std::string askPassword(const std::string&) override {
        EXPECT_TRUE(false);
        return "";
    }

private:
    std::string _output;
};

TEST(ProgressBarTest, testProgressBar) {
    auto console = make_shared<MockConsole>();

    ProgressBar bar(console, "Preamble", 2000);
    console->EXPECT_OUTPUT("\n\rPreamble 0%");

    // when updating to 0, doesn't reprint
    bar.update(0);
    console->EXPECT_OUTPUT("");

    // update to half
    bar.update(1000);
    console->EXPECT_OUTPUT("\rPreamble 50%");

    // when updating to same value, doesn't reprint
    bar.update(1000);
    console->EXPECT_OUTPUT("");

    // when updating to value with same percentage, doesn't reprint
    bar.update(1001);
    console->EXPECT_OUTPUT("");

    // update to 0
    bar.update(0);
    console->EXPECT_OUTPUT("\rPreamble 0%");

    // update to full
    bar.update(2000);
    console->EXPECT_OUTPUT("\rPreamble 100%");
}