diff options
author | Lee Jarvis <ljjarvis@gmail.com> | 2014-11-18 22:16:20 +0000 |
---|---|---|
committer | Lee Jarvis <ljjarvis@gmail.com> | 2014-11-18 22:29:15 +0000 |
commit | e78399b4841d709ccc02de14c583f92ef8a649ff (patch) | |
tree | 012575c9523f7e8ba712d8421f189e0aec5ed65e /test | |
parent | c67a27c4b7554cbf66536e5bcebed60ff4cc7feb (diff) | |
download | slop-e78399b4841d709ccc02de14c583f92ef8a649ff.tar.gz |
Start of rewrite
Diffstat (limited to 'test')
-rw-r--r-- | test/command_test.rb | 99 | ||||
-rw-r--r-- | test/helper.rb | 12 | ||||
-rw-r--r-- | test/option_test.rb | 122 | ||||
-rw-r--r-- | test/options_test.rb | 56 | ||||
-rw-r--r-- | test/slop_test.rb | 27 | ||||
-rw-r--r-- | test/test_helper.rb | 6 |
6 files changed, 62 insertions, 260 deletions
diff --git a/test/command_test.rb b/test/command_test.rb deleted file mode 100644 index abe4b4f..0000000 --- a/test/command_test.rb +++ /dev/null @@ -1,99 +0,0 @@ -require 'helper' - -class CommandTest < TestCase - - def setup - @command = Slop::Command.new(:test) - end - - def teardown - @command = nil - end - - def option(*args, &block) - @command.option(*args, &block) - end - - def options(*args) - args.map { |a| option(*Array(a)) } - end - - def parse(items, &block) - @command.parse(items, &block) - end - - test "option" do - @command.option :user - assert_kind_of Slop::Option, @command.options[:user] - end - - test "command" do - @command.command :add - assert_kind_of Slop::Command, @command.commands[:add] - end - - test "parse with runner" do - run = nil - options :foo, :bar - @command.process do |command, args| - run = 1 - assert command.is_a?(Slop::Command) - assert_equal ['foo', 'bar'], args - end - parse %w[foo bar] - assert_equal 1, run - end - - test "[]" do - option "user=" - parse %w(--user Lee) - assert_equal "Lee", @command[:user] - end - - test "present?" do - options "user", "verbose" - parse %w(--user) - assert @command.present?("user") - assert @command.user? - refute @command.verbose? - end - - test "global?" do - assert Slop.new.global? - refute @command.global? - end - - test "to_hash" do - options "user=", "verbose", "other" - parse %w(--user Lee --verbose) - assert_equal({user: "Lee", verbose: nil, other: nil}, @command.to_hash) - - @command.command(:foo) { option :bar= } - parse %w(foo --bar baz etc) - assert_equal({user: "Lee", verbose: nil, other: nil, foo: {bar: "baz"}}, @command.to_hash) - end - - test "short_flag_prefix" do - assert_equal '@', Slop.new(short_flag_prefix: '@').short_flag_prefix - assert_equal '-', @command.short_flag_prefix - end - - test "long_flag_prefix" do - assert_equal '!!', Slop.new(long_flag_prefix: '!!').long_flag_prefix - assert_equal '--', @command.long_flag_prefix - end - - test "flag_match" do - assert @command.flag_match?("--user") - refute @command.flag_match?("user") - assert Slop.new(short_flag_prefix: '@').flag_match?('@foo') - end - - test "clean_flag" do - assert_equal "user", @command.clean_flag("--user") - assert_equal "u", @command.clean_flag("-u") - assert_equal "user", @command.clean_flag("user") - assert_equal "@user", @command.clean_flag("@user") - end - -end diff --git a/test/helper.rb b/test/helper.rb deleted file mode 100644 index e6ec32f..0000000 --- a/test/helper.rb +++ /dev/null @@ -1,12 +0,0 @@ -$VERBOSE = true - -require 'slop' - -require 'minitest/autorun' -require 'stringio' - -class TestCase < Minitest::Test - def self.test(name, &block) - define_method("test_#{name.gsub(/\W/, '_')}", &block) if block - end -end
\ No newline at end of file diff --git a/test/option_test.rb b/test/option_test.rb deleted file mode 100644 index 4d02140..0000000 --- a/test/option_test.rb +++ /dev/null @@ -1,122 +0,0 @@ -require 'helper' - -class OptionTest < TestCase - - def option(*args, &block) - Slop::Option.build(Slop.new, args, &block) - end - - def option_to_a(*args, &block) - option = option(*args, &block) - [option.short, option.long, option.description] - end - - def option_as(as, value, config = {}) - config.merge!(as: as) - option = Slop::Option.build(Slop.new, ["foo=", config]) - option.value = value - option.value - end - - test "::option" do - assert_equal ['u', nil, nil], option_to_a('u') - assert_equal ['u', nil, nil], option_to_a('u=') - - assert_equal [nil, 'user', nil], option_to_a('user') - assert_equal [nil, 'user', nil], option_to_a('user=') - - assert_equal ['u', 'user', nil], option_to_a('u=', 'user') - assert_equal ['u', 'user', nil], option_to_a('u', 'user=') - - assert_equal ['u', 'user', 'Foo'], option_to_a('u', 'user=', 'Foo') - assert_equal [nil, 'user', 'Foo'], option_to_a('user', 'Foo') - assert_equal ['u', nil, 'Foo Bar'], option_to_a('u', 'Foo Bar') - - assert_equal ['u', nil, 'Foo Bar'], option_to_a('u', 'Foo Bar', lorem: 'ipsum') - end - - test "argument?" do - assert option("user=").argument? - refute option("user").argument? - end - - test "optional_argument?" do - assert option("user", optional_argument: true).optional_argument? - refute option("user").optional_argument? - end - - test "runner" do - assert_equal "foo", option("user", runner: "foo").runner - assert_kind_of Proc, option("user") { }.runner - refute option("user").runner - end - - test "call" do - called = true - option = option("user") { called = true } - option.value = 'foo' - assert_equal 'foo', option.call - assert called - end - - test "execute" do - option = option("user") - assert_equal 0, option.count - option.execute - assert_equal 1, option.count - end - - test "key" do - assert_equal "foo", option("f", "foo").key - assert_equal "foo", option("foo").key - assert_equal "f", option("f").key - end - - test "help" do - skip "not implemented" - end - - test "default" do - assert_equal nil, option("f").call - assert_equal "foo", option("f", default: "foo").call - end - - test "as(String)" do - assert_equal "foo", option_as(String, "foo") - end - - test "as(Symbol)" do - assert_equal :foo, option_as(Symbol, "foo") - end - - test "as(Array)" do - assert_equal ["foo", "bar"], option_as(Array, "foo,bar") - assert_equal ["foo", "bar"], option_as(Array, "foo:bar", delimiter: ":") - assert_equal ["foo", "bar:baz"], option_as(Array, "foo:bar:baz", delimiter: ":", limit: 2) - end - - test "as(Integer)" do - assert_equal 1, option_as(Integer, "1") - end - - test "as(Float)" do - assert_equal 1.4, option_as(Float, "1.4") - end - - test "as(Range)" do - assert_equal((1..10), option_as(Range, "1..10")) - assert_equal((1..10), option_as(Range, "1-10")) - assert_equal((1..10), option_as(Range, "1,10")) - assert_equal((1...10), option_as(Range, "1...10")) - assert_equal((-1..10), option_as(Range, "-1..10")) - assert_equal((1..-10), option_as(Range, "1..-10")) - assert_equal((1..1), option_as(Range, "1")) - assert_equal((-1..10), option_as(Range, "-1..10", optional_argument: true)) - end - - test "as(Custom)" do - reverse = proc { |v| v.reverse } - assert_equal "oof", option_as(reverse, "foo") - end - -end diff --git a/test/options_test.rb b/test/options_test.rb new file mode 100644 index 0000000..97c45d9 --- /dev/null +++ b/test/options_test.rb @@ -0,0 +1,56 @@ +require 'test_helper' + +describe Slop::Options do + before do + @options = Slop::Options.new + end + + describe "#add" do + it "defaults to string type" do + assert_kind_of Slop::StringOption, @options.add("--foo") + end + + it "accepts custom types" do + module Slop; class FooOption < Option; end; end + assert_kind_of Slop::FooOption, @options.add("--foo", type: :foo) + end + + it "adds multiple flags" do + option = @options.add("-f", "-F", "--foo") + assert_equal %w(-f -F --foo), option.flags + end + + it "accepts a trailing description" do + option = @options.add("--foo", "fooey") + assert_equal "fooey", option.desc + end + + it "adds the option" do + option = @options.add("--foo") + assert_equal [option], @options.to_a + end + + it "raises an error when a duplicate flag is used" do + @options.add("--foo") + assert_raises(ArgumentError) { @options.add("--foo") } + end + end + + describe "#method_missing" do + it "uses the method name as an option type" do + option = @options.string("--name") + assert_kind_of Slop::StringOption, option + end + + it "raises if a type doesn't exist" do + assert_raises(NoMethodError) { @options.unknown } + end + end + + describe "#respond_to?" do + it "handles custom types" do + module Slop; class BarOption < Option; end; end + assert @options.respond_to?(:bar) + end + end +end diff --git a/test/slop_test.rb b/test/slop_test.rb deleted file mode 100644 index d7b44d0..0000000 --- a/test/slop_test.rb +++ /dev/null @@ -1,27 +0,0 @@ -require 'helper' - -class SlopTest < TestCase - - test "::parse!" do - assert_kind_of Slop::Command, Slop.parse!(%w(foo)) - end - - test "::parse" do - assert_kind_of Slop::Command, Slop.parse(%w(foo)) - end - - test "::new" do - assert Slop.new.global? - end - - test "errors" do - begin - Slop.parse(%w(--foo)) - rescue Slop::Error => e - assert_kind_of Slop::Command, e.command - assert_kind_of Slop::Command, e.opts - assert e.message.include?("foo") - end - end - -end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..8a7b470 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,6 @@ +$VERBOSE = true + +require 'slop' + +require 'minitest/autorun' +require 'stringio' |