| 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): |
| 40 self.repeats_remaining = None |
| 41 |
| 39 def test_prompt_repeat(self): | 42 def test_prompt_repeat(self): |
| 40 self.repeatsRemaining = 2 | 43 self.repeats_remaining = 2 |
| 41 | 44 |
| 42 def mock_raw_input(message): | 45 def mock_raw_input(_): |
| 43 self.repeatsRemaining -= 1 | 46 self.repeats_remaining -= 1 |
| 44 if not self.repeatsRemaining: | 47 if not self.repeats_remaining: |
| 45 return UserTest.example_user_response | 48 return UserTest.example_user_response |
| 46 return None | 49 return None |
| 47 self.assertEqual(User.prompt("input", repeat=self.repeatsRemaining, | 50 self.assertEqual(User.prompt("input", repeat=self.repeats_remaining, |
| 48 raw_input=mock_raw_input), UserTest.example
_user_response) | 51 raw_input_func=mock_raw_input), UserTest.ex
ample_user_response) |
| 49 | 52 |
| 50 def test_prompt_when_exceeded_repeats(self): | 53 def test_prompt_when_exceeded_repeats(self): |
| 51 self.repeatsRemaining = 2 | 54 self.repeats_remaining = 2 |
| 52 | 55 |
| 53 def mock_raw_input(message): | 56 def mock_raw_input(_): |
| 54 self.repeatsRemaining -= 1 | 57 self.repeats_remaining -= 1 |
| 55 return None | 58 return None |
| 56 self.assertIsNone(User.prompt("input", repeat=self.repeatsRemaining, raw
_input=mock_raw_input)) | 59 self.assertIsNone(User.prompt("input", repeat=self.repeats_remaining, ra
w_input_func=mock_raw_input)) |
| 57 | 60 |
| 58 def test_prompt_with_multiple_lists(self): | 61 def test_prompt_with_multiple_lists(self): |
| 59 def run_prompt_test(inputs, expected_result, can_choose_multiple=False): | 62 def run_prompt_test(inputs, expected_result, can_choose_multiple=False): |
| 60 def mock_raw_input(message): | 63 def mock_raw_input(_): |
| 61 return inputs.pop(0) | 64 return inputs.pop(0) |
| 62 output_capture = OutputCapture() | 65 output_capture = OutputCapture() |
| 63 actual_result = output_capture.assert_outputs( | 66 actual_result = output_capture.assert_outputs( |
| 64 self, | 67 self, |
| 65 User.prompt_with_multiple_lists, | 68 User.prompt_with_multiple_lists, |
| 66 args=["title", ["subtitle1", "subtitle2"], [["foo", "bar"], ["fo
obar", "barbaz", "foobaz"]]], | 69 args=["title", ["subtitle1", "subtitle2"], [["foo", "bar"], ["fo
obar", "barbaz", "foobaz"]]], |
| 67 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input":
mock_raw_input}, | 70 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input_f
unc": mock_raw_input}, |
| 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") | 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") |
| 69 self.assertEqual(actual_result, expected_result) | 72 self.assertEqual(actual_result, expected_result) |
| 70 self.assertEqual(len(inputs), 0) | 73 self.assertEqual(len(inputs), 0) |
| 71 | 74 |
| 72 run_prompt_test(["1"], "foo") | 75 run_prompt_test(["1"], "foo") |
| 73 run_prompt_test(["badinput", "2"], "bar") | 76 run_prompt_test(["badinput", "2"], "bar") |
| 74 run_prompt_test(["3"], "foobar") | 77 run_prompt_test(["3"], "foobar") |
| 75 run_prompt_test(["4"], "barbaz") | 78 run_prompt_test(["4"], "barbaz") |
| 76 run_prompt_test(["5"], "foobaz") | 79 run_prompt_test(["5"], "foobaz") |
| 77 | 80 |
| 78 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True) | 81 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True) |
| 79 run_prompt_test(["1-3"], ["foo", "bar", "foobar"], can_choose_multiple=T
rue) | 82 run_prompt_test(["1-3"], ["foo", "bar", "foobar"], can_choose_multiple=T
rue) |
| 80 run_prompt_test(["1-2,3"], ["foo", "bar", "foobar"], can_choose_multiple
=True) | 83 run_prompt_test(["1-2,3"], ["foo", "bar", "foobar"], can_choose_multiple
=True) |
| 81 run_prompt_test(["2-1,3"], ["foobar"], can_choose_multiple=True) | 84 run_prompt_test(["2-1,3"], ["foobar"], can_choose_multiple=True) |
| 82 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True
) | 85 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True
) |
| 83 run_prompt_test(["all"], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], c
an_choose_multiple=True) | 86 run_prompt_test(["all"], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], c
an_choose_multiple=True) |
| 84 run_prompt_test([""], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], can_
choose_multiple=True) | 87 run_prompt_test([""], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], can_
choose_multiple=True) |
| 85 run_prompt_test([" "], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], ca
n_choose_multiple=True) | 88 run_prompt_test([" "], ["foo", "bar", 'foobar', 'barbaz', 'foobaz'], ca
n_choose_multiple=True) |
| 86 run_prompt_test(["badinput", "all"], ["foo", "bar", 'foobar', 'barbaz',
'foobaz'], can_choose_multiple=True) | 89 run_prompt_test(["badinput", "all"], ["foo", "bar", 'foobar', 'barbaz',
'foobaz'], can_choose_multiple=True) |
| 87 | 90 |
| 88 def test_prompt_with_list(self): | 91 def test_prompt_with_list(self): |
| 89 def run_prompt_test(inputs, expected_result, can_choose_multiple=False): | 92 def run_prompt_test(inputs, expected_result, can_choose_multiple=False): |
| 90 def mock_raw_input(message): | 93 def mock_raw_input(_): |
| 91 return inputs.pop(0) | 94 return inputs.pop(0) |
| 92 output_capture = OutputCapture() | 95 output_capture = OutputCapture() |
| 93 actual_result = output_capture.assert_outputs( | 96 actual_result = output_capture.assert_outputs( |
| 94 self, | 97 self, |
| 95 User.prompt_with_list, | 98 User.prompt_with_list, |
| 96 args=["title", ["foo", "bar"]], | 99 args=["title", ["foo", "bar"]], |
| 97 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input":
mock_raw_input}, | 100 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input_f
unc": mock_raw_input}, |
| 98 expected_stdout="title\n 1. foo\n 2. bar\n") | 101 expected_stdout="title\n 1. foo\n 2. bar\n") |
| 99 self.assertEqual(actual_result, expected_result) | 102 self.assertEqual(actual_result, expected_result) |
| 100 self.assertEqual(len(inputs), 0) | 103 self.assertEqual(len(inputs), 0) |
| 101 | 104 |
| 102 run_prompt_test(["1"], "foo") | 105 run_prompt_test(["1"], "foo") |
| 103 run_prompt_test(["badinput", "2"], "bar") | 106 run_prompt_test(["badinput", "2"], "bar") |
| 104 | 107 |
| 105 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True) | 108 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True) |
| 106 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True
) | 109 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True
) |
| 107 run_prompt_test(["all"], ["foo", "bar"], can_choose_multiple=True) | 110 run_prompt_test(["all"], ["foo", "bar"], can_choose_multiple=True) |
| (...skipping 13 matching lines...) Expand all Loading... |
| 121 (("Continue? [y/N]: ", False), (User.DEFAULT_NO, 'q')), | 124 (("Continue? [y/N]: ", False), (User.DEFAULT_NO, 'q')), |
| 122 ) | 125 ) |
| 123 for test_case in test_cases: | 126 for test_case in test_cases: |
| 124 expected, inputs = test_case | 127 expected, inputs = test_case |
| 125 | 128 |
| 126 def mock_raw_input(message): | 129 def mock_raw_input(message): |
| 127 self.assertEqual(expected[0], message) | 130 self.assertEqual(expected[0], message) |
| 128 return inputs[1] | 131 return inputs[1] |
| 129 | 132 |
| 130 result = User().confirm(default=inputs[0], | 133 result = User().confirm(default=inputs[0], |
| 131 raw_input=mock_raw_input) | 134 raw_input_func=mock_raw_input) |
| 132 self.assertEqual(expected[1], result) | 135 self.assertEqual(expected[1], result) |
| OLD | NEW |