PageRenderTime 22ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 1ms

/test/option_parser.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 94 lines | 75 code | 16 blank | 3 comment | 1 complexity | d62616d99cb1fcf48c69a30b67aaeb46 MD5 | raw file
 1# Option Parser
 2# -------------
 3
 4# Ensure that the OptionParser handles arguments correctly.
 5return unless require?
 6{OptionParser} = require './../lib/coffeescript/optparse'
 7
 8flags = [
 9  ['-r', '--required [DIR]',  'desc required']
10  ['-o', '--optional',        'desc optional']
11  ['-l', '--list [FILES*]',   'desc list']
12]
13
14banner = '''
15  banner text
16'''
17
18opt = new OptionParser flags, banner
19
20test "basic arguments", ->
21  args = ['one', 'two', 'three', '-r', 'dir']
22  result = opt.parse args
23  arrayEq args, result.arguments
24  eq undefined, result.required
25
26test "boolean and parameterised options", ->
27  result = opt.parse ['--optional', '-r', 'folder', 'one', 'two']
28  ok result.optional
29  eq 'folder', result.required
30  arrayEq ['one', 'two'], result.arguments
31
32test "list options", ->
33  result = opt.parse ['-l', 'one.txt', '-l', 'two.txt', 'three']
34  arrayEq ['one.txt', 'two.txt'], result.list
35  arrayEq ['three'], result.arguments
36
37test "-- and interesting combinations", ->
38  result = opt.parse ['-o','-r','a','-r','b','-o','--','-a','b','--c','d']
39  arrayEq ['-a', 'b', '--c', 'd'], result.arguments
40  ok result.optional
41  eq 'b', result.required
42
43  args = ['--','-o','a','-r','c','-o','--','-a','arg0','-b','arg1']
44  result = opt.parse args
45  eq undefined, result.optional
46  eq undefined, result.required
47  arrayEq args[1..], result.arguments
48
49test "throw if multiple flags try to use the same short or long name", ->
50  throws -> new OptionParser [
51    ['-r', '--required [DIR]', 'required']
52    ['-r', '--long',           'switch']
53  ]
54
55  throws -> new OptionParser [
56    ['-a', '--append [STR]', 'append']
57    ['-b', '--append',       'append with -b short opt']
58  ]
59
60  throws -> new OptionParser [
61    ['--just-long', 'desc']
62    ['--just-long', 'another desc']
63  ]
64
65  throws -> new OptionParser [
66    ['-j', '--just-long', 'desc']
67    ['--just-long', 'another desc']
68  ]
69
70  throws -> new OptionParser [
71    ['--just-long',       'desc']
72    ['-j', '--just-long', 'another desc']
73  ]
74
75test "outputs expected help text", ->
76  expectedBanner = '''
77
78banner text
79
80  -r, --required     desc required
81  -o, --optional     desc optional
82  -l, --list         desc list
83
84  '''
85  ok opt.help() is expectedBanner
86
87  expected = [
88    ''
89    '  -r, --required     desc required'
90    '  -o, --optional     desc optional'
91    '  -l, --list         desc list'
92    ''
93  ].join('\n')
94  ok new OptionParser(flags).help() is expected