Commits

John Mulligan committed b8e9867

add straightforward cli parsing test

  • Participants
  • Parent commits 2b9367c

Comments (0)

Files changed (1)

File test/test_cli_parsing.py

+#!/usr/bin/env python
+
+
+import unittest
+import sys
+
+from vanity import cli
+
+
+CLI_A1 = '--output foo.txt bar.txt'
+CLI_A2 = 'bar.txt -o foo.txt -e --verbose'
+CLI_A3 = 'bar.txt -O foo.txt -x --verbose'
+
+OPTS_SET_A = [
+    ('output', 'o', '', 'Output file'),
+    ('extra', 'e', None, 'Add extra markers'),
+    ('verbose', '', None, 'Be verbose'),
+    ]
+
+OPTS_SET_A2 = [
+    {'name': 'output', 'short':'o', 'default':'', 'help': 'Output file'},
+    {'name': 'extra', 'short':'e', 'help': 'Add extra markers'},
+    {'name': 'verbose', 'default': None, 'help': 'Be verbose'},
+    ]
+
+
+CLI_B1 = '--batch  scoop.dat'
+CLI_B2 = '-t normal/foo -l en_GB scoop.dat'
+CLI_B3 = '--batch magic.dat -l fr_CA'
+
+OPTS_SET_B = [
+    ('trust', 't', '', 'path to trust db'),
+    dict(name='batch', help='accept batched input stream'),
+    cli.Option('locale', 'l', default='en_US', help='specify locale'),
+    ]
+
+
+CLI_C1 = '-i pep.html -i cool.html -o blah -o moose.html'
+
+OPTS_SET_C = [
+    ('output', 'o', '', 'output file name (default: stdout)'),
+    ('include', 'i', [], 'include template source file'),
+    ]
+
+
+def asargs(txt):
+    return txt.split()
+
+class TestCliParsing(unittest.TestCase):
+    def test_arguments_call(self):
+        args = cli.arguments()
+        self.assertEqual(args, sys.argv[1:])
+
+    def test_basic_parsing(self):
+        opts, args = cli.parse(OPTS_SET_A, asargs(CLI_A1))
+        self.assertEqual(opts, dict(output='foo.txt', extra=None, verbose=None))
+        self.assertEqual(args, ['bar.txt'])
+
+    def test_basic_parsing2(self):
+        opts, args = cli.parse(OPTS_SET_A, asargs(CLI_A2))
+        self.assertEqual(opts, dict(output='foo.txt', extra=True, verbose=True))
+        self.assertEqual(args, ['bar.txt'])
+
+    def test_basic_parsing_maplist(self):
+        opts, args = cli.parse(OPTS_SET_A2, asargs(CLI_A1))
+        self.assertEqual(opts, dict(output='foo.txt', extra=None, verbose=None))
+        self.assertEqual(args, ['bar.txt'])
+
+    def test_basic_parsing_maplist2(self):
+        opts, args = cli.parse(OPTS_SET_A2, asargs(CLI_A2))
+        self.assertEqual(opts, dict(output='foo.txt', extra=True, verbose=True))
+        self.assertEqual(args, ['bar.txt'])
+
+    def test_basic_error(self):
+        self.assertRaises(cli.InvalidOption,
+            cli.parse, OPTS_SET_A, asargs(CLI_A3))
+
+    def test_basic_usage(self):
+        use = '\n'.join(cli.usage(OPTS_SET_A))
+        use2 = '\n'.join(cli.usage(OPTS_SET_A2))
+        self.assertEqual(use, use2)
+
+    def test_mixed_opts(self):
+        opts, args = cli.parse(OPTS_SET_B, asargs(CLI_B1))
+        self.assertEqual(args, ['scoop.dat'])
+
+    def test_strict_parsing(self):
+        opts, args = cli.parse(OPTS_SET_B, asargs(CLI_B3), strict=True)
+        self.assertEqual(args, ['magic.dat', '-l', 'fr_CA'])
+        self.assertEqual(opts, {
+            'locale': 'en_US',
+            'trust': '',
+            'batch': True,
+            })
+
+    def test_parsing_multiopt(self):
+        opts, args = cli.parse(OPTS_SET_C, asargs(CLI_C1))
+        self.assertEqual(args, [])
+        self.assertEqual(opts, {
+            'include': ['pep.html', 'cool.html'],
+            'output': 'moose.html',
+            })
+
+    def test_merging_tables(self):
+        t1 = cli.OptionTable(OPTS_SET_A)
+        t2 = cli.OptionTable(OPTS_SET_B)
+        t = t1 + t2
+        opts, args = cli.parse(t, asargs(CLI_B2))
+        self.assertEqual(args, ['scoop.dat'])
+        self.assertEqual(opts, {
+            'locale': 'en_GB',
+            'trust': 'normal/foo',
+            'batch': None,
+            'output': '',
+            'verbose': None,
+            'extra': None,
+            })
+
+    def test_table_tricks1(self):
+        t1 = cli.OptionTable(OPTS_SET_A)
+        self.assert_('output' in t1)
+        self.assertEqual(t1.getlong('--extra'),
+            t1.getlong('extra'))
+        self.assertEqual(t1.getlong('--output'),
+            t1.getlong('output'))
+        self.assertRaises(KeyError, 
+            t1.getlong, '--foobar')
+        self.assertEqual(t1.getshort('-o'),
+            t1.getshort('o'))
+        self.assertRaises(KeyError, 
+            t1.getshort, '-w')
+        self.assertEqual(['extra', 'output', 'verbose'],
+            sorted(t1.longopts()))
+        self.assertEqual(['e', 'o'], sorted(t1.shortopts()))
+
+
+
+if __name__ == '__main__':
+    unittest.main()