Don't print a newline before asking i/o questions

This commit is contained in:
Sebastian Messmer 2016-01-25 14:20:16 +01:00
parent 10ac4eef29
commit 36cac2f3da
3 changed files with 5 additions and 17 deletions

View File

@ -67,7 +67,7 @@ unsigned int IOStreamConsole::ask(const string &question, const vector<string> &
if(options.size() == 0) { if(options.size() == 0) {
throw std::invalid_argument("options should have at least one entry"); throw std::invalid_argument("options should have at least one entry");
} }
_output << "\n" << question << "\n"; _output << question << "\n";
for (unsigned int i = 0; i < options.size(); ++i) { for (unsigned int i = 0; i < options.size(); ++i) {
_output << " [" << (i+1) << "] " << options[i] << "\n"; _output << " [" << (i+1) << "] " << options[i] << "\n";
} }
@ -90,7 +90,7 @@ function<optional<bool>(const string &input)> parseYesNo() {
} }
bool IOStreamConsole::askYesNo(const string &question) { bool IOStreamConsole::askYesNo(const string &question) {
_output << "\n" << question << "\n"; _output << question << "\n";
return _askForChoice("Your choice [y/n]: ", parseYesNo()); return _askForChoice("Your choice [y/n]: ", parseYesNo());
} }

View File

