summaryrefslogtreecommitdiff
path: root/spec/ruby/library/getoptlong
diff options
context:
space:
mode:
authoreregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
committereregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-09-20 20:18:52 +0000
commit1d15d5f08032acf1b7bceacbb450d617ff6e0931 (patch)
treea3785a79899302bc149e4a6e72f624ac27dc1f10 /spec/ruby/library/getoptlong
parent75bfc6440d595bf339007f4fb280fd4d743e89c1 (diff)
Move spec/rubyspec to spec/ruby for consistency
* Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/ruby/library/getoptlong')
-rw-r--r--spec/ruby/library/getoptlong/each_option_spec.rb7
-rw-r--r--spec/ruby/library/getoptlong/each_spec.rb7
-rw-r--r--spec/ruby/library/getoptlong/error_message_spec.rb23
-rw-r--r--spec/ruby/library/getoptlong/get_option_spec.rb7
-rw-r--r--spec/ruby/library/getoptlong/get_spec.rb7
-rw-r--r--spec/ruby/library/getoptlong/initialize_spec.rb28
-rw-r--r--spec/ruby/library/getoptlong/ordering_spec.rb38
-rw-r--r--spec/ruby/library/getoptlong/set_options_spec.rb98
-rw-r--r--spec/ruby/library/getoptlong/shared/each.rb18
-rw-r--r--spec/ruby/library/getoptlong/shared/get.rb64
-rw-r--r--spec/ruby/library/getoptlong/terminate_spec.rb30
-rw-r--r--spec/ruby/library/getoptlong/terminated_spec.rb17
12 files changed, 344 insertions, 0 deletions
diff --git a/spec/ruby/library/getoptlong/each_option_spec.rb b/spec/ruby/library/getoptlong/each_option_spec.rb
new file mode 100644
index 0000000000..c58815bfa9
--- /dev/null
+++ b/spec/ruby/library/getoptlong/each_option_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+require File.expand_path('../shared/each', __FILE__)
+
+describe "GetoptLong#each_option" do
+ it_behaves_like(:getoptlong_each, :each_option)
+end
diff --git a/spec/ruby/library/getoptlong/each_spec.rb b/spec/ruby/library/getoptlong/each_spec.rb
new file mode 100644
index 0000000000..d09f84a6db
--- /dev/null
+++ b/spec/ruby/library/getoptlong/each_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+require File.expand_path('../shared/each', __FILE__)
+
+describe "GetoptLong#each" do
+ it_behaves_like(:getoptlong_each, :each)
+end
diff --git a/spec/ruby/library/getoptlong/error_message_spec.rb b/spec/ruby/library/getoptlong/error_message_spec.rb
new file mode 100644
index 0000000000..3f44f538c6
--- /dev/null
+++ b/spec/ruby/library/getoptlong/error_message_spec.rb
@@ -0,0 +1,23 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+
+describe "GetoptLong#error_message" do
+ it "returns nil if no error occurred" do
+ opts = GetoptLong.new
+ opts.error_message.should == nil
+ end
+
+ it "returns the error message of the last error that occurred" do
+ argv [] do
+ opts = GetoptLong.new
+ opts.quiet = true
+ opts.get
+ -> {
+ opts.ordering = GetoptLong::PERMUTE
+ }.should raise_error(ArgumentError) { |e|
+ e.message.should == "argument error"
+ opts.error_message.should == "argument error"
+ }
+ end
+ end
+end
diff --git a/spec/ruby/library/getoptlong/get_option_spec.rb b/spec/ruby/library/getoptlong/get_option_spec.rb
new file mode 100644
index 0000000000..c56903e68e
--- /dev/null
+++ b/spec/ruby/library/getoptlong/get_option_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+require File.expand_path('../shared/get', __FILE__)
+
+describe "GetoptLong#get_option" do
+ it_behaves_like(:getoptlong_get, :get_option)
+end
diff --git a/spec/ruby/library/getoptlong/get_spec.rb b/spec/ruby/library/getoptlong/get_spec.rb
new file mode 100644
index 0000000000..ba1a1be6ad
--- /dev/null
+++ b/spec/ruby/library/getoptlong/get_spec.rb
@@ -0,0 +1,7 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+require File.expand_path('../shared/get', __FILE__)
+
+describe "GetoptLong#get" do
+ it_behaves_like(:getoptlong_get, :get)
+end
diff --git a/spec/ruby/library/getoptlong/initialize_spec.rb b/spec/ruby/library/getoptlong/initialize_spec.rb
new file mode 100644
index 0000000000..6ac46b8b5d
--- /dev/null
+++ b/spec/ruby/library/getoptlong/initialize_spec.rb
@@ -0,0 +1,28 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+
+describe "GetoptLong#initialize" do
+ it "sets ordering to REQUIRE_ORDER if ENV['POSIXLY_CORRECT'] is set" do
+ begin
+ old_env_value = ENV["POSIXLY_CORRECT"]
+ ENV["POSIXLY_CORRECT"] = ""
+
+ opt = GetoptLong.new
+ opt.ordering.should == GetoptLong::REQUIRE_ORDER
+ ensure
+ ENV["POSIXLY_CORRECT"] = old_env_value
+ end
+ end
+
+ it "sets ordering to PERMUTE if ENV['POSIXLY_CORRECT'] is not set" do
+ begin
+ old_env_value = ENV["POSIXLY_CORRECT"]
+ ENV["POSIXLY_CORRECT"] = nil
+
+ opt = GetoptLong.new
+ opt.ordering.should == GetoptLong::PERMUTE
+ ensure
+ ENV["POSIXLY_CORRECT"] = old_env_value
+ end
+ end
+end
diff --git a/spec/ruby/library/getoptlong/ordering_spec.rb b/spec/ruby/library/getoptlong/ordering_spec.rb
new file mode 100644
index 0000000000..e445de2255
--- /dev/null
+++ b/spec/ruby/library/getoptlong/ordering_spec.rb
@@ -0,0 +1,38 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+
+describe "GetoptLong#ordering=" do
+ it "raises an ArgumentError if called after processing has started" do
+ argv [ "--size", "10k", "--verbose" ] do
+ opts = GetoptLong.new([ '--size', GetoptLong::REQUIRED_ARGUMENT ],
+ [ '--verbose', GetoptLong::NO_ARGUMENT ])
+ opts.quiet = true
+ opts.get
+
+ lambda {
+ opts.ordering = GetoptLong::PERMUTE
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "raises an ArgumentError if given an invalid value" do
+ opts = GetoptLong.new
+
+ lambda {
+ opts.ordering = 12345
+ }.should raise_error(ArgumentError)
+ end
+
+ it "does not allow changing ordering to PERMUTE if ENV['POSIXLY_CORRECT'] is set" do
+ begin
+ old_env_value = ENV['POSIXLY_CORRECT']
+ ENV['POSIXLY_CORRECT'] = ""
+
+ opts = GetoptLong.new
+ opts.ordering = GetoptLong::PERMUTE
+ opts.ordering.should == GetoptLong::REQUIRE_ORDER
+ ensure
+ ENV['POSIXLY_CORRECT'] = old_env_value
+ end
+ end
+end
diff --git a/spec/ruby/library/getoptlong/set_options_spec.rb b/spec/ruby/library/getoptlong/set_options_spec.rb
new file mode 100644
index 0000000000..39d6991bf5
--- /dev/null
+++ b/spec/ruby/library/getoptlong/set_options_spec.rb
@@ -0,0 +1,98 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+
+describe "GetoptLong#set_options" do
+ before :each do
+ @opts = GetoptLong.new
+ end
+
+ it "allows setting command line options" do
+ argv ["--size", "10k", "-v", "arg1", "arg2"] do
+ @opts.set_options(
+ ["--size", GetoptLong::REQUIRED_ARGUMENT],
+ ["--verbose", "-v", GetoptLong::NO_ARGUMENT]
+ )
+
+ @opts.get.should == ["--size", "10k"]
+ @opts.get.should == ["--verbose", ""]
+ @opts.get.should == nil
+ end
+ end
+
+ it "discards previously defined command line options" do
+ argv ["--size", "10k", "-v", "arg1", "arg2"] do
+ @opts.set_options(
+ ["--size", GetoptLong::REQUIRED_ARGUMENT],
+ ["--verbose", "-v", GetoptLong::NO_ARGUMENT]
+ )
+
+ @opts.set_options(
+ ["-s", "--size", GetoptLong::REQUIRED_ARGUMENT],
+ ["-v", GetoptLong::NO_ARGUMENT]
+ )
+
+ @opts.get.should == ["-s", "10k"]
+ @opts.get.should == ["-v", ""]
+ @opts.get.should == nil
+ end
+ end
+
+ it "raises an ArgumentError if too many argument flags where given" do
+ argv [] do
+ lambda {
+ @opts.set_options(["--size", GetoptLong::NO_ARGUMENT, GetoptLong::REQUIRED_ARGUMENT])
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "raises a RuntimeError if processing has already started" do
+ argv [] do
+ @opts.get
+ lambda {
+ @opts.set_options()
+ }.should raise_error(RuntimeError)
+ end
+ end
+
+ it "raises an ArgumentError if no argument flag was given" do
+ argv [] do
+ lambda {
+ @opts.set_options(["--size"])
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "raises an ArgumentError if one of the given arguments is not an Array" do
+ argv [] do
+ lambda {
+ @opts.set_options(
+ ["--size", GetoptLong::REQUIRED_ARGUMENT],
+ "test")
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "raises an ArgumentError if the same option is given twice" do
+ argv [] do
+ lambda {
+ @opts.set_options(
+ ["--size", GetoptLong::NO_ARGUMENT],
+ ["--size", GetoptLong::OPTIONAL_ARGUMENT])
+ }.should raise_error(ArgumentError)
+
+ lambda {
+ @opts.set_options(
+ ["--size", GetoptLong::NO_ARGUMENT],
+ ["-s", "--size", GetoptLong::OPTIONAL_ARGUMENT])
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ it "raises an ArgumentError if the given option is invalid" do
+ argv [] do
+ lambda {
+ @opts.set_options(["-size", GetoptLong::NO_ARGUMENT])
+ }.should raise_error(ArgumentError)
+ end
+ end
+end
diff --git a/spec/ruby/library/getoptlong/shared/each.rb b/spec/ruby/library/getoptlong/shared/each.rb
new file mode 100644
index 0000000000..b534e24c0f
--- /dev/null
+++ b/spec/ruby/library/getoptlong/shared/each.rb
@@ -0,0 +1,18 @@
+describe :getoptlong_each, shared: true do
+ before :each do
+ @opts = GetoptLong.new(
+ [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ],
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
+ [ '--query', '-q', GetoptLong::NO_ARGUMENT ],
+ [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ]
+ )
+ end
+
+ it "passes each argument/value pair to the block" do
+ argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do
+ pairs = []
+ @opts.send(@method) { |arg, val| pairs << [ arg, val ] }
+ pairs.should == [ [ "--size", "10k" ], [ "--verbose", "" ], [ "--query", ""] ]
+ end
+ end
+end
diff --git a/spec/ruby/library/getoptlong/shared/get.rb b/spec/ruby/library/getoptlong/shared/get.rb
new file mode 100644
index 0000000000..91a0fbaacc
--- /dev/null
+++ b/spec/ruby/library/getoptlong/shared/get.rb
@@ -0,0 +1,64 @@
+describe :getoptlong_get, shared: true do
+ before :each do
+ @opts = GetoptLong.new(
+ [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ],
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
+ [ '--query', '-q', GetoptLong::NO_ARGUMENT ],
+ [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ]
+ )
+ @opts.quiet = true # silence using $deferr
+ end
+
+ it "returns the next option name and its argument as an Array" do
+ argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do
+ @opts.send(@method).should == [ "--size", "10k" ]
+ @opts.send(@method).should == [ "--verbose", "" ]
+ @opts.send(@method).should == [ "--query", ""]
+ @opts.send(@method).should == nil
+ end
+ end
+
+ it "shifts ARGV on each call" do
+ argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do
+ @opts.send(@method)
+ ARGV.should == [ "-v", "-q", "a.txt", "b.txt" ]
+
+ @opts.send(@method)
+ ARGV.should == [ "-q", "a.txt", "b.txt" ]
+
+ @opts.send(@method)
+ ARGV.should == [ "a.txt", "b.txt" ]
+
+ @opts.send(@method)
+ ARGV.should == [ "a.txt", "b.txt" ]
+ end
+ end
+
+ it "terminates processing when encountering '--'" do
+ argv [ "--size", "10k", "--", "-v", "-q", "a.txt", "b.txt" ] do
+ @opts.send(@method)
+ ARGV.should == ["--", "-v", "-q", "a.txt", "b.txt"]
+
+ @opts.send(@method)
+ ARGV.should == ["-v", "-q", "a.txt", "b.txt"]
+
+ @opts.send(@method)
+ ARGV.should == ["-v", "-q", "a.txt", "b.txt"]
+ end
+ end
+
+ it "raises a if an argument was required, but none given" do
+ argv [ "--size" ] do
+ lambda { @opts.send(@method) }.should raise_error(GetoptLong::MissingArgument)
+ end
+ end
+
+ ruby_version_is "2.5" do
+ # https://bugs.ruby-lang.org/issues/13858
+ it "returns multiline argument" do
+ argv [ "--size=\n10k\n" ] do
+ @opts.send(@method).should == [ "--size", "\n10k\n" ]
+ end
+ end
+ end
+end
diff --git a/spec/ruby/library/getoptlong/terminate_spec.rb b/spec/ruby/library/getoptlong/terminate_spec.rb
new file mode 100644
index 0000000000..ad9f9a1623
--- /dev/null
+++ b/spec/ruby/library/getoptlong/terminate_spec.rb
@@ -0,0 +1,30 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+
+describe "GetoptLong#terminate" do
+ before :each do
+ @opts = GetoptLong.new(
+ [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ],
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
+ [ '--query', '-q', GetoptLong::NO_ARGUMENT ],
+ [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ]
+ )
+ end
+
+ it "terminates option proccessing" do
+ argv [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ] do
+ @opts.get.should == [ "--size", "10k" ]
+ @opts.terminate
+ @opts.get.should == nil
+ end
+ end
+
+ it "returns self when option processsing is terminated" do
+ @opts.terminate.should == @opts
+ end
+
+ it "returns nil when option processing was already terminated" do
+ @opts.terminate
+ @opts.terminate.should == nil
+ end
+end
diff --git a/spec/ruby/library/getoptlong/terminated_spec.rb b/spec/ruby/library/getoptlong/terminated_spec.rb
new file mode 100644
index 0000000000..feaf2bc09e
--- /dev/null
+++ b/spec/ruby/library/getoptlong/terminated_spec.rb
@@ -0,0 +1,17 @@
+require File.expand_path('../../../spec_helper', __FILE__)
+require 'getoptlong'
+
+describe "GetoptLong#terminated?" do
+ it "returns true if option processing has terminated" do
+ argv [ "--size", "10k" ] do
+ opts = GetoptLong.new(["--size", GetoptLong::REQUIRED_ARGUMENT])
+ opts.terminated?.should == false
+
+ opts.get.should == ["--size", "10k"]
+ opts.terminated?.should == false
+
+ opts.get.should == nil
+ opts.terminated?.should == true
+ end
+ end
+end