]> arthur.barton.de Git - bup.git/blob - lib/bup/t/toptions.py
options.py: clean up handling of --no-* options.
[bup.git] / lib / bup / t / toptions.py
1 from bup import options
2 from wvtest import *
3
4
5 @wvtest
6 def test_optdict():
7     d = options.OptDict({
8         'x': ('x', False),
9         'y': ('y', False),
10         'z': ('z', False),
11         'other_thing': ('other_thing', False),
12         'no_other_thing': ('other_thing', True),
13         'no_z': ('z', True),
14         'no_smart': ('smart', True),
15         'smart': ('smart', False),
16         'stupid': ('smart', True),
17         'no_smart': ('smart', False),
18     })
19     WVPASS('foo')
20     d['x'] = 5
21     d['y'] = 4
22     d['z'] = 99
23     d['no_other_thing'] = 5
24     WVPASSEQ(d.x, 5)
25     WVPASSEQ(d.y, 4)
26     WVPASSEQ(d.z, 99)
27     WVPASSEQ(d.no_z, False)
28     WVPASSEQ(d.no_other_thing, True)
29     WVEXCEPT(KeyError, lambda: d.p)
30
31
32 invalid_optspec0 = """
33 """
34
35
36 invalid_optspec1 = """
37 prog <whatever>
38 """
39
40
41 invalid_optspec2 = """
42 --
43 x,y
44 """
45
46
47 @wvtest
48 def test_invalid_optspec():
49     WVPASS(options.Options(invalid_optspec0).parse([]))
50     WVPASS(options.Options(invalid_optspec1).parse([]))
51     WVPASS(options.Options(invalid_optspec2).parse([]))
52
53
54 optspec = """
55 prog <optionset> [stuff...]
56 prog [-t] <boggle>
57 --
58 t       test
59 q,quiet   quiet
60 l,longoption=   long option with parameters and a really really long description that will require wrapping
61 p= short option with parameters
62 onlylong  long option with no short
63 neveropt never called options
64 deftest1=  a default option with default [1]
65 deftest2=  a default option with [1] default [2]
66 deftest3=  a default option with [3] no actual default
67 deftest4=  a default option with [[square]]
68 deftest5=  a default option with "correct" [[square]
69 s,smart,no-stupid  disable stupidity
70 x,extended,no-simple   extended mode [2]
71 #,compress=  set compression level [5]
72 """
73
74 @wvtest
75 def test_options():
76     o = options.Options(optspec)
77     (opt,flags,extra) = o.parse(['-tttqp', 7, '--longoption', '19',
78                                  'hanky', '--onlylong', '-7'])
79     WVPASSEQ(flags[0], ('-t', ''))
80     WVPASSEQ(flags[1], ('-t', ''))
81     WVPASSEQ(flags[2], ('-t', ''))
82     WVPASSEQ(flags[3], ('-q', ''))
83     WVPASSEQ(flags[4], ('-p', 7))
84     WVPASSEQ(flags[5], ('--longoption', '19'))
85     WVPASSEQ(extra, ['hanky'])
86     WVPASSEQ((opt.t, opt.q, opt.p, opt.l, opt.onlylong,
87               opt.neveropt), (3,1,7,19,1,None))
88     WVPASSEQ((opt.deftest1, opt.deftest2, opt.deftest3, opt.deftest4,
89               opt.deftest5), (1,2,None,None,'[square'))
90     WVPASSEQ((opt.stupid, opt.no_stupid), (True, None))
91     WVPASSEQ((opt.smart, opt.no_smart), (None, True))
92     WVPASSEQ((opt.x, opt.extended, opt.no_simple), (2,2,2))
93     WVPASSEQ((opt.no_x, opt.no_extended, opt.simple), (False,False,False))
94     WVPASSEQ(opt['#'], 7)
95     WVPASSEQ(opt.compress, 7)
96
97     (opt,flags,extra) = o.parse(['--onlylong', '-t', '--no-onlylong',
98                                  '--smart', '--simple'])
99     WVPASSEQ((opt.t, opt.q, opt.onlylong), (1, None, 0))
100     WVPASSEQ((opt.stupid, opt.no_stupid), (False, True))
101     WVPASSEQ((opt.smart, opt.no_smart), (True, False))
102     WVPASSEQ((opt.x, opt.extended, opt.no_simple), (0,0,0))
103     WVPASSEQ((opt.no_x, opt.no_extended, opt.simple), (True,True,True))