forked from italorossi/ishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_shell.py
102 lines (87 loc) · 4.24 KB
/
test_shell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import unittest
from ishell.console import Console
from ishell.command import Command
class TestConsole(unittest.TestCase):
def test_console_creation(self):
"""Console must be created."""
c = Console()
assert isinstance(c, Console)
def test_console_has_prompt(self):
"""Console should have a default prompt string."""
c = Console()
assert c.prompt == "Prompt"
assert c.prompt_delim == ">"
class TestCommand(unittest.TestCase):
def test_command_creation(self):
"""Command must be created with name and default help message."""
cmd = Command('configure')
assert cmd.name == 'configure'
assert cmd.help == 'No help provided'
assert cmd.dynamic_args == False
def test_simple_completion(self):
"""Command must complete with only one option."""
cmd1 = Command('configure')
cmd2 = Command('terminal')
cmd1.addChild(cmd2)
candidates = cmd1.complete('', '', 0, run=False, full_line='configure ')
assert 'terminal ' == candidates
candidates = cmd1.complete('', '', 1, run=False, full_line='configure ')
assert None == candidates
def test_double_completion(self):
"""Command must complete with two options."""
cmd1 = Command('configure')
cmd2 = Command('terminal')
cmd3 = Command('interface')
cmd1.addChild(cmd2)
cmd1.addChild(cmd3)
# State 0 must print all commands followed by help message
# and return None as candidates
candidates = cmd1.complete('', '', 0, run=False, full_line='configure ')
assert None == candidates
candidates = cmd1.complete('', 'in', 0, run=False, full_line='configure in')
assert 'interface ' == candidates
candidates = cmd1.complete('', 't', 0, run=False, full_line='configure t')
assert 'terminal ' == candidates
def test_completion_with_buffer(self):
"""Command must complete correctly with buffer provided."""
cmd1 = Command('configure')
cmd2 = Command('terminal')
cmd1.addChild(cmd2)
candidates = cmd1.complete(['t'], 't', 0, run=False, full_line='configure ')
assert 'terminal ' == candidates
candidates = cmd1.complete(['t'], 't', 1, run=False, full_line='configure ')
assert None == candidates
def test_completion_with_dynamic_arg(self):
cmd1 = Command('show')
cmd2 = Command('call', dynamic_args=True)
cmd3 = Command('calls', dynamic_args=True)
cmd2.args = lambda: ['100', '101']
cmd3.args = lambda: ['continuous', 'raw']
cmd1.addChild(cmd2)
cmd1.addChild(cmd3)
candidates = cmd1.complete(['c'], '', 0, run=False, full_line='show calls')
self.assertEqual(None, candidates)
candidates = cmd1.complete(['c'], 'c', 0, run=False, full_line='show calls')
self.assertEqual('call ', candidates)
candidates = cmd1.complete(['c'], 'c', 1, run=False, full_line='show calls')
self.assertEqual('calls ', candidates)
candidates = cmd2.complete([''], '', 0, run=False, full_line='show calls')
self.assertEqual(None, candidates)
candidates = cmd2.complete([''], '1', 0, run=False, full_line='show calls')
self.assertEqual('100', candidates)
candidates = cmd2.complete([''], '1', 1, run=False, full_line='show calls')
self.assertEqual('101', candidates)
candidates = cmd3.complete([''], '', 0, run=False, full_line='show calls c')
self.assertEqual(None, candidates)
candidates = cmd3.complete([''], 'c', 0, run=False, full_line='show calls c')
self.assertEqual('continuous', candidates)
candidates = cmd3.complete([''], 'r', 0, run=False, full_line='show calls c')
self.assertEqual('raw', candidates)
candidates = cmd1.complete(['calls', 'c'], 'c', 0, run=False, full_line='show calls c')
self.assertEqual('continuous', candidates)
candidates = cmd2.complete(['1'], '1', 0, run=False, full_line='show calls c')
self.assertEqual('100', candidates)
candidates = cmd2.complete(['1'], '1', 1, run=False, full_line='show calls c')
self.assertEqual('101', candidates)
if __name__ == '__main__':
unittest.main()