7d9667e439a86daea70ba984b85e7dd832078e90
[sdc/sdc-distribution-client.git] /
1 # -*- encoding: utf-8 -*-
2 #
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
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
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
13 #  under the License.
14
15 from cliff.interactive import InteractiveApp
16
17
18 class FakeApp(object):
19     NAME = 'Fake'
20
21
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)
26
27
28 def _test_completenames(expecteds, prefix):
29     app = make_interactive_app('hips', 'hippo', 'nonmatching')
30     assert set(app.completenames(prefix)) == set(expecteds)
31
32
33 def test_cmd2_completenames():
34     # cmd2.Cmd define do_help method
35     _test_completenames(['help'], 'he')
36
37
38 def test_cliff_completenames():
39     _test_completenames(['hips', 'hippo'], 'hip')
40
41
42 def test_no_completenames():
43     _test_completenames([], 'taz')
44
45
46 def test_both_completenames():
47     # cmd2.Cmd define do_hi and do_history methods
48     _test_completenames(['hi', 'history', 'hips', 'hippo'], 'hi')
49
50
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
57
58
59 def test_empty_text_completedefault():
60     # line = 'show ' + begidx = 5 implies text = ''
61     _test_completedefault(['file', 'folder', ' long'], 'show ', 5)
62
63
64 def test_nonempty_text_completedefault2():
65     # line = 'show f' + begidx = 6 implies text = 'f'
66     _test_completedefault(['file', 'folder'], 'show f', 5)
67
68
69 def test_long_completedefault():
70     _test_completedefault(['long'], 'show  ', 6)
71
72
73 def test_no_completedefault():
74     _test_completedefault([], 'taz ', 4)