/
optparse_spec.rb
128 lines (107 loc) · 4.12 KB
/
optparse_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#
# Cloud Foundry 2012.02.03 Beta
# Copyright (c) [2009-2012] VMware, Inc. All Rights Reserved.
#
# This product is licensed to you under the Apache License, Version 2.0 (the "License").
# You may not use this product except in compliance with the License.
#
# This product includes a number of subcomponents with
# separate copyright notices and license terms. Your use of these
# subcomponents is subject to the terms and conditions of the
# subcomponent's license, as noted in the LICENSE file.
#
require 'spec_helper'
require 'cli/optparse'
describe "Uaa opts parser" do
it "should parse empty args" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse([])
command.should be_nil
args.should be_empty
options[:verbose].should be_false
result.should_not be_nil
end
it "should exit with non nil status if there is a valid switch but no command" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(["--verbose"])
command.should be_nil
args.should be_empty
options[:verbose].should be_true
result.should_not be_nil
end
it "should accept --trace as an alias for --verbose" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(["--trace"])
options[:verbose].should be_true
result.should_not be_nil
end
{
:client_id => "client-id",
:client_secret => "client-secret"
}.each do |underscore,hyphen|
it "should accept #{hyphen} as an alias for #{underscore}" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(["--#{hyphen}", "foo"])
options[underscore].should be_true
end
end
it "should accept --grant-type as an alias for --grant_type" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(%w[login --grant_type foo])
options[:grant_type].should be_true
end
it "should exit cleanly on -v" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(["-v"])
result.should_not be_nil
end
it "should exit cleanly on -h" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(["-h"])
result.should_not be_nil
end
it "should exit with status=false on illegal switch" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(["-foo"])
result.should_not be_nil
result.should be_false
end
it "should exit with status=false on illegal command" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(["foo"])
result.should be_false
end
it "should exit cleanly on 'help decode'" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(%w[help decode])
result.should be_false
end
it "should exit cleanly on 'help'" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(["help"])
result.should be_false
end
it "should extract global options" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(%w[--client_id foo login])
result.should be_true
options[:client_id].should == "foo"
command.should == :login
args.should be_empty
end
it "should allow global options after command" do
pending "fix option ordering"
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(%w[login --verbose])
result.should be_true
options.should == {:verbose=>true}
command.should == :login
args.should be_empty
end
it "should allow equals sign separator" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(%w[--client_id=foo login])
result.should be_true
options[:client_id].should == "foo"
command.should == :login
args.should be_empty
end
it "should extract command arguments" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(%w[login marissa])
result.should be_true
command.should == :login
args.should == ["marissa"]
end
it "should allow login-specific options" do
command, args, options, result = Cloudfoundry::Uaa::OptParser.parse(%w[login --grant_type client_credentials])
result.should be_true
options[:grant_type].should == "client_credentials"
command.should == :login
end
end