def test_prompt_yesnos(self, mock_input): """The input methods that ask yes/no questions should call _yesno_prompt() with a prompt and a default, and they should return the response from _yesno_prompt(). """ prompts = [ 'Double down?', 'Hit?', 'Insure?', 'Next game?', 'Split?', ] exp_resp = [model.IsYes('n') for _ in range(len(prompts))] exp_calls = [call(prompt, 'y') for prompt in prompts] mock_input.return_value = model.IsYes('n') ui = cli.LogUI() act_resp = [] act_resp.append(ui.doubledown_prompt()) act_resp.append(ui.hit_prompt()) act_resp.append(ui.insure_prompt()) act_resp.append(ui.nextgame_prompt()) act_resp.append(ui.split_prompt()) act_calls = mock_input.mock_calls self.assertListEqual(exp_calls, act_calls) self.assertListEqual(exp_resp, act_resp)
def test__yesnos(self, mock_yesno, _): """The individual yes/no prompts should sent their prompt and a default response value to _yesno_prompt and return the response. """ exp_resp = model.IsYes('y') exp_calls = [ call('Double down?', 'y'), call('Hit?', 'y'), call('Buy insurance?', 'y'), call('Play another round?', 'y'), call('Split your hand?', 'y'), ] mock_yesno.return_value = exp_resp ui = cli.TableUI() ui.start() act_resps = [] act_resps.append(ui.doubledown_prompt()) act_resps.append(ui.hit_prompt()) act_resps.append(ui.insure_prompt()) act_resps.append(ui.nextgame_prompt()) act_resps.append(ui.split_prompt()) act_calls = mock_yesno.mock_calls[-5:] ui.end() for act_resp in act_resps: self.assertEqual(exp_resp, act_resp) for exp, act in zip(exp_calls, act_calls): self.assertEqual(exp, act)
def _yesno_prompt(self, prompt: str, default: Union[str, bool] = True) -> model.IsYes: """Prompt the user for a yes/no answer.""" response = None fmt = '{} [yn] > ' # Repeat the prompt until you get a valid response. while not response: untrusted: Union[str, bool] = input(fmt.format(prompt)) # Allow the response to default to true. Saves typing when # playing. if not untrusted: untrusted = default # Determine if the input is valid. try: response = model.IsYes(untrusted) # If it's not valid, the ValueError will be caught, # response won't be set, so the prompt will be repeated. except ValueError: pass return response
def _yesno_prompt(self, prompt, default): prompt = f'{prompt} [yn] > ' valid = None while not valid: resp = self._prompt(prompt, default) try: valid = model.IsYes(resp) except ValueError: pass return valid
def test_insure(self, mock_input): """When the user chooses to double down, will_insure_user() returns True. """ expected = 10 mock_input.return_value = model.IsYes('y') g = game.Engine(None, None, None, None, expected * 2) actual = willinsure.will_insure_user(None, g) mock_input.assert_called() self.assertEqual(expected, actual)
def test_not_double_down(self, mock_input): """When the user chooses to double down, will_double_down_user() returns False. """ expected = False mock_input.return_value = model.IsYes(expected) g = game.Engine(None, None, None, None, None) actual = willdoubledown.will_double_down_user(None, None, g) mock_input.assert_called() self.assertEqual(expected, actual)
def test_stand(self, mock_input): """When the user chooses to split, will_split_user() returns False. """ expected = False mock_input.return_value = model.IsYes(expected) g = game.Engine(None, None, None, None, None) actual = willsplit.will_split_user(None, None, g) mock_input.assert_called() self.assertEqual(expected, actual)
def test__yesno_prompt_unit_valid(self, mock_main): """If the user responds with an invalid value, the prompt should be repeated. """ exp_resp = model.IsYes('n') ui = cli.TableUI() mock_main.return_value = (item for item in [None, None, 'z', ' ', 'n']) ui.start() act_resp = ui._yesno_prompt('spam', 'y') ui.end() self.assertEqual(exp_resp.value, act_resp.value)
def test_hit(self, mock_input): """When the user chooses to hit, will_hit_user() returns True. """ expected = True mock_input.return_value = model.IsYes(expected) ui = cli.TableUI() g = game.Engine(None, None, None, None, None) actual = willhit.will_hit_user(None, None, g) mock_input.assert_called() self.assertEqual(expected, actual)
def test_yesno_prompt(self, mock_input): """Given a prompt and a default value, prompt the use for a yes/no answer and return the result. """ exp_resp = model.IsYes('y') exp_call = call('spam [yn] > ') mock_input.return_value = 'y' ui = cli.LogUI() act_resp = ui._yesno_prompt('spam', 'y') act_call = mock_input.mock_calls[-1] self.assertEqual(exp_resp.value, act_resp.value) self.assertEqual(exp_call, act_call)
def test__yesno_prompt(self, mock_prompt, _): """When called, _yesno_prompt() should prompt the user for a yes/no answer. The response should be returned. """ exp_resp = model.IsYes('y') exp_call = call('Play another round? [yn] > ', 'y') ui = cli.TableUI() mock_prompt.return_value = 'y' ui.start() act_resp = ui._yesno_prompt('Play another round?', 'y') ui.end() act_call = mock_prompt.mock_calls[-1] self.assertEqual(exp_resp.value, act_resp.value) self.assertEqual(exp_call, act_call)
def mock_run_terminal_only_yesno(): values = [None, model.IsYes(True)] for value in values: yield value