def test_empty_default(self): """Tests functionality when default is explicitly None.""" self.mock.raw_input.side_effect = ['y', 'n', '', 'n'] self.assertTrue(common.confirm('A question', default=None)) self.assertFalse(common.confirm('A question', default=None)) self.assertFalse(common.confirm('A question', default=None)) msg = common.emphasize( common.colorize('A question [y/n]: ', common.BASH_MAGENTA_MARKER)) another_msg = common.emphasize( common.colorize('Please type either "y" or "n": ', common.BASH_MAGENTA_MARKER)) self.mock.raw_input.assert_has_calls([mock.call(msg)] * 3 + [mock.call(another_msg)]) self.assert_n_calls(4, [self.mock.raw_input])
def test_returns_when_correct(self): """Tests that the method only returns when the answer fits validation.""" question = 'Initial Question' error_message = 'Please answer correctly' validate_fn = lambda x: x == 'correct' result = common.ask(question, error_message, validate_fn) self.assert_n_calls(4, [self.mock.raw_input]) self.mock.raw_input.assert_has_calls([ mock.call( common.emphasize( common.colorize('Initial Question: ', common.BASH_MAGENTA_MARKER))), mock.call( common.emphasize( common.colorize('Please answer correctly: ', common.BASH_MAGENTA_MARKER))) ]) self.assertEqual(result, 'correct')
def test_no_default(self): """Tests functionality when no is the default.""" self.mock.raw_input.side_effect = ['y', 'n', ''] self.assertTrue(common.confirm('A question', default='n')) self.assertFalse(common.confirm('A question', default='n')) self.assertFalse(common.confirm('A question', default='n')) msg = common.emphasize( common.colorize('A question [y/N]: ', common.BASH_MAGENTA_MARKER)) self.mock.raw_input.assert_has_calls([mock.call(msg)] * 3) self.assert_n_calls(3, [self.mock.raw_input])
def test_emphasize(self): """Test emphasize.""" self.assertEqual('style', common.emphasize('s')) self.mock.style.assert_called_once_with('s', common.BASH_BOLD_MARKER, common.BASH_RESET_STYLE_MARKER)