libcryfs/test/cpp-utils/io/ConsoleTest.h

88 lines
2.7 KiB
C
Raw Normal View History

2015-10-15 12:58:16 +02:00
#pragma once
2015-09-12 20:07:44 +02:00
#ifndef MESSMER_CPPUTILS_TEST_IO_CONSOLETEST_H
#define MESSMER_CPPUTILS_TEST_IO_CONSOLETEST_H
#include <gtest/gtest.h>
2015-09-12 20:07:44 +02:00
2016-09-24 20:47:53 +02:00
#include "cpp-utils/io/IOStreamConsole.h"
2015-09-12 20:07:44 +02:00
#include <future>
#include <thread>
2016-02-11 12:53:42 +01:00
#include "cpp-utils/io/pipestream.h"
2015-09-12 20:07:44 +02:00
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]() {
2016-09-24 20:47:53 +02:00
return _console.askYesNo(question, true);
2015-09-12 20:07:44 +02:00
});
}
2018-09-04 01:51:59 +02:00
std::future<std::string> askPassword(const std::string &question) {
return std::async(std::launch::async, [this, question]() {
return _console.askPassword(question);
});
}
2015-09-12 20:07:44 +02:00
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;
2015-09-12 20:07:44 +02:00
_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);
}
2018-09-04 01:51:59 +02:00
std::future<std::string> askPassword(const std::string &question) {
return _console.askPassword(question);
}
2015-09-12 20:07:44 +02:00
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