diff options
Diffstat (limited to 'spec/ruby/shared')
-rw-r--r-- | spec/ruby/shared/file/exist.rb | 5 | ||||
-rw-r--r-- | spec/ruby/shared/kernel/at_exit.rb | 11 | ||||
-rw-r--r-- | spec/ruby/shared/kernel/object_id.rb | 28 | ||||
-rw-r--r-- | spec/ruby/shared/kernel/raise.rb | 11 | ||||
-rw-r--r-- | spec/ruby/shared/queue/deque.rb | 57 | ||||
-rw-r--r-- | spec/ruby/shared/rational/coerce.rb | 34 | ||||
-rw-r--r-- | spec/ruby/shared/rational/marshal_dump.rb | 5 | ||||
-rw-r--r-- | spec/ruby/shared/rational/marshal_load.rb | 5 | ||||
-rw-r--r-- | spec/ruby/shared/rational/quo.rb | 5 | ||||
-rw-r--r-- | spec/ruby/shared/sizedqueue/enque.rb | 58 | ||||
-rw-r--r-- | spec/ruby/shared/string/end_with.rb | 4 | ||||
-rw-r--r-- | spec/ruby/shared/string/start_with.rb | 12 | ||||
-rw-r--r-- | spec/ruby/shared/string/times.rb | 20 | ||||
-rw-r--r-- | spec/ruby/shared/types/rb_num2dbl_fails.rb | 17 |
14 files changed, 155 insertions, 117 deletions
diff --git a/spec/ruby/shared/file/exist.rb b/spec/ruby/shared/file/exist.rb index 3bd97711b4..67424146c5 100644 --- a/spec/ruby/shared/file/exist.rb +++ b/spec/ruby/shared/file/exist.rb @@ -4,11 +4,6 @@ describe :file_exist, shared: true do @object.send(@method, 'a_fake_file').should == false end - it "returns true if the file exist using the alias exists?" do - @object.send(@method, __FILE__).should == true - @object.send(@method, 'a_fake_file').should == false - end - it "raises an ArgumentError if not passed one argument" do -> { @object.send(@method) }.should raise_error(ArgumentError) -> { @object.send(@method, __FILE__, __FILE__) }.should raise_error(ArgumentError) 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..1917a4c923 100644 --- a/spec/ruby/shared/kernel/raise.rb +++ b/spec/ruby/shared/kernel/raise.rb @@ -146,4 +146,15 @@ describe :kernel_raise, shared: true 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 end diff --git a/spec/ruby/shared/queue/deque.rb b/spec/ruby/shared/queue/deque.rb index 616e56ec8a..0abba5301e 100644 --- a/spec/ruby/shared/queue/deque.rb +++ b/spec/ruby/shared/queue/deque.rb @@ -37,6 +37,15 @@ describe :queue_deq, shared: true do q.send(@method).should == 1 end + it "converts false-ish for non_blocking to boolean" do + q = @object.call + q << 1 + q << 2 + + q.send(@method, false).should == 1 + q.send(@method, nil).should == 2 + end + it "returns nil for a closed empty queue" do q = @object.call q.close @@ -61,7 +70,7 @@ describe :queue_deq, shared: true do q = @object.call t = Thread.new { - q.send(@method, timeout: 1).should == 1 + q.send(@method, timeout: TIME_TOLERANCE).should == 1 } Thread.pass until t.status == "sleep" && q.num_waiting == 1 q << 1 @@ -71,10 +80,9 @@ describe :queue_deq, shared: true do 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 + Thread.new { + q.send(@method, timeout: 0.001).should == nil + }.join end it "does nothing if the timeout is nil" do @@ -82,7 +90,7 @@ describe :queue_deq, shared: true do t = Thread.new { q.send(@method, timeout: nil).should == 1 } - t.join(0.2).should == nil + Thread.pass until t.status == "sleep" && q.num_waiting == 1 q << 1 t.join end @@ -96,23 +104,26 @@ describe :queue_deq, shared: true do 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", - ) + -> { + 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", - ) + -> { + 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 end @@ -143,5 +154,13 @@ describe :queue_deq, shared: true do q.close -> { q.send(@method, true) }.should raise_error(ThreadError) end + + it "converts true-ish non_blocking argument to true" do + q = @object.call + + -> { q.send(@method, true) }.should raise_error(ThreadError) + -> { q.send(@method, 1) }.should raise_error(ThreadError) + -> { q.send(@method, "") }.should raise_error(ThreadError) + 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/marshal_dump.rb b/spec/ruby/shared/rational/marshal_dump.rb deleted file mode 100644 index 09782b45a5..0000000000 --- a/spec/ruby/shared/rational/marshal_dump.rb +++ /dev/null @@ -1,5 +0,0 @@ -require_relative '../../spec_helper' - -describe :rational_marshal_dump, shared: true do - it "needs to be reviewed for spec completeness" -end diff --git a/spec/ruby/shared/rational/marshal_load.rb b/spec/ruby/shared/rational/marshal_load.rb deleted file mode 100644 index 20bdd6fdf4..0000000000 --- a/spec/ruby/shared/rational/marshal_load.rb +++ /dev/null @@ -1,5 +0,0 @@ -require_relative '../../spec_helper' - -describe :rational_marshal_load, shared: true do - it "needs to be reviewed for spec completeness" -end diff --git a/spec/ruby/shared/rational/quo.rb b/spec/ruby/shared/rational/quo.rb deleted file mode 100644 index 53b32fed2f..0000000000 --- a/spec/ruby/shared/rational/quo.rb +++ /dev/null @@ -1,5 +0,0 @@ -require_relative '../../spec_helper' - -describe :rational_quo, shared: true do - it "needs to be reviewed for spec completeness" -end diff --git a/spec/ruby/shared/sizedqueue/enque.rb b/spec/ruby/shared/sizedqueue/enque.rb index 059f1025a7..2f25517675 100644 --- a/spec/ruby/shared/sizedqueue/enque.rb +++ b/spec/ruby/shared/sizedqueue/enque.rb @@ -37,7 +37,7 @@ describe :sizedqueue_enq, shared: true do q << 1 t = Thread.new { - -> { q.send(@method, 2) }.should raise_error(ClosedQueueError) + -> { q.send(@method, 2) }.should raise_error(ClosedQueueError, "queue closed") } Thread.pass until q.num_waiting == 1 @@ -55,7 +55,7 @@ describe :sizedqueue_enq, shared: true do q << 1 t = Thread.new { - q.send(@method, 2, timeout: 1).should == q + q.send(@method, 2, timeout: TIME_TOLERANCE).should == q } Thread.pass until t.status == "sleep" && q.num_waiting == 1 q.pop @@ -73,7 +73,7 @@ describe :sizedqueue_enq, shared: true do t.join end - it "returns nil if no space is avialable and timeout is 0" do + 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 @@ -82,31 +82,49 @@ describe :sizedqueue_enq, shared: true do 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 + 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", - ) + -> { + 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", - ) + -> { + 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..94a7f97513 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")) + "ab".dup.force_encoding("UTF-16BE").send(@method).should_not.end_with?( + "b".dup.force_encoding("UTF-16BE")) end end diff --git a/spec/ruby/shared/string/start_with.rb b/spec/ruby/shared/string/start_with.rb index 6932a017b6..4b947a3bbf 100644 --- a/spec/ruby/shared/string/start_with.rb +++ b/spec/ruby/shared/string/start_with.rb @@ -70,7 +70,15 @@ describe :start_with, shared: true do $1.should be_nil end - it "does not check that we are not matching part of a character" do - "\xC3\xA9".send(@method).should.start_with?("\xC3") + ruby_version_is ""..."3.3" do + it "does not check that we are not matching part of a character" do + "\xC3\xA9".send(@method).should.start_with?("\xC3") + end + end + + ruby_version_is "3.3" do # #19784 + it "checks that we are not matching part of a character" do + "\xC3\xA9".send(@method).should_not.start_with?("\xC3") + end end end diff --git a/spec/ruby/shared/string/times.rb b/spec/ruby/shared/string/times.rb index 8ca9507570..aaf748bad9 100644 --- a/spec/ruby/shared/string/times.rb +++ b/spec/ruby/shared/string/times.rb @@ -32,24 +32,14 @@ describe :string_times, shared: true do @object.call("", max_long).should == "" end - ruby_version_is ''...'3.0' do - it "returns subclass instances" do - @object.call(MyString.new("cool"), 0).should be_an_instance_of(MyString) - @object.call(MyString.new("cool"), 1).should be_an_instance_of(MyString) - @object.call(MyString.new("cool"), 2).should be_an_instance_of(MyString) - end - end - - ruby_version_is '3.0' do - it "returns String instances" do - @object.call(MyString.new("cool"), 0).should be_an_instance_of(String) - @object.call(MyString.new("cool"), 1).should be_an_instance_of(String) - @object.call(MyString.new("cool"), 2).should be_an_instance_of(String) - end + it "returns String instances" do + @object.call(MyString.new("cool"), 0).should be_an_instance_of(String) + @object.call(MyString.new("cool"), 1).should be_an_instance_of(String) + @object.call(MyString.new("cool"), 2).should be_an_instance_of(String) 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 diff --git a/spec/ruby/shared/types/rb_num2dbl_fails.rb b/spec/ruby/shared/types/rb_num2dbl_fails.rb new file mode 100644 index 0000000000..ec7cc11986 --- /dev/null +++ b/spec/ruby/shared/types/rb_num2dbl_fails.rb @@ -0,0 +1,17 @@ +# +# Shared tests for rb_num2dbl related conversion failures. +# +# Usage example: +# it_behaves_like :rb_num2dbl_fails, nil, -> v { o = A.new; o.foo(v) } +# + +describe :rb_num2dbl_fails, shared: true do + it "fails if string is provided" do + -> { @object.call("123") }.should raise_error(TypeError, "no implicit conversion to float from string") + end + + it "fails if boolean is provided" do + -> { @object.call(true) }.should raise_error(TypeError, "no implicit conversion to float from true") + -> { @object.call(false) }.should raise_error(TypeError, "no implicit conversion to float from false") + end +end |