1 # -*- encoding: utf-8 -*-
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
15 from cliff.interactive import InteractiveApp
18 class FakeApp(object):
22 def make_interactive_app(*command_names):
23 fake_command_manager = [(x, None) for x in command_names]
24 return InteractiveApp(FakeApp, fake_command_manager,
25 stdin=None, stdout=None)
28 def _test_completenames(expecteds, prefix):
29 app = make_interactive_app('hips', 'hippo', 'nonmatching')
30 assert set(app.completenames(prefix)) == set(expecteds)
33 def test_cmd2_completenames():
34 # cmd2.Cmd define do_help method
35 _test_completenames(['help'], 'he')
38 def test_cliff_completenames():
39 _test_completenames(['hips', 'hippo'], 'hip')
42 def test_no_completenames():
43 _test_completenames([], 'taz')
46 def test_both_completenames():
47 # cmd2.Cmd define do_hi and do_history methods
48 _test_completenames(['hi', 'history', 'hips', 'hippo'], 'hi')
51 def _test_completedefault(expecteds, line, begidx):
52 command_names = set(['show file', 'show folder', 'show long', 'list all'])
53 app = make_interactive_app(*command_names)
54 observeds = app.completedefault(None, line, begidx, None)
55 assert set(observeds) == set(expecteds)
56 assert set([line[:begidx] + x for x in observeds]) <= command_names
59 def test_empty_text_completedefault():
60 # line = 'show ' + begidx = 5 implies text = ''
61 _test_completedefault(['file', 'folder', ' long'], 'show ', 5)
64 def test_nonempty_text_completedefault2():
65 # line = 'show f' + begidx = 6 implies text = 'f'
66 _test_completedefault(['file', 'folder'], 'show f', 5)
69 def test_long_completedefault():
70 _test_completedefault(['long'], 'show ', 6)
73 def test_no_completedefault():
74 _test_completedefault([], 'taz ', 4)