test/test_command_dsl.rb in cri-2.5.0 vs test/test_command_dsl.rb in cri-2.6.0

- old
+ new

@@ -3,17 +3,17 @@ class Cri::CommandDSLTestCase < Cri::TestCase def test_create_command # Define dsl = Cri::CommandDSL.new - dsl.instance_eval do + dsl.instance_eval do name 'moo' usage 'dunno whatever' summary 'does stuff' description 'This command does a lot of stuff.' - option :a, :aaa, 'opt a', :argument => :optional + option :a, :aaa, 'opt a', :argument => :optional, :multiple => true required :b, :bbb, 'opt b' optional :c, :ccc, 'opt c' flag :d, :ddd, 'opt d' forbidden :e, :eee, 'opt e' @@ -34,15 +34,15 @@ assert_equal 'does stuff', command.summary assert_equal 'This command does a lot of stuff.', command.description # Check options expected_option_definitions = Set.new([ - { :short => 'a', :long => 'aaa', :desc => 'opt a', :argument => :optional, :block => nil }, - { :short => 'b', :long => 'bbb', :desc => 'opt b', :argument => :required, :block => nil }, - { :short => 'c', :long => 'ccc', :desc => 'opt c', :argument => :optional, :block => nil }, - { :short => 'd', :long => 'ddd', :desc => 'opt d', :argument => :forbidden, :block => nil }, - { :short => 'e', :long => 'eee', :desc => 'opt e', :argument => :forbidden, :block => nil } + { :short => 'a', :long => 'aaa', :desc => 'opt a', :argument => :optional, :multiple => true, :block => nil }, + { :short => 'b', :long => 'bbb', :desc => 'opt b', :argument => :required, :multiple => false, :block => nil }, + { :short => 'c', :long => 'ccc', :desc => 'opt c', :argument => :optional, :multiple => false, :block => nil }, + { :short => 'd', :long => 'ddd', :desc => 'opt d', :argument => :forbidden, :multiple => false, :block => nil }, + { :short => 'e', :long => 'eee', :desc => 'opt e', :argument => :forbidden, :multiple => false, :block => nil } ]) actual_option_definitions = Set.new(command.option_definitions) assert_equal expected_option_definitions, actual_option_definitions end @@ -69,25 +69,62 @@ command.run(%w( -s --long )) assert_equal :probably, $did_it_work # Check options expected_option_definitions = Set.new([ - { :short => 's', :long => nil, :desc => 'short', :argument => :forbidden, :block => nil }, - { :short => nil, :long => 'long', :desc => 'long', :argument => :forbidden, :block => nil } - ]) + { :short => 's', :long => nil, :desc => 'short', :argument => :forbidden, :multiple => false, :block => nil }, + { :short => nil, :long => 'long', :desc => 'long', :argument => :forbidden, :multiple => false, :block => nil } + ]) actual_option_definitions = Set.new(command.option_definitions) assert_equal expected_option_definitions, actual_option_definitions end - def test_optional_options + def test_multiple # Define dsl = Cri::CommandDSL.new + dsl.instance_eval do + flag :f, :flag, 'sample flag option', :multiple => true + required :r, :required, 'sample required option', :multiple => true + optional :o, :optional, 'sample optional option', :multiple => true + + run { |opts, args| } + end + command = dsl.command + + # Check options + expected_option_definitions = Set.new([ + { :short => 'f', :long => 'flag', :desc => 'sample flag option', :argument => :forbidden, :multiple => true, :block => nil }, + { :short => 'r', :long => 'required', :desc => 'sample required option', :argument => :required, :multiple => true, :block => nil }, + { :short => 'o', :long => 'optional', :desc => 'sample optional option', :argument => :optional, :multiple => true, :block => nil }, + ]) + actual_option_definitions = Set.new(command.option_definitions) + assert_equal expected_option_definitions, actual_option_definitions + end + + def test_required_short_and_long + # Define + dsl = Cri::CommandDSL.new assert_raises ArgumentError do dsl.instance_eval do + option nil, nil, 'meh' + end + end + assert_raises ArgumentError do + dsl.instance_eval do flag nil, nil, 'meh' end end + assert_raises ArgumentError do + dsl.instance_eval do + required nil, nil, 'meh' + end + end + assert_raises ArgumentError do + dsl.instance_eval do + optional nil, nil, 'meh' + end + end end def test_subcommand # Define dsl = Cri::CommandDSL.new @@ -113,9 +150,19 @@ end command = dsl.command # Check assert_equal %w( aah moo ), command.aliases.sort + end + + def test_run_arity + dsl = Cri::CommandDSL.new + assert_raises ArgumentError do + dsl.instance_eval do + run do |a, b, c, d, e| + end + end + end end def test_runner # Define dsl = Cri::CommandDSL.new