forked from kadirpekel/komandr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
60 lines (44 loc) · 1.75 KB
/
tests.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
# TODO: Add much more tests
import unittest
import komandr
from mock import patch
class TestKomandr(unittest.TestCase):
def tearDown(self):
komandr.main.default_subcommand = None
def testCommand(self):
def foo(bar, baz=None):
return bar, baz
komandr.command(foo)
self.assertEqual(('1', '2'),
komandr.execute(['foo', '1', '--baz', '2']))
def testDefaultSubcommand(self):
def foo(bar, baz=None):
return bar, baz
komandr.command(foo)
# Without main.default_subcommand a `SystemExit` exception would raise.
with self.assertRaises(SystemExit) as ex:
komandr.execute([])
self.assertEqual(2, ex.exception.code)
# With main.default_subcommand='foo', subcommand named `foo` would be
# called by default when no subcommand.
komandr.main.default_subcommand = 'foo'
self.assertEqual(('1', '2'), komandr.execute(['1', '--baz', '2']))
def testDefaultSubcommandWithoutOptions(self):
def foo():
return 'bar'
komandr.command(foo)
komandr.main.default_subcommand = 'foo'
self.assertEqual('bar', komandr.execute([]))
def testOptionsNotTriggerDefaultSubcommand(self):
def foo(baz='bar'):
return baz
komandr.command(foo)
komandr.main.default_subcommand = 'foo'
with patch.object(komandr.main.parser, 'print_help') as mock:
with self.assertRaises(SystemExit) as ex:
komandr.execute(['--help'])
self.assertEqual(0, ex.exception.code)
self.assertTrue(mock.called)
self.assertEqual('bar baz', komandr.execute(['--baz', 'bar baz']))
if __name__ == '__main__':
unittest.main()