test/test_command.rb in cri-2.15.6 vs test/test_command.rb in cri-2.15.7

- old
+ new

@@ -124,29 +124,29 @@ def test_invoke_simple_without_opts_or_args out, err = capture_io_while do simple_cmd.run(%w[]) end - assert_equal ['Awesome moo!', '', ''], lines(out) + assert_equal ['Awesome moo!', '', 'ddd=false,eee=false'], lines(out) assert_equal [], lines(err) end def test_invoke_simple_with_args out, err = capture_io_while do simple_cmd.run(%w[abc xyz]) end - assert_equal ['Awesome moo!', 'abc,xyz', ''], lines(out) + assert_equal ['Awesome moo!', 'abc,xyz', 'ddd=false,eee=false'], lines(out) assert_equal [], lines(err) end def test_invoke_simple_with_opts out, err = capture_io_while do simple_cmd.run(%w[-c -b x]) end - assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true'], lines(out) + assert_equal ['Awesome moo!', '', 'bbb=x,ccc=true,ddd=false,eee=false'], lines(out) assert_equal [], lines(err) end def test_invoke_simple_with_missing_opt_arg out, err = capture_io_while do @@ -214,11 +214,11 @@ def test_invoke_simple_with_opt_with_block out, err = capture_io_while do simple_cmd.run(%w[-a 123]) end - assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123'], lines(out) + assert_equal ['moo:123', 'Awesome moo!', '', 'aaa=123,ddd=false,eee=false'], lines(out) assert_equal [], lines(err) end def test_invoke_nested_without_opts_or_args out, err = capture_io_while do @@ -244,11 +244,11 @@ def test_invoke_nested_with_correct_command_name out, err = capture_io_while do nested_cmd.run(%w[sub]) end - assert_equal ['Sub-awesome!', '', ''], lines(out) + assert_equal ['Sub-awesome!', '', 'ddd=false,eee=false,ppp=false,qqq=false'], lines(out) assert_equal [], lines(err) end def test_invoke_nested_with_incorrect_command_name out, err = capture_io_while do @@ -295,20 +295,20 @@ def test_invoke_nested_with_alias out, err = capture_io_while do nested_cmd.run(%w[sup]) end - assert_equal ['Sub-awesome!', '', ''], lines(out) + assert_equal ['Sub-awesome!', '', 'ddd=false,eee=false,ppp=false,qqq=false'], lines(out) assert_equal [], lines(err) end def test_invoke_nested_with_options_before_command out, err = capture_io_while do nested_cmd.run(%w[-a 666 sub]) end - assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666'], lines(out) + assert_equal ['super:666', 'Sub-awesome!', '', 'aaa=666,ddd=false,eee=false,ppp=false,qqq=false'], lines(out) assert_equal [], lines(err) end def test_invoke_nested_with_run_block out, err = capture_io_while do @@ -891,9 +891,77 @@ # test -t first out, err = capture_io_while do cmd_a.run(%w[-t b c]) end assert_equal ['test? true!'], lines(out) + assert_equal [], lines(err) + end + + def test_flag_defaults_to_false + cmd = Cri::Command.define do + name 'a' + option :f, :force2, 'push with force', argument: :forbidden + + run do |opts, _args, _cmd| + puts "Force? #{opts[:force2].inspect}!" + end + end + + out, err = capture_io_while do + cmd.run(%w[]) + end + assert_equal ['Force? false!'], lines(out) + assert_equal [], lines(err) + end + + def test_required_option_defaults_to_given_value + cmd = Cri::Command.define do + name 'a' + option :a, :animal, 'specify animal', argument: :required, default: 'cow' + + run do |opts, _args, _cmd| + puts "Animal = #{opts[:animal]}" + end + end + + out, err = capture_io_while do + cmd.run(%w[]) + end + assert_equal ['Animal = cow'], lines(out) + assert_equal [], lines(err) + end + + def test_optional_option_defaults_to_given_value + cmd = Cri::Command.define do + name 'a' + option :a, :animal, 'specify animal', argument: :optional, default: 'cow' + + run do |opts, _args, _cmd| + puts "Animal = #{opts[:animal]}" + end + end + + out, err = capture_io_while do + cmd.run(%w[]) + end + assert_equal ['Animal = cow'], lines(out) + assert_equal [], lines(err) + end + + def test_required_option_defaults_to_given_value_with_transform + cmd = Cri::Command.define do + name 'a' + option :a, :animal, 'specify animal', argument: :required, transform: ->(a) { a.upcase }, default: 'cow' + + run do |opts, _args, _cmd| + puts "Animal = #{opts[:animal]}" + end + end + + out, err = capture_io_while do + cmd.run(%w[]) + end + assert_equal ['Animal = cow'], lines(out) assert_equal [], lines(err) end end end