Class: Trollop::Test::Trollop
- Inherits:
-
Test::Unit::TestCase
- Object
- Test::Unit::TestCase
- Trollop::Test::Trollop
- Defined in:
- lib/vendor/mainline/test/test_trollop.rb
Instance Method Summary collapse
- #assert_parses_correctly(parser, commandline, expected_opts, expected_leftovers) ⇒ Object
- #setup ⇒ Object
- #short_options_with_multiple_options_does_not_affect_flags_type ⇒ Object
- #test_accepts_arguments_with_spaces ⇒ Object
- #test_alternate_args ⇒ Object
- #test_ambigious_multi_plus_array_default_resolved_as_specified_by_documentation ⇒ Object
-
#test_argflags_demand_args ⇒ Object
flags that take an argument error unless given one.
-
#test_arglessflags_refuse_args ⇒ Object
flags that don’t take arguments ignore them.
- #test_arguments_passed_through_block ⇒ Object
- #test_auto_generated_long_names_convert_underscores_to_hyphens ⇒ Object
- #test_combined_short_options_with_multiple_arguments ⇒ Object
- #test_conflict_error_messages ⇒ Object
-
#test_conflicting_longs_detected ⇒ Object
two args can’t have the same :long.
-
#test_conflicting_names_are_detected ⇒ Object
two args can’t have the same name.
-
#test_conflicting_shorts_detected ⇒ Object
two args can’t have the same :short.
- #test_conflicts ⇒ Object
- #test_date_formatting ⇒ Object
- #test_default_shorts_assigned_only_after_user_shorts ⇒ Object
- #test_depend_error_messages ⇒ Object
- #test_depends ⇒ Object
- #test_doubledash_ends_option_processing ⇒ Object
- #test_flag_defaults ⇒ Object
- #test_floating_point_formatting ⇒ Object
- #test_given_keys ⇒ Object
- #test_help_has_default_banner ⇒ Object
- #test_help_preserves_positions ⇒ Object
- #test_io_arg_type ⇒ Object
- #test_long_detects_bad_names ⇒ Object
- #test_long_options_also_take_equals ⇒ Object
- #test_long_options_with_multiple_arguments ⇒ Object
- #test_long_options_with_multiple_options ⇒ Object
- #test_long_options_with_multiple_options_and_arguments ⇒ Object
- #test_multi_args_autobox_defaults ⇒ Object
- #test_multi_args_default_to_empty_array ⇒ Object
- #test_openstruct_style_access ⇒ Object
- #test_options_can_be_set_multiple_times_if_specified ⇒ Object
- #test_options_cant_be_set_multiple_times_if_not_specified ⇒ Object
- #test_required_flags_are_required ⇒ Object
- #test_short_autocreation_is_ok_with_running_out_of_chars ⇒ Object
- #test_short_autocreation_skips_dashes_and_numbers ⇒ Object
- #test_short_can_be_nothing ⇒ Object
- #test_short_detects_bad_names ⇒ Object
- #test_short_names_created_automatically ⇒ Object
- #test_short_options_can_be_weird ⇒ Object
- #test_short_options_cant_be_numeric ⇒ Object
- #test_short_options_combine ⇒ Object
- #test_short_options_with_multiple_arguments ⇒ Object
- #test_short_options_with_multiple_options ⇒ Object
- #test_short_options_with_multiple_options_and_arguments ⇒ Object
- #test_simple_interface_handles_die ⇒ Object
- #test_simple_interface_handles_help ⇒ Object
- #test_simple_interface_handles_regular_usage ⇒ Object
- #test_simple_interface_handles_version ⇒ Object
- #test_special_flags_work ⇒ Object
- #test_stopwords_mixed ⇒ Object
- #test_stopwords_multiple_stopwords ⇒ Object
- #test_stopwords_no_stopwords ⇒ Object
- #test_stopwords_with_short_args ⇒ Object
- #test_syntax_check ⇒ Object
-
#test_two_required_one_missing_accuses_correctly ⇒ Object
courtesy neill zero.
-
#test_type_and_default_must_match ⇒ Object
:type and :default must match if both are specified.
-
#test_type_correctly_derived_from_default ⇒ Object
type is correctly derived from :default.
-
#test_typed_args_refuse_args_of_other_types ⇒ Object
flags that require args of a specific type refuse args of other types.
- #test_unknown_arguments ⇒ Object
- #test_unknown_subcommand ⇒ Object
- #test_version_and_help_long_args_can_be_overridden ⇒ Object
- #test_version_and_help_override_errors ⇒ Object
- #test_version_and_help_short_args_can_be_overridden ⇒ Object
- #test_version_only_appears_if_set ⇒ Object
- #test_wrap ⇒ Object
Instance Method Details
#assert_parses_correctly(parser, commandline, expected_opts, expected_leftovers) ⇒ Object
867 868 869 870 871 872 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 867 def assert_parses_correctly(parser, commandline, expected_opts, expected_leftovers) opts = parser.parse commandline assert_equal expected_opts, opts assert_equal expected_leftovers, parser.leftovers end |
#setup ⇒ Object
14 15 16 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 14 def setup @p = Parser.new end |
#short_options_with_multiple_options_does_not_affect_flags_type ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 431 def opts = nil assert_nothing_raised do @p.opt :xarg, "desc", :short => "-x", :type => :flag, :multi => true end assert_nothing_raised { opts = @p.parse %w(-x a) } assert_equal true, opts[:xarg] assert_equal %w(a), @p.leftovers assert_nothing_raised { opts = @p.parse %w(-x a -x b) } assert_equal true, opts[:xarg] assert_equal %w(a b), @p.leftovers assert_nothing_raised { opts = @p.parse %w(-xx a -x b) } assert_equal true, opts[:xarg] assert_equal %w(a b), @p.leftovers end |
#test_accepts_arguments_with_spaces ⇒ Object
1032 1033 1034 1035 1036 1037 1038 1039 1040 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 1032 def test_accepts_arguments_with_spaces @p.opt :arg1, "arg", :type => String @p.opt :arg2, "arg2", :type => String opts = @p.parse ["--arg1", "hello there", "--arg2=hello there"] assert_equal "hello there", opts[:arg1] assert_equal "hello there", opts[:arg2] assert_equal 0, @p.leftovers.size end |
#test_alternate_args ⇒ Object
896 897 898 899 900 901 902 903 904 905 906 907 908 909 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 896 def test_alternate_args args = %w(-a -b -c) opts = ::Trollop.(args) do opt :alpher, "Ralph Alpher", :short => "-a" opt :bethe, "Hans Bethe", :short => "-b" opt :gamow, "George Gamow", :short => "-c" end physicists_with_humor = [:alpher, :bethe, :gamow] physicists_with_humor.each do |physicist| assert_equal true, opts[physicist] end end |
#test_ambigious_multi_plus_array_default_resolved_as_specified_by_documentation ⇒ Object
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 962 def test_ambigious_multi_plus_array_default_resolved_as_specified_by_documentation @p.opt :arg1, "desc", :default => ["potato"], :multi => true @p.opt :arg2, "desc", :default => ["potato"], :multi => true, :type => :strings @p.opt :arg3, "desc", :default => ["potato"] @p.opt :arg4, "desc", :default => ["potato", "rhubarb"], :short => :none, :multi => true ## arg1 should be multi-occurring but not multi-valued opts = @p.parse %w(--arg1 one two) assert_equal ["one"], opts[:arg1] assert_equal ["two"], @p.leftovers opts = @p.parse %w(--arg1 one --arg1 two) assert_equal ["one", "two"], opts[:arg1] assert_equal [], @p.leftovers ## arg2 should be multi-valued and multi-occurring opts = @p.parse %w(--arg2 one two) assert_equal [["one", "two"]], opts[:arg2] assert_equal [], @p.leftovers ## arg3 should be multi-valued but not multi-occurring opts = @p.parse %w(--arg3 one two) assert_equal ["one", "two"], opts[:arg3] assert_equal [], @p.leftovers ## arg4 should be multi-valued but not multi-occurring opts = @p.parse %w() assert_equal ["potato", "rhubarb"], opts[:arg4] end |
#test_argflags_demand_args ⇒ Object
flags that take an argument error unless given one
46 47 48 49 50 51 52 53 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 46 def test_argflags_demand_args @p.opt "goodarg", "desc", :type => String @p.opt "goodarg2", "desc", :type => String assert_nothing_raised { @p.parse(%w(--goodarg goat)) } assert_raise(CommandlineError) { @p.parse(%w(--goodarg --goodarg2 goat)) } assert_raise(CommandlineError) { @p.parse(%w(--goodarg)) } end |
#test_arglessflags_refuse_args ⇒ Object
flags that don’t take arguments ignore them
56 57 58 59 60 61 62 63 64 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 56 def test_arglessflags_refuse_args @p.opt "goodarg" @p.opt "goodarg2" assert_nothing_raised { @p.parse(%w(--goodarg)) } assert_nothing_raised { @p.parse(%w(--goodarg --goodarg2)) } opts = @p.parse %w(--goodarg a) assert_equal true, opts["goodarg"] assert_equal ["a"], @p.leftovers end |
#test_arguments_passed_through_block ⇒ Object
597 598 599 600 601 602 603 604 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 597 def test_arguments_passed_through_block @goat = 3 boat = 4 Parser.new(@goat) do |goat| boat = goat end assert_equal @goat, boat end |
#test_auto_generated_long_names_convert_underscores_to_hyphens ⇒ Object
592 593 594 595 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 592 def test_auto_generated_long_names_convert_underscores_to_hyphens @p.opt :hello_there assert_equal "hello-there", @p.specs[:hello_there][:long] end |
#test_combined_short_options_with_multiple_arguments ⇒ Object
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 489 def @p.opt :arg1, "desc", :short => "a" @p.opt :arg2, "desc", :short => "b" @p.opt :arg3, "desc", :short => "c", :type => :ints @p.opt :arg4, "desc", :short => "d", :type => :floats opts = nil assert_nothing_raised { opts = @p.parse %w(-abc 4 6 9) } assert_equal true, opts[:arg1] assert_equal true, opts[:arg2] assert_equal [4, 6, 9], opts[:arg3] assert_nothing_raised { opts = @p.parse %w(-ac 4 6 9 -bd 3.14 2.41) } assert_equal true, opts[:arg1] assert_equal true, opts[:arg2] assert_equal [4, 6, 9], opts[:arg3] assert_equal [3.14, 2.41], opts[:arg4] assert_raises(CommandlineError) { opts = @p.parse %w(-abcd 3.14 2.41) } end |
#test_conflict_error_messages ⇒ Object
705 706 707 708 709 710 711 712 713 714 715 716 717 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 705 def @p.opt :one @p.opt "two" @p.conflicts :one, "two" begin @p.parse %w(--one --two) flunk "no error thrown" rescue CommandlineError => e assert_match(/--one/, e.) assert_match(/--two/, e.) end end |
#test_conflicting_longs_detected ⇒ Object
two args can’t have the same :long
247 248 249 250 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 247 def test_conflicting_longs_detected assert_nothing_raised { @p.opt "goodarg", "desc", :long => "--goodarg" } assert_raise(ArgumentError) { @p.opt "badarg", "desc", :long => "--goodarg" } end |
#test_conflicting_names_are_detected ⇒ Object
two args can’t have the same name
241 242 243 244 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 241 def test_conflicting_names_are_detected assert_nothing_raised { @p.opt "goodarg" } assert_raise(ArgumentError) { @p.opt "goodarg" } end |
#test_conflicting_shorts_detected ⇒ Object
two args can’t have the same :short
253 254 255 256 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 253 def test_conflicting_shorts_detected assert_nothing_raised { @p.opt "goodarg", "desc", :short => "-g" } assert_raise(ArgumentError) { @p.opt "badarg", "desc", :short => "-g" } end |
#test_conflicts ⇒ Object
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 675 def test_conflicts @p.opt :one assert_raises(ArgumentError) { @p.conflicts :one, :two } @p.opt :two assert_nothing_raised { @p.conflicts :one, :two } assert_nothing_raised { @p.parse %w(--one) } assert_nothing_raised { @p.parse %w(--two) } assert_raises(CommandlineError) { opts = @p.parse %w(--one --two) } @p.opt :hello @p.opt :yellow @p.opt :mellow @p.opt :jello @p.conflicts :hello, :yellow, :mellow, :jello assert_raises(CommandlineError) { opts = @p.parse %w(--hello --yellow --mellow --jello) } assert_raises(CommandlineError) { opts = @p.parse %w(--hello --mellow --jello) } assert_raises(CommandlineError) { opts = @p.parse %w(--hello --jello) } assert_nothing_raised { opts = @p.parse %w(--hello) } assert_nothing_raised { opts = @p.parse %w(--jello) } assert_nothing_raised { opts = @p.parse %w(--yellow) } assert_nothing_raised { opts = @p.parse %w(--mellow) } assert_nothing_raised { opts = @p.parse %w(--mellow --one) } assert_nothing_raised { opts = @p.parse %w(--mellow --two) } assert_raises(CommandlineError) { opts = @p.parse %w(--mellow --two --jello) } assert_raises(CommandlineError) { opts = @p.parse %w(--one --mellow --two --jello) } end |
#test_date_formatting ⇒ Object
377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 377 def test_date_formatting @p.opt :arg, "desc", :type => :date, :short => 'd' opts = nil assert_nothing_raised { opts = @p.parse(['-d', 'Jan 4, 2007']) } assert_equal Date.civil(2007, 1, 4), opts[:arg] begin require 'chronic' assert_nothing_raised { opts = @p.parse(['-d', 'today']) } assert_equal Date.today, opts[:arg] rescue LoadError # chronic is not available end end |
#test_default_shorts_assigned_only_after_user_shorts ⇒ Object
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 1013 def test_default_shorts_assigned_only_after_user_shorts @p.opt :aab, "aaa" # should be assigned to -b @p.opt :ccd, "bbb" # should be assigned to -d @p.opt :user1, "user1", :short => 'a' @p.opt :user2, "user2", :short => 'c' opts = @p.parse %w(-a -b) assert opts[:user1] assert !opts[:user2] assert opts[:aab] assert !opts[:ccd] opts = @p.parse %w(-c -d) assert !opts[:user1] assert opts[:user2] assert !opts[:aab] assert opts[:ccd] end |
#test_depend_error_messages ⇒ Object
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 746 def @p.opt :one @p.opt "two" @p.depends :one, "two" assert_nothing_raised { @p.parse %w(--one --two) } begin @p.parse %w(--one) flunk "no error thrown" rescue CommandlineError => e assert_match(/--one/, e.) assert_match(/--two/, e.) end begin @p.parse %w(--two) flunk "no error thrown" rescue CommandlineError => e assert_match(/--one/, e.) assert_match(/--two/, e.) end end |
#test_depends ⇒ Object
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 719 def test_depends @p.opt :one assert_raises(ArgumentError) { @p.depends :one, :two } @p.opt :two assert_nothing_raised { @p.depends :one, :two } assert_nothing_raised { opts = @p.parse %w(--one --two) } assert_raises(CommandlineError) { @p.parse %w(--one) } assert_raises(CommandlineError) { @p.parse %w(--two) } @p.opt :hello @p.opt :yellow @p.opt :mellow @p.opt :jello @p.depends :hello, :yellow, :mellow, :jello assert_nothing_raised { opts = @p.parse %w(--hello --yellow --mellow --jello) } assert_raises(CommandlineError) { opts = @p.parse %w(--hello --mellow --jello) } assert_raises(CommandlineError) { opts = @p.parse %w(--hello --jello) } assert_raises(CommandlineError) { opts = @p.parse %w(--hello) } assert_raises(CommandlineError) { opts = @p.parse %w(--mellow) } assert_nothing_raised { opts = @p.parse %w(--hello --yellow --mellow --jello --one --two) } assert_nothing_raised { opts = @p.parse %w(--hello --yellow --mellow --jello --one --two a b c) } assert_raises(CommandlineError) { opts = @p.parse %w(--mellow --two --jello --one) } end |
#test_doubledash_ends_option_processing ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 308 def test_doubledash_ends_option_processing @p.opt :arg1, "desc", :short => "a", :default => 0 @p.opt :arg2, "desc", :short => "b", :default => 0 opts = nil assert_nothing_raised { opts = @p.parse %w(-- -a 3 -b 2) } assert_equal opts[:arg1], 0 assert_equal opts[:arg2], 0 assert_equal %w(-a 3 -b 2), @p.leftovers assert_nothing_raised { opts = @p.parse %w(-a 3 -- -b 2) } assert_equal opts[:arg1], 3 assert_equal opts[:arg2], 0 assert_equal %w(-b 2), @p.leftovers assert_nothing_raised { opts = @p.parse %w(-a 3 -b 2 --) } assert_equal opts[:arg1], 3 assert_equal opts[:arg2], 2 assert_equal %w(), @p.leftovers end |
#test_flag_defaults ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 258 def test_flag_defaults @p.opt "defaultfalse", "desc" @p.opt "defaulttrue", "desc", :default => true opts = @p.parse [] assert_equal false, opts["defaultfalse"] assert_equal true, opts["defaulttrue"] opts = @p.parse %w(--defaultfalse --defaulttrue) assert_equal true, opts["defaultfalse"] assert_equal false, opts["defaulttrue"] end |
#test_floating_point_formatting ⇒ Object
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 347 def test_floating_point_formatting @p.opt :arg, "desc", :type => :float, :short => "f" opts = nil assert_nothing_raised { opts = @p.parse %w(-f 1) } assert_equal 1.0, opts[:arg] assert_nothing_raised { opts = @p.parse %w(-f 1.0) } assert_equal 1.0, opts[:arg] assert_nothing_raised { opts = @p.parse %w(-f 0.1) } assert_equal 0.1, opts[:arg] assert_nothing_raised { opts = @p.parse %w(-f .1) } assert_equal 0.1, opts[:arg] assert_nothing_raised { opts = @p.parse %w(-f .99999999999999999999) } assert_equal 1.0, opts[:arg] assert_nothing_raised { opts = @p.parse %w(-f -1) } assert_equal(-1.0, opts[:arg]) assert_nothing_raised { opts = @p.parse %w(-f -1.0) } assert_equal(-1.0, opts[:arg]) assert_nothing_raised { opts = @p.parse %w(-f -0.1) } assert_equal(-0.1, opts[:arg]) assert_nothing_raised { opts = @p.parse %w(-f -.1) } assert_equal(-0.1, opts[:arg]) assert_raises(CommandlineError) { @p.parse %w(-f a) } assert_raises(CommandlineError) { @p.parse %w(-f 1a) } assert_raises(CommandlineError) { @p.parse %w(-f 1.a) } assert_raises(CommandlineError) { @p.parse %w(-f a.1) } assert_raises(CommandlineError) { @p.parse %w(-f 1.0.0) } assert_raises(CommandlineError) { @p.parse %w(-f .) } assert_raises(CommandlineError) { @p.parse %w(-f -.) } end |
#test_given_keys ⇒ Object
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 992 def test_given_keys @p.opt :arg1 @p.opt :arg2 opts = @p.parse %w(--arg1) assert opts[:arg1_given] assert !opts[:arg2_given] opts = @p.parse %w(--arg2) assert !opts[:arg1_given] assert opts[:arg2_given] opts = @p.parse [] assert !opts[:arg1_given] assert !opts[:arg2_given] opts = @p.parse %w(--arg1 --arg2) assert opts[:arg1_given] assert opts[:arg2_given] end |
#test_help_has_default_banner ⇒ Object
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 606 def @p = Parser.new sio = StringIO.new "w" @p.parse [] @p.educate sio help = sio.string.split "\n" assert help[0] =~ /options/i assert_equal 2, help.length # options, then -h @p = Parser.new @p.version "my version" sio = StringIO.new "w" @p.parse [] @p.educate sio help = sio.string.split "\n" assert help[0] =~ /my version/i assert_equal 4, help.length # version, options, -h, -v @p = Parser.new @p. "my own banner" sio = StringIO.new "w" @p.parse [] @p.educate sio help = sio.string.split "\n" assert help[0] =~ /my own banner/i assert_equal 2, help.length # banner, -h end |
#test_help_preserves_positions ⇒ Object
634 635 636 637 638 639 640 641 642 643 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 634 def test_help_preserves_positions @p.opt :zzz, "zzz" @p.opt :aaa, "aaa" sio = StringIO.new "w" @p.educate sio help = sio.string.split "\n" assert help[1] =~ /zzz/ assert help[2] =~ /aaa/ end |
#test_io_arg_type ⇒ Object
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 911 def test_io_arg_type @p.opt :arg, "desc", :type => :io @p.opt :arg2, "desc", :type => IO @p.opt :arg3, "desc", :default => $stdout opts = nil assert_nothing_raised { opts = @p.parse() } assert_equal $stdout, opts[:arg3] assert_nothing_raised { opts = @p.parse %w(--arg /dev/null) } assert_kind_of File, opts[:arg] assert_equal "/dev/null", opts[:arg].path #TODO: move to mocks #assert_nothing_raised { opts = @p.parse %w(--arg2 http://google.com/) } #assert_kind_of StringIO, opts[:arg2] assert_nothing_raised { opts = @p.parse %w(--arg3 stdin) } assert_equal $stdin, opts[:arg3] assert_raises(CommandlineError) { opts = @p.parse %w(--arg /fdasfasef/fessafef/asdfasdfa/fesasf) } end |
#test_long_detects_bad_names ⇒ Object
177 178 179 180 181 182 183 184 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 177 def test_long_detects_bad_names assert_nothing_raised { @p.opt "goodarg", "desc", :long => "none" } assert_nothing_raised { @p.opt "goodarg2", "desc", :long => "--two" } assert_raise(ArgumentError) { @p.opt "badarg", "desc", :long => "" } assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :long => "--" } assert_raise(ArgumentError) { @p.opt "badarg3", "desc", :long => "-one" } assert_raise(ArgumentError) { @p.opt "badarg4", "desc", :long => "---toomany" } end |
#test_long_options_also_take_equals ⇒ Object
578 579 580 581 582 583 584 585 586 587 588 589 590 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 578 def @p.opt :arg, "desc", :long => "arg", :type => String, :default => "hello" opts = nil assert_nothing_raised { opts = @p.parse %w() } assert_equal "hello", opts[:arg] assert_nothing_raised { opts = @p.parse %w(--arg goat) } assert_equal "goat", opts[:arg] assert_nothing_raised { opts = @p.parse %w(--arg=goat) } assert_equal "goat", opts[:arg] ## actually, this next one is valid. empty string for --arg, and goat as a ## leftover. ## assert_raises(CommandlineError) { opts = @p.parse %w(--arg= goat) } end |
#test_long_options_with_multiple_arguments ⇒ Object
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 522 def opts = nil @p.opt :xarg, "desc", :type => :ints assert_nothing_raised { opts = @p.parse %w(--xarg 3 2 5) } assert_equal [3, 2, 5], opts[:xarg] assert_equal [], @p.leftovers assert_nothing_raised { opts = @p.parse %w(--xarg=3) } assert_equal [3], opts[:xarg] assert_equal [], @p.leftovers @p.opt :yarg, "desc", :type => :floats assert_nothing_raised { opts = @p.parse %w(--yarg 3.14 2.41 5.66) } assert_equal [3.14, 2.41, 5.66], opts[:yarg] assert_equal [], @p.leftovers assert_nothing_raised { opts = @p.parse %w(--yarg=3.14) } assert_equal [3.14], opts[:yarg] assert_equal [], @p.leftovers @p.opt :zarg, "desc", :type => :strings assert_nothing_raised { opts = @p.parse %w(--zarg a b c) } assert_equal %w(a b c), opts[:zarg] assert_equal [], @p.leftovers assert_nothing_raised { opts = @p.parse %w(--zarg=a) } assert_equal %w(a), opts[:zarg] assert_equal [], @p.leftovers end |
#test_long_options_with_multiple_options ⇒ Object
511 512 513 514 515 516 517 518 519 520 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 511 def @p.opt :xarg, "desc", :type => String, :multi => true opts = nil assert_nothing_raised { opts = @p.parse %w(--xarg=a --xarg=b) } assert_equal %w(a b), opts[:xarg] assert_equal [], @p.leftovers assert_nothing_raised { opts = @p.parse %w(--xarg a --xarg b) } assert_equal %w(a b), opts[:xarg] assert_equal [], @p.leftovers end |
#test_long_options_with_multiple_options_and_arguments ⇒ Object
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 550 def opts = nil @p.opt :xarg, "desc", :type => :ints, :multi => true assert_nothing_raised { opts = @p.parse %w(--xarg 3 2 5 --xarg 2 1) } assert_equal [[3, 2, 5], [2, 1]], opts[:xarg] assert_equal [], @p.leftovers assert_nothing_raised { opts = @p.parse %w(--xarg=3 --xarg=2) } assert_equal [[3], [2]], opts[:xarg] assert_equal [], @p.leftovers @p.opt :yarg, "desc", :type => :floats, :multi => true assert_nothing_raised { opts = @p.parse %w(--yarg 3.14 2.72 5 --yarg 2.41 1.41) } assert_equal [[3.14, 2.72, 5], [2.41, 1.41]], opts[:yarg] assert_equal [], @p.leftovers assert_nothing_raised { opts = @p.parse %w(--yarg=3.14 --yarg=2.41) } assert_equal [[3.14], [2.41]], opts[:yarg] assert_equal [], @p.leftovers @p.opt :zarg, "desc", :type => :strings, :multi => true assert_nothing_raised { opts = @p.parse %w(--zarg a b c --zarg d e) } assert_equal [%w(a b c), %w(d e)], opts[:zarg] assert_equal [], @p.leftovers assert_nothing_raised { opts = @p.parse %w(--zarg=a --zarg=d) } assert_equal [%w(a), %w(d)], opts[:zarg] assert_equal [], @p.leftovers end |
#test_multi_args_autobox_defaults ⇒ Object
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 946 def test_multi_args_autobox_defaults @p.opt :arg1, "desc", :default => "hello", :multi => true @p.opt :arg2, "desc", :default => ["hello"], :multi => true opts = @p.parse assert_equal ["hello"], opts[:arg1] assert_equal ["hello"], opts[:arg2] opts = @p.parse %w(--arg1 hello) assert_equal ["hello"], opts[:arg1] assert_equal ["hello"], opts[:arg2] opts = @p.parse %w(--arg1 hello --arg1 there) assert_equal ["hello", "there"], opts[:arg1] end |
#test_multi_args_default_to_empty_array ⇒ Object
1042 1043 1044 1045 1046 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 1042 def test_multi_args_default_to_empty_array @p.opt :arg1, "arg", :multi => true opts = @p.parse [] assert_equal [], opts[:arg1] end |
#test_openstruct_style_access ⇒ Object
934 935 936 937 938 939 940 941 942 943 944 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 934 def test_openstruct_style_access @p.opt "arg1", "desc", :type => :int @p.opt :arg2, "desc", :type => :int opts = @p.parse(%w(--arg1 3 --arg2 4)) assert_nothing_raised { opts.arg1 } assert_nothing_raised { opts.arg2 } assert_equal 3, opts.arg1 assert_equal 4, opts.arg2 end |
#test_options_can_be_set_multiple_times_if_specified ⇒ Object
411 412 413 414 415 416 417 418 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 411 def assert_nothing_raised do @p.opt :arg, "desc", :short => "-x", :multi => true end assert_nothing_raised { @p.parse %w(-x) } assert_nothing_raised { @p.parse %w(-x -x) } assert_nothing_raised { @p.parse %w(-xx) } end |
#test_options_cant_be_set_multiple_times_if_not_specified ⇒ Object
404 405 406 407 408 409 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 404 def @p.opt :arg, "desc", :short => "-x" assert_nothing_raised { @p.parse %w(-x) } assert_raises(CommandlineError) { @p.parse %w(-x -x) } assert_raises(CommandlineError) { @p.parse %w(-xx) } end |
#test_required_flags_are_required ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 34 def test_required_flags_are_required @p.opt "arg", "desc", :required => true @p.opt "arg2", "desc", :required => false @p.opt "arg3", "desc", :required => false assert_nothing_raised { @p.parse(%w(--arg)) } assert_nothing_raised { @p.parse(%w(--arg --arg2)) } assert_raise(CommandlineError) { @p.parse(%w(--arg2)) } assert_raise(CommandlineError) { @p.parse(%w(--arg2 --arg3)) } end |
#test_short_autocreation_is_ok_with_running_out_of_chars ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 219 def test_short_autocreation_is_ok_with_running_out_of_chars @p.opt :arg1 # auto: a @p.opt :arg2 # auto: r @p.opt :arg3 # auto: g @p.opt :arg4 # auto: uh oh! assert_nothing_raised { @p.parse [] } end |
#test_short_autocreation_skips_dashes_and_numbers ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 204 def test_short_autocreation_skips_dashes_and_numbers @p.opt :arg # auto: a @p.opt :arg_potato # auto: r @p.opt :arg_muffin # auto: g assert_nothing_raised { @p.opt :arg_daisy } # auto: d (not _)! assert_nothing_raised { @p.opt :arg_r2d2f } # auto: f (not 2)! opts = @p.parse %w(-f -d) assert_equal true, opts[:arg_daisy] assert_equal true, opts[:arg_r2d2f] assert_equal false, opts[:arg] assert_equal false, opts[:arg_potato] assert_equal false, opts[:arg_muffin] end |
#test_short_can_be_nothing ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 227 def test_short_can_be_nothing assert_nothing_raised do @p.opt "arg", "desc", :short => :none @p.parse [] end sio = StringIO.new "w" @p.educate sio assert sio.string =~ /--arg:\s+desc/ assert_raise(CommandlineError) { @p.parse %w(-a) } end |
#test_short_detects_bad_names ⇒ Object
186 187 188 189 190 191 192 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 186 def test_short_detects_bad_names assert_nothing_raised { @p.opt "goodarg", "desc", :short => "a" } assert_nothing_raised { @p.opt "goodarg2", "desc", :short => "-b" } assert_raise(ArgumentError) { @p.opt "badarg", "desc", :short => "" } assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :short => "-ab" } assert_raise(ArgumentError) { @p.opt "badarg3", "desc", :short => "--t" } end |
#test_short_names_created_automatically ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 194 def test_short_names_created_automatically @p.opt "arg" @p.opt "arg2" @p.opt "arg3" opts = @p.parse %w(-a -g) assert_equal true, opts["arg"] assert_equal false, opts["arg2"] assert_equal true, opts["arg3"] end |
#test_short_options_can_be_weird ⇒ Object
398 399 400 401 402 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 398 def assert_nothing_raised { @p.opt :arg1, "desc", :short => "#" } assert_nothing_raised { @p.opt :arg2, "desc", :short => "." } assert_raises(ArgumentError) { @p.opt :arg3, "desc", :short => "-" } end |
#test_short_options_cant_be_numeric ⇒ Object
391 392 393 394 395 396 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 391 def assert_raises(ArgumentError) { @p.opt :arg, "desc", :short => "-1" } @p.opt :a1b, "desc" @p.opt :a2b, "desc" assert_not_equal "2", @p.specs[:a2b][:short] end |
#test_short_options_combine ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 276 def @p.opt :arg1, "desc", :short => "a" @p.opt :arg2, "desc", :short => "b" @p.opt :arg3, "desc", :short => "c", :type => :int opts = nil assert_nothing_raised { opts = @p.parse %w(-a -b) } assert_equal true, opts[:arg1] assert_equal true, opts[:arg2] assert_equal nil, opts[:arg3] assert_nothing_raised { opts = @p.parse %w(-ab) } assert_equal true, opts[:arg1] assert_equal true, opts[:arg2] assert_equal nil, opts[:arg3] assert_nothing_raised { opts = @p.parse %w(-ac 4 -b) } assert_equal true, opts[:arg1] assert_equal true, opts[:arg2] assert_equal 4, opts[:arg3] assert_raises(CommandlineError) { @p.parse %w(-cab 4) } assert_raises(CommandlineError) { @p.parse %w(-cba 4) } end |
#test_short_options_with_multiple_arguments ⇒ Object
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 451 def opts = nil @p.opt :xarg, "desc", :type => :ints assert_nothing_raised { opts = @p.parse %w(-x 3 4 0) } assert_equal [3, 4, 0], opts[:xarg] assert_equal [], @p.leftovers @p.opt :yarg, "desc", :type => :floats assert_nothing_raised { opts = @p.parse %w(-y 3.14 4.21 0.66) } assert_equal [3.14, 4.21, 0.66], opts[:yarg] assert_equal [], @p.leftovers @p.opt :zarg, "desc", :type => :strings assert_nothing_raised { opts = @p.parse %w(-z a b c) } assert_equal %w(a b c), opts[:zarg] assert_equal [], @p.leftovers end |
#test_short_options_with_multiple_options ⇒ Object
420 421 422 423 424 425 426 427 428 429 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 420 def opts = nil assert_nothing_raised do @p.opt :xarg, "desc", :short => "-x", :type => String, :multi => true end assert_nothing_raised { opts = @p.parse %w(-x a -x b) } assert_equal %w(a b), opts[:xarg] assert_equal [], @p.leftovers end |
#test_short_options_with_multiple_options_and_arguments ⇒ Object
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 470 def opts = nil @p.opt :xarg, "desc", :type => :ints, :multi => true assert_nothing_raised { opts = @p.parse %w(-x 3 4 5 -x 6 7) } assert_equal [[3, 4, 5], [6, 7]], opts[:xarg] assert_equal [], @p.leftovers @p.opt :yarg, "desc", :type => :floats, :multi => true assert_nothing_raised { opts = @p.parse %w(-y 3.14 4.21 5.66 -y 6.99 7.01) } assert_equal [[3.14, 4.21, 5.66], [6.99, 7.01]], opts[:yarg] assert_equal [], @p.leftovers @p.opt :zarg, "desc", :type => :strings, :multi => true assert_nothing_raised { opts = @p.parse %w(-z a b c -z d e) } assert_equal [%w(a b c), %w(d e)], opts[:zarg] assert_equal [], @p.leftovers end |
#test_simple_interface_handles_die ⇒ Object
1083 1084 1085 1086 1087 1088 1089 1090 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 1083 def test_simple_interface_handles_die ARGV.clear ARGV.unshift "--potato" opts = ::Trollop:: do opt :potato end assert_raises(SystemExit) { ::Trollop::die :potato, "is invalid" } end |
#test_simple_interface_handles_help ⇒ Object
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 1048 def test_simple_interface_handles_help ARGV.clear ARGV.unshift "-h" assert_raises(SystemExit) do opts = ::Trollop:: do opt :potato end raise "broken" end end |
#test_simple_interface_handles_regular_usage ⇒ Object
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 1071 def test_simple_interface_handles_regular_usage ARGV.clear ARGV.unshift "--potato" opts = nil assert_nothing_raised do opts = ::Trollop:: do opt :potato end end assert opts[:potato] end |
#test_simple_interface_handles_version ⇒ Object
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 1059 def test_simple_interface_handles_version ARGV.clear ARGV.unshift "-v" assert_raises(SystemExit) do opts = ::Trollop:: do version "1.2" opt :potato end raise "broken" end end |
#test_special_flags_work ⇒ Object
270 271 272 273 274 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 270 def test_special_flags_work @p.version "asdf fdas" assert_raise(VersionNeeded) { @p.parse(%w(-v)) } assert_raise(HelpNeeded) { @p.parse(%w(-h)) } end |
#test_stopwords_mixed ⇒ Object
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 792 def test_stopwords_mixed @p.opt "arg1", :default => false @p.opt "arg2", :default => false @p.stop_on %w(happy sad) opts = @p.parse %w(--arg1 happy --arg2) assert_equal true, opts["arg1"] assert_equal false, opts["arg2"] ## restart parsing @p.leftovers.shift opts = @p.parse @p.leftovers assert_equal false, opts["arg1"] assert_equal true, opts["arg2"] end |
#test_stopwords_multiple_stopwords ⇒ Object
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 824 def test_stopwords_multiple_stopwords @p.opt "arg1", :default => false @p.opt "arg2", :default => false @p.stop_on %w(happy sad) opts = @p.parse %w(happy sad --arg1 --arg2) assert_equal false, opts["arg1"] assert_equal false, opts["arg2"] ## restart parsing @p.leftovers.shift opts = @p.parse @p.leftovers assert_equal false, opts["arg1"] assert_equal false, opts["arg2"] ## restart parsing again @p.leftovers.shift opts = @p.parse @p.leftovers assert_equal true, opts["arg1"] assert_equal true, opts["arg2"] end |
#test_stopwords_no_stopwords ⇒ Object
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 808 def test_stopwords_no_stopwords @p.opt "arg1", :default => false @p.opt "arg2", :default => false @p.stop_on %w(happy sad) opts = @p.parse %w(--arg1 --arg2) assert_equal true, opts["arg1"] assert_equal true, opts["arg2"] ## restart parsing @p.leftovers.shift opts = @p.parse @p.leftovers assert_equal false, opts["arg1"] assert_equal false, opts["arg2"] end |
#test_stopwords_with_short_args ⇒ Object
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 846 def test_stopwords_with_short_args @p.opt :global_option, "This is a global option", :short => "-g" @p.stop_on %w(sub-command-1 sub-command-2) global_opts = @p.parse %w(-g sub-command-1 -c) cmd = @p.leftovers.shift @q = Parser.new @q.opt :cmd_option, "This is an option only for the subcommand", :short => "-c" cmd_opts = @q.parse @p.leftovers assert_equal true, global_opts[:global_option] assert_nil global_opts[:cmd_option] assert_equal true, cmd_opts[:cmd_option] assert_nil cmd_opts[:global_option] assert_equal cmd, "sub-command-1" assert_equal @q.leftovers, [] end |
#test_syntax_check ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 25 def test_syntax_check @p.opt "arg" assert_nothing_raised { @p.parse(%w(--arg)) } assert_nothing_raised { @p.parse(%w(arg)) } assert_raise(CommandlineError) { @p.parse(%w(---arg)) } assert_raise(CommandlineError) { @p.parse(%w(-arg)) } end |
#test_two_required_one_missing_accuses_correctly ⇒ Object
courtesy neill zero
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 771 def test_two_required_one_missing_accuses_correctly @p.opt "arg1", "desc1", :required => true @p.opt "arg2", "desc2", :required => true begin @p.parse(%w(--arg1)) flunk "should have failed on a missing req" rescue CommandlineError => e assert e. =~ /arg2/, "didn't mention arg2 in the error msg: #{e.}" end begin @p.parse(%w(--arg2)) flunk "should have failed on a missing req" rescue CommandlineError => e assert e. =~ /arg1/, "didn't mention arg1 in the error msg: #{e.}" end assert_nothing_raised { @p.parse(%w(--arg1 --arg2)) } end |
#test_type_and_default_must_match ⇒ Object
:type and :default must match if both are specified
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 161 def test_type_and_default_must_match assert_raise(ArgumentError) { @p.opt "badarg", "desc", :type => :int, :default => "hello" } assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :type => :String, :default => 4 } assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :type => :String, :default => ["hi"] } assert_raise(ArgumentError) { @p.opt "badarg2", "desc", :type => :ints, :default => [3.14] } assert_nothing_raised { @p.opt "argsi", "desc", :type => :int, :default => 4 } assert_nothing_raised { @p.opt "argsf", "desc", :type => :float, :default => 3.14 } assert_nothing_raised { @p.opt "argsd", "desc", :type => :date, :default => Date.today } assert_nothing_raised { @p.opt "argss", "desc", :type => :string, :default => "yo" } assert_nothing_raised { @p.opt "argmi", "desc", :type => :ints, :default => [4] } assert_nothing_raised { @p.opt "argmf", "desc", :type => :floats, :default => [3.14] } assert_nothing_raised { @p.opt "argmd", "desc", :type => :dates, :default => [Date.today] } assert_nothing_raised { @p.opt "argmst", "desc", :type => :strings, :default => ["yo"] } end |
#test_type_correctly_derived_from_default ⇒ Object
type is correctly derived from :default
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 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 78 def test_type_correctly_derived_from_default assert_raise(ArgumentError) { @p.opt "badarg", "desc", :default => [] } opts = nil # single arg: int assert_nothing_raised { @p.opt "argsi", "desc", :default => 0 } assert_nothing_raised { opts = @p.parse(%w(--)) } assert_equal 0, opts["argsi"] assert_nothing_raised { opts = @p.parse(%w(--argsi 4)) } assert_equal 4, opts["argsi"] assert_raise(CommandlineError) { @p.parse(%w(--argsi 4.2)) } assert_raise(CommandlineError) { @p.parse(%w(--argsi hello)) } # single arg: float assert_nothing_raised { @p.opt "argsf", "desc", :default => 3.14 } assert_nothing_raised { opts = @p.parse(%w(--)) } assert_equal 3.14, opts["argsf"] assert_nothing_raised { opts = @p.parse(%w(--argsf 2.41)) } assert_equal 2.41, opts["argsf"] assert_nothing_raised { opts = @p.parse(%w(--argsf 2)) } assert_equal 2, opts["argsf"] assert_nothing_raised { opts = @p.parse(%w(--argsf 1.0e-2)) } assert_equal 1.0e-2, opts["argsf"] assert_raise(CommandlineError) { @p.parse(%w(--argsf hello)) } # single arg: date date = Date.today assert_nothing_raised { @p.opt "argsd", "desc", :default => date } assert_nothing_raised { opts = @p.parse(%w(--)) } assert_equal Date.today, opts["argsd"] assert_nothing_raised { opts = @p.parse(['--argsd', 'Jan 4, 2007']) } assert_equal Date.civil(2007, 1, 4), opts["argsd"] assert_raise(CommandlineError) { @p.parse(%w(--argsd hello)) } # single arg: string assert_nothing_raised { @p.opt "argss", "desc", :default => "foobar" } assert_nothing_raised { opts = @p.parse(%w(--)) } assert_equal "foobar", opts["argss"] assert_nothing_raised { opts = @p.parse(%w(--argss 2.41)) } assert_equal "2.41", opts["argss"] assert_nothing_raised { opts = @p.parse(%w(--argss hello)) } assert_equal "hello", opts["argss"] # multi args: ints assert_nothing_raised { @p.opt "argmi", "desc", :default => [3, 5] } assert_nothing_raised { opts = @p.parse(%w(--)) } assert_equal [3, 5], opts["argmi"] assert_nothing_raised { opts = @p.parse(%w(--argmi 4)) } assert_equal [4], opts["argmi"] assert_raise(CommandlineError) { @p.parse(%w(--argmi 4.2)) } assert_raise(CommandlineError) { @p.parse(%w(--argmi hello)) } # multi args: floats assert_nothing_raised { @p.opt "argmf", "desc", :default => [3.34, 5.21] } assert_nothing_raised { opts = @p.parse(%w(--)) } assert_equal [3.34, 5.21], opts["argmf"] assert_nothing_raised { opts = @p.parse(%w(--argmf 2)) } assert_equal [2], opts["argmf"] assert_nothing_raised { opts = @p.parse(%w(--argmf 4.0)) } assert_equal [4.0], opts["argmf"] assert_raise(CommandlineError) { @p.parse(%w(--argmf hello)) } # multi args: dates dates = [Date.today, Date.civil(2007, 1, 4)] assert_nothing_raised { @p.opt "argmd", "desc", :default => dates } assert_nothing_raised { opts = @p.parse(%w(--)) } assert_equal dates, opts["argmd"] assert_nothing_raised { opts = @p.parse(['--argmd', 'Jan 4, 2007']) } assert_equal [Date.civil(2007, 1, 4)], opts["argmd"] assert_raise(CommandlineError) { @p.parse(%w(--argmd hello)) } # multi args: strings assert_nothing_raised { @p.opt "argmst", "desc", :default => %w(hello world) } assert_nothing_raised { opts = @p.parse(%w(--)) } assert_equal %w(hello world), opts["argmst"] assert_nothing_raised { opts = @p.parse(%w(--argmst 3.4)) } assert_equal ["3.4"], opts["argmst"] assert_nothing_raised { opts = @p.parse(%w(--argmst goodbye)) } assert_equal ["goodbye"], opts["argmst"] end |
#test_typed_args_refuse_args_of_other_types ⇒ Object
flags that require args of a specific type refuse args of other types
68 69 70 71 72 73 74 75 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 68 def test_typed_args_refuse_args_of_other_types assert_nothing_raised { @p.opt "goodarg", "desc", :type => :int } assert_raise(ArgumentError) { @p.opt "badarg", "desc", :type => :asdf } assert_nothing_raised { @p.parse(%w(--goodarg 3)) } assert_raise(CommandlineError) { @p.parse(%w(--goodarg 4.2)) } assert_raise(CommandlineError) { @p.parse(%w(--goodarg hello)) } end |
#test_unknown_arguments ⇒ Object
18 19 20 21 22 23 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 18 def test_unknown_arguments assert_raise(CommandlineError) { @p.parse(%w(--arg)) } @p.opt "arg" assert_nothing_raised { @p.parse(%w(--arg)) } assert_raise(CommandlineError) { @p.parse(%w(--arg2)) } end |
#test_unknown_subcommand ⇒ Object
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 874 def test_unknown_subcommand @p.opt :global_flag, "Global flag", :short => "-g", :type => :flag @p.opt :global_param, "Global parameter", :short => "-p", :default => 5 @p.stop_on_unknown expected_opts = { :global_flag => true, :help => false, :global_param => 5, :global_flag_given => true } expected_leftovers = [ "my_subcommand", "-c" ] assert_parses_correctly @p, %w(--global-flag my_subcommand -c), \ expected_opts, expected_leftovers assert_parses_correctly @p, %w(-g my_subcommand -c), \ expected_opts, expected_leftovers expected_opts = { :global_flag => false, :help => false, :global_param => 5, :global_param_given => true } expected_leftovers = [ "my_subcommand", "-c" ] assert_parses_correctly @p, %w(-p 5 my_subcommand -c), \ expected_opts, expected_leftovers assert_parses_correctly @p, %w(--global-param 5 my_subcommand -c), \ expected_opts, expected_leftovers end |
#test_version_and_help_long_args_can_be_overridden ⇒ Object
656 657 658 659 660 661 662 663 664 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 656 def test_version_and_help_long_args_can_be_overridden @p.opt :asdf, "desc", :long => "help" @p.opt :asdf2, "desc2", :long => "version" assert_nothing_raised { @p.parse %w() } assert_nothing_raised { @p.parse %w(--help) } assert_nothing_raised { @p.parse %w(--version) } assert_nothing_raised { @p.parse %w(-h) } assert_nothing_raised { @p.parse %w(-v) } end |
#test_version_and_help_override_errors ⇒ Object
666 667 668 669 670 671 672 673 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 666 def test_version_and_help_override_errors @p.opt :asdf, "desc", :type => String @p.version "version" assert_nothing_raised { @p.parse %w(--asdf goat) } assert_raises(CommandlineError) { @p.parse %w(--asdf) } assert_raises(HelpNeeded) { @p.parse %w(--asdf --help) } assert_raises(VersionNeeded) { @p.parse %w(--asdf --version) } end |
#test_version_and_help_short_args_can_be_overridden ⇒ Object
645 646 647 648 649 650 651 652 653 654 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 645 def test_version_and_help_short_args_can_be_overridden @p.opt :verbose, "desc", :short => "-v" @p.opt :hello, "desc", :short => "-h" @p.version "version" assert_nothing_raised { @p.parse(%w(-v)) } assert_raises(VersionNeeded) { @p.parse(%w(--version)) } assert_nothing_raised { @p.parse(%w(-h)) } assert_raises(HelpNeeded) { @p.parse(%w(--help)) } end |
#test_version_only_appears_if_set ⇒ Object
301 302 303 304 305 306 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 301 def test_version_only_appears_if_set @p.opt "arg" assert_raise(CommandlineError) { @p.parse %w(-v) } @p.version "trollop 1.2.3.4" assert_raise(VersionNeeded) { @p.parse %w(-v) } end |
#test_wrap ⇒ Object
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/vendor/mainline/test/test_trollop.rb', line 326 def test_wrap assert_equal [""], @p.wrap("") assert_equal ["a"], @p.wrap("a") assert_equal ["one two", "three"], @p.wrap("one two three", :width => 8) assert_equal ["one two three"], @p.wrap("one two three", :width => 80) assert_equal ["one", "two", "three"], @p.wrap("one two three", :width => 3) assert_equal ["onetwothree"], @p.wrap("onetwothree", :width => 3) assert_equal [ "Test is an awesome program that does something very, very important.", "", "Usage:", " test [options] <filenames>+", "where [options] are:"], @p.wrap(<<EOM, :width => 100) Test is an awesome program that does something very, very important. Usage: test [options] <filenames>+ where [options] are: EOM end |