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