def test_single_letter_responses(self): with patch_stdin('y\n'): with redirect_output() as out: output = boolean_prompt("Yes or no?") self.assertEqual(out.getvalue(), "Yes or no? [y/n] ") self.assertTrue(output) with patch_stdin('N\n'): with redirect_output() as out: output = boolean_prompt("Yes or no?") self.assertEqual(out.getvalue(), "Yes or no? [y/n] ") self.assertFalse(output)
def test_different_capitalizations(self): with patch_stdin('Yes\n'): with redirect_output() as out: output = boolean_prompt("Yes or no?") self.assertEqual(out.getvalue(), "Yes or no? [y/n] ") self.assertTrue(output) with patch_stdin('NO\n'): with redirect_output() as out: output = boolean_prompt("Yes or no?") self.assertEqual(out.getvalue(), "Yes or no? [y/n] ") self.assertFalse(output)
def test_default_with_valid_response(self): with patch_stdin('nope\nNo\n'): with redirect_output(StringIO()) as out: output = boolean_prompt("Yes or no?", default=True) self.assertEqual( out.getvalue(), "Yes or no? [Y/n] " "\nPlease enter 'yes' or 'no'.\n\n" "Yes or no? [Y/n] ") self.assertFalse(output) with patch_stdin('YES\n'): with redirect_output(StringIO()) as out: output = boolean_prompt("Yes or no?", default=True) self.assertEqual(out.getvalue(), "Yes or no? [Y/n] ") self.assertTrue(output)
def test_default_with_nonblank_responses(self): with patch_stdin('yep\nyes\n'): with redirect_output(StringIO()) as out: output = boolean_prompt("Yes or no?", default=True) self.assertEqual( out.getvalue(), "Yes or no? [Y/n] " "\nPlease enter 'yes' or 'no'.\n\n" "Yes or no? [Y/n] ") self.assertTrue(output)
def test_first_response_invalid(self): with patch_stdin('yeah\nyes'): with redirect_output(StringIO()) as out: output = boolean_prompt("Yes or no?") self.assertEqual( out.getvalue(), "Yes or no? [y/n] " "\nPlease enter 'yes' or 'no'.\n\n" "Yes or no? [y/n] ") self.assertTrue(output)
def test_multiple_invalid_responses(self): with patch_stdin('eh\n\nnope\nno'): with redirect_output(StringIO()) as out: output = boolean_prompt("Yes or no?") self.assertEqual( out.getvalue(), "Yes or no? [y/n] " "\nPlease enter 'yes' or 'no'.\n\n" "Yes or no? [y/n] " "\nPlease enter 'yes' or 'no'.\n\n" "Yes or no? [y/n] " "\nPlease enter 'yes' or 'no'.\n\n" "Yes or no? [y/n] ") self.assertFalse(output)
def test_default_of_false(self): with patch_stdin('\n'): with redirect_output(StringIO()) as out: output = boolean_prompt("Yes or no?", default=False) self.assertEqual(out.getvalue(), "Yes or no? [y/N] ") self.assertFalse(output)
def test_whitespace(self): with patch_stdin(' yes \n'): with redirect_output() as out: output = boolean_prompt("Is the sky blue?") self.assertEqual(out.getvalue(), "Is the sky blue? [y/n] ") self.assertTrue(output)
def test_no(self): with patch_stdin('no\n'): with redirect_output() as out: output = boolean_prompt("Yes or no?") self.assertEqual(out.getvalue(), "Yes or no? [y/n] ") self.assertFalse(output)