\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef.upgrade/embedded/lib/ruby/gems/2.3.0/gems/slop-3.6.0/test/

Viewing File: option_test.rb

require 'helper'

class OptionTest < TestCase
  def option(*args, &block)
    Slop.new.on(*args, &block)
  end

  def option_with_argument(*args, &block)
    options = args.shift
    slop = Slop.new
    option = slop.opt(*args)
    slop.parse(options)
    slop.options.find {|opt| opt.key == option.key }
  end

  def option_value(*args, &block)
    option_with_argument(*args, &block).value
  end

  test "expects_argument?" do
    assert option(:f=).expects_argument?
    assert option(:foo=).expects_argument?
    assert option(:foo, :argument => true).expects_argument?
  end

  test "accepts_optional_argument?" do
    refute option(:f=).accepts_optional_argument?
    assert option(:f=, :argument => :optional).accepts_optional_argument?
    assert option(:f, :optional_argument => true).accepts_optional_argument?
  end

  test "key" do
    assert_equal 'foo', option(:foo).key
    assert_equal 'foo', option(:f, :foo).key
    assert_equal 'f', option(:f).key
  end

  test "call" do
    foo = nil
    option(:f, :callback => proc { foo = "bar" }).call
    assert_equal "bar", foo
    option(:f) { foo = "baz" }.call
    assert_equal "baz", foo
    option(:f) { |o| assert_equal 1, o }.call(1)
  end

  # type casting

  test "proc/custom type cast" do
    assert_equal 1, option_value(%w'-f 1', :f=, :as => proc {|x| x.to_i })
    assert_equal "oof", option_value(%w'-f foo', :f=, :as => proc {|x| x.reverse })
  end

  test "integer type cast" do
    opts = Slop.new
    opts.on :f=, :as => Integer
    opts.parse %w'-f 1'
    assert_equal 1, opts[:f]

    opts = Slop.new(:strict => true) { on :r=, :as => Integer }
    assert_raises(Slop::InvalidArgumentError) { opts.parse %w/-r abc/ }
  end

  test "float type cast" do
    opts = Slop.new(:strict => true) { on :r=, :as => Float }
    assert_raises(Slop::InvalidArgumentError) { opts.parse %w/-r abc/ }
  end

  test "symbol type cast" do
    assert_equal :foo, option_value(%w'-f foo', :f=, :as => Symbol)
  end

  test "range type cast" do
    assert_equal((1..10), option_value(%w/-r 1..10/, :r=, :as => Range))
    assert_equal((1..10), option_value(%w/-r 1-10/, :r=, :as => Range))
    assert_equal((1..10), option_value(%w/-r 1,10/, :r=, :as => Range))
    assert_equal((1...10), option_value(%w/-r 1...10/, :r=, :as => Range))
    assert_equal((-1..10), option_value(%w/-r -1..10/, :r=, :as => Range))
    assert_equal((1..-10), option_value(%w/-r 1..-10/, :r=, :as => Range))
    assert_equal((1..1), option_value(%w/-r 1/, :r=, :as => Range))
    assert_equal((-1..10), option_value(%w/-r -1..10/, :r, :as => Range, :optional_argument => true))

    opts = Slop.new(:strict => true) { on :r=, :as => Range }
    assert_raises(Slop::InvalidArgumentError) { opts.parse %w/-r abc/ }
  end

  test "array type cast" do
    assert_equal %w/lee john bill/, option_value(%w/-p lee,john,bill/, :p=, :as => Array)
    assert_equal %w/lee john bill jeff jill/, option_value(%w/-p lee,john,bill -p jeff,jill/, :p=, :as => Array)
    assert_equal %w/lee john bill/, option_value(%w/-p lee:john:bill/, :p=, :as => Array, :delimiter => ':')
    assert_equal %w/lee john,bill/, option_value(%w/-p lee,john,bill/, :p=, :as => Array, :limit => 2)
    assert_equal %w/lee john:bill/, option_value(%w/-p lee:john:bill/, :p=, :as => Array, :limit => 2, :delimiter => ':')
  end

  test "regexp type cast" do
    assert_equal Regexp.new("foo"), option_value(%w/-p foo/, :p=, :as => Regexp)
  end

  test "adding custom types" do
    opts = Slop.new
    opt = opts.on :f=, :as => :reverse
    opt.types[:reverse] = proc { |v| v.reverse }
    opts.parse %w'-f bar'
    assert_equal 'rab', opt.value
  end

  test "count type" do
    assert_equal 3, option_value(%w/-c -c -c/, :c, :as => :count)
    assert_equal 0, option_value(%w/-a -b -z/, :c, :as => :count)
    assert_equal 3, option_value(%w/-vvv/, :v, :as => :count)
  end

  # end type casting tests

  test "using a default value as fallback" do
    opts = Slop.new
    opts.on :f, :argument => :optional, :default => 'foo'
    opts.parse %w'-f'
    assert_equal 'foo', opts[:f]
  end

  test "printing options" do
    slop = Slop.new
    slop.opt :n, :name=, 'Your name'
    slop.opt :age=, 'Your age'
    slop.opt :V, 'Display the version'

    assert_equal "    -n, --name      Your name", slop.fetch_option(:name).to_s
    assert_equal "        --age       Your age", slop.fetch_option(:age).to_s
    assert_equal "    -V,             Display the version", slop.fetch_option(:V).help
  end

  test "printing options that have defaults" do
    opts = Slop.new
    opts.on :n, :name=, 'Your name', :default => 'Lee'

    assert_equal "    -n, --name      Your name (default: Lee)", opts.fetch_option(:name).to_s
  end

  test "overwriting the help text" do
    slop = Slop.new
    slop.on :foo, :help => '    -f, --foo  SOMETHING FOOEY'
    assert_equal '    -f, --foo  SOMETHING FOOEY', slop.fetch_option(:foo).help
  end
end