File: ConsoleTest.h

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 (87 lines) | stat: -rw-r--r-- 2,723 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#pragma once
#ifndef MESSMER_CPPUTILS_TEST_IO_CONSOLETEST_H
#define MESSMER_CPPUTILS_TEST_IO_CONSOLETEST_H

#include <gtest/gtest.h>

#include "cpp-utils/io/IOStreamConsole.h"

#include <future>
#include <thread>
#include "cpp-utils/io/pipestream.h"

class ConsoleThread {
public:
    ConsoleThread(std::ostream *ostr, std::istream *istr): _console(ostr, istr) {}
    std::future<unsigned int> ask(const std::string &question, const std::vector<std::string> &options) {
        return std::async(std::launch::async, [this, question, options]() {
            return _console.ask(question, options);
        });
    }
    std::future<bool> askYesNo(const std::string &question) {
        return std::async(std::launch::async, [this, question]() {
            return _console.askYesNo(question, true);
        });
    }
    std::future<std::string> askPassword(const std::string &question) {
        return std::async(std::launch::async, [this, question]() {
            return _console.askPassword(question);
        });
    }
    void print(const std::string &output) {
        _console.print(output);
    }
private:
    cpputils::IOStreamConsole _console;
};

class ConsoleTest: public ::testing::Test {
public:
    ConsoleTest(): _inputStr(), _outputStr(), _input(&_inputStr), _output(&_outputStr), _console(&_output, &_input) {}

    void EXPECT_OUTPUT_LINES(std::initializer_list<std::string> lines) {
        for (const std::string &line : lines) {
            EXPECT_OUTPUT_LINE(line);
        }
    }

    void EXPECT_OUTPUT_LINE(const std::string &expected, char delimiter = '\n', const std::string &expected_after_delimiter = "") {
        std::string actual;
        std::getline(_output, actual, delimiter);
        EXPECT_EQ(expected, actual);
        for (const char expected_char : expected_after_delimiter) {
            char actual_char = 0;
            _output.get(actual_char);
            EXPECT_EQ(expected_char, actual_char);
        }
    }

    void sendInputLine(const std::string &line) {
        _input << line << "\n" << std::flush;
    }

    std::future<unsigned int> ask(const std::string &question, const std::vector<std::string> &options) {
        return _console.ask(question, options);
    }

    std::future<bool> askYesNo(const std::string &question) {
        return _console.askYesNo(question);
    }

    std::future<std::string> askPassword(const std::string &question) {
        return _console.askPassword(question);
    }

    void print(const std::string &output) {
        _console.print(output);
    }

private:
    cpputils::pipestream _inputStr;
    cpputils::pipestream _outputStr;
    std::iostream _input;
    std::iostream _output;
    ConsoleThread _console;
};

#endif