aiee

Python modules for common command line interfacing utils
git clone git://git.defalsify.org/aiee.git
Info | Log | Files | Refs | LICENSE

test_arg.py (7035B)


      1 # standard imports
      2 import unittest
      3 import argparse
      4 
      5 # local imports
      6 from aiee.arg import (
      7         ArgFlag,
      8         Arg,
      9         process_args,
     10         )
     11 
     12 
     13 class TestArg(unittest.TestCase):
     14 
     15     def test_arg(self):
     16         flag = ArgFlag()
     17         flag.add('foo')
     18         flag.add('bar')
     19         flag.alias('baz', flag.FOO, flag.BAR)
     20 
     21         arg = Arg(flag)
     22         arg.add('x', 'baz')
     23         r = arg.get('baz')[0]
     24 
     25         self.assertEqual(r[0], '-x')
     26         self.assertIsNone(r[1])
     27         self.assertEqual(r[2], 'x')
     28 
     29         arg.set_long('x', 'xyzzy')
     30         r = arg.get('baz')[0]
     31         self.assertEqual(r[0], '-x')
     32         self.assertEqual(r[1], '--xyzzy')
     33         self.assertEqual(r[2], 'x')
     34 
     35         arg.add('y', 'foo', dest='yyy')
     36         r = arg.get('foo')[0]
     37         self.assertEqual(r[0], '-y')
     38         self.assertIsNone(r[1])
     39         self.assertEqual(r[2], 'yyy')
     40 
     41         arg.set_long('y', 'yy', dest='yyyyy')
     42         r = arg.get('foo')[0]
     43         self.assertEqual(r[0], '-y')
     44         self.assertEqual(r[1], '--yy')
     45         self.assertEqual(r[2], 'yyyyy')
     46 
     47 
     48     def test_arg_multi(self):
     49         flag = ArgFlag()
     50         flag.add('foo')
     51 
     52         arg = Arg(flag)
     53         arg.add('x', 'foo')
     54         arg.add('y', 'foo')
     55 
     56         r = arg.get('foo')
     57         self.assertEqual(len(r), 2)
     58         self.assertEqual(r[0][0], '-x')
     59         self.assertEqual(r[1][0], '-y')
     60 
     61 
     62     def test_arg_longonly(self):
     63         flag = ArgFlag()
     64         flag.add('foo')
     65 
     66         arg = Arg(flag)
     67         arg.add('x', 'foo')
     68         arg.add_long('yyy', 'foo')
     69 
     70         r = arg.get('foo')
     71         self.assertEqual(len(r), 2)
     72         self.assertEqual(r[0][0], '-x')
     73         self.assertEqual(r[1][1], '--yyy')
     74 
     75 
     76     def test_arg_iter(self):
     77         flags = ArgFlag()
     78         flags.add('foo')
     79         flags.add('bar')
     80 
     81         r = []
     82         arg = Arg(flags)
     83         for flag in arg:
     84             r.append(flag)
     85 
     86         self.assertEqual(len(r), 0)
     87 
     88         r = []
     89         arg.add('y', 'bar')
     90         for flag in arg:
     91             r.append(flag)
     92         self.assertListEqual(r, [flags.BAR])
     93 
     94         r = []
     95         arg.add('x', 'foo')
     96         for flag in arg:
     97             r.append(flag)
     98         self.assertListEqual(r, [flags.FOO, flags.BAR])
     99 
    100 
    101     def test_arg_iter_mix(self):
    102         flags = ArgFlag()
    103         flags.add('foo')
    104         flags.add('bar')
    105 
    106         arg = Arg(flags)
    107 
    108         r = []
    109         arg.add('y', 'bar')
    110         for flag in arg:
    111             r.append(flag)
    112         self.assertListEqual(r, [flags.BAR])
    113 
    114         r = []
    115         arg.add_long('xxx', 'foo')
    116         for flag in arg:
    117             r.append(flag)
    118         self.assertListEqual(r, [flags.FOO, flags.BAR])
    119 
    120 
    121     def test_process_argparser(self):
    122         flags = ArgFlag()
    123         flags.add('foo')
    124 
    125         arg = Arg(flags)
    126         arg.add('x', 'foo')
    127 
    128         argparser = argparse.ArgumentParser()
    129         argparser = process_args(argparser, arg, flags.FOO)
    130         r = argparser.parse_args(['-x', '13'])
    131 
    132         self.assertEqual(r.x, '13')
    133 
    134     
    135     def test_process_argparser_multi(self):
    136         flags = ArgFlag()
    137         flags.add('foo')
    138 
    139         arg = Arg(flags)
    140         arg.add('x', 'foo')
    141         arg.add('y', 'foo')
    142         arg.add_long('zzz', 'foo')
    143 
    144         argparser = argparse.ArgumentParser()
    145         argparser = process_args(argparser, arg, flags.FOO)
    146         r = argparser.parse_args(['-x', '13', '-y', '42', '--zzz', '666'])
    147 
    148         self.assertEqual(r.x, '13')
    149         self.assertEqual(r.y, '42')
    150         self.assertEqual(r.zzz, '666')
    151 
    152 
    153     def test_process_argparser_multi_alias(self):
    154         flags = ArgFlag()
    155         flags.add('foo')
    156         flags.add('bar')
    157         flags.alias('baz', 'foo', 'bar')
    158 
    159         arg = Arg(flags)
    160         arg.add('x', 'foo')
    161         arg.add('y', 'bar')
    162 
    163         argparser = argparse.ArgumentParser()
    164         argparser = process_args(argparser, arg, flags.BAZ)
    165         r = argparser.parse_args(['-x', '13', '-y', '42'])
    166 
    167         self.assertEqual(r.x, '13')
    168         self.assertEqual(r.y, '42')
    169 
    170 
    171     def test_process_argparser_multi_alias_selective(self):
    172         flags = ArgFlag()
    173         flags.add('foo')
    174         flags.add('bar')
    175         flags.alias('baz', 'foo', 'bar')
    176 
    177         arg = Arg(flags)
    178         arg.add('y', 'bar')
    179 
    180         argparser = argparse.ArgumentParser()
    181         argparser = process_args(argparser, arg, flags.BAZ)
    182         r = argparser.parse_args(['-y', '42'])
    183 
    184         self.assertEqual(r.y, '42')
    185 
    186         argparser = argparse.ArgumentParser()
    187         argparser = process_args(argparser, arg, flags.FOO)
    188 
    189         with self.assertRaises(SystemExit):
    190             r = argparser.parse_args(['-y', '42'])
    191 
    192 
    193     def test_process_argparser_typ(self):
    194         flags = ArgFlag()
    195         flags.add('foo')
    196 
    197         arg = Arg(flags)
    198         arg.add('x', 'foo', typ=int)
    199 
    200         argparser = argparse.ArgumentParser()
    201         argparser = process_args(argparser, arg, flags.FOO)
    202         
    203         r = argparser.parse_args(['-x', '13'])
    204 
    205         self.assertEqual(r.x, 13)
    206 
    207 
    208     def test_xargs(self):
    209         flags = ArgFlag()
    210         flags.add('foo')
    211         
    212         args = Arg(flags)
    213         args.add('x', 'foo', action='append', help='bah')
    214 
    215         argparser = argparse.ArgumentParser()
    216         argparser = process_args(argparser, args, flags.FOO)
    217 
    218         r = argparser.parse_args(['-x', '13', '-x', '42'])
    219         self.assertListEqual(r.x, ['13', '42'])
    220 
    221         args.add('y', 'foo', help='snuh', me='bar')
    222         argparser = argparse.ArgumentParser()
    223 
    224         with self.assertRaises(TypeError):
    225             process_args(argparser, args, flags)
    226 
    227 
    228     def test_bool_type(self):
    229         flags = ArgFlag()
    230         flags.add('foo')
    231         
    232         args = Arg(flags)
    233         args.add('x', 'foo', typ=bool)
    234 
    235         argparser = argparse.ArgumentParser()
    236         argparser = process_args(argparser, args, flags.FOO)
    237 
    238         r = argparser.parse_args(['-x'])
    239 
    240         self.assertIsInstance(r.x, bool)
    241 
    242 
    243     def test_val(self):
    244         flags = ArgFlag()
    245         flags.add('foo')
    246         
    247         args = Arg(flags)
    248         args.add('x', 'foo', typ=bool)
    249 
    250         r = args.val('foo')
    251         self.assertEqual(r, flags.FOO)
    252 
    253 
    254     def test_match(self):
    255         flags = ArgFlag()
    256         flags.add('foo')
    257         flags.add('bar')
    258         flags.alias('baz', 'foo', 'bar')
    259         
    260         args = Arg(flags)
    261         args.add('x', 'foo', typ=bool)
    262         self.assertFalse(args.match('foo', 0))
    263         self.assertTrue(args.match('foo', flags.FOO))
    264         self.assertFalse(args.match('bar', flags.BAR))
    265         self.assertFalse(args.match('bar', flags.BAZ))
    266         self.assertFalse(args.match('baz', flags.BAR))
    267         self.assertFalse(args.match('baz', flags.BAZ))
    268 
    269         args.add('y', 'bar', typ=bool)
    270         self.assertTrue(args.match('foo', flags.FOO))
    271         self.assertTrue(args.match('bar', flags.BAR))
    272         self.assertTrue(args.match('bar', flags.BAZ))
    273         self.assertFalse(args.match('baz', flags.BAR))
    274         self.assertTrue(args.match('baz', flags.BAZ))
    275 
    276 
    277 if __name__ == '__main__':
    278     unittest.main()