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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
require 'test_helper'
describe Slop::StringOption do
before do
@options = Slop::Options.new
@age = @options.string "--name"
@minus = @options.string "--zipcode"
@result = @options.parse %w(--name Foo --zipcode 12345)
end
it "returns the value as a string" do
assert_equal "Foo", @result[:name]
assert_equal "12345", @result[:zipcode]
end
end
describe Slop::SymbolOption do
before do
@options = Slop::Options.new
@age = @options.symbol "--name"
@minus = @options.symbol "--zipcode"
@result = @options.parse %w(--name Foo --zipcode 12345)
end
it "returns the value as a symbol" do
assert_equal :Foo, @result[:name]
assert_equal :'12345', @result[:zipcode]
end
end
describe Slop::BoolOption do
before do
@options = Slop::Options.new
@verbose = @options.bool "--verbose", validate_type: true
@quiet = @options.bool "--quiet"
@inversed = @options.bool "--inversed", default: true
@explicit = @options.bool "--explicit", validate_type: true
@bloc = @options.bool("--bloc"){|val| (@bloc_val ||= []) << val}
@result = @options.parse %w(--verbose --no-inversed
--bloc --no-bloc
--explicit=false)
end
it "returns true if used" do
assert_equal true, @result[:verbose]
end
it "returns false if not used" do
assert_equal false, @result[:quiet]
end
it "can be inversed via --no- prefix" do
assert_equal false, @result[:inversed]
end
it "will invert the value passed to &block via --no- prefix" do
assert_equal [true, false], @bloc_val
end
it "returns false when explicitly false" do
assert_equal false, @result[:explicit]
end
it "raises with invalid types" do
assert_raises(Slop::InvalidOptionValue) do
@result.parser.parse %w(--verbose foo)
end
end
# Like above but without validate_type
it "returns true if used and ignores the value" do
@result.parser.parse %w(--quiet foo)
assert_equal true, @result[:quiet]
end
end
describe Slop::IntegerOption do
before do
@options = Slop::Options.new
@age = @options.integer "--age"
@minus = @options.integer "--minus", validate_type: true
@plus = @options.integer "--plus"
@result = @options.parse %w(--age 20 --minus -10 --plus +30)
end
it "returns the value as an integer" do
assert_equal 20, @result[:age]
assert_equal (-10), @result[:minus]
assert_equal 30, @result[:plus]
end
it "returns nil for non-numbers by default" do
@result.parser.parse %w(--age hello)
assert_nil @result[:age]
end
it "raises with invalid types" do
assert_raises(Slop::InvalidOptionValue) do
@result.parser.parse %w(--minus foo)
end
end
end
describe Slop::FloatOption do
before do
@options = Slop::Options.new
@apr = @options.float "--apr"
@apr_value = 2.9
@minus = @options.float "--minus", validate_type: true
@plus = @options.float "--plus"
@scientific_notation = @options.float "--scientific-notation"
@scientific_notation_value = 4e21
@result = @options.parse %W(--apr #{@apr_value} --minus -6.1 --plus +9.4 --scientific-notation #{@scientific_notation_value})
end
it "returns the value as a float" do
assert_equal @apr_value, @result[:apr]
assert_equal (-6.1), @result[:minus]
assert_equal 9.4, @result[:plus]
end
it "parses scientific notations" do
assert_equal @scientific_notation_value, @result[:scientific_notation]
@scientific_notation_value = 4E21
@result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
assert_equal @scientific_notation_value, @result[:scientific_notation]
@scientific_notation_value = 4.0e21
@result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
assert_equal @scientific_notation_value, @result[:scientific_notation]
@scientific_notation_value = -4e21
@result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
assert_equal @scientific_notation_value, @result[:scientific_notation]
@scientific_notation_value = 4e-21
@result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
assert_equal @scientific_notation_value, @result[:scientific_notation]
end
it "returns nil for non-numbers by default" do
@result.parser.parse %w(--apr hello)
assert_nil @result[:apr]
end
it "raises with invalid types" do
assert_raises(Slop::InvalidOptionValue) do
@result.parser.parse %w(--minus foo)
end
end
end
describe Slop::ArrayOption do
before do
@options = Slop::Options.new
@files = @options.array "--files"
@multi = @options.array "-M", delimiter: nil
@delim = @options.array "-d", delimiter: ":"
@limit = @options.array "-l", limit: 2
@result = @options.parse %w(--files foo.txt,bar.rb)
end
it "defaults to []" do
assert_equal [], @result[:d]
end
it "parses comma separated args" do
assert_equal %w(foo.txt bar.rb), @result[:files]
end
it "collects multiple option values" do
@result.parser.parse %w(--files foo.txt --files bar.rb)
assert_equal %w(foo.txt bar.rb), @result[:files]
end
it "collects multiple option values with no delimiter" do
@result.parser.parse %w(-M foo,bar -M bar,qux)
assert_equal %w(foo,bar bar,qux), @result[:M]
end
it "can use a custom delimiter" do
@result.parser.parse %w(-d foo.txt:bar.rb)
assert_equal %w(foo.txt bar.rb), @result[:d]
end
it "can use a custom limit" do
@result.parser.parse %w(-l foo,bar,baz)
assert_equal ["foo", "bar,baz"], @result[:l]
end
end
describe Slop::NullOption do
before do
@options = Slop::Options.new
@version = @options.null('--version')
@result = @options.parse %w(--version)
end
it 'has a return value of true' do
assert_equal true, @result[:version]
end
it 'is not included in to_hash' do
assert_equal({}, @result.to_hash)
end
end
describe Slop::RegexpOption do
before do
@options = Slop::Options.new
@exclude = @options.regexp "--exclude"
@exclude_value = "redirect|news"
@result = @options.parse %W(--exclude #{@exclude_value})
end
it "returns the value as a Regexp" do
assert_equal Regexp.new(@exclude_value), @result[:exclude]
end
end
|