1 # Copyright (C) 2010 Research in Motion Ltd. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
13 # * Neither the name of Research in Motion Ltd. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 from webkitpy.common.system.outputcapture import OutputCapture
32 from webkitpy.common.system.user import User
34 class UserTest(unittest.TestCase):
36 example_user_response = "example user response"
38 def test_prompt_repeat(self):
39 self.repeatsRemaining = 2
40 def mock_raw_input(message):
41 self.repeatsRemaining -= 1
42 if not self.repeatsRemaining:
43 return UserTest.example_user_response
45 self.assertEqual(User.prompt("input", repeat=self.repeatsRemaining, raw_input=mock_raw_input), UserTest.example_user_response)
47 def test_prompt_when_exceeded_repeats(self):
48 self.repeatsRemaining = 2
49 def mock_raw_input(message):
50 self.repeatsRemaining -= 1
52 self.assertEqual(User.prompt("input", repeat=self.repeatsRemaining, raw_input=mock_raw_input), None)
54 def test_prompt_with_list(self):
55 def run_prompt_test(inputs, expected_result, can_choose_multiple=False):
56 def mock_raw_input(message):
58 output_capture = OutputCapture()
59 actual_result = output_capture.assert_outputs(
61 User.prompt_with_list,
62 args=["title", ["foo", "bar"]],
63 kwargs={"can_choose_multiple": can_choose_multiple, "raw_input": mock_raw_input},
64 expected_stdout="title\n 1. foo\n 2. bar\n")
65 self.assertEqual(actual_result, expected_result)
66 self.assertEqual(len(inputs), 0)
68 run_prompt_test(["1"], "foo")
69 run_prompt_test(["badinput", "2"], "bar")
71 run_prompt_test(["1,2"], ["foo", "bar"], can_choose_multiple=True)
72 run_prompt_test([" 1, 2 "], ["foo", "bar"], can_choose_multiple=True)
73 run_prompt_test(["all"], ["foo", "bar"], can_choose_multiple=True)
74 run_prompt_test([""], ["foo", "bar"], can_choose_multiple=True)
75 run_prompt_test([" "], ["foo", "bar"], can_choose_multiple=True)
76 run_prompt_test(["badinput", "all"], ["foo", "bar"], can_choose_multiple=True)
78 def test_confirm(self):
80 (("Continue? [Y/n]: ", True), (User.DEFAULT_YES, 'y')),
81 (("Continue? [Y/n]: ", False), (User.DEFAULT_YES, 'n')),
82 (("Continue? [Y/n]: ", True), (User.DEFAULT_YES, '')),
83 (("Continue? [Y/n]: ", False), (User.DEFAULT_YES, 'q')),
84 (("Continue? [y/N]: ", True), (User.DEFAULT_NO, 'y')),
85 (("Continue? [y/N]: ", False), (User.DEFAULT_NO, 'n')),
86 (("Continue? [y/N]: ", False), (User.DEFAULT_NO, '')),
87 (("Continue? [y/N]: ", False), (User.DEFAULT_NO, 'q')),
89 for test_case in test_cases:
90 expected, inputs = test_case
92 def mock_raw_input(message):
93 self.assertEquals(expected[0], message)
96 result = User().confirm(default=inputs[0],
97 raw_input=mock_raw_input)
98 self.assertEquals(expected[1], result)
100 def test_warn_if_application_is_xcode(self):
101 output = OutputCapture()
103 output.assert_outputs(self, user._warn_if_application_is_xcode, ["TextMate"])
104 output.assert_outputs(self, user._warn_if_application_is_xcode, ["/Applications/TextMate.app"])
105 output.assert_outputs(self, user._warn_if_application_is_xcode, ["XCode"]) # case sensitive matching
107 xcode_warning = "Instead of using Xcode.app, consider using EDITOR=\"xed --wait\".\n"
108 output.assert_outputs(self, user._warn_if_application_is_xcode, ["Xcode"], expected_stdout=xcode_warning)
109 output.assert_outputs(self, user._warn_if_application_is_xcode, ["/Developer/Applications/Xcode.app"], expected_stdout=xcode_warning)