| OLD | NEW |
| 1 # Copyright (C) 2010 Research in Motion Ltd. All rights reserved. | 1 # Copyright (C) 2010 Research in Motion Ltd. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 import unittest | 29 import unittest |
| 30 | 30 |
| 31 from webkitpy.common.system.outputcapture import OutputCapture | 31 from webkitpy.common.system.outputcapture import OutputCapture |
| 32 from webkitpy.common.system.user import User | 32 from webkitpy.common.system.user import User |
| 33 | 33 |
| 34 | 34 |
| 35 class UserTest(unittest.TestCase): | 35 class UserTest(unittest.TestCase): |
| 36 | 36 |
| 37 example_user_response = "example user response" | 37 example_user_response = "example user response" |
| 38 | 38 |
| 39 def setUp(self): | 39 def test_prompt_repeat(self): |
| 40 self.repeats_remaining = None | 40 self.repeatsRemaining = 2 |
| 41 | 41 |
| 42 def test_prompt_repeat(self): | 42 def mock_raw_input(message): |
| 43 self.repeats_remaining = 2 | 43 self.repeatsRemaining -= 1 |
| 44 | 44 if not self.repeatsRemaining: |
| 45 def mock_raw_input(_): | |
| 46 self.repeats_remaining -= 1 | |
| 47 if not self.repeats_remaining: | |
| 48 return UserTest.example_user_response | 45 return UserTest.example_user_response |
| 49 return None | 46 return None |
| 50 self.assertEqual(User.prompt("input", repeat=self.repeats_remaining, | 47 self.assertEqual(User.prompt("input", repeat=self.repeatsRemaining, |
| 51 raw_input_func=mock_raw_input), UserTest.ex
ample_user_response) | 48 raw_input=mock_raw_input), UserTest.example
_user_response) |
| 52 | 49 |
| 53 def test_prompt_when_exceeded_repeats(self): | 50 def test_prompt_when_exceeded_repeats(self): |
| 54 self.repeats_remaining = 2 | 51 self.repeatsRemaining = 2 |
| 55 | 52 |
| 56 def mock_raw_input(_): | 53 def mock_raw_input(message): |
| 57 self.repeats_remaining -= 1 | 54 self.repeatsRemaining -= 1 |
| 58 return None | 55 return None |
| 59 self.assertIsNone(User.prompt("input", repeat=self.repeats_remaining, ra
w_input_func=mock_raw_input)) | 56 self.assertIsNone(User.prompt("input", repeat=self.repeatsRemaining, raw
_input=mock_raw_input)) |
| 60 | 57 |
| 61 def test_prompt_with_multiple_lists(self): | 58 def test_prompt_with_multiple_lists(self): |
| 62 def run_prompt_test(inputs, expected_result, can_choose_multiple=False): | 59 def run_prompt_test(inputs, expected_result, can_choose_multiple=False): |
| 63 def mock_raw_input(_): | 60 def mock_raw_input(message): |
| 64 return inputs.pop(0) | 61 return inputs.pop(0) |
| 65 output_capture = OutputCapture() | 62 output_capture = OutputCapture() |
| 66 actual_result = output_capture.assert_outputs( | 63 actual_result = output_capture.assert_outputs( |
| 67 self, | 64 self, |
| 68 User.prompt_with_multiple_lists, | 65 User.prompt_with_multiple_lists, |
| 69 args=["title", ["subtitle1", "subtitle2"], [["foo", "bar"], ["fo
obar", "barbaz", "foobaz"]]], | 66 args=["title", ["subtitle1", "subtitle2"], [["foo", "bar"], ["fo
obar", "barbaz", "foobaz"]]], |
| 70 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input_f
unc": mock_raw_input}, | 67 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input":
mock_raw_input}, |
| 71 expected_stdout="title\n\nsubtitle1\n 1. foo\n 2. bar\n\nsubtitl
e2\n 3. foobar\n 4. barbaz\n 5. foobaz\n") | 68 expected_stdout="title\n\nsubtitle1\n 1. foo\n 2. bar\n\nsubtitl
e2\n 3. foobar\n 4. barbaz\n 5. foobaz\n") |
| 72 self.assertEqual(actual_result, expected_result) | 69 self.assertEqual(actual_result, expected_result) |
| 73 self.assertEqual(len(inputs), 0) | 70 self.assertEqual(len(inputs), 0) |
| 74 | 71 |
| 75 run_prompt_test(["1"], "foo") | 72 run_prompt_test(["1"], "foo") |
| 76 run_prompt_test(["badinput", "2"], "bar") | 73 run_prompt_test(["badinput", "2"], "bar") |
| 77 run_prompt_test(["3"], "foobar") | 74 run_prompt_test(["3"], "foobar") |
| 78 run_prompt_test(["4"], "barbaz") | 75 run_prompt_test(["4"], "barbaz") |
| 79 run_prompt_test(["5"], "foobaz") | 76 run_prompt_test(["5"], "foobaz") |
| 80 | 77 |
| 81 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True) | 78 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True) |
| 82 run_prompt_test(["1-3"], ["foo", "bar", "foobar"], can_choose_multiple=T
rue) | 79 run_prompt_test(["1-3"], ["foo", "bar", "foobar"], can_choose_multiple=T
rue) |
| 83 run_prompt_test(["1-2,3"], ["foo", "bar", "foobar"], can_choose_multiple
=True) | 80 run_prompt_test(["1-2,3"], ["foo", "bar", "foobar"], can_choose_multiple
=True) |
| 84 run_prompt_test(["2-1,3"], ["foobar"], can_choose_multiple=True) | 81 run_prompt_test(["2-1,3"], ["foobar"], can_choose_multiple=True) |
| 85 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True
) | 82 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True
) |
| 86 run_prompt_test(["all"], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], c
an_choose_multiple=True) | 83 run_prompt_test(["all"], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], c
an_choose_multiple=True) |
| 87 run_prompt_test([""], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], can_
choose_multiple=True) | 84 run_prompt_test([""], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], can_
choose_multiple=True) |
| 88 run_prompt_test([" "], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], ca
n_choose_multiple=True) | 85 run_prompt_test([" "], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], ca
n_choose_multiple=True) |
| 89 run_prompt_test(["badinput", "all"], ["foo", "bar", 'foobar', 'barbaz',
'foobaz'], can_choose_multiple=True) | 86 run_prompt_test(["badinput", "all"], ["foo", "bar", 'foobar', 'barbaz',
'foobaz'], can_choose_multiple=True) |
| 90 | 87 |
| 91 def test_prompt_with_list(self): | 88 def test_prompt_with_list(self): |
| 92 def run_prompt_test(inputs, expected_result, can_choose_multiple=False): | 89 def run_prompt_test(inputs, expected_result, can_choose_multiple=False): |
| 93 def mock_raw_input(_): | 90 def mock_raw_input(message): |
| 94 return inputs.pop(0) | 91 return inputs.pop(0) |
| 95 output_capture = OutputCapture() | 92 output_capture = OutputCapture() |
| 96 actual_result = output_capture.assert_outputs( | 93 actual_result = output_capture.assert_outputs( |
| 97 self, | 94 self, |
| 98 User.prompt_with_list, | 95 User.prompt_with_list, |
| 99 args=["title", ["foo", "bar"]], | 96 args=["title", ["foo", "bar"]], |
| 100 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input_f
unc": mock_raw_input}, | 97 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input":
mock_raw_input}, |
| 101 expected_stdout="title\n 1. foo\n 2. bar\n") | 98 expected_stdout="title\n 1. foo\n 2. bar\n") |
| 102 self.assertEqual(actual_result, expected_result) | 99 self.assertEqual(actual_result, expected_result) |
| 103 self.assertEqual(len(inputs), 0) | 100 self.assertEqual(len(inputs), 0) |
| 104 | 101 |
| 105 run_prompt_test(["1"], "foo") | 102 run_prompt_test(["1"], "foo") |
| 106 run_prompt_test(["badinput", "2"], "bar") | 103 run_prompt_test(["badinput", "2"], "bar") |
| 107 | 104 |
| 108 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True) | 105 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True) |
| 109 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True
) | 106 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True
) |
| 110 run_prompt_test(["all"], ["foo", "bar"], can_choose_multiple=True) | 107 run_prompt_test(["all"], ["foo", "bar"], can_choose_multiple=True) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 124 (("Continue? [y/N]: ", False), (User.DEFAULT_NO, 'q')), | 121 (("Continue? [y/N]: ", False), (User.DEFAULT_NO, 'q')), |
| 125 ) | 122 ) |
| 126 for test_case in test_cases: | 123 for test_case in test_cases: |
| 127 expected, inputs = test_case | 124 expected, inputs = test_case |
| 128 | 125 |
| 129 def mock_raw_input(message): | 126 def mock_raw_input(message): |
| 130 self.assertEqual(expected[0], message) | 127 self.assertEqual(expected[0], message) |
| 131 return inputs[1] | 128 return inputs[1] |
| 132 | 129 |
| 133 result = User().confirm(default=inputs[0], | 130 result = User().confirm(default=inputs[0], |
| 134 raw_input_func=mock_raw_input) | 131 raw_input=mock_raw_input) |
| 135 self.assertEqual(expected[1], result) | 132 self.assertEqual(expected[1], result) |
| OLD | NEW |