@ -20,7 +20,6 @@ TEST_F(ConsoleTest_Ask, CrashesWithoutOptions) {
TEST_F(ConsoleTest_Ask, OneOption) { TEST_F(ConsoleTest_Ask, OneOption) {
auto chosen = ask("My Question?", {"First Option"}); auto chosen = ask("My Question?", {"First Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] First Option" " [1] First Option"
}); });
@ -32,7 +31,6 @@ TEST_F(ConsoleTest_Ask, OneOption) {
TEST_F(ConsoleTest_Ask, TwoOptions_ChooseFirst) { TEST_F(ConsoleTest_Ask, TwoOptions_ChooseFirst) {
auto chosen = ask("My Question?", {"First Option", "Second Option"}); auto chosen = ask("My Question?", {"First Option", "Second Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] First Option", " [1] First Option",
" [2] Second Option" " [2] Second Option"
@ -45,7 +43,6 @@ TEST_F(ConsoleTest_Ask, TwoOptions_ChooseFirst) {
TEST_F(ConsoleTest_Ask, TwoOptions_ChooseSecond) { TEST_F(ConsoleTest_Ask, TwoOptions_ChooseSecond) {
auto chosen = ask("My Question?", {"First Option", "Second Option"}); auto chosen = ask("My Question?", {"First Option", "Second Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] First Option", " [1] First Option",
" [2] Second Option" " [2] Second Option"
@ -58,7 +55,6 @@ TEST_F(ConsoleTest_Ask, TwoOptions_ChooseSecond) {
TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseFirst) { TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseFirst) {
auto chosen = ask("My Other Question?", {"1st Option", "2nd Option", "3rd Option"}); auto chosen = ask("My Other Question?", {"1st Option", "2nd Option", "3rd Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Other Question?", "My Other Question?",
" [1] 1st Option", " [1] 1st Option",
" [2] 2nd Option", " [2] 2nd Option",
@ -72,7 +68,6 @@ TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseFirst) {
TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseSecond) { TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseSecond) {
auto chosen = ask("My Question?", {"1st Option", "2nd Option", "3rd Option"}); auto chosen = ask("My Question?", {"1st Option", "2nd Option", "3rd Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] 1st Option", " [1] 1st Option",
" [2] 2nd Option", " [2] 2nd Option",
@ -86,7 +81,6 @@ TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseSecond) {
TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseThird) { TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseThird) {
auto chosen = ask("My Question?", {"1st Option", "2nd Option", "3rd Option"}); auto chosen = ask("My Question?", {"1st Option", "2nd Option", "3rd Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] 1st Option", " [1] 1st Option",
" [2] 2nd Option", " [2] 2nd Option",
@ -100,7 +94,6 @@ TEST_F(ConsoleTest_Ask, ThreeOptions_ChooseThird) {
TEST_F(ConsoleTest_Ask, InputWithLeadingSpaces) { TEST_F(ConsoleTest_Ask, InputWithLeadingSpaces) {
auto chosen = ask("My Question?", {"First Option", "Second Option"}); auto chosen = ask("My Question?", {"First Option", "Second Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] First Option", " [1] First Option",
" [2] Second Option" " [2] Second Option"
@ -113,7 +106,6 @@ TEST_F(ConsoleTest_Ask, InputWithLeadingSpaces) {
TEST_F(ConsoleTest_Ask, InputWithFollowingSpaces) { TEST_F(ConsoleTest_Ask, InputWithFollowingSpaces) {
auto chosen = ask("My Question?", {"First Option", "Second Option"}); auto chosen = ask("My Question?", {"First Option", "Second Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] First Option", " [1] First Option",
" [2] Second Option" " [2] Second Option"
@ -126,7 +118,6 @@ TEST_F(ConsoleTest_Ask, InputWithFollowingSpaces) {
TEST_F(ConsoleTest_Ask, InputWithLeadingAndFollowingSpaces) { TEST_F(ConsoleTest_Ask, InputWithLeadingAndFollowingSpaces) {
auto chosen = ask("My Question?", {"First Option", "Second Option"}); auto chosen = ask("My Question?", {"First Option", "Second Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] First Option", " [1] First Option",
" [2] Second Option" " [2] Second Option"
@ -139,7 +130,6 @@ TEST_F(ConsoleTest_Ask, InputWithLeadingAndFollowingSpaces) {
TEST_F(ConsoleTest_Ask, InputEmptyLine) { TEST_F(ConsoleTest_Ask, InputEmptyLine) {
auto chosen = ask("My Question?", {"First Option", "Second Option"}); auto chosen = ask("My Question?", {"First Option", "Second Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] First Option", " [1] First Option",
" [2] Second Option" " [2] Second Option"
@ -156,7 +146,6 @@ TEST_F(ConsoleTest_Ask, InputEmptyLine) {
TEST_F(ConsoleTest_Ask, InputWrongNumbers) { TEST_F(ConsoleTest_Ask, InputWrongNumbers) {
auto chosen = ask("My Question?", {"1st Option", "2nd Option"}); auto chosen = ask("My Question?", {"1st Option", "2nd Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] 1st Option", " [1] 1st Option",
" [2] 2nd Option", " [2] 2nd Option",
@ -179,7 +168,6 @@ TEST_F(ConsoleTest_Ask, InputWrongNumbers) {
TEST_F(ConsoleTest_Ask, InputNonNumbers) { TEST_F(ConsoleTest_Ask, InputNonNumbers) {
auto chosen = ask("My Question?", {"1st Option", "2nd Option"}); auto chosen = ask("My Question?", {"1st Option", "2nd Option"});
EXPECT_OUTPUT_LINES({ EXPECT_OUTPUT_LINES({
"",
"My Question?", "My Question?",
" [1] 1st Option", " [1] 1st Option",
" [2] 2nd Option", " [2] 2nd Option",

View File

@ -14,7 +14,7 @@ public:
void EXPECT_RESULT_ON_INPUT(const bool expected, const string &input) { void EXPECT_RESULT_ON_INPUT(const bool expected, const string &input) {
auto chosen = askYesNo("Are you sure blablub?"); auto chosen = askYesNo("Are you sure blablub?");
EXPECT_OUTPUT_LINES({"", "Are you sure blablub?"}); EXPECT_OUTPUT_LINES({"Are you sure blablub?"});
EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " "); EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " ");
sendInputLine(input); sendInputLine(input);
EXPECT_EQ(expected, chosen.get()); EXPECT_EQ(expected, chosen.get());
@ -67,7 +67,7 @@ TEST_F(ConsoleTest_AskYesNo, InputWithLeadingAndFollowingSpaces) {
TEST_F(ConsoleTest_AskYesNo, InputEmptyLine) { TEST_F(ConsoleTest_AskYesNo, InputEmptyLine) {
auto chosen = askYesNo("My Question?"); auto chosen = askYesNo("My Question?");
EXPECT_OUTPUT_LINES({"", "My Question?"}); EXPECT_OUTPUT_LINES({"My Question?"});
EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " "); EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " ");
sendInputLine(""); sendInputLine("");
EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " "); EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " ");
@ -79,7 +79,7 @@ TEST_F(ConsoleTest_AskYesNo, InputEmptyLine) {
TEST_F(ConsoleTest_AskYesNo, WrongInput) { TEST_F(ConsoleTest_AskYesNo, WrongInput) {
auto chosen = askYesNo("My Question?"); auto chosen = askYesNo("My Question?");
EXPECT_OUTPUT_LINES({"", "My Question?"}); EXPECT_OUTPUT_LINES({"My Question?"});
EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " "); EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " ");
sendInputLine("0"); sendInputLine("0");
EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " "); EXPECT_OUTPUT_LINE("Your choice [y/n]", ':', " ");