]> arthur.barton.de Git - bup.git/blob - lib/bup/t/toptions.py
options.py: don't crash given semi-invalid optspecs.
[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     WVPASS('foo')
9     d['x'] = 5
10     d['y'] = 4
11     d['z'] = 99
12     d['no_other_thing'] = 5
13     WVPASSEQ(d.x, 5)
14     WVPASSEQ(d.y, 4)
15     WVPASSEQ(d.z, 99)
16     WVPASSEQ(d.no_z, False)
17     WVPASSEQ(d.no_other_thing, True)
18     try:
19         print d.p
20     except:
21         WVPASS("invalid args don't match")
22     else:
23         WVFAIL("exception expected")
24
25
26 invalid_optspec0 = """
27 """
28
29
30 invalid_optspec1 = """
31 prog <whatever>
32 """
33
34
35 invalid_optspec2 = """
36 --
37 x,y
38 """
39
40
41 @wvtest
42 def test_invalid_optspec():
43     WVPASS(options.Options(invalid_optspec0).parse([]))
44     WVPASS(options.Options(invalid_optspec1).parse([]))
45     WVPASS(options.Options(invalid_optspec2).parse([]))
46
47
48 optspec = """
49 prog <optionset> [stuff...]
50 prog [-t] <boggle>
51 --
52 t       test
53 q,quiet   quiet
54 l,longoption=   long option with parameters and a really really long description that will require wrapping
55 p= short option with parameters
56 onlylong  long option with no short
57 neveropt never called options
58 deftest1=  a default option with default [1]
59 deftest2=  a default option with [1] default [2]
60 deftest3=  a default option with [3] no actual default
61 deftest4=  a default option with [[square]]
62 deftest5=  a default option with "correct" [[square]
63 no-stupid  disable stupidity
64 #,compress=  set compression level [5]
65 """
66
67 @wvtest
68 def test_options():
69     o = options.Options(optspec)
70     (opt,flags,extra) = o.parse(['-tttqp', 7, '--longoption', '19',
71                                  'hanky', '--onlylong', '-7'])
72     WVPASSEQ(flags[0], ('-t', ''))
73     WVPASSEQ(flags[1], ('-t', ''))
74     WVPASSEQ(flags[2], ('-t', ''))
75     WVPASSEQ(flags[3], ('-q', ''))
76     WVPASSEQ(flags[4], ('-p', 7))
77     WVPASSEQ(flags[5], ('--longoption', '19'))
78     WVPASSEQ(extra, ['hanky'])
79     WVPASSEQ((opt.t, opt.q, opt.p, opt.l, opt.onlylong,
80               opt.neveropt), (3,1,7,19,1,None))
81     WVPASSEQ((opt.deftest1, opt.deftest2, opt.deftest3, opt.deftest4,
82               opt.deftest5), (1,2,None,None,'[square'))
83     WVPASSEQ((opt.stupid, opt.no_stupid), (True, False))
84     WVPASSEQ(opt['#'], 7)
85     WVPASSEQ(opt.compress, 7)
86
87     (opt,flags,extra) = o.parse(['--onlylong', '-t', '--no-onlylong'])
88     WVPASSEQ((opt.t, opt.q, opt.onlylong), (1, None, 0))