summaryrefslogtreecommitdiff
path: root/spec/ruby/shared
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/shared')
-rw-r--r--spec/ruby/shared/enumerator/enum_for.rb57
-rw-r--r--spec/ruby/shared/enumerator/with_index.rb33
-rw-r--r--spec/ruby/shared/enumerator/with_object.rb42
-rw-r--r--spec/ruby/shared/fiber/resume.rb58
-rw-r--r--spec/ruby/shared/file/size.rb2
-rw-r--r--spec/ruby/shared/file/socket.rb32
-rw-r--r--spec/ruby/shared/file/sticky.rb2
-rw-r--r--spec/ruby/shared/io/putc.rb2
-rw-r--r--spec/ruby/shared/kernel/at_exit.rb11
-rw-r--r--spec/ruby/shared/kernel/object_id.rb28
-rw-r--r--spec/ruby/shared/kernel/raise.rb351
-rw-r--r--spec/ruby/shared/math/atanh.rb44
-rw-r--r--spec/ruby/shared/process/fork.rb10
-rw-r--r--spec/ruby/shared/queue/deque.rb122
-rw-r--r--spec/ruby/shared/queue/freeze.rb18
-rw-r--r--spec/ruby/shared/rational/Rational.rb150
-rw-r--r--spec/ruby/shared/rational/abs.rb11
-rw-r--r--spec/ruby/shared/rational/arithmetic_exception_in_coerce.rb11
-rw-r--r--spec/ruby/shared/rational/ceil.rb45
-rw-r--r--spec/ruby/shared/rational/coerce.rb34
-rw-r--r--spec/ruby/shared/rational/comparison.rb95
-rw-r--r--spec/ruby/shared/rational/denominator.rb14
-rw-r--r--spec/ruby/shared/rational/div.rb54
-rw-r--r--spec/ruby/shared/rational/divide.rb71
-rw-r--r--spec/ruby/shared/rational/divmod.rb42
-rw-r--r--spec/ruby/shared/rational/equal_value.rb39
-rw-r--r--spec/ruby/shared/rational/exponent.rb196
-rw-r--r--spec/ruby/shared/rational/fdiv.rb5
-rw-r--r--spec/ruby/shared/rational/floor.rb45
-rw-r--r--spec/ruby/shared/rational/hash.rb9
-rw-r--r--spec/ruby/shared/rational/inspect.rb14
-rw-r--r--spec/ruby/shared/rational/modulo.rb43
-rw-r--r--spec/ruby/shared/rational/multiply.rb62
-rw-r--r--spec/ruby/shared/rational/numerator.rb10
-rw-r--r--spec/ruby/shared/rational/plus.rb48
-rw-r--r--spec/ruby/shared/rational/remainder.rb5
-rw-r--r--spec/ruby/shared/rational/round.rb106
-rw-r--r--spec/ruby/shared/rational/to_f.rb16
-rw-r--r--spec/ruby/shared/rational/to_i.rb12
-rw-r--r--spec/ruby/shared/rational/to_r.rb11
-rw-r--r--spec/ruby/shared/rational/to_s.rb14
-rw-r--r--spec/ruby/shared/rational/truncate.rb71
-rw-r--r--spec/ruby/shared/sizedqueue/enque.rb156
-rw-r--r--spec/ruby/shared/string/end_with.rb4
-rw-r--r--spec/ruby/shared/string/times.rb6
-rw-r--r--spec/ruby/shared/time/yday.rb18
46 files changed, 547 insertions, 1682 deletions
diff --git a/spec/ruby/shared/enumerator/enum_for.rb b/spec/ruby/shared/enumerator/enum_for.rb
deleted file mode 100644
index a67a76c461..0000000000
--- a/spec/ruby/shared/enumerator/enum_for.rb
+++ /dev/null
@@ -1,57 +0,0 @@
-describe :enum_for, shared: true do
- it "is defined in Kernel" do
- Kernel.method_defined?(@method).should be_true
- end
-
- it "returns a new enumerator" do
- "abc".send(@method).should be_an_instance_of(Enumerator)
- end
-
- it "defaults the first argument to :each" do
- enum = [1,2].send(@method)
- enum.map { |v| v }.should == [1,2].each { |v| v }
- end
-
- it "sets regexp matches in the caller" do
- "wawa".send(@method, :scan, /./).map {|o| $& }.should == ["w", "a", "w", "a"]
- a = []
- "wawa".send(@method, :scan, /./).each {|o| a << $& }
- a.should == ["w", "a", "w", "a"]
- end
-
- it "exposes multi-arg yields as an array" do
- o = Object.new
- def o.each
- yield :a
- yield :b1, :b2
- yield [:c]
- yield :d1, :d2
- yield :e1, :e2, :e3
- end
-
- enum = o.send(@method)
- enum.next.should == :a
- enum.next.should == [:b1, :b2]
- enum.next.should == [:c]
- enum.next.should == [:d1, :d2]
- enum.next.should == [:e1, :e2, :e3]
- end
-
- it "uses the passed block's value to calculate the size of the enumerator" do
- Object.new.enum_for { 100 }.size.should == 100
- end
-
- it "defers the evaluation of the passed block until #size is called" do
- ScratchPad.record []
-
- enum = Object.new.enum_for do
- ScratchPad << :called
- 100
- end
-
- ScratchPad.recorded.should be_empty
-
- enum.size
- ScratchPad.recorded.should == [:called]
- end
-end
diff --git a/spec/ruby/shared/enumerator/with_index.rb b/spec/ruby/shared/enumerator/with_index.rb
deleted file mode 100644
index 89f40070e0..0000000000
--- a/spec/ruby/shared/enumerator/with_index.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :enum_with_index, shared: true do
-
- require_relative '../../fixtures/enumerator/classes'
-
- before :each do
- @origin = [1, 2, 3, 4]
- @enum = @origin.to_enum
- end
-
- it "passes each element and its index to block" do
- a = []
- @enum.send(@method) { |o, i| a << [o, i] }
- a.should == [[1, 0], [2, 1], [3, 2], [4, 3]]
- end
-
- it "returns the object being enumerated when given a block" do
- @enum.send(@method) { |o, i| :glark }.should equal(@origin)
- end
-
- it "binds splat arguments properly" do
- acc = []
- @enum.send(@method) { |*b| c,d = b; acc << c; acc << d }
- [1, 0, 2, 1, 3, 2, 4, 3].should == acc
- end
-
- it "returns an enumerator if no block is supplied" do
- ewi = @enum.send(@method)
- ewi.should be_an_instance_of(Enumerator)
- ewi.to_a.should == [[1, 0], [2, 1], [3, 2], [4, 3]]
- end
-end
diff --git a/spec/ruby/shared/enumerator/with_object.rb b/spec/ruby/shared/enumerator/with_object.rb
deleted file mode 100644
index c2e3a79366..0000000000
--- a/spec/ruby/shared/enumerator/with_object.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :enum_with_object, shared: true do
- before :each do
- @enum = [:a, :b].to_enum
- @memo = ''
- @block_params = @enum.send(@method, @memo).to_a
- end
-
- it "receives an argument" do
- @enum.method(@method).arity.should == 1
- end
-
- context "with block" do
- it "returns the given object" do
- ret = @enum.send(@method, @memo) do |elm, memo|
- # nothing
- end
- ret.should equal(@memo)
- end
-
- context "the block parameter" do
- it "passes each element to first parameter" do
- @block_params[0][0].should equal(:a)
- @block_params[1][0].should equal(:b)
- end
-
- it "passes the given object to last parameter" do
- @block_params[0][1].should equal(@memo)
- @block_params[1][1].should equal(@memo)
- end
- end
- end
-
- context "without block" do
- it "returns new Enumerator" do
- ret = @enum.send(@method, @memo)
- ret.should be_an_instance_of(Enumerator)
- ret.should_not equal(@enum)
- end
- end
-end
diff --git a/spec/ruby/shared/fiber/resume.rb b/spec/ruby/shared/fiber/resume.rb
deleted file mode 100644
index f3477804ad..0000000000
--- a/spec/ruby/shared/fiber/resume.rb
+++ /dev/null
@@ -1,58 +0,0 @@
-describe :fiber_resume, shared: true do
- it "can be invoked from the root Fiber" do
- fiber = Fiber.new { :fiber }
- fiber.send(@method).should == :fiber
- end
-
- it "raises a FiberError if invoked from a different Thread" do
- fiber = Fiber.new { 42 }
- Thread.new do
- -> {
- fiber.send(@method)
- }.should raise_error(FiberError)
- end.join
-
- # Check the Fiber can still be used
- fiber.send(@method).should == 42
- end
-
- it "passes control to the beginning of the block on first invocation" do
- invoked = false
- fiber = Fiber.new { invoked = true }
- fiber.send(@method)
- invoked.should be_true
- end
-
- it "returns the last value encountered on first invocation" do
- fiber = Fiber.new { 1+1; true }
- fiber.send(@method).should be_true
- end
-
- it "runs until the end of the block" do
- obj = mock('obj')
- obj.should_receive(:do).once
- fiber = Fiber.new { 1 + 2; a = "glark"; obj.do }
- fiber.send(@method)
- end
-
- it "accepts any number of arguments" do
- fiber = Fiber.new { |a| }
- -> { fiber.send(@method, *(1..10).to_a) }.should_not raise_error
- end
-
- it "raises a FiberError if the Fiber is dead" do
- fiber = Fiber.new { true }
- fiber.send(@method)
- -> { fiber.send(@method) }.should raise_error(FiberError)
- end
-
- it "raises a LocalJumpError if the block includes a return statement" do
- fiber = Fiber.new { return; }
- -> { fiber.send(@method) }.should raise_error(LocalJumpError)
- end
-
- it "raises a LocalJumpError if the block includes a break statement" do
- fiber = Fiber.new { break; }
- -> { fiber.send(@method) }.should raise_error(LocalJumpError)
- end
-end
diff --git a/spec/ruby/shared/file/size.rb b/spec/ruby/shared/file/size.rb
index 880dfbb612..cba99fe6a5 100644
--- a/spec/ruby/shared/file/size.rb
+++ b/spec/ruby/shared/file/size.rb
@@ -72,7 +72,7 @@ describe :file_size_nil_when_missing, shared: true do
end
it "returns nil if file_name doesn't exist or has 0 size" do
- @object.send(@method, @missing).should == nil
+ @object.send(@method, @missing).should == nil
end
end
diff --git a/spec/ruby/shared/file/socket.rb b/spec/ruby/shared/file/socket.rb
index 55a1cfd284..ef6c482d1c 100644
--- a/spec/ruby/shared/file/socket.rb
+++ b/spec/ruby/shared/file/socket.rb
@@ -1,3 +1,33 @@
describe :file_socket, shared: true do
- it "accepts an object that has a #to_path method"
+ it "returns false if the file is not a socket" do
+ filename = tmp("i_exist")
+ touch(filename)
+
+ @object.send(@method, filename).should == false
+
+ rm_r filename
+ end
+
+ it "returns true if the file is a socket" do
+ require 'socket'
+
+ # We need a really short name here.
+ # On Linux the path length is limited to 107, see unix(7).
+ name = tmp("s")
+ server = UNIXServer.new(name)
+
+ @object.send(@method, name).should == true
+
+ server.close
+ rm_r name
+ end
+
+ it "accepts an object that has a #to_path method" do
+ obj = Object.new
+ def obj.to_path
+ __FILE__
+ end
+
+ @object.send(@method, obj).should == false
+ end
end
diff --git a/spec/ruby/shared/file/sticky.rb b/spec/ruby/shared/file/sticky.rb
index 38bb6ed26b..e07fa22fd7 100644
--- a/spec/ruby/shared/file/sticky.rb
+++ b/spec/ruby/shared/file/sticky.rb
@@ -8,7 +8,7 @@ describe :file_sticky, shared: true do
Dir.rmdir(@dir) if File.exist?(@dir)
end
- platform_is_not :windows, :darwin, :freebsd, :netbsd, :openbsd, :solaris, :aix do
+ platform_is_not :windows, :darwin, :freebsd, :netbsd, :openbsd, :aix do
it "returns true if the named file has the sticky bit, otherwise false" do
Dir.mkdir @dir, 01755
diff --git a/spec/ruby/shared/io/putc.rb b/spec/ruby/shared/io/putc.rb
index e6012c0098..cdf18ac9fd 100644
--- a/spec/ruby/shared/io/putc.rb
+++ b/spec/ruby/shared/io/putc.rb
@@ -1,4 +1,4 @@
-# -*- encoding: binary -*-
+# encoding: binary
describe :io_putc, shared: true do
after :each do
@io.close if @io && !@io.closed?
diff --git a/spec/ruby/shared/kernel/at_exit.rb b/spec/ruby/shared/kernel/at_exit.rb
index 26ad361a5b..29db79bb39 100644
--- a/spec/ruby/shared/kernel/at_exit.rb
+++ b/spec/ruby/shared/kernel/at_exit.rb
@@ -30,6 +30,12 @@ describe :kernel_at_exit, shared: true do
result.lines.should.include?("The exception matches: true (message=foo)\n")
end
+ it "gives access to an exception raised in a previous handler" do
+ code = "#{@method} { print '$!.message = ' + $!.message }; #{@method} { raise 'foo' }"
+ result = ruby_exe(code, args: "2>&1", exit_status: 1)
+ result.lines.should.include?("$!.message = foo")
+ end
+
it "both exceptions in a handler and in the main script are printed" do
code = "#{@method} { raise 'at_exit_error' }; raise 'main_script_error'"
result = ruby_exe(code, args: "2>&1", exit_status: 1)
@@ -54,7 +60,10 @@ describe :kernel_at_exit, shared: true do
result = ruby_exe('{', options: "-r#{script}", args: "2>&1", exit_status: 1)
$?.should_not.success?
result.should.include?("handler ran\n")
- result.should.include?("syntax error")
+
+ # it's tempting not to rely on error message and rely only on exception class name,
+ # but CRuby before 3.2 doesn't print class name for syntax error
+ result.should include_any_of("syntax error", "SyntaxError")
end
it "calls the nested handler right after the outer one if a handler is nested into another handler" do
diff --git a/spec/ruby/shared/kernel/object_id.rb b/spec/ruby/shared/kernel/object_id.rb
index 7acdb27554..099df8ff94 100644
--- a/spec/ruby/shared/kernel/object_id.rb
+++ b/spec/ruby/shared/kernel/object_id.rb
@@ -52,10 +52,30 @@ describe :object_id, shared: true do
o1.send(@method).should_not == o2.send(@method)
end
- it "returns a different value for two String literals" do
- o1 = "hello"
- o2 = "hello"
- o1.send(@method).should_not == o2.send(@method)
+ guard -> { "test".frozen? && "test".equal?("test") } do # --enable-frozen-string-literal in $RUBYOPT
+ it "returns the same value for two identical String literals" do
+ o1 = "hello"
+ o2 = "hello"
+ o1.send(@method).should == o2.send(@method)
+ end
+ end
+
+ guard -> { "test".frozen? && !"test".equal?("test") } do # chilled string literals
+ it "returns a different frozen value for two String literals" do
+ o1 = "hello"
+ o2 = "hello"
+ o1.send(@method).should_not == o2.send(@method)
+ o1.frozen?.should == true
+ o2.frozen?.should == true
+ end
+ end
+
+ guard -> { !"test".frozen? } do
+ it "returns a different value for two String literals" do
+ o1 = "hello"
+ o2 = "hello"
+ o1.send(@method).should_not == o2.send(@method)
+ end
end
it "returns a different value for an object and its dup" do
diff --git a/spec/ruby/shared/kernel/raise.rb b/spec/ruby/shared/kernel/raise.rb
index 82fb0333c8..2be06ea797 100644
--- a/spec/ruby/shared/kernel/raise.rb
+++ b/spec/ruby/shared/kernel/raise.rb
@@ -49,21 +49,6 @@ describe :kernel_raise, shared: true do
end
end
- it "does not allow message and extra keyword arguments" do
- data_error = Class.new(StandardError) do
- attr_reader :data
- def initialize(data)
- @data = data
- end
- end
-
- -> { @object.raise(data_error, {a: 1}, b: 2) }.should raise_error(StandardError) do |e|
- [TypeError, ArgumentError].should.include?(e.class)
- end
-
- -> { @object.raise(data_error, {a: 1}, [], b: 2) }.should raise_error(ArgumentError)
- end
-
it "raises RuntimeError if no exception class is given" do
-> { @object.raise }.should raise_error(RuntimeError, "")
end
@@ -74,7 +59,7 @@ describe :kernel_raise, shared: true do
end
it "raises a RuntimeError if string given" do
- -> { @object.raise("a bad thing") }.should raise_error(RuntimeError)
+ -> { @object.raise("a bad thing") }.should raise_error(RuntimeError, "a bad thing")
end
it "passes no arguments to the constructor when given only an exception class" do
@@ -86,64 +71,328 @@ describe :kernel_raise, shared: true do
end
it "raises a TypeError when passed a non-Exception object" do
- -> { @object.raise(Object.new) }.should raise_error(TypeError)
+ -> { @object.raise(Object.new) }.should raise_error(TypeError, "exception class/object expected")
+ -> { @object.raise(Object.new, "message") }.should raise_error(TypeError, "exception class/object expected")
+ -> { @object.raise(Object.new, "message", []) }.should raise_error(TypeError, "exception class/object expected")
end
it "raises a TypeError when passed true" do
- -> { @object.raise(true) }.should raise_error(TypeError)
+ -> { @object.raise(true) }.should raise_error(TypeError, "exception class/object expected")
end
it "raises a TypeError when passed false" do
- -> { @object.raise(false) }.should raise_error(TypeError)
+ -> { @object.raise(false) }.should raise_error(TypeError, "exception class/object expected")
end
it "raises a TypeError when passed nil" do
- -> { @object.raise(nil) }.should raise_error(TypeError)
+ -> { @object.raise(nil) }.should raise_error(TypeError, "exception class/object expected")
+ end
+
+ it "raises a TypeError when passed a message and an extra argument" do
+ -> { @object.raise("message", {cause: RuntimeError.new()}) }.should raise_error(TypeError, "exception class/object expected")
+ end
+
+ it "raises TypeError when passed a non-Exception object but it responds to #exception method that doesn't return an instance of Exception class" do
+ e = Object.new
+ def e.exception
+ Array
+ end
+
+ -> {
+ @object.raise e
+ }.should raise_error(TypeError, "exception object expected")
end
it "re-raises a previously rescued exception without overwriting the backtrace" do
- # This spec is written using #backtrace and matching the line number
- # from the string, as backtrace_locations is a more advanced
- # method that is not always supported by implementations.
- #
- initial_raise_line = nil
- raise_again_line = nil
- raised_again = nil
-
- if defined?(FiberSpecs::NewFiberToRaise) and @object == FiberSpecs::NewFiberToRaise
- fiber = Fiber.new do
+ exception = nil
+
+ begin
+ raise "raised"
+ rescue => exception
+ # Ignore.
+ end
+
+ backtrace = exception.backtrace
+
+ begin
+ raised_exception = @object.raise(exception)
+ rescue => raised_exception
+ # Ignore.
+ end
+
+ raised_exception.backtrace.should == backtrace
+ raised_exception.should == exception
+ end
+
+ it "allows Exception, message, and backtrace parameters" do
+ -> do
+ @object.raise(ArgumentError, "message", caller)
+ end.should raise_error(ArgumentError, "message")
+ end
+
+ ruby_version_is "3.4" do
+ locations = caller_locations(1, 2)
+ it "allows Exception, message, and backtrace_locations parameters" do
+ -> do
+ @object.raise(ArgumentError, "message", locations)
+ end.should raise_error(ArgumentError, "message") { |error|
+ error.backtrace_locations.map(&:to_s).should == locations.map(&:to_s)
+ }
+ end
+ end
+
+ ruby_version_is "4.0" do
+ it "allows cause keyword argument" do
+ cause = StandardError.new("original error")
+ result = nil
+
+ -> do
+ @object.raise("new error", cause: cause)
+ end.should raise_error(RuntimeError, "new error") do |error|
+ error.cause.should == cause
+ end
+ end
+
+ it "raises an ArgumentError when only cause is given" do
+ cause = StandardError.new("cause")
+ -> do
+ @object.raise(cause: cause)
+ end.should raise_error(ArgumentError, "only cause is given with no arguments")
+ end
+
+ it "raises an ArgumentError when only cause is given and is nil" do
+ -> do
+ @object.raise(cause: nil)
+ end.should raise_error(ArgumentError, "only cause is given with no arguments")
+ end
+
+ it "raises a TypeError when given cause is not an instance of Exception" do
+ cause = Object.new
+ -> do
+ @object.raise("message", cause: cause)
+ end.should raise_error(TypeError, "exception object expected")
+ end
+
+ it "doesn't set given cause when it equals the raised exception" do
+ cause = StandardError.new("cause")
+ result = nil
+
+ -> do
+ @object.raise(cause, cause: cause)
+ end.should raise_error(StandardError, "cause") do |error|
+ error.should == cause
+ error.cause.should == nil
+ end
+ end
+
+ it "accepts cause equal an exception" do
+ error = RuntimeError.new("message")
+ result = nil
+
+ -> do
+ @object.raise(error, cause: error)
+ end.should raise_error(RuntimeError, "message") do |e|
+ e.cause.should == nil
+ end
+ end
+
+ it "rejects circular causes" do
+ -> {
begin
- initial_raise_line = __LINE__; Fiber.yield
- rescue => raised
+ raise "Error 1"
+ rescue => error1
begin
- raise_again_line = __LINE__; Fiber.yield raised
- rescue => raised_again
- raised_again
+ raise "Error 2"
+ rescue => error2
+ begin
+ raise "Error 3"
+ rescue => error3
+ @object.raise(error1, cause: error3)
+ end
end
end
+ }.should raise_error(ArgumentError, "circular causes")
+ end
+
+ it "supports exception class with message and cause" do
+ cause = StandardError.new("cause message")
+ result = nil
+
+ -> do
+ @object.raise(ArgumentError, "argument error message", cause: cause)
+ end.should raise_error(ArgumentError, "argument error message") do |error|
+ error.should be_kind_of(ArgumentError)
+ error.message.should == "argument error message"
+ error.cause.should == cause
+ end
+ end
+
+ it "supports exception class with message, backtrace and cause" do
+ cause = StandardError.new("cause message")
+ backtrace = ["line1", "line2"]
+ result = nil
+
+ -> do
+ @object.raise(ArgumentError, "argument error message", backtrace, cause: cause)
+ end.should raise_error(ArgumentError, "argument error message") do |error|
+ error.should be_kind_of(ArgumentError)
+ error.message.should == "argument error message"
+ error.cause.should == cause
+ error.backtrace.should == backtrace
end
- fiber.resume
- raised = fiber.raise 'raised'
- raised_again = fiber.raise raised
- else
- begin
- initial_raise_line = __LINE__; @object.raise 'raised'
- rescue => raised
+ end
+
+ it "supports automatic cause chaining" do
+ -> do
begin
- raise_again_line = __LINE__; @object.raise raised
- rescue => raised_again
- raised_again
+ raise "first error"
+ rescue
+ # No explicit cause - should chain automatically:
+ @object.raise("second error")
end
+ end.should raise_error(RuntimeError, "second error") do |error|
+ error.cause.should be_kind_of(RuntimeError)
+ error.cause.message.should == "first error"
end
end
- raised_again.backtrace.first.should include("#{__FILE__}:#{initial_raise_line}:")
- raised_again.backtrace.first.should_not include("#{__FILE__}:#{raise_again_line}:")
+ it "supports cause: nil to prevent automatic cause chaining" do
+ -> do
+ begin
+ raise "first error"
+ rescue
+ # Explicit nil prevents chaining:
+ @object.raise("second error", cause: nil)
+ end
+ end.should raise_error(RuntimeError, "second error") do |error|
+ error.cause.should == nil
+ end
+ end
end
+end
- it "allows Exception, message, and backtrace parameters" do
- -> do
- @object.raise(ArgumentError, "message", caller)
- end.should raise_error(ArgumentError, "message")
+describe :kernel_raise_across_contexts, shared: true do
+ ruby_version_is "4.0" do
+ describe "with cause keyword argument" do
+ it "uses the cause from the calling context" do
+ original_cause = nil
+ result = nil
+
+ # We have no cause ($!) and we don't specify one explicitly either:
+ @object.raise("second error") do |&block|
+ begin
+ begin
+ raise "first error"
+ rescue => original_cause
+ # We have a cause here ($!) but we should ignore it:
+ block.call
+ end
+ rescue => result
+ # Ignore.
+ end
+ end
+
+ result.should be_kind_of(RuntimeError)
+ result.message.should == "second error"
+ result.cause.should == nil
+ end
+
+ it "accepts a cause keyword argument that overrides the last exception" do
+ original_cause = nil
+ override_cause = StandardError.new("override cause")
+ result = nil
+
+ begin
+ raise "outer error"
+ rescue
+ # We have an existing cause, but we want to override it:
+ @object.raise("second error", cause: override_cause) do |&block|
+ begin
+ begin
+ raise "first error"
+ rescue => original_cause
+ # We also have an existing cause here:
+ block.call
+ end
+ rescue => result
+ # Ignore.
+ end
+ end
+ end
+
+ result.should be_kind_of(RuntimeError)
+ result.message.should == "second error"
+ result.cause.should == override_cause
+ end
+
+ it "supports automatic cause chaining from calling context" do
+ result = nil
+
+ @object.raise("new error") do |&block|
+ begin
+ begin
+ raise "original error"
+ rescue
+ block.call # Let the context yield/sleep
+ end
+ rescue => result
+ # Ignore.
+ end
+ end
+
+ result.should be_kind_of(RuntimeError)
+ result.message.should == "new error"
+ # Calling context has no current exception:
+ result.cause.should == nil
+ end
+
+ it "supports explicit cause: nil to prevent cause chaining" do
+ result = nil
+
+ begin
+ raise "calling context error"
+ rescue
+ @object.raise("new error", cause: nil) do |&block|
+ begin
+ begin
+ raise "target context error"
+ rescue
+ block.call # Let the context yield/sleep
+ end
+ rescue => result
+ # Ignore.
+ end
+ end
+
+ result.should be_kind_of(RuntimeError)
+ result.message.should == "new error"
+ result.cause.should == nil
+ end
+ end
+
+ it "raises TypeError when cause is not an Exception" do
+ -> {
+ @object.raise("error", cause: "not an exception") do |&block|
+ begin
+ block.call # Let the context yield/sleep
+ rescue
+ # Ignore - we expect the TypeError to be raised in the calling context
+ end
+ end
+ }.should raise_error(TypeError, "exception object expected")
+ end
+
+ it "raises ArgumentError when only cause is given with no arguments" do
+ -> {
+ @object.raise(cause: StandardError.new("cause")) do |&block|
+ begin
+ block.call # Let the context yield/sleep
+ rescue
+ # Ignore - we expect the ArgumentError to be raised in the calling context
+ end
+ end
+ }.should raise_error(ArgumentError, "only cause is given with no arguments")
+ end
+ end
end
end
diff --git a/spec/ruby/shared/math/atanh.rb b/spec/ruby/shared/math/atanh.rb
deleted file mode 100644
index 3fb64153a0..0000000000
--- a/spec/ruby/shared/math/atanh.rb
+++ /dev/null
@@ -1,44 +0,0 @@
-describe :math_atanh_base, shared: true do
- it "returns a float" do
- @object.send(@method, 0.5).should be_an_instance_of(Float)
- end
-
- it "returns the inverse hyperbolic tangent of the argument" do
- @object.send(@method, 0.0).should == 0.0
- @object.send(@method, -0.0).should == -0.0
- @object.send(@method, 0.5).should be_close(0.549306144334055, TOLERANCE)
- @object.send(@method, -0.2).should be_close(-0.202732554054082, TOLERANCE)
- end
-
- it "raises a TypeError if the argument is nil" do
- -> { @object.send(@method, nil) }.should raise_error(TypeError)
- end
-
- it "raises a TypeError if the argument is not a Numeric" do
- -> { @object.send(@method, "test") }.should raise_error(TypeError)
- end
-
- it "returns Infinity if x == 1.0" do
- @object.send(@method, 1.0).should == Float::INFINITY
- end
-
- it "return -Infinity if x == -1.0" do
- @object.send(@method, -1.0).should == -Float::INFINITY
- end
-end
-
-describe :math_atanh_private, shared: true do
- it "is a private instance method" do
- Math.should have_private_instance_method(@method)
- end
-end
-
-describe :math_atanh_no_complex, shared: true do
- it "raises a Math::DomainError for arguments greater than 1.0" do
- -> { @object.send(@method, 1.0 + Float::EPSILON) }.should raise_error(Math::DomainError)
- end
-
- it "raises a Math::DomainError for arguments less than -1.0" do
- -> { @object.send(@method, -1.0 - Float::EPSILON) }.should raise_error(Math::DomainError)
- end
-end
diff --git a/spec/ruby/shared/process/fork.rb b/spec/ruby/shared/process/fork.rb
index 11e18d7b1c..8dbb3d0da4 100644
--- a/spec/ruby/shared/process/fork.rb
+++ b/spec/ruby/shared/process/fork.rb
@@ -23,31 +23,31 @@ describe :process_fork, shared: true do
end
it "returns status zero" do
- pid = Process.fork { exit! 0 }
+ pid = @object.fork { exit! 0 }
_, result = Process.wait2(pid)
result.exitstatus.should == 0
end
it "returns status zero" do
- pid = Process.fork { exit 0 }
+ pid = @object.fork { exit 0 }
_, result = Process.wait2(pid)
result.exitstatus.should == 0
end
it "returns status zero" do
- pid = Process.fork {}
+ pid = @object.fork {}
_, result = Process.wait2(pid)
result.exitstatus.should == 0
end
it "returns status non-zero" do
- pid = Process.fork { exit! 42 }
+ pid = @object.fork { exit! 42 }
_, result = Process.wait2(pid)
result.exitstatus.should == 42
end
it "returns status non-zero" do
- pid = Process.fork { exit 42 }
+ pid = @object.fork { exit 42 }
_, result = Process.wait2(pid)
result.exitstatus.should == 42
end
diff --git a/spec/ruby/shared/queue/deque.rb b/spec/ruby/shared/queue/deque.rb
index 9e6b45009d..a154da6274 100644
--- a/spec/ruby/shared/queue/deque.rb
+++ b/spec/ruby/shared/queue/deque.rb
@@ -65,70 +65,64 @@ describe :queue_deq, shared: true do
end
describe "with a timeout" do
- ruby_version_is "3.2" do
- it "returns an item if one is available in time" do
- q = @object.call
-
- t = Thread.new {
- q.send(@method, timeout: 1).should == 1
- }
- Thread.pass until t.status == "sleep" && q.num_waiting == 1
- q << 1
- t.join
- end
-
- it "returns nil if no item is available in time" do
- q = @object.call
-
- t = Thread.new {
- q.send(@method, timeout: 0.1).should == nil
- }
- t.join
- end
-
- it "does nothing if the timeout is nil" do
- q = @object.call
- t = Thread.new {
- q.send(@method, timeout: nil).should == 1
- }
- t.join(0.2).should == nil
- q << 1
- t.join
- end
-
- it "immediately returns nil if no item is available and the timeout is 0" do
- q = @object.call
- q << 1
- q.send(@method, timeout: 0).should == 1
- q.send(@method, timeout: 0).should == nil
- end
-
- it "raise TypeError if timeout is not a valid numeric" do
- q = @object.call
- -> { q.send(@method, timeout: "1") }.should raise_error(
- TypeError,
- "no implicit conversion to float from string",
- )
-
- -> { q.send(@method, timeout: false) }.should raise_error(
- TypeError,
- "no implicit conversion to float from false",
- )
- end
-
- it "raise ArgumentError if non_block = true is passed too" do
- q = @object.call
- -> { q.send(@method, true, timeout: 1) }.should raise_error(
- ArgumentError,
- "can't set a timeout if non_block is enabled",
- )
- end
-
- it "returns nil for a closed empty queue" do
- q = @object.call
- q.close
- q.send(@method, timeout: 0).should == nil
- end
+ it "returns an item if one is available in time" do
+ q = @object.call
+
+ t = Thread.new {
+ q.send(@method, timeout: TIME_TOLERANCE).should == 1
+ }
+ Thread.pass until t.status == "sleep" && q.num_waiting == 1
+ q << 1
+ t.join
+ end
+
+ it "returns nil if no item is available in time" do
+ q = @object.call
+
+ Thread.new {
+ q.send(@method, timeout: 0.001).should == nil
+ }.join
+ end
+
+ it "does nothing if the timeout is nil" do
+ q = @object.call
+ t = Thread.new {
+ q.send(@method, timeout: nil).should == 1
+ }
+ Thread.pass until t.status == "sleep" && q.num_waiting == 1
+ q << 1
+ t.join
+ end
+
+ it "immediately returns nil if no item is available and the timeout is 0" do
+ q = @object.call
+ q << 1
+ q.send(@method, timeout: 0).should == 1
+ q.send(@method, timeout: 0).should == nil
+ end
+
+ it "raise TypeError if timeout is not a valid numeric" do
+ q = @object.call
+ -> {
+ q.send(@method, timeout: "1")
+ }.should raise_error(TypeError, "no implicit conversion to float from string")
+
+ -> {
+ q.send(@method, timeout: false)
+ }.should raise_error(TypeError, "no implicit conversion to float from false")
+ end
+
+ it "raise ArgumentError if non_block = true is passed too" do
+ q = @object.call
+ -> {
+ q.send(@method, true, timeout: 1)
+ }.should raise_error(ArgumentError, "can't set a timeout if non_block is enabled")
+ end
+
+ it "returns nil for a closed empty queue" do
+ q = @object.call
+ q.close
+ q.send(@method, timeout: 0).should == nil
end
end
diff --git a/spec/ruby/shared/queue/freeze.rb b/spec/ruby/shared/queue/freeze.rb
new file mode 100644
index 0000000000..4c506a4235
--- /dev/null
+++ b/spec/ruby/shared/queue/freeze.rb
@@ -0,0 +1,18 @@
+describe :queue_freeze, shared: true do
+ ruby_version_is ""..."3.3" do
+ it "can be frozen" do
+ queue = @object.call
+ queue.freeze
+ queue.should.frozen?
+ end
+ end
+
+ ruby_version_is "3.3" do
+ it "raises an exception when freezing" do
+ queue = @object.call
+ -> {
+ queue.freeze
+ }.should raise_error(TypeError, "cannot freeze #{queue}")
+ end
+ end
+end
diff --git a/spec/ruby/shared/rational/Rational.rb b/spec/ruby/shared/rational/Rational.rb
deleted file mode 100644
index 500f7ed271..0000000000
--- a/spec/ruby/shared/rational/Rational.rb
+++ /dev/null
@@ -1,150 +0,0 @@
-require_relative '../../spec_helper'
-require_relative '../../fixtures/rational'
-
-describe :kernel_Rational, shared: true do
- describe "passed Integer" do
- # Guard against the Mathn library
- guard -> { !defined?(Math.rsqrt) } do
- it "returns a new Rational number with 1 as the denominator" do
- Rational(1).should eql(Rational(1, 1))
- Rational(-3).should eql(Rational(-3, 1))
- Rational(bignum_value).should eql(Rational(bignum_value, 1))
- end
- end
- end
-
- describe "passed two integers" do
- it "returns a new Rational number" do
- rat = Rational(1, 2)
- rat.numerator.should == 1
- rat.denominator.should == 2
- rat.should be_an_instance_of(Rational)
-
- rat = Rational(-3, -5)
- rat.numerator.should == 3
- rat.denominator.should == 5
- rat.should be_an_instance_of(Rational)
-
- rat = Rational(bignum_value, 3)
- rat.numerator.should == bignum_value
- rat.denominator.should == 3
- rat.should be_an_instance_of(Rational)
- end
-
- it "reduces the Rational" do
- rat = Rational(2, 4)
- rat.numerator.should == 1
- rat.denominator.should == 2
-
- rat = Rational(3, 9)
- rat.numerator.should == 1
- rat.denominator.should == 3
- end
- end
-
- describe "when passed a String" do
- it "converts the String to a Rational using the same method as String#to_r" do
- r = Rational(13, 25)
- s_r = ".52".to_r
- r_s = Rational(".52")
-
- r_s.should == r
- r_s.should == s_r
- end
-
- it "scales the Rational value of the first argument by the Rational value of the second" do
- Rational(".52", ".6").should == Rational(13, 15)
- Rational(".52", "1.6").should == Rational(13, 40)
- end
-
- it "does not use the same method as Float#to_r" do
- r = Rational(3, 5)
- f_r = 0.6.to_r
- r_s = Rational("0.6")
-
- r_s.should == r
- r_s.should_not == f_r
- end
- end
-
- describe "when passed a Numeric" do
- it "calls #to_r to convert the first argument to a Rational" do
- num = RationalSpecs::SubNumeric.new(2)
-
- Rational(num).should == Rational(2)
- end
- end
-
- describe "when passed a Complex" do
- it "returns a Rational from the real part if the imaginary part is 0" do
- Rational(Complex(1, 0)).should == Rational(1)
- end
-
- it "raises a RangeError if the imaginary part is not 0" do
- -> { Rational(Complex(1, 2)) }.should raise_error(RangeError)
- end
- end
-
- it "raises a ZeroDivisionError if the second argument is 0" do
- -> { Rational(1, 0) }.should raise_error(ZeroDivisionError, "divided by 0")
- -> { Rational(1, 0.0) }.should raise_error(ZeroDivisionError, "divided by 0")
- end
-
- it "raises a TypeError if the first argument is nil" do
- -> { Rational(nil) }.should raise_error(TypeError)
- end
-
- it "raises a TypeError if the second argument is nil" do
- -> { Rational(1, nil) }.should raise_error(TypeError)
- end
-
- it "raises a TypeError if the first argument is a Symbol" do
- -> { Rational(:sym) }.should raise_error(TypeError)
- end
-
- it "raises a TypeError if the second argument is a Symbol" do
- -> { Rational(1, :sym) }.should raise_error(TypeError)
- end
-
- describe "when passed exception: false" do
- describe "and [non-Numeric]" do
- it "swallows an error" do
- Rational(:sym, exception: false).should == nil
- Rational("abc", exception: false).should == nil
- end
- end
-
- describe "and [non-Numeric, Numeric]" do
- it "swallows an error" do
- Rational(:sym, 1, exception: false).should == nil
- Rational("abc", 1, exception: false).should == nil
- end
- end
-
- describe "and [anything, non-Numeric]" do
- it "swallows an error" do
- Rational(:sym, :sym, exception: false).should == nil
- Rational("abc", :sym, exception: false).should == nil
- end
- end
-
- describe "and non-Numeric String arguments" do
- it "swallows an error" do
- Rational("a", "b", exception: false).should == nil
- Rational("a", 0, exception: false).should == nil
- Rational(0, "b", exception: false).should == nil
- end
- end
-
- describe "and nil arguments" do
- it "swallows an error" do
- Rational(nil, exception: false).should == nil
- Rational(nil, nil, exception: false).should == nil
- end
- end
- end
-
- it "freezes its result" do
- Rational(1).frozen?.should == true
- end
-end
diff --git a/spec/ruby/shared/rational/abs.rb b/spec/ruby/shared/rational/abs.rb
deleted file mode 100644
index 8beb20da7e..0000000000
--- a/spec/ruby/shared/rational/abs.rb
+++ /dev/null
@@ -1,11 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_abs, shared: true do
- it "returns self's absolute value" do
- Rational(3, 4).send(@method).should == Rational(3, 4)
- Rational(-3, 4).send(@method).should == Rational(3, 4)
- Rational(3, -4).send(@method).should == Rational(3, 4)
-
- Rational(bignum_value, -bignum_value).send(@method).should == Rational(bignum_value, bignum_value)
- end
-end
diff --git a/spec/ruby/shared/rational/arithmetic_exception_in_coerce.rb b/spec/ruby/shared/rational/arithmetic_exception_in_coerce.rb
deleted file mode 100644
index 0dff91d522..0000000000
--- a/spec/ruby/shared/rational/arithmetic_exception_in_coerce.rb
+++ /dev/null
@@ -1,11 +0,0 @@
-require_relative '../../fixtures/rational'
-
-describe :rational_arithmetic_exception_in_coerce, shared: true do
- it "does not rescue exception raised in other#coerce" do
- b = mock("numeric with failed #coerce")
- b.should_receive(:coerce).and_raise(RationalSpecs::CoerceError)
-
- # e.g. Rational(3, 4) + b
- -> { Rational(3, 4).send(@method, b) }.should raise_error(RationalSpecs::CoerceError)
- end
-end
diff --git a/spec/ruby/shared/rational/ceil.rb b/spec/ruby/shared/rational/ceil.rb
deleted file mode 100644
index f1cf60d2be..0000000000
--- a/spec/ruby/shared/rational/ceil.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_ceil, shared: true do
- before do
- @rational = Rational(2200, 7)
- end
-
- describe "with no arguments (precision = 0)" do
- it "returns an Integer" do
- @rational.ceil.should be_kind_of(Integer)
- end
-
- it "returns the truncated value toward positive infinity" do
- @rational.ceil.should == 315
- Rational(1, 2).ceil.should == 1
- Rational(-1, 2).ceil.should == 0
- end
- end
-
- describe "with a precision < 0" do
- it "returns an Integer" do
- @rational.ceil(-2).should be_kind_of(Integer)
- @rational.ceil(-1).should be_kind_of(Integer)
- end
-
- it "moves the truncation point n decimal places left" do
- @rational.ceil(-3).should == 1000
- @rational.ceil(-2).should == 400
- @rational.ceil(-1).should == 320
- end
- end
-
- describe "with precision > 0" do
- it "returns a Rational" do
- @rational.ceil(1).should be_kind_of(Rational)
- @rational.ceil(2).should be_kind_of(Rational)
- end
-
- it "moves the truncation point n decimal places right" do
- @rational.ceil(1).should == Rational(3143, 10)
- @rational.ceil(2).should == Rational(31429, 100)
- @rational.ceil(3).should == Rational(157143, 500)
- end
- end
-end
diff --git a/spec/ruby/shared/rational/coerce.rb b/spec/ruby/shared/rational/coerce.rb
deleted file mode 100644
index ccc8901ba0..0000000000
--- a/spec/ruby/shared/rational/coerce.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-require_relative '../../spec_helper'
-
-require 'bigdecimal'
-
-describe :rational_coerce, shared: true do
- it "returns the passed argument, self as Float, when given a Float" do
- result = Rational(3, 4).coerce(1.0)
- result.should == [1.0, 0.75]
- result.first.is_a?(Float).should be_true
- result.last.is_a?(Float).should be_true
- end
-
- it "returns the passed argument, self as Rational, when given an Integer" do
- result = Rational(3, 4).coerce(10)
- result.should == [Rational(10, 1), Rational(3, 4)]
- result.first.is_a?(Rational).should be_true
- result.last.is_a?(Rational).should be_true
- end
-
- it "coerces to Rational, when given a Complex" do
- Rational(3, 4).coerce(Complex(5)).should == [Rational(5, 1), Rational(3, 4)]
- Rational(12, 4).coerce(Complex(5, 1)).should == [Complex(5, 1), Complex(3)]
- end
-
- it "returns [argument, self] when given a Rational" do
- Rational(3, 7).coerce(Rational(9, 2)).should == [Rational(9, 2), Rational(3, 7)]
- end
-
- it "raises an error when passed a BigDecimal" do
- -> {
- Rational(500, 3).coerce(BigDecimal('166.666666666'))
- }.should raise_error(TypeError, /BigDecimal can't be coerced into Rational/)
- end
-end
diff --git a/spec/ruby/shared/rational/comparison.rb b/spec/ruby/shared/rational/comparison.rb
deleted file mode 100644
index 860462f579..0000000000
--- a/spec/ruby/shared/rational/comparison.rb
+++ /dev/null
@@ -1,95 +0,0 @@
-require_relative '../../spec_helper'
-require_relative '../../fixtures/rational'
-
-describe :rational_cmp_rat, shared: true do
- it "returns 1 when self is greater than the passed argument" do
- (Rational(4, 4) <=> Rational(3, 4)).should equal(1)
- (Rational(-3, 4) <=> Rational(-4, 4)).should equal(1)
- end
-
- it "returns 0 when self is equal to the passed argument" do
- (Rational(4, 4) <=> Rational(4, 4)).should equal(0)
- (Rational(-3, 4) <=> Rational(-3, 4)).should equal(0)
- end
-
- it "returns -1 when self is less than the passed argument" do
- (Rational(3, 4) <=> Rational(4, 4)).should equal(-1)
- (Rational(-4, 4) <=> Rational(-3, 4)).should equal(-1)
- end
-end
-
-describe :rational_cmp_int, shared: true do
- it "returns 1 when self is greater than the passed argument" do
- (Rational(4, 4) <=> 0).should equal(1)
- (Rational(4, 4) <=> -10).should equal(1)
- (Rational(-3, 4) <=> -1).should equal(1)
- end
-
- it "returns 0 when self is equal to the passed argument" do
- (Rational(4, 4) <=> 1).should equal(0)
- (Rational(-8, 4) <=> -2).should equal(0)
- end
-
- it "returns -1 when self is less than the passed argument" do
- (Rational(3, 4) <=> 1).should equal(-1)
- (Rational(-4, 4) <=> 0).should equal(-1)
- end
-end
-
-describe :rational_cmp_float, shared: true do
- it "returns 1 when self is greater than the passed argument" do
- (Rational(4, 4) <=> 0.5).should equal(1)
- (Rational(4, 4) <=> -1.5).should equal(1)
- (Rational(-3, 4) <=> -0.8).should equal(1)
- end
-
- it "returns 0 when self is equal to the passed argument" do
- (Rational(4, 4) <=> 1.0).should equal(0)
- (Rational(-6, 4) <=> -1.5).should equal(0)
- end
-
- it "returns -1 when self is less than the passed argument" do
- (Rational(3, 4) <=> 1.2).should equal(-1)
- (Rational(-4, 4) <=> 0.5).should equal(-1)
- end
-end
-
-describe :rational_cmp_coerce, shared: true do
- it "calls #coerce on the passed argument with self" do
- rational = Rational(3, 4)
-
- obj = mock("Object")
- obj.should_receive(:coerce).with(rational).and_return([1, 2])
-
- rational <=> obj
- end
-
- it "calls #<=> on the coerced Rational with the coerced Object" do
- rational = Rational(3, 4)
-
- coerced_rational = mock("Coerced Rational")
- coerced_rational.should_receive(:<=>).and_return(:result)
-
- coerced_obj = mock("Coerced Object")
-
- obj = mock("Object")
- obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
-
- (rational <=> obj).should == :result
- end
-end
-
-describe :rational_cmp_coerce_exception, shared: true do
- it "does not rescue exception raised in other#coerce" do
- b = mock("numeric with failed #coerce")
- b.should_receive(:coerce).and_raise(RationalSpecs::CoerceError)
-
- -> { Rational(3, 4) <=> b }.should raise_error(RationalSpecs::CoerceError)
- end
-end
-
-describe :rational_cmp_other, shared: true do
- it "returns nil" do
- (Rational <=> mock("Object")).should be_nil
- end
-end
diff --git a/spec/ruby/shared/rational/denominator.rb b/spec/ruby/shared/rational/denominator.rb
deleted file mode 100644
index 10d46aacb3..0000000000
--- a/spec/ruby/shared/rational/denominator.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_denominator, shared: true do
- it "returns the denominator" do
- Rational(3, 4).denominator.should equal(4)
- Rational(3, -4).denominator.should equal(4)
-
- Rational(1, bignum_value).denominator.should == bignum_value
- end
-
- it "returns 1 if no denominator was given" do
- Rational(80).denominator.should == 1
- end
-end
diff --git a/spec/ruby/shared/rational/div.rb b/spec/ruby/shared/rational/div.rb
deleted file mode 100644
index d5bd9e6644..0000000000
--- a/spec/ruby/shared/rational/div.rb
+++ /dev/null
@@ -1,54 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_div_rat, shared: true do
- it "performs integer division and returns the result" do
- Rational(2, 3).div(Rational(2, 3)).should == 1
- Rational(-2, 9).div(Rational(-9, 2)).should == 0
- end
-
- it "raises a ZeroDivisionError when the argument has a numerator of 0" do
- -> { Rational(3, 4).div(Rational(0, 3)) }.should raise_error(ZeroDivisionError)
- end
-
- it "raises a ZeroDivisionError when the argument has a numerator of 0.0" do
- -> { Rational(3, 4).div(Rational(0.0, 3)) }.should raise_error(ZeroDivisionError)
- end
-end
-
-describe :rational_div_float, shared: true do
- it "performs integer division and returns the result" do
- Rational(2, 3).div(30.333).should == 0
- Rational(2, 9).div(Rational(-8.6)).should == -1
- Rational(3.12).div(0.5).should == 6
- end
-
- it "raises a ZeroDivisionError when the argument is 0.0" do
- -> { Rational(3, 4).div(0.0) }.should raise_error(ZeroDivisionError)
- end
-end
-
-describe :rational_div_int, shared: true do
- it "performs integer division and returns the result" do
- Rational(2, 1).div(1).should == 2
- Rational(25, 5).div(-50).should == -1
- end
-
- it "raises a ZeroDivisionError when the argument is 0" do
- -> { Rational(3, 4).div(0) }.should raise_error(ZeroDivisionError)
- end
-end
-
-describe :rational_div, shared: true do
- it "returns an Integer" do
- Rational(229, 21).div(82).should be_kind_of(Integer)
- end
-
- it "raises an ArgumentError if passed more than one argument" do
- -> { Rational(3, 4).div(2,3) }.should raise_error(ArgumentError)
- end
-
- # See http://redmine.ruby-lang.org/issues/show/1648
- it "raises a TypeError if passed a non-numeric argument" do
- -> { Rational(3, 4).div([]) }.should raise_error(TypeError)
- end
-end
diff --git a/spec/ruby/shared/rational/divide.rb b/spec/ruby/shared/rational/divide.rb
deleted file mode 100644
index 7d6d66390f..0000000000
--- a/spec/ruby/shared/rational/divide.rb
+++ /dev/null
@@ -1,71 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_divide_rat, shared: true do
- it "returns self divided by other as a Rational" do
- Rational(3, 4).send(@method, Rational(3, 4)).should eql(Rational(1, 1))
- Rational(2, 4).send(@method, Rational(1, 4)).should eql(Rational(2, 1))
-
- Rational(2, 4).send(@method, 2).should == Rational(1, 4)
- Rational(6, 7).send(@method, -2).should == Rational(-3, 7)
- end
-
- it "raises a ZeroDivisionError when passed a Rational with a numerator of 0" do
- -> { Rational(3, 4).send(@method, Rational(0, 1)) }.should raise_error(ZeroDivisionError)
- end
-end
-
-describe :rational_divide_int, shared: true do
- it "returns self divided by other as a Rational" do
- Rational(3, 4).send(@method, 2).should eql(Rational(3, 8))
- Rational(2, 4).send(@method, 2).should eql(Rational(1, 4))
- Rational(6, 7).send(@method, -2).should eql(Rational(-3, 7))
- end
-
- it "raises a ZeroDivisionError when passed 0" do
- -> { Rational(3, 4).send(@method, 0) }.should raise_error(ZeroDivisionError)
- end
-end
-
-describe :rational_divide_float, shared: true do
- it "returns self divided by other as a Float" do
- Rational(3, 4).send(@method, 0.75).should eql(1.0)
- Rational(3, 4).send(@method, 0.25).should eql(3.0)
- Rational(3, 4).send(@method, 0.3).should eql(2.5)
-
- Rational(-3, 4).send(@method, 0.3).should eql(-2.5)
- Rational(3, -4).send(@method, 0.3).should eql(-2.5)
- Rational(3, 4).send(@method, -0.3).should eql(-2.5)
- end
-
- it "returns infinity when passed 0" do
- Rational(3, 4).send(@method, 0.0).infinite?.should eql(1)
- Rational(-3, -4).send(@method, 0.0).infinite?.should eql(1)
-
- Rational(-3, 4).send(@method, 0.0).infinite?.should eql(-1)
- Rational(3, -4).send(@method, 0.0).infinite?.should eql(-1)
- end
-end
-
-describe :rational_divide, shared: true do
- it "calls #coerce on the passed argument with self" do
- rational = Rational(3, 4)
- obj = mock("Object")
- obj.should_receive(:coerce).with(rational).and_return([1, 2])
-
- rational.send(@method, obj)
- end
-
- it "calls #/ on the coerced Rational with the coerced Object" do
- rational = Rational(3, 4)
-
- coerced_rational = mock("Coerced Rational")
- coerced_rational.should_receive(:/).and_return(:result)
-
- coerced_obj = mock("Coerced Object")
-
- obj = mock("Object")
- obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
-
- rational.send(@method, obj).should == :result
- end
-end
diff --git a/spec/ruby/shared/rational/divmod.rb b/spec/ruby/shared/rational/divmod.rb
deleted file mode 100644
index 9e23a18186..0000000000
--- a/spec/ruby/shared/rational/divmod.rb
+++ /dev/null
@@ -1,42 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_divmod_rat, shared: true do
- it "returns the quotient as Integer and the remainder as Rational" do
- Rational(7, 4).divmod(Rational(1, 2)).should eql([3, Rational(1, 4)])
- Rational(7, 4).divmod(Rational(-1, 2)).should eql([-4, Rational(-1, 4)])
- Rational(0, 4).divmod(Rational(4, 3)).should eql([0, Rational(0, 1)])
-
- Rational(bignum_value, 4).divmod(Rational(4, 3)).should eql([3458764513820540928, Rational(0, 1)])
- end
-
- it "raises a ZeroDivisionError when passed a Rational with a numerator of 0" do
- -> { Rational(7, 4).divmod(Rational(0, 3)) }.should raise_error(ZeroDivisionError)
- end
-end
-
-describe :rational_divmod_int, shared: true do
- it "returns the quotient as Integer and the remainder as Rational" do
- Rational(7, 4).divmod(2).should eql([0, Rational(7, 4)])
- Rational(7, 4).divmod(-2).should eql([-1, Rational(-1, 4)])
-
- Rational(bignum_value, 4).divmod(3).should eql([1537228672809129301, Rational(1, 1)])
- end
-
- it "raises a ZeroDivisionError when passed 0" do
- -> { Rational(7, 4).divmod(0) }.should raise_error(ZeroDivisionError)
- end
-end
-
-describe :rational_divmod_float, shared: true do
- it "returns the quotient as Integer and the remainder as Float" do
- Rational(7, 4).divmod(0.5).should eql([3, 0.25])
- end
-
- it "returns the quotient as Integer and the remainder as Float" do
- Rational(7, 4).divmod(-0.5).should eql([-4, -0.25])
- end
-
- it "raises a ZeroDivisionError when passed 0" do
- -> { Rational(7, 4).divmod(0.0) }.should raise_error(ZeroDivisionError)
- end
-end
diff --git a/spec/ruby/shared/rational/equal_value.rb b/spec/ruby/shared/rational/equal_value.rb
deleted file mode 100644
index b2e7e09415..0000000000
--- a/spec/ruby/shared/rational/equal_value.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_equal_value_rat, shared: true do
- it "returns true if self has the same numerator and denominator as the passed argument" do
- (Rational(3, 4) == Rational(3, 4)).should be_true
- (Rational(-3, -4) == Rational(3, 4)).should be_true
- (Rational(-4, 5) == Rational(4, -5)).should be_true
-
- (Rational(bignum_value, 3) == Rational(bignum_value, 3)).should be_true
- (Rational(-bignum_value, 3) == Rational(bignum_value, -3)).should be_true
- end
-end
-
-describe :rational_equal_value_int, shared: true do
- it "returns true if self has the passed argument as numerator and a denominator of 1" do
- # Rational(x, y) reduces x and y automatically
- (Rational(4, 2) == 2).should be_true
- (Rational(-4, 2) == -2).should be_true
- (Rational(4, -2) == -2).should be_true
- end
-end
-
-describe :rational_equal_value_float, shared: true do
- it "converts self to a Float and compares it with the passed argument" do
- (Rational(3, 4) == 0.75).should be_true
- (Rational(4, 2) == 2.0).should be_true
- (Rational(-4, 2) == -2.0).should be_true
- (Rational(4, -2) == -2.0).should be_true
- end
-end
-
-describe :rational_equal_value, shared: true do
- it "returns the result of calling #== with self on the passed argument" do
- obj = mock("Object")
- obj.should_receive(:==).and_return(:result)
-
- (Rational(3, 4) == obj).should_not be_false
- end
-end
diff --git a/spec/ruby/shared/rational/exponent.rb b/spec/ruby/shared/rational/exponent.rb
deleted file mode 100644
index b0e9b23574..0000000000
--- a/spec/ruby/shared/rational/exponent.rb
+++ /dev/null
@@ -1,196 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_exponent, shared: true do
- describe "when passed Rational" do
- # Guard against the Mathn library
- guard -> { !defined?(Math.rsqrt) } do
- it "returns Rational(1) if the exponent is Rational(0)" do
- (Rational(0) ** Rational(0)).should eql(Rational(1))
- (Rational(1) ** Rational(0)).should eql(Rational(1))
- (Rational(3, 4) ** Rational(0)).should eql(Rational(1))
- (Rational(-1) ** Rational(0)).should eql(Rational(1))
- (Rational(-3, 4) ** Rational(0)).should eql(Rational(1))
- (Rational(bignum_value) ** Rational(0)).should eql(Rational(1))
- (Rational(-bignum_value) ** Rational(0)).should eql(Rational(1))
- end
-
- it "returns self raised to the argument as a Rational if the exponent's denominator is 1" do
- (Rational(3, 4) ** Rational(1, 1)).should eql(Rational(3, 4))
- (Rational(3, 4) ** Rational(2, 1)).should eql(Rational(9, 16))
- (Rational(3, 4) ** Rational(-1, 1)).should eql(Rational(4, 3))
- (Rational(3, 4) ** Rational(-2, 1)).should eql(Rational(16, 9))
- end
-
- it "returns self raised to the argument as a Float if the exponent's denominator is not 1" do
- (Rational(3, 4) ** Rational(4, 3)).should be_close(0.681420222312052, TOLERANCE)
- (Rational(3, 4) ** Rational(-4, 3)).should be_close(1.46752322173095, TOLERANCE)
- (Rational(3, 4) ** Rational(4, -3)).should be_close(1.46752322173095, TOLERANCE)
- end
-
- it "returns a complex number when self is negative and the passed argument is not 0" do
- (Rational(-3, 4) ** Rational(-4, 3)).should be_close(Complex(-0.7337616108654732, 1.2709123906625817), TOLERANCE)
- end
- end
- end
-
- describe "when passed Integer" do
- it "returns the Rational value of self raised to the passed argument" do
- (Rational(3, 4) ** 4).should == Rational(81, 256)
- (Rational(3, 4) ** -4).should == Rational(256, 81)
- (Rational(-3, 4) ** -4).should == Rational(256, 81)
- (Rational(3, -4) ** -4).should == Rational(256, 81)
-
- (Rational(bignum_value, 4) ** 4).should == Rational(452312848583266388373324160190187140051835877600158453279131187530910662656, 1)
- (Rational(3, bignum_value) ** -4).should == Rational(115792089237316195423570985008687907853269984665640564039457584007913129639936, 81)
- (Rational(-bignum_value, 4) ** -4).should == Rational(1, 452312848583266388373324160190187140051835877600158453279131187530910662656)
- (Rational(3, -bignum_value) ** -4).should == Rational(115792089237316195423570985008687907853269984665640564039457584007913129639936, 81)
- end
-
- # Guard against the Mathn library
- guard -> { !defined?(Math.rsqrt) } do
- it "returns Rational(1, 1) when the passed argument is 0" do
- (Rational(3, 4) ** 0).should eql(Rational(1, 1))
- (Rational(-3, 4) ** 0).should eql(Rational(1, 1))
- (Rational(3, -4) ** 0).should eql(Rational(1, 1))
-
- (Rational(bignum_value, 4) ** 0).should eql(Rational(1, 1))
- (Rational(3, -bignum_value) ** 0).should eql(Rational(1, 1))
- end
- end
- end
-
- describe "when passed Bignum" do
- # #5713
- it "returns Rational(0) when self is Rational(0) and the exponent is positive" do
- (Rational(0) ** bignum_value).should eql(Rational(0))
- end
-
- it "raises ZeroDivisionError when self is Rational(0) and the exponent is negative" do
- -> { Rational(0) ** -bignum_value }.should raise_error(ZeroDivisionError)
- end
-
- it "returns Rational(1) when self is Rational(1)" do
- (Rational(1) ** bignum_value).should eql(Rational(1))
- (Rational(1) ** -bignum_value).should eql(Rational(1))
- end
-
- it "returns Rational(1) when self is Rational(-1) and the exponent is positive and even" do
- (Rational(-1) ** bignum_value(0)).should eql(Rational(1))
- (Rational(-1) ** bignum_value(2)).should eql(Rational(1))
- end
-
- it "returns Rational(-1) when self is Rational(-1) and the exponent is positive and odd" do
- (Rational(-1) ** bignum_value(1)).should eql(Rational(-1))
- (Rational(-1) ** bignum_value(3)).should eql(Rational(-1))
- end
-
- it "returns positive Infinity when self is > 1" do
- -> {
- (Rational(2) ** bignum_value).infinite?.should == 1
- }.should complain(/warning: in a\*\*b, b may be too big/)
- -> {
- (Rational(fixnum_max) ** bignum_value).infinite?.should == 1
- }.should complain(/warning: in a\*\*b, b may be too big/)
- end
-
- it "returns 0.0 when self is > 1 and the exponent is negative" do
- -> {
- (Rational(2) ** -bignum_value).should eql(0.0)
- }.should complain(/warning: in a\*\*b, b may be too big/)
- -> {
- (Rational(fixnum_max) ** -bignum_value).should eql(0.0)
- }.should complain(/warning: in a\*\*b, b may be too big/)
- end
-
- # Fails on linux due to pow() bugs in glibc: http://sources.redhat.com/bugzilla/show_bug.cgi?id=3866
- platform_is_not :linux do
- it "returns positive Infinity when self < -1" do
- -> {
- (Rational(-2) ** bignum_value).infinite?.should == 1
- }.should complain(/warning: in a\*\*b, b may be too big/)
- -> {
- (Rational(-2) ** (bignum_value + 1)).infinite?.should == 1
- }.should complain(/warning: in a\*\*b, b may be too big/)
- -> {
- (Rational(fixnum_min) ** bignum_value).infinite?.should == 1
- }.should complain(/warning: in a\*\*b, b may be too big/)
- end
-
- it "returns 0.0 when self is < -1 and the exponent is negative" do
- -> {
- (Rational(-2) ** -bignum_value).should eql(0.0)
- }.should complain(/warning: in a\*\*b, b may be too big/)
- -> {
- (Rational(fixnum_min) ** -bignum_value).should eql(0.0)
- }.should complain(/warning: in a\*\*b, b may be too big/)
- end
- end
- end
-
- describe "when passed Float" do
- it "returns self converted to Float and raised to the passed argument" do
- (Rational(3, 1) ** 3.0).should eql(27.0)
- (Rational(3, 1) ** 1.5).should be_close(5.19615242270663, TOLERANCE)
- (Rational(3, 1) ** -1.5).should be_close(0.192450089729875, TOLERANCE)
- end
-
- it "returns a complex number if self is negative and the passed argument is not 0" do
- (Rational(-3, 2) ** 1.5).should be_close(Complex(0.0, -1.8371173070873836), TOLERANCE)
- (Rational(3, -2) ** 1.5).should be_close(Complex(0.0, -1.8371173070873836), TOLERANCE)
- (Rational(3, -2) ** -1.5).should be_close(Complex(0.0, 0.5443310539518174), TOLERANCE)
- end
-
- it "returns Complex(1.0) when the passed argument is 0.0" do
- (Rational(3, 4) ** 0.0).should == Complex(1.0)
- (Rational(-3, 4) ** 0.0).should == Complex(1.0)
- (Rational(-3, 4) ** 0.0).should == Complex(1.0)
- end
- end
-
- it "calls #coerce on the passed argument with self" do
- rational = Rational(3, 4)
- obj = mock("Object")
- obj.should_receive(:coerce).with(rational).and_return([1, 2])
-
- rational ** obj
- end
-
- it "calls #** on the coerced Rational with the coerced Object" do
- rational = Rational(3, 4)
-
- coerced_rational = mock("Coerced Rational")
- coerced_rational.should_receive(:**).and_return(:result)
-
- coerced_obj = mock("Coerced Object")
-
- obj = mock("Object")
- obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
-
- (rational ** obj).should == :result
- end
-
- it "raises ZeroDivisionError for Rational(0, 1) passed a negative Integer" do
- [-1, -4, -9999].each do |exponent|
- -> { Rational(0, 1) ** exponent }.should raise_error(ZeroDivisionError, "divided by 0")
- end
- end
-
- it "raises ZeroDivisionError for Rational(0, 1) passed a negative Rational with denominator 1" do
- [Rational(-1, 1), Rational(-3, 1)].each do |exponent|
- -> { Rational(0, 1) ** exponent }.should raise_error(ZeroDivisionError, "divided by 0")
- end
- end
-
- # #7513
- it "raises ZeroDivisionError for Rational(0, 1) passed a negative Rational" do
- -> { Rational(0, 1) ** Rational(-3, 2) }.should raise_error(ZeroDivisionError, "divided by 0")
- end
-
- platform_is_not :solaris do # See https://github.com/ruby/spec/issues/134
- it "returns Infinity for Rational(0, 1) passed a negative Float" do
- [-1.0, -3.0, -3.14].each do |exponent|
- (Rational(0, 1) ** exponent).infinite?.should == 1
- end
- end
- end
-end
diff --git a/spec/ruby/shared/rational/fdiv.rb b/spec/ruby/shared/rational/fdiv.rb
deleted file mode 100644
index 6911ade8ac..0000000000
--- a/spec/ruby/shared/rational/fdiv.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_fdiv, shared: true do
- it "needs to be reviewed for spec completeness"
-end
diff --git a/spec/ruby/shared/rational/floor.rb b/spec/ruby/shared/rational/floor.rb
deleted file mode 100644
index ddf7fdbd17..0000000000
--- a/spec/ruby/shared/rational/floor.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_floor, shared: true do
- before do
- @rational = Rational(2200, 7)
- end
-
- describe "with no arguments (precision = 0)" do
- it "returns an integer" do
- @rational.floor.should be_kind_of(Integer)
- end
-
- it "returns the truncated value toward negative infinity" do
- @rational.floor.should == 314
- Rational(1, 2).floor.should == 0
- Rational(-1, 2).floor.should == -1
- end
- end
-
- describe "with a precision < 0" do
- it "returns an integer" do
- @rational.floor(-2).should be_kind_of(Integer)
- @rational.floor(-1).should be_kind_of(Integer)
- end
-
- it "moves the truncation point n decimal places left" do
- @rational.floor(-3).should == 0
- @rational.floor(-2).should == 300
- @rational.floor(-1).should == 310
- end
- end
-
- describe "with a precision > 0" do
- it "returns a Rational" do
- @rational.floor(1).should be_kind_of(Rational)
- @rational.floor(2).should be_kind_of(Rational)
- end
-
- it "moves the truncation point n decimal places right" do
- @rational.floor(1).should == Rational(1571, 5)
- @rational.floor(2).should == Rational(7857, 25)
- @rational.floor(3).should == Rational(62857, 200)
- end
- end
-end
diff --git a/spec/ruby/shared/rational/hash.rb b/spec/ruby/shared/rational/hash.rb
deleted file mode 100644
index 50f21cec20..0000000000
--- a/spec/ruby/shared/rational/hash.rb
+++ /dev/null
@@ -1,9 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_hash, shared: true do
- # BUG: Rational(2, 3).hash == Rational(3, 2).hash
- it "is static" do
- Rational(2, 3).hash.should == Rational(2, 3).hash
- Rational(2, 4).hash.should_not == Rational(2, 3).hash
- end
-end
diff --git a/spec/ruby/shared/rational/inspect.rb b/spec/ruby/shared/rational/inspect.rb
deleted file mode 100644
index 19691a2f25..0000000000
--- a/spec/ruby/shared/rational/inspect.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_inspect, shared: true do
- it "returns a string representation of self" do
- Rational(3, 4).inspect.should == "(3/4)"
- Rational(-5, 8).inspect.should == "(-5/8)"
- Rational(-1, -2).inspect.should == "(1/2)"
-
- # Guard against the Mathn library
- guard -> { !defined?(Math.rsqrt) } do
- Rational(bignum_value, 1).inspect.should == "(#{bignum_value}/1)"
- end
- end
-end
diff --git a/spec/ruby/shared/rational/modulo.rb b/spec/ruby/shared/rational/modulo.rb
deleted file mode 100644
index 9e4b0c49e6..0000000000
--- a/spec/ruby/shared/rational/modulo.rb
+++ /dev/null
@@ -1,43 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_modulo, shared: true do
- it "returns the remainder when this value is divided by other" do
- Rational(2, 3).send(@method, Rational(2, 3)).should == Rational(0, 1)
- Rational(4, 3).send(@method, Rational(2, 3)).should == Rational(0, 1)
- Rational(2, -3).send(@method, Rational(-2, 3)).should == Rational(0, 1)
- Rational(0, -1).send(@method, -1).should == Rational(0, 1)
-
- Rational(7, 4).send(@method, Rational(1, 2)).should == Rational(1, 4)
- Rational(7, 4).send(@method, 1).should == Rational(3, 4)
- Rational(7, 4).send(@method, Rational(1, 7)).should == Rational(1, 28)
-
- Rational(3, 4).send(@method, -1).should == Rational(-1, 4)
- Rational(1, -5).send(@method, -1).should == Rational(-1, 5)
- end
-
- it "returns a Float value when the argument is Float" do
- Rational(7, 4).send(@method, 1.0).should be_kind_of(Float)
- Rational(7, 4).send(@method, 1.0).should == 0.75
- Rational(7, 4).send(@method, 0.26).should be_close(0.19, 0.0001)
- end
-
- it "raises ZeroDivisionError on zero denominator" do
- -> {
- Rational(3, 5).send(@method, Rational(0, 1))
- }.should raise_error(ZeroDivisionError)
-
- -> {
- Rational(0, 1).send(@method, Rational(0, 1))
- }.should raise_error(ZeroDivisionError)
-
- -> {
- Rational(3, 5).send(@method, 0)
- }.should raise_error(ZeroDivisionError)
- end
-
- it "raises a ZeroDivisionError when the argument is 0.0" do
- -> {
- Rational(3, 5).send(@method, 0.0)
- }.should raise_error(ZeroDivisionError)
- end
-end
diff --git a/spec/ruby/shared/rational/multiply.rb b/spec/ruby/shared/rational/multiply.rb
deleted file mode 100644
index 9c861cf79d..0000000000
--- a/spec/ruby/shared/rational/multiply.rb
+++ /dev/null
@@ -1,62 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_multiply_rat, shared: true do
- it "returns self divided by other as a Rational" do
- (Rational(3, 4) * Rational(3, 4)).should eql(Rational(9, 16))
- (Rational(2, 4) * Rational(1, 4)).should eql(Rational(1, 8))
-
- (Rational(3, 4) * Rational(0, 1)).should eql(Rational(0, 4))
- end
-end
-
-describe :rational_multiply_int, shared: true do
- it "returns self divided by other as a Rational" do
- (Rational(3, 4) * 2).should eql(Rational(3, 2))
- (Rational(2, 4) * 2).should eql(Rational(1, 1))
- (Rational(6, 7) * -2).should eql(Rational(-12, 7))
-
- (Rational(3, 4) * 0).should eql(Rational(0, 4))
- end
-end
-
-describe :rational_multiply_float, shared: true do
- it "returns self divided by other as a Float" do
- (Rational(3, 4) * 0.75).should eql(0.5625)
- (Rational(3, 4) * 0.25).should eql(0.1875)
- (Rational(3, 4) * 0.3).should be_close(0.225, TOLERANCE)
-
- (Rational(-3, 4) * 0.3).should be_close(-0.225, TOLERANCE)
- (Rational(3, -4) * 0.3).should be_close(-0.225, TOLERANCE)
- (Rational(3, 4) * -0.3).should be_close(-0.225, TOLERANCE)
-
- (Rational(3, 4) * 0.0).should eql(0.0)
- (Rational(-3, -4) * 0.0).should eql(0.0)
-
- (Rational(-3, 4) * 0.0).should eql(0.0)
- (Rational(3, -4) * 0.0).should eql(0.0)
- end
-end
-
-describe :rational_multiply, shared: true do
- it "calls #coerce on the passed argument with self" do
- rational = Rational(3, 4)
- obj = mock("Object")
- obj.should_receive(:coerce).with(rational).and_return([1, 2])
-
- rational * obj
- end
-
- it "calls #* on the coerced Rational with the coerced Object" do
- rational = Rational(3, 4)
-
- coerced_rational = mock("Coerced Rational")
- coerced_rational.should_receive(:*).and_return(:result)
-
- coerced_obj = mock("Coerced Object")
-
- obj = mock("Object")
- obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
-
- (rational * obj).should == :result
- end
-end
diff --git a/spec/ruby/shared/rational/numerator.rb b/spec/ruby/shared/rational/numerator.rb
deleted file mode 100644
index 50d768168c..0000000000
--- a/spec/ruby/shared/rational/numerator.rb
+++ /dev/null
@@ -1,10 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_numerator, shared: true do
- it "returns the numerator" do
- Rational(3, 4).numerator.should equal(3)
- Rational(3, -4).numerator.should equal(-3)
-
- Rational(bignum_value, 1).numerator.should == bignum_value
- end
-end
diff --git a/spec/ruby/shared/rational/plus.rb b/spec/ruby/shared/rational/plus.rb
deleted file mode 100644
index b126360ee4..0000000000
--- a/spec/ruby/shared/rational/plus.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_plus_rat, shared: true do
- it "returns the result of subtracting other from self as a Rational" do
- (Rational(3, 4) + Rational(0, 1)).should eql(Rational(3, 4))
- (Rational(3, 4) + Rational(1, 4)).should eql(Rational(1, 1))
-
- (Rational(3, 4) + Rational(2, 1)).should eql(Rational(11, 4))
- end
-end
-
-describe :rational_plus_int, shared: true do
- it "returns the result of subtracting other from self as a Rational" do
- (Rational(3, 4) + 1).should eql(Rational(7, 4))
- (Rational(3, 4) + 2).should eql(Rational(11, 4))
- end
-end
-
-describe :rational_plus_float, shared: true do
- it "returns the result of subtracting other from self as a Float" do
- (Rational(3, 4) + 0.2).should eql(0.95)
- (Rational(3, 4) + 2.5).should eql(3.25)
- end
-end
-
-describe :rational_plus, shared: true do
- it "calls #coerce on the passed argument with self" do
- rational = Rational(3, 4)
- obj = mock("Object")
- obj.should_receive(:coerce).with(rational).and_return([1, 2])
-
- rational + obj
- end
-
- it "calls #+ on the coerced Rational with the coerced Object" do
- rational = Rational(3, 4)
-
- coerced_rational = mock("Coerced Rational")
- coerced_rational.should_receive(:+).and_return(:result)
-
- coerced_obj = mock("Coerced Object")
-
- obj = mock("Object")
- obj.should_receive(:coerce).and_return([coerced_rational, coerced_obj])
-
- (rational + obj).should == :result
- end
-end
diff --git a/spec/ruby/shared/rational/remainder.rb b/spec/ruby/shared/rational/remainder.rb
deleted file mode 100644
index dd907608db..0000000000
--- a/spec/ruby/shared/rational/remainder.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_remainder, shared: true do
- it "needs to be reviewed for spec completeness"
-end
diff --git a/spec/ruby/shared/rational/round.rb b/spec/ruby/shared/rational/round.rb
deleted file mode 100644
index 5b159ee3e6..0000000000
--- a/spec/ruby/shared/rational/round.rb
+++ /dev/null
@@ -1,106 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_round, shared: true do
- before do
- @rational = Rational(2200, 7)
- end
-
- describe "with no arguments (precision = 0)" do
- it "returns an integer" do
- @rational.round.should be_kind_of(Integer)
- Rational(0, 1).round(0).should be_kind_of(Integer)
- Rational(124, 1).round(0).should be_kind_of(Integer)
- end
-
- it "returns the truncated value toward the nearest integer" do
- @rational.round.should == 314
- Rational(0, 1).round(0).should == 0
- Rational(2, 1).round(0).should == 2
- end
-
- it "returns the rounded value toward the nearest integer" do
- Rational(1, 2).round.should == 1
- Rational(-1, 2).round.should == -1
- Rational(3, 2).round.should == 2
- Rational(-3, 2).round.should == -2
- Rational(5, 2).round.should == 3
- Rational(-5, 2).round.should == -3
- end
- end
-
- describe "with a precision < 0" do
- it "returns an integer" do
- @rational.round(-2).should be_kind_of(Integer)
- @rational.round(-1).should be_kind_of(Integer)
- Rational(0, 1).round(-1).should be_kind_of(Integer)
- Rational(2, 1).round(-1).should be_kind_of(Integer)
- end
-
- it "moves the truncation point n decimal places left" do
- @rational.round(-3).should == 0
- @rational.round(-2).should == 300
- @rational.round(-1).should == 310
- end
- end
-
- describe "with a precision > 0" do
- it "returns a Rational" do
- @rational.round(1).should be_kind_of(Rational)
- @rational.round(2).should be_kind_of(Rational)
- # Guard against the Mathn library
- guard -> { !defined?(Math.rsqrt) } do
- Rational(0, 1).round(1).should be_kind_of(Rational)
- Rational(2, 1).round(1).should be_kind_of(Rational)
- end
- end
-
- it "moves the truncation point n decimal places right" do
- @rational.round(1).should == Rational(3143, 10)
- @rational.round(2).should == Rational(31429, 100)
- @rational.round(3).should == Rational(157143, 500)
- Rational(0, 1).round(1).should == Rational(0, 1)
- Rational(2, 1).round(1).should == Rational(2, 1)
- end
-
- it "doesn't alter the value if the precision is too great" do
- Rational(3, 2).round(10).should == Rational(3, 2).round(20)
- end
-
- # #6605
- it "doesn't fail when rounding to an absurdly large positive precision" do
- Rational(3, 2).round(2_097_171).should == Rational(3, 2)
- end
- end
-
- describe "with half option" do
- it "returns an Integer when precision is not passed" do
- Rational(10, 4).round(half: nil).should == 3
- Rational(10, 4).round(half: :up).should == 3
- Rational(10, 4).round(half: :down).should == 2
- Rational(10, 4).round(half: :even).should == 2
- Rational(-10, 4).round(half: nil).should == -3
- Rational(-10, 4).round(half: :up).should == -3
- Rational(-10, 4).round(half: :down).should == -2
- Rational(-10, 4).round(half: :even).should == -2
- end
-
- it "returns a Rational when the precision is greater than 0" do
- Rational(25, 100).round(1, half: nil).should == Rational(3, 10)
- Rational(25, 100).round(1, half: :up).should == Rational(3, 10)
- Rational(25, 100).round(1, half: :down).should == Rational(1, 5)
- Rational(25, 100).round(1, half: :even).should == Rational(1, 5)
- Rational(35, 100).round(1, half: nil).should == Rational(2, 5)
- Rational(35, 100).round(1, half: :up).should == Rational(2, 5)
- Rational(35, 100).round(1, half: :down).should == Rational(3, 10)
- Rational(35, 100).round(1, half: :even).should == Rational(2, 5)
- Rational(-25, 100).round(1, half: nil).should == Rational(-3, 10)
- Rational(-25, 100).round(1, half: :up).should == Rational(-3, 10)
- Rational(-25, 100).round(1, half: :down).should == Rational(-1, 5)
- Rational(-25, 100).round(1, half: :even).should == Rational(-1, 5)
- end
-
- it "raise for a non-existent round mode" do
- -> { Rational(10, 4).round(half: :nonsense) }.should raise_error(ArgumentError, "invalid rounding mode: nonsense")
- end
- end
-end
diff --git a/spec/ruby/shared/rational/to_f.rb b/spec/ruby/shared/rational/to_f.rb
deleted file mode 100644
index 472a585daa..0000000000
--- a/spec/ruby/shared/rational/to_f.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_to_f, shared: true do
- it "returns self converted to a Float" do
- Rational(3, 4).to_f.should eql(0.75)
- Rational(3, -4).to_f.should eql(-0.75)
- Rational(-1, 4).to_f.should eql(-0.25)
- Rational(-1, -4).to_f.should eql(0.25)
- end
-
- it "converts to a Float for large numerator and denominator" do
- num = 1000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146010000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146010000000000000000000000000000000000048148248609680896326399448564623182963452541226153892315137780403285956264146009
- den = 2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
- Rational(num, den).to_f.should == 500.0
- end
-end
diff --git a/spec/ruby/shared/rational/to_i.rb b/spec/ruby/shared/rational/to_i.rb
deleted file mode 100644
index 9be1183aa4..0000000000
--- a/spec/ruby/shared/rational/to_i.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_to_i, shared: true do
- it "converts self to an Integer by truncation" do
- Rational(7, 4).to_i.should eql(1)
- Rational(11, 4).to_i.should eql(2)
- end
-
- it "converts self to an Integer by truncation" do
- Rational(-7, 4).to_i.should eql(-1)
- end
-end
diff --git a/spec/ruby/shared/rational/to_r.rb b/spec/ruby/shared/rational/to_r.rb
deleted file mode 100644
index 372c086850..0000000000
--- a/spec/ruby/shared/rational/to_r.rb
+++ /dev/null
@@ -1,11 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_to_r, shared: true do
- it "returns self" do
- a = Rational(3, 4)
- a.to_r.should equal(a)
-
- a = Rational(bignum_value, 4)
- a.to_r.should equal(a)
- end
-end
diff --git a/spec/ruby/shared/rational/to_s.rb b/spec/ruby/shared/rational/to_s.rb
deleted file mode 100644
index e90c6e5e39..0000000000
--- a/spec/ruby/shared/rational/to_s.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_to_s, shared: true do
- it "returns a string representation of self" do
- # Guard against the Mathn library
- guard -> { !defined?(Math.rsqrt) } do
- Rational(1, 1).to_s.should == "1/1"
- Rational(2, 1).to_s.should == "2/1"
- end
- Rational(1, 2).to_s.should == "1/2"
- Rational(-1, 3).to_s.should == "-1/3"
- Rational(1, -3).to_s.should == "-1/3"
- end
-end
diff --git a/spec/ruby/shared/rational/truncate.rb b/spec/ruby/shared/rational/truncate.rb
deleted file mode 100644
index df5198ca02..0000000000
--- a/spec/ruby/shared/rational/truncate.rb
+++ /dev/null
@@ -1,71 +0,0 @@
-require_relative '../../spec_helper'
-
-describe :rational_truncate, shared: true do
- before do
- @rational = Rational(2200, 7)
- end
-
- describe "with no arguments (precision = 0)" do
- it "returns an integer" do
- @rational.truncate.should be_kind_of(Integer)
- end
-
- it "returns the truncated value toward 0" do
- @rational.truncate.should == 314
- Rational(1, 2).truncate.should == 0
- Rational(-1, 2).truncate.should == 0
- end
- end
-
- describe "with an explicit precision = 0" do
- it "returns an integer" do
- @rational.truncate(0).should be_kind_of(Integer)
- end
-
- it "returns the truncated value toward 0" do
- @rational.truncate(0).should == 314
- Rational(1, 2).truncate(0).should == 0
- Rational(-1, 2).truncate(0).should == 0
- end
- end
-
- describe "with a precision < 0" do
- it "returns an integer" do
- @rational.truncate(-2).should be_kind_of(Integer)
- @rational.truncate(-1).should be_kind_of(Integer)
- end
-
- it "moves the truncation point n decimal places left" do
- @rational.truncate(-3).should == 0
- @rational.truncate(-2).should == 300
- @rational.truncate(-1).should == 310
- end
- end
-
- describe "with a precision > 0" do
- it "returns a Rational" do
- @rational.truncate(1).should be_kind_of(Rational)
- @rational.truncate(2).should be_kind_of(Rational)
- end
-
- it "moves the truncation point n decimal places right" do
- @rational.truncate(1).should == Rational(1571, 5)
- @rational.truncate(2).should == Rational(7857, 25)
- @rational.truncate(3).should == Rational(62857, 200)
- end
- end
-
- describe "with an invalid value for precision" do
- it "raises a TypeError" do
- -> { @rational.truncate(nil) }.should raise_error(TypeError, "not an integer")
- -> { @rational.truncate(1.0) }.should raise_error(TypeError, "not an integer")
- -> { @rational.truncate('') }.should raise_error(TypeError, "not an integer")
- end
-
- it "does not call to_int on the argument" do
- object = Object.new
- object.should_not_receive(:to_int)
- -> { @rational.truncate(object) }.should raise_error(TypeError, "not an integer")
- end
- end
-end
diff --git a/spec/ruby/shared/sizedqueue/enque.rb b/spec/ruby/shared/sizedqueue/enque.rb
index 6307f3c3ca..6804af3fb3 100644
--- a/spec/ruby/shared/sizedqueue/enque.rb
+++ b/spec/ruby/shared/sizedqueue/enque.rb
@@ -49,87 +49,81 @@ describe :sizedqueue_enq, shared: true do
end
describe "with a timeout" do
- ruby_version_is "3.2" do
- it "returns self if the item was pushed in time" do
- q = @object.call(1)
- q << 1
-
- t = Thread.new {
- q.send(@method, 2, timeout: 1).should == q
- }
- Thread.pass until t.status == "sleep" && q.num_waiting == 1
- q.pop
- t.join
- end
-
- it "does nothing if the timeout is nil" do
- q = @object.call(1)
- q << 1
- t = Thread.new {
- q.send(@method, 2, timeout: nil).should == q
- }
- t.join(0.2).should == nil
- q.pop
- t.join
- end
-
- it "returns nil if no space is available and timeout is 0" do
- q = @object.call(1)
- q.send(@method, 1, timeout: 0).should == q
- q.send(@method, 2, timeout: 0).should == nil
- end
-
- it "returns nil if no space is available in time" do
- q = @object.call(1)
- q << 1
- t = Thread.new {
- q.send(@method, 2, timeout: 0.1).should == nil
- }
- t.join
- end
-
- it "raise TypeError if timeout is not a valid numeric" do
- q = @object.call(1)
- -> { q.send(@method, 2, timeout: "1") }.should raise_error(
- TypeError,
- "no implicit conversion to float from string",
- )
-
- -> { q.send(@method, 2, timeout: false) }.should raise_error(
- TypeError,
- "no implicit conversion to float from false",
- )
- end
-
- it "raise ArgumentError if non_block = true is passed too" do
- q = @object.call(1)
- -> { q.send(@method, 2, true, timeout: 1) }.should raise_error(
- ArgumentError,
- "can't set a timeout if non_block is enabled",
- )
- end
-
- it "raise ClosedQueueError when closed before enqueued" do
- q = @object.call(1)
- q.close
- -> { q.send(@method, 2, timeout: 1) }.should raise_error(ClosedQueueError, "queue closed")
- end
-
- it "interrupts enqueuing threads with ClosedQueueError when the queue is closed" do
- q = @object.call(1)
- q << 1
-
- t = Thread.new {
- -> { q.send(@method, 1, timeout: 10) }.should raise_error(ClosedQueueError, "queue closed")
- }
-
- Thread.pass until q.num_waiting == 1
-
- q.close
-
- t.join
- q.pop.should == 1
- end
+ it "returns self if the item was pushed in time" do
+ q = @object.call(1)
+ q << 1
+
+ t = Thread.new {
+ q.send(@method, 2, timeout: TIME_TOLERANCE).should == q
+ }
+ Thread.pass until t.status == "sleep" && q.num_waiting == 1
+ q.pop
+ t.join
+ end
+
+ it "does nothing if the timeout is nil" do
+ q = @object.call(1)
+ q << 1
+ t = Thread.new {
+ q.send(@method, 2, timeout: nil).should == q
+ }
+ t.join(0.2).should == nil
+ q.pop
+ t.join
+ end
+
+ it "returns nil if no space is available and timeout is 0" do
+ q = @object.call(1)
+ q.send(@method, 1, timeout: 0).should == q
+ q.send(@method, 2, timeout: 0).should == nil
+ end
+
+ it "returns nil if no space is available in time" do
+ q = @object.call(1)
+ q << 1
+ Thread.new {
+ q.send(@method, 2, timeout: 0.001).should == nil
+ }.join
+ end
+
+ it "raise TypeError if timeout is not a valid numeric" do
+ q = @object.call(1)
+ -> {
+ q.send(@method, 2, timeout: "1")
+ }.should raise_error(TypeError, "no implicit conversion to float from string")
+
+ -> {
+ q.send(@method, 2, timeout: false)
+ }.should raise_error(TypeError, "no implicit conversion to float from false")
+ end
+
+ it "raise ArgumentError if non_block = true is passed too" do
+ q = @object.call(1)
+ -> {
+ q.send(@method, 2, true, timeout: 1)
+ }.should raise_error(ArgumentError, "can't set a timeout if non_block is enabled")
+ end
+
+ it "raise ClosedQueueError when closed before enqueued" do
+ q = @object.call(1)
+ q.close
+ -> { q.send(@method, 2, timeout: 1) }.should raise_error(ClosedQueueError, "queue closed")
+ end
+
+ it "interrupts enqueuing threads with ClosedQueueError when the queue is closed" do
+ q = @object.call(1)
+ q << 1
+
+ t = Thread.new {
+ -> { q.send(@method, 1, timeout: TIME_TOLERANCE) }.should raise_error(ClosedQueueError, "queue closed")
+ }
+
+ Thread.pass until q.num_waiting == 1
+
+ q.close
+
+ t.join
+ q.pop.should == 1
end
end
end
diff --git a/spec/ruby/shared/string/end_with.rb b/spec/ruby/shared/string/end_with.rb
index 0e4c1386e8..08f43c1bce 100644
--- a/spec/ruby/shared/string/end_with.rb
+++ b/spec/ruby/shared/string/end_with.rb
@@ -55,7 +55,7 @@ describe :end_with, shared: true do
it "checks that we are starting to match at the head of a character" do
"\xC3\xA9".send(@method).should_not.end_with?("\xA9")
"\xe3\x81\x82".send(@method).should_not.end_with?("\x82")
- "ab".force_encoding("UTF-16BE").send(@method).should_not.end_with?(
- "b".force_encoding("UTF-16BE"))
+ "\xd8\x00\xdc\x00".dup.force_encoding("UTF-16BE").send(@method).should_not.end_with?(
+ "\xdc\x00".dup.force_encoding("UTF-16BE"))
end
end
diff --git a/spec/ruby/shared/string/times.rb b/spec/ruby/shared/string/times.rb
index be3b622f73..4814f894cf 100644
--- a/spec/ruby/shared/string/times.rb
+++ b/spec/ruby/shared/string/times.rb
@@ -39,18 +39,18 @@ describe :string_times, shared: true do
end
it "returns a String in the same encoding as self" do
- str = "\xE3\x81\x82".force_encoding Encoding::UTF_8
+ str = "\xE3\x81\x82".dup.force_encoding Encoding::UTF_8
result = @object.call(str, 2)
result.encoding.should equal(Encoding::UTF_8)
end
- platform_is wordsize: 32 do
+ platform_is c_long_size: 32 do
it "raises an ArgumentError if the length of the resulting string doesn't fit into a long" do
-> { @object.call("abc", (2 ** 31) - 1) }.should raise_error(ArgumentError)
end
end
- platform_is wordsize: 64 do
+ platform_is c_long_size: 64 do
it "raises an ArgumentError if the length of the resulting string doesn't fit into a long" do
-> { @object.call("abc", (2 ** 63) - 1) }.should raise_error(ArgumentError)
end
diff --git a/spec/ruby/shared/time/yday.rb b/spec/ruby/shared/time/yday.rb
new file mode 100644
index 0000000000..f81c45261c
--- /dev/null
+++ b/spec/ruby/shared/time/yday.rb
@@ -0,0 +1,18 @@
+describe :time_yday, shared: true do
+ it 'returns the correct value for each day of each month' do
+ mdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
+
+ yday = 1
+ mdays.each_with_index do |days, month|
+ days.times do |day|
+ @method.call(2014, month+1, day+1).should == yday
+ yday += 1
+ end
+ end
+ end
+
+ it 'supports leap years' do
+ @method.call(2016, 2, 29).should == 31 + 29
+ @method.call(2016, 3, 1).should == 31 + 29 + 1
+ end
+end