From 5914418fb1fc2a389817919f0e7a324cc83d7c0e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Wed, 29 Jul 2015 22:37:12 -0400 Subject: [PATCH] Test: Corrected util.cpp/confirm calls to check std::cin::eof --- src/util.cpp | 26 +++++++++++++++----------- test/delete.t | 4 ++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index d2d55d5a7..1f4e583db 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -77,13 +77,13 @@ bool confirm (const std::string& question) std::string answer {""}; std::getline (std::cin, answer); context.debug ("STDIN '" + answer + "'"); - answer = std::cin.eof() ? STRING_UTIL_CONFIRM_NO : lowerCase (trim (answer)); + answer = std::cin.eof () ? STRING_UTIL_CONFIRM_NO : lowerCase (trim (answer)); autoComplete (answer, options, matches, 1); // Hard-coded 1. } - while (matches.size () != 1); + while (! std::cin.eof () && matches.size () != 1); - return matches[0] == STRING_UTIL_CONFIRM_YES ? true : false; + return matches.size () == 1 && matches[0] == STRING_UTIL_CONFIRM_YES ? true : false; } //////////////////////////////////////////////////////////////////////////////// @@ -114,17 +114,21 @@ int confirm4 (const std::string& question) std::string answer {""}; std::getline (std::cin, answer); context.debug ("STDIN '" + answer + "'"); - answer = trim (answer); + answer = std::cin.eof () ? STRING_UTIL_CONFIRM_QUIT : lowerCase (trim (answer)); autoComplete (answer, options, matches, 1); // Hard-coded 1. } - while (matches.size () != 1); + while (! std::cin.eof () && matches.size () != 1); - if (matches[0] == STRING_UTIL_CONFIRM_YES_U) return 1; - else if (matches[0] == STRING_UTIL_CONFIRM_YES) return 1; - else if (matches[0] == STRING_UTIL_CONFIRM_ALL_U) return 2; - else if (matches[0] == STRING_UTIL_CONFIRM_ALL) return 2; - else if (matches[0] == STRING_UTIL_CONFIRM_QUIT) return 3; - else return 0; + if (matches.size () == 1) + { + if (matches[0] == STRING_UTIL_CONFIRM_YES_U) return 1; + else if (matches[0] == STRING_UTIL_CONFIRM_YES) return 1; + else if (matches[0] == STRING_UTIL_CONFIRM_ALL_U) return 2; + else if (matches[0] == STRING_UTIL_CONFIRM_ALL) return 2; + else if (matches[0] == STRING_UTIL_CONFIRM_QUIT) return 3; + } + + return 0; } //////////////////////////////////////////////////////////////////////////////// diff --git a/test/delete.t b/test/delete.t index fd5ff1571..a6415e347 100755 --- a/test/delete.t +++ b/test/delete.t @@ -106,7 +106,7 @@ class TestDelete(TestCase): # Would expect 2 yes via input only 1 sent code, out, err = self._validate_prompt_loop(input="y\n") - self.assertEqual(code, 0) + self.assertEqual(code, 1) def test_delete_bulk_prompt_loop(self): """Delete prompt with closed STDIN causes infinite loop and floods stdout (bulk)""" @@ -121,7 +121,7 @@ class TestDelete(TestCase): # Would expect 3 yes via input only 2 sent code, out, err = self._validate_prompt_loop(input="y\ny\n") - self.assertEqual(code, 0) + self.assertEqual(code, 1) def _validate_prompt_loop(self, input=""): """Helper method to check if task flooded stream on closed STDIN"""