From b46da8d84efeb97cb52ba0560d5b149ccda0d561 Mon Sep 17 00:00:00 2001 From: eregon Date: Wed, 13 Jun 2018 21:58:54 +0000 Subject: Update to ruby/spec@4bb0f25 * Specs added by TruffleRuby. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63654 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/command_line/dash_upper_c_spec.rb | 5 + spec/ruby/core/array/pack/buffer_spec.rb | 4 +- spec/ruby/core/basicobject/instance_eval_spec.rb | 8 ++ spec/ruby/core/exception/dup_spec.rb | 61 ++++++++++ spec/ruby/core/exception/fixtures/common.rb | 20 ++++ spec/ruby/core/file/expand_path_spec.rb | 2 +- spec/ruby/core/file/fixtures/file_types.rb | 30 ++--- spec/ruby/core/file/open_spec.rb | 2 +- spec/ruby/core/file/pipe_spec.rb | 2 +- spec/ruby/core/file/stat/pipe_spec.rb | 2 +- spec/ruby/core/integer/dup_spec.rb | 15 +++ spec/ruby/core/io/write_spec.rb | 2 +- spec/ruby/core/marshal/dump_spec.rb | 10 ++ spec/ruby/core/string/capitalize_spec.rb | 139 ++++++++++++++++++++++- spec/ruby/core/string/casecmp_spec.rb | 20 ++++ spec/ruby/core/string/downcase_spec.rb | 139 ++++++++++++++++++++++- spec/ruby/core/string/swapcase_spec.rb | 129 ++++++++++++++++++++- spec/ruby/core/string/upcase_spec.rb | 130 +++++++++++++++++++-- spec/ruby/core/time/comparison_spec.rb | 10 ++ spec/ruby/language/send_spec.rb | 8 ++ spec/ruby/optional/capi/class_spec.rb | 6 + spec/ruby/optional/capi/ext/string_spec.c | 46 +++++++- spec/ruby/optional/capi/fixtures/class.rb | 11 +- spec/ruby/optional/capi/string_spec.rb | 68 +++++++++-- 24 files changed, 812 insertions(+), 57 deletions(-) create mode 100644 spec/ruby/core/exception/dup_spec.rb create mode 100644 spec/ruby/core/integer/dup_spec.rb (limited to 'spec/ruby') diff --git a/spec/ruby/command_line/dash_upper_c_spec.rb b/spec/ruby/command_line/dash_upper_c_spec.rb index c8274b0c2d..761beaadab 100644 --- a/spec/ruby/command_line/dash_upper_c_spec.rb +++ b/spec/ruby/command_line/dash_upper_c_spec.rb @@ -11,6 +11,11 @@ describe 'The -C command line option' do output.should == @tempdir end + it 'does not need a space after -C for the argument' do + output = ruby_exe(@script, options: "-C#{@tempdir}") + output.should == @tempdir + end + it 'changes the PWD when using -e' do output = ruby_exe(nil, options: "-C #{@tempdir} -e 'print Dir.pwd'") output.should == @tempdir diff --git a/spec/ruby/core/array/pack/buffer_spec.rb b/spec/ruby/core/array/pack/buffer_spec.rb index b3bba76cdc..f2dc3e1930 100644 --- a/spec/ruby/core/array/pack/buffer_spec.rb +++ b/spec/ruby/core/array/pack/buffer_spec.rb @@ -3,7 +3,7 @@ require_relative '../../../spec_helper' ruby_version_is '2.4' do - describe "Aray#pack with `buffer` option" do + describe "Array#pack with :buffer option" do it "returns specified buffer" do n = [ 65, 66, 67 ] buffer = " "*3 @@ -36,7 +36,7 @@ ruby_version_is '2.4' do n.pack("@3ccc", buffer: buffer).should == "123ABC" end - it "fills the gap with \0 if buffer content is shorter than offset" do + it "fills the gap with \\0 if buffer content is shorter than offset" do n = [ 65, 66, 67 ] buffer = "123" n.pack("@6ccc", buffer: buffer).should == "123\0\0\0ABC" diff --git a/spec/ruby/core/basicobject/instance_eval_spec.rb b/spec/ruby/core/basicobject/instance_eval_spec.rb index fc3af6fa83..24b4d6dc69 100644 --- a/spec/ruby/core/basicobject/instance_eval_spec.rb +++ b/spec/ruby/core/basicobject/instance_eval_spec.rb @@ -177,4 +177,12 @@ end end err.backtrace.first.split(":")[0..1].should == ["b_file", "-98"] end + + it "has access to the caller's local variables" do + x = nil + + instance_eval "x = :value" + + x.should == :value + end end diff --git a/spec/ruby/core/exception/dup_spec.rb b/spec/ruby/core/exception/dup_spec.rb new file mode 100644 index 0000000000..f5a067ed2f --- /dev/null +++ b/spec/ruby/core/exception/dup_spec.rb @@ -0,0 +1,61 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/common' + +describe "Exception#dup" do + before :each do + @obj = ExceptionSpecs::InitializeException.new("my exception") + end + + it "calls #initialize_copy on the new instance" do + dup = @obj.dup + ScratchPad.recorded.should_not == @obj.object_id + ScratchPad.recorded.should == dup.object_id + end + + it "copies instance variables" do + dup = @obj.dup + dup.ivar.should == 1 + end + + it "does not copy singleton methods" do + def @obj.special() :the_one end + dup = @obj.dup + lambda { dup.special }.should raise_error(NameError) + end + + it "does not copy modules included in the singleton class" do + class << @obj + include ExceptionSpecs::ExceptionModule + end + + dup = @obj.dup + lambda { dup.repr }.should raise_error(NameError) + end + + it "does not copy constants defined in the singleton class" do + class << @obj + CLONE = :clone + end + + dup = @obj.dup + lambda { class << dup; CLONE; end }.should raise_error(NameError) + end + + it "does copy the message" do + @obj.dup.message.should == @obj.message + end + + it "does copy the backtrace" do + begin + # Explicitly raise so a backtrace is associated with the exception. + # It's tempting to call `set_backtrace` instead, but that complicates + # the test because it might affect other state (e.g., instance variables) + # on some implementations. + raise ExceptionSpecs::InitializeException.new("my exception") + rescue => e + @obj = e + end + + @obj.dup.backtrace.should == @obj.backtrace + end +end \ No newline at end of file diff --git a/spec/ruby/core/exception/fixtures/common.rb b/spec/ruby/core/exception/fixtures/common.rb index 9ddabace11..0ffb3ed855 100644 --- a/spec/ruby/core/exception/fixtures/common.rb +++ b/spec/ruby/core/exception/fixtures/common.rb @@ -46,6 +46,26 @@ module ExceptionSpecs "" end end + + class InitializeException < StandardError + attr_reader :ivar + + def initialize(message = nil) + super + @ivar = 1 + end + + def initialize_copy(other) + super + ScratchPad.record object_id + end + end + + module ExceptionModule + def repr + 1 + end + end end module NoMethodErrorSpecs diff --git a/spec/ruby/core/file/expand_path_spec.rb b/spec/ruby/core/file/expand_path_spec.rb index 9d08cbb02b..99279aec85 100644 --- a/spec/ruby/core/file/expand_path_spec.rb +++ b/spec/ruby/core/file/expand_path_spec.rb @@ -109,8 +109,8 @@ describe "File.expand_path" do File.expand_path(Dir.pwd).should == Dir.pwd File.expand_path('~/').should == @home File.expand_path('~/..badfilename').should == "#{@home}/..badfilename" - File.expand_path('..').should == Dir.pwd.split('/')[0...-1].join("/") File.expand_path('~/a','~/b').should == "#{@home}/a" + File.expand_path('..').should == File.dirname(Dir.pwd) end it "does not replace multiple '/' at the beginning of the path" do diff --git a/spec/ruby/core/file/fixtures/file_types.rb b/spec/ruby/core/file/fixtures/file_types.rb index 397cac9b6b..a36817fb4e 100644 --- a/spec/ruby/core/file/fixtures/file_types.rb +++ b/spec/ruby/core/file/fixtures/file_types.rb @@ -5,17 +5,11 @@ module FileSpecs @file = tmp("test.txt") @dir = Dir.pwd @fifo = tmp("test_fifo") + @link = tmp("test_link") platform_is_not :windows do - @block = `find /dev /devices -type b 2> /dev/null`.split("\n").first - @char = `{ tty || find /dev /devices -type c; } 2> /dev/null`.split("\n").last - - %w[/dev /usr/bin /usr/local/bin].each do |dir| - links = `find #{dir} -type l 2> /dev/null`.split("\n") - next if links.empty? - @link = links.first - break - end + @block = `find /dev /devices -type b 2>/dev/null`.split("\n").first + @char = `{ tty || find /dev /devices -type c; } 2>/dev/null`.split("\n").last end @configured = true @@ -32,24 +26,29 @@ module FileSpecs yield @dir end - # TODO: need a platform-independent helper here def self.fifo - system "mkfifo #{@fifo} 2> /dev/null" + File.mkfifo(@fifo) yield @fifo ensure rm_r @fifo end def self.block_device + raise "Could not find a block device" unless @block yield @block end def self.character_device + raise "Could not find a character device" unless @char yield @char end def self.symlink + touch(@file) + File.symlink(@file, @link) yield @link + ensure + rm_r @file, @link end def self.socket @@ -57,8 +56,11 @@ module FileSpecs name = tmp("ftype_socket.socket") rm_r name socket = UNIXServer.new name - yield name - socket.close - rm_r name + begin + yield name + ensure + socket.close + rm_r name + end end end diff --git a/spec/ruby/core/file/open_spec.rb b/spec/ruby/core/file/open_spec.rb index 6ccac75a9a..a22a856dd9 100644 --- a/spec/ruby/core/file/open_spec.rb +++ b/spec/ruby/core/file/open_spec.rb @@ -604,7 +604,7 @@ describe "File.open" do describe "on a FIFO" do before :each do @fifo = tmp("File_open_fifo") - system "mkfifo #{@fifo}" + File.mkfifo(@fifo) end after :each do diff --git a/spec/ruby/core/file/pipe_spec.rb b/spec/ruby/core/file/pipe_spec.rb index 64e6fce09a..01d72dbe85 100644 --- a/spec/ruby/core/file/pipe_spec.rb +++ b/spec/ruby/core/file/pipe_spec.rb @@ -22,7 +22,7 @@ describe "File.pipe?" do platform_is_not :windows do it "returns true if the file is a pipe" do filename = tmp("i_am_a_pipe") - system "mkfifo #{filename}" + File.mkfifo(filename) File.pipe?(filename).should == true diff --git a/spec/ruby/core/file/stat/pipe_spec.rb b/spec/ruby/core/file/stat/pipe_spec.rb index 027b40eaaa..7abb6c742a 100644 --- a/spec/ruby/core/file/stat/pipe_spec.rb +++ b/spec/ruby/core/file/stat/pipe_spec.rb @@ -20,7 +20,7 @@ describe "File::Stat#pipe?" do platform_is_not :windows do it "returns true if the file is a pipe" do filename = tmp("i_am_a_pipe") - system "mkfifo #{filename}" + File.mkfifo(filename) st = File.stat(filename) st.pipe?.should == true diff --git a/spec/ruby/core/integer/dup_spec.rb b/spec/ruby/core/integer/dup_spec.rb new file mode 100644 index 0000000000..214367f0b4 --- /dev/null +++ b/spec/ruby/core/integer/dup_spec.rb @@ -0,0 +1,15 @@ +require_relative '../../spec_helper' + +ruby_version_is '2.4' do + describe "Integer#dup" do + it "returns self for small integers" do + integer = 1_000 + integer.dup.should equal(integer) + end + + it "returns self for large integers" do + integer = 4_611_686_018_427_387_905 + integer.dup.should equal(integer) + end + end +end diff --git a/spec/ruby/core/io/write_spec.rb b/spec/ruby/core/io/write_spec.rb index 1c48a42584..e6db3351db 100644 --- a/spec/ruby/core/io/write_spec.rb +++ b/spec/ruby/core/io/write_spec.rb @@ -103,7 +103,7 @@ describe "IO.write" do describe "on a FIFO" do before :each do @fifo = tmp("File_open_fifo") - system "mkfifo #{@fifo}" + File.mkfifo(@fifo) end after :each do diff --git a/spec/ruby/core/marshal/dump_spec.rb b/spec/ruby/core/marshal/dump_spec.rb index 7718c118ef..f214abc31d 100644 --- a/spec/ruby/core/marshal/dump_spec.rb +++ b/spec/ruby/core/marshal/dump_spec.rb @@ -539,6 +539,16 @@ describe "Marshal.dump" do end + describe "when passed a StringIO" do + + it "should raise an error" do + require "stringio" + + lambda { Marshal.dump(StringIO.new) }.should raise_error(TypeError) + end + + end + it "raises a TypeError if marshalling a Method instance" do lambda { Marshal.dump(Marshal.method(:dump)) }.should raise_error(TypeError) end diff --git a/spec/ruby/core/string/capitalize_spec.rb b/spec/ruby/core/string/capitalize_spec.rb index e279f7d061..10f9ab00a1 100644 --- a/spec/ruby/core/string/capitalize_spec.rb +++ b/spec/ruby/core/string/capitalize_spec.rb @@ -26,8 +26,65 @@ describe "String#capitalize" do end ruby_version_is '2.4' do - it "works for all of Unicode" do - "äöü".capitalize.should == "Äöü" + describe "full Unicode case mapping" do + it "works for all of Unicode with no option" do + "äöÜ".capitalize.should == "Äöü" + end + + it "only capitalizes the first resulting character when upcasing a character produces a multi-character sequence" do + "ß".capitalize.should == "Ss" + end + + it "updates string metadata" do + capitalized = "ßeT".capitalize + + capitalized.should == "Sset" + capitalized.size.should == 4 + capitalized.bytesize.should == 4 + capitalized.ascii_only?.should be_true + end + end + + describe "ASCII-only case mapping" do + it "does not capitalize non-ASCII characters" do + "ßet".capitalize(:ascii).should == "ßet" + end + end + + describe "full Unicode case mapping adapted for Turkic languages" do + it "capitalizes ASCII characters according to Turkic semantics" do + "iSa".capitalize(:turkic).should == "İsa" + end + + it "allows Lithuanian as an extra option" do + "iSa".capitalize(:turkic, :lithuanian).should == "İsa" + end + + it "does not allow any other additional option" do + lambda { "iSa".capitalize(:turkic, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + "iß".capitalize(:lithuanian).should == "Iß" + end + + it "allows Turkic as an extra option (and applies Turkic semantics)" do + "iß".capitalize(:lithuanian, :turkic).should == "İß" + end + + it "does not allow any other additional option" do + lambda { "iß".capitalize(:lithuanian, :ascii) }.should raise_error(ArgumentError) + end + end + + it "does not allow the :fold option for upcasing" do + lambda { "abc".capitalize(:fold) }.should raise_error(ArgumentError) + end + + it "does not allow invalid options" do + lambda { "abc".capitalize(:invalid_option) }.should raise_error(ArgumentError) end end @@ -45,10 +102,80 @@ describe "String#capitalize!" do end ruby_version_is '2.4' do - it "capitalizes self in place for all of Unicode" do - a = "äöü" - a.capitalize!.should equal(a) - a.should == "Äöü" + describe "full Unicode case mapping" do + it "modifies self in place for all of Unicode with no option" do + a = "äöÜ" + a.capitalize! + a.should == "Äöü" + end + + it "only capitalizes the first resulting character when upcasing a character produces a multi-character sequence" do + a = "ß" + a.capitalize! + a.should == "Ss" + end + + it "updates string metadata" do + capitalized = "ßeT" + capitalized.capitalize! + + capitalized.should == "Sset" + capitalized.size.should == 4 + capitalized.bytesize.should == 4 + capitalized.ascii_only?.should be_true + end + end + + describe "modifies self in place for ASCII-only case mapping" do + it "does not capitalize non-ASCII characters" do + a = "ßet" + a.capitalize!(:ascii) + a.should == "ßet" + end + end + + describe "modifies self in place for full Unicode case mapping adapted for Turkic languages" do + it "capitalizes ASCII characters according to Turkic semantics" do + a = "iSa" + a.capitalize!(:turkic) + a.should == "İsa" + end + + it "allows Lithuanian as an extra option" do + a = "iSa" + a.capitalize!(:turkic, :lithuanian) + a.should == "İsa" + end + + it "does not allow any other additional option" do + lambda { a = "iSa"; a.capitalize!(:turkic, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "modifies self in place for full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + a = "iß" + a.capitalize!(:lithuanian) + a.should == "Iß" + end + + it "allows Turkic as an extra option (and applies Turkic semantics)" do + a = "iß" + a.capitalize!(:lithuanian, :turkic) + a.should == "İß" + end + + it "does not allow any other additional option" do + lambda { a = "iß"; a.capitalize!(:lithuanian, :ascii) }.should raise_error(ArgumentError) + end + end + + it "does not allow the :fold option for upcasing" do + lambda { a = "abc"; a.capitalize!(:fold) }.should raise_error(ArgumentError) + end + + it "does not allow invalid options" do + lambda { a = "abc"; a.capitalize!(:invalid_option) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/string/casecmp_spec.rb b/spec/ruby/core/string/casecmp_spec.rb index 9e89e1314d..87be999964 100644 --- a/spec/ruby/core/string/casecmp_spec.rb +++ b/spec/ruby/core/string/casecmp_spec.rb @@ -37,6 +37,10 @@ describe "String#casecmp independent of case" do end end + it "returns nil if incompatible encodings" do + "あれ".casecmp("れ".encode(Encoding::EUC_JP)).should be_nil + end + describe "in UTF-8 mode" do describe "for non-ASCII characters" do before :each do @@ -96,6 +100,12 @@ describe "String#casecmp independent of case" do it "returns 1 when numerically greater than other" do @lower_a_tilde.casecmp(@upper_a_tilde).should == 1 end + + ruby_version_is "2.4" do + it "does not case fold" do + "ß".casecmp("ss").should == 1 + end + end end describe "when comparing a subclass instance" do @@ -138,6 +148,10 @@ ruby_version_is "2.4" do "abc".casecmp?(other).should == true end + it "returns nil if incompatible encodings" do + "あれ".casecmp?("れ".encode(Encoding::EUC_JP)).should be_nil + end + describe 'for UNICODE characters' do it 'returns true when downcase(:fold) on unicode' do 'äöü'.casecmp?('ÄÖÜ').should == true @@ -181,6 +195,12 @@ ruby_version_is "2.4" do end end + ruby_version_is "2.4" do + it "case folds" do + "ß".casecmp?("ss").should be_true + end + end + ruby_version_is "2.4" ... "2.5" do it "raises a TypeError if other can't be converted to a string" do lambda { "abc".casecmp?(mock('abc')) }.should raise_error(TypeError) diff --git a/spec/ruby/core/string/downcase_spec.rb b/spec/ruby/core/string/downcase_spec.rb index 30116db5e6..9fb93902b1 100644 --- a/spec/ruby/core/string/downcase_spec.rb +++ b/spec/ruby/core/string/downcase_spec.rb @@ -23,8 +23,64 @@ describe "String#downcase" do end ruby_version_is '2.4' do - it "works for all of Unicode" do - "ÄÖÜ".downcase.should == "äöü" + describe "full Unicode case mapping" do + it "works for all of Unicode with no option" do + "ÄÖÜ".downcase.should == "äöü" + end + + it "updates string metadata" do + downcased = "\u{212A}ING".downcase + + downcased.should == "king" + downcased.size.should == 4 + downcased.bytesize.should == 4 + downcased.ascii_only?.should be_true + end + end + + describe "ASCII-only case mapping" do + it "does not downcase non-ASCII characters" do + "CÅR".downcase(:ascii).should == "cÅr" + end + end + + describe "full Unicode case mapping adapted for Turkic languages" do + it "downcases characters according to Turkic semantics" do + "İ".downcase(:turkic).should == "i" + end + + it "allows Lithuanian as an extra option" do + "İ".downcase(:turkic, :lithuanian).should == "i" + end + + it "does not allow any other additional option" do + lambda { "İ".downcase(:turkic, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + "İS".downcase(:lithuanian).should == "i\u{307}s" + end + + it "allows Turkic as an extra option (and applies Turkic semantics)" do + "İS".downcase(:lithuanian, :turkic).should == "is" + end + + it "does not allow any other additional option" do + lambda { "İS".downcase(:lithuanian, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "case folding" do + it "case folds special characters" do + "ß".downcase.should == "ß" + "ß".downcase(:fold).should == "ss" + end + end + + it "does not allow invalid options" do + lambda { "ABC".downcase(:invalid_option) }.should raise_error(ArgumentError) end end @@ -47,10 +103,81 @@ describe "String#downcase!" do end ruby_version_is '2.4' do - it "modifies self in place for all of Unicode" do - a = "ÄÖÜ" - a.downcase!.should equal(a) - a.should == "äöü" + describe "full Unicode case mapping" do + it "modifies self in place for all of Unicode with no option" do + a = "ÄÖÜ" + a.downcase! + a.should == "äöü" + end + + it "updates string metadata" do + downcased = "\u{212A}ING" + downcased.downcase! + + downcased.should == "king" + downcased.size.should == 4 + downcased.bytesize.should == 4 + downcased.ascii_only?.should be_true + end + end + + describe "ASCII-only case mapping" do + it "does not downcase non-ASCII characters" do + a = "CÅR" + a.downcase!(:ascii) + a.should == "cÅr" + end + end + + describe "full Unicode case mapping adapted for Turkic languages" do + it "downcases characters according to Turkic semantics" do + a = "İ" + a.downcase!(:turkic) + a.should == "i" + end + + it "allows Lithuanian as an extra option" do + a = "İ" + a.downcase!(:turkic, :lithuanian) + a.should == "i" + end + + it "does not allow any other additional option" do + lambda { a = "İ"; a.downcase!(:turkic, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + a = "İS" + a.downcase!(:lithuanian) + a.should == "i\u{307}s" + end + + it "allows Turkic as an extra option (and applies Turkic semantics)" do + a = "İS" + a.downcase!(:lithuanian, :turkic) + a.should == "is" + end + + it "does not allow any other additional option" do + lambda { a = "İS"; a.downcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "case folding" do + it "case folds special characters" do + a = "ß" + a.downcase! + a.should == "ß" + + a.downcase!(:fold) + a.should == "ss" + end + end + + it "does not allow invalid options" do + lambda { a = "ABC"; a.downcase!(:invalid_option) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/string/swapcase_spec.rb b/spec/ruby/core/string/swapcase_spec.rb index 6d00906aad..bb89dee48f 100644 --- a/spec/ruby/core/string/swapcase_spec.rb +++ b/spec/ruby/core/string/swapcase_spec.rb @@ -23,8 +23,61 @@ describe "String#swapcase" do end ruby_version_is '2.4' do - it "works for all of Unicode" do - "äÖü".swapcase.should == "ÄöÜ" + describe "full Unicode case mapping" do + it "works for all of Unicode with no option" do + "äÖü".swapcase.should == "ÄöÜ" + end + + it "updates string metadata" do + swapcased = "Aßet".swapcase + + swapcased.should == "aSSET" + swapcased.size.should == 5 + swapcased.bytesize.should == 5 + swapcased.ascii_only?.should be_true + end + end + + describe "ASCII-only case mapping" do + it "does not swapcase non-ASCII characters" do + "aßet".swapcase(:ascii).should == "AßET" + end + end + + describe "full Unicode case mapping adapted for Turkic languages" do + it "swaps case of ASCII characters according to Turkic semantics" do + "aiS".swapcase(:turkic).should == "Aİs" + end + + it "allows Lithuanian as an extra option" do + "aiS".swapcase(:turkic, :lithuanian).should == "Aİs" + end + + it "does not allow any other additional option" do + lambda { "aiS".swapcase(:turkic, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + "Iß".swapcase(:lithuanian).should == "iSS" + end + + it "allows Turkic as an extra option (and applies Turkic semantics)" do + "iS".swapcase(:lithuanian, :turkic).should == "İs" + end + + it "does not allow any other additional option" do + lambda { "aiS".swapcase(:lithuanian, :ascii) }.should raise_error(ArgumentError) + end + end + + it "does not allow the :fold option for upcasing" do + lambda { "abc".swapcase(:fold) }.should raise_error(ArgumentError) + end + + it "does not allow invalid options" do + lambda { "abc".swapcase(:invalid_option) }.should raise_error(ArgumentError) end end @@ -42,10 +95,74 @@ describe "String#swapcase!" do end ruby_version_is '2.4' do - it "modifies self in place for all of Unicode" do - a = "äÖü" - a.swapcase!.should equal(a) - a.should == "ÄöÜ" + describe "full Unicode case mapping" do + it "modifies self in place for all of Unicode with no option" do + a = "äÖü" + a.swapcase! + a.should == "ÄöÜ" + end + + it "updates string metadata" do + swapcased = "Aßet" + swapcased.swapcase! + + swapcased.should == "aSSET" + swapcased.size.should == 5 + swapcased.bytesize.should == 5 + swapcased.ascii_only?.should be_true + end + end + + describe "modifies self in place for ASCII-only case mapping" do + it "does not swapcase non-ASCII characters" do + a = "aßet" + a.swapcase!(:ascii) + a.should == "AßET" + end + end + + describe "modifies self in place for full Unicode case mapping adapted for Turkic languages" do + it "swaps case of ASCII characters according to Turkic semantics" do + a = "aiS" + a.swapcase!(:turkic) + a.should == "Aİs" + end + + it "allows Lithuanian as an extra option" do + a = "aiS" + a.swapcase!(:turkic, :lithuanian) + a.should == "Aİs" + end + + it "does not allow any other additional option" do + lambda { a = "aiS"; a.swapcase!(:turkic, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + a = "Iß" + a.swapcase!(:lithuanian) + a.should == "iSS" + end + + it "allows Turkic as an extra option (and applies Turkic semantics)" do + a = "iS" + a.swapcase!(:lithuanian, :turkic) + a.should == "İs" + end + + it "does not allow any other additional option" do + lambda { a = "aiS"; a.swapcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError) + end + end + + it "does not allow the :fold option for upcasing" do + lambda { a = "abc"; a.swapcase!(:fold) }.should raise_error(ArgumentError) + end + + it "does not allow invalid options" do + lambda { a = "abc"; a.swapcase!(:invalid_option) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/string/upcase_spec.rb b/spec/ruby/core/string/upcase_spec.rb index 001cf1f495..347f567be9 100644 --- a/spec/ruby/core/string/upcase_spec.rb +++ b/spec/ruby/core/string/upcase_spec.rb @@ -23,8 +23,61 @@ describe "String#upcase" do end ruby_version_is '2.4' do - it "works for all of Unicode" do - "äöü".upcase.should == "ÄÖÜ" + describe "full Unicode case mapping" do + it "works for all of Unicode with no option" do + "äöü".upcase.should == "ÄÖÜ" + end + + it "updates string metadata" do + upcased = "aßet".upcase + + upcased.should == "ASSET" + upcased.size.should == 5 + upcased.bytesize.should == 5 + upcased.ascii_only?.should be_true + end + end + + describe "ASCII-only case mapping" do + it "does not upcase non-ASCII characters" do + "aßet".upcase(:ascii).should == "AßET" + end + end + + describe "full Unicode case mapping adapted for Turkic languages" do + it "upcases ASCII characters according to Turkic semantics" do + "i".upcase(:turkic).should == "İ" + end + + it "allows Lithuanian as an extra option" do + "i".upcase(:turkic, :lithuanian).should == "İ" + end + + it "does not allow any other additional option" do + lambda { "i".upcase(:turkic, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + "iß".upcase(:lithuanian).should == "ISS" + end + + it "allows Turkic as an extra option (and applies Turkic semantics)" do + "iß".upcase(:lithuanian, :turkic).should == "İSS" + end + + it "does not allow any other additional option" do + lambda { "iß".upcase(:lithuanian, :ascii) }.should raise_error(ArgumentError) + end + end + + it "does not allow the :fold option for upcasing" do + lambda { "abc".upcase(:fold) }.should raise_error(ArgumentError) + end + + it "does not allow invalid options" do + lambda { "abc".upcase(:invalid_option) }.should raise_error(ArgumentError) end end @@ -46,12 +99,75 @@ describe "String#upcase!" do a.should == "HELLO" end - ruby_version_is '2.4' do - it "modifies self in place for all of Unicode" do - a = "äöü" - a.upcase!.should equal(a) - a.should == "ÄÖÜ" + describe "full Unicode case mapping" do + it "modifies self in place for all of Unicode with no option" do + a = "äöü" + a.upcase! + a.should == "ÄÖÜ" + end + + it "updates string metadata for self" do + upcased = "aßet" + upcased.upcase! + + upcased.should == "ASSET" + upcased.size.should == 5 + upcased.bytesize.should == 5 + upcased.ascii_only?.should be_true + end + end + + describe "modifies self in place for ASCII-only case mapping" do + it "does not upcase non-ASCII characters" do + a = "aßet" + a.upcase!(:ascii) + a.should == "AßET" + end + end + + describe "modifies self in place for full Unicode case mapping adapted for Turkic languages" do + it "upcases ASCII characters according to Turkic semantics" do + a = "i" + a.upcase!(:turkic) + a.should == "İ" + end + + it "allows Lithuanian as an extra option" do + a = "i" + a.upcase!(:turkic, :lithuanian) + a.should == "İ" + end + + it "does not allow any other additional option" do + lambda { a = "i"; a.upcase!(:turkic, :ascii) }.should raise_error(ArgumentError) + end + end + + describe "modifies self in place for full Unicode case mapping adapted for Lithuanian" do + it "currently works the same as full Unicode case mapping" do + a = "iß" + a.upcase!(:lithuanian) + a.should == "ISS" + end + + it "allows Turkic as an extra option (and applies Turkic semantics)" do + a = "iß" + a.upcase!(:lithuanian, :turkic) + a.should == "İSS" + end + + it "does not allow any other additional option" do + lambda { a = "iß"; a.upcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError) + end + end + + it "does not allow the :fold option for upcasing" do + lambda { a = "abc"; a.upcase!(:fold) }.should raise_error(ArgumentError) + end + + it "does not allow invalid options" do + lambda { a = "abc"; a.upcase!(:invalid_option) }.should raise_error(ArgumentError) end end diff --git a/spec/ruby/core/time/comparison_spec.rb b/spec/ruby/core/time/comparison_spec.rb index 44d8778da4..5b53c9fb50 100644 --- a/spec/ruby/core/time/comparison_spec.rb +++ b/spec/ruby/core/time/comparison_spec.rb @@ -45,6 +45,16 @@ describe "Time#<=>" do (Time.at(100, 0) <=> Time.at(100, Rational(1,1000))).should == -1 end + it "returns nil when compared to an Integer because Time does not respond to #coerce" do + time = Time.at(1) + time.respond_to?(:coerce).should == false + time.should_receive(:respond_to?).exactly(2).and_return(false) + -> { + (time <=> 2).should == nil + (2 <=> time).should == nil + }.should_not complain + end + describe "given a non-Time argument" do it "returns nil if argument <=> self returns nil" do t = Time.now diff --git a/spec/ruby/language/send_spec.rb b/spec/ruby/language/send_spec.rb index a5b5ec8aa6..8bef80173c 100644 --- a/spec/ruby/language/send_spec.rb +++ b/spec/ruby/language/send_spec.rb @@ -198,10 +198,18 @@ describe "Invoking a method" do lambda { no_such_method }.should raise_error NameError end + it "should omit the method_missing call from the backtrace for NameError" do + lambda { no_such_method }.should raise_error { |e| e.backtrace.first.should_not include("method_missing") } + end + it "raises NoMethodError if invoked as an unambiguous method call" do lambda { no_such_method() }.should raise_error NoMethodError lambda { no_such_method(1,2,3) }.should raise_error NoMethodError end + + it "should omit the method_missing call from the backtrace for NoMethodError" do + lambda { no_such_method() }.should raise_error { |e| e.backtrace.first.should_not include("method_missing") } + end end end diff --git a/spec/ruby/optional/capi/class_spec.rb b/spec/ruby/optional/capi/class_spec.rb index acfb8e3728..bab4f52831 100644 --- a/spec/ruby/optional/capi/class_spec.rb +++ b/spec/ruby/optional/capi/class_spec.rb @@ -95,6 +95,12 @@ describe "C-API Class function" do obj.call_super_method.should == :super_method end + it "calls the method in the superclass with the correct self" do + @s.define_call_super_method CApiClassSpecs::SubSelf, "call_super_method" + obj = CApiClassSpecs::SubSelf.new + obj.call_super_method.should equal obj + end + it "calls the method in the superclass through two native levels" do @s.define_call_super_method CApiClassSpecs::Sub, "call_super_method" @s.define_call_super_method CApiClassSpecs::SubSub, "call_super_method" diff --git a/spec/ruby/optional/capi/ext/string_spec.c b/spec/ruby/optional/capi/ext/string_spec.c index 929d69f9e5..1b9c11a149 100644 --- a/spec/ruby/optional/capi/ext/string_spec.c +++ b/spec/ruby/optional/capi/ext/string_spec.c @@ -12,6 +12,19 @@ extern "C" { #endif +/* Make sure the RSTRING_PTR and the bytes are in native memory. + * On TruffleRuby RSTRING_PTR and the bytes remain in managed memory + * until they must be written to native memory. + * In some specs we want to test using the native memory. */ +char* NATIVE_RSTRING_PTR(VALUE str) { + char* ptr = RSTRING_PTR(str); + char** native = malloc(sizeof(char*)); + *native = ptr; + ptr = *native; + free(native); + return ptr; +} + #ifdef HAVE_RB_CSTR2INUM VALUE string_spec_rb_cstr2inum(VALUE self, VALUE str, VALUE inum) { int num = FIX2INT(inum); @@ -65,6 +78,10 @@ VALUE string_spec_rb_str_buf_new(VALUE self, VALUE len, VALUE str) { return buf; } + +VALUE string_spec_rb_str_capacity(VALUE self, VALUE str) { + return SIZET2NUM(rb_str_capacity(str)); +} #endif #ifdef HAVE_RB_STR_BUF_NEW2 @@ -182,6 +199,10 @@ VALUE string_spec_rb_str_new(VALUE self, VALUE str, VALUE len) { return rb_str_new(RSTRING_PTR(str), FIX2INT(len)); } +VALUE string_spec_rb_str_new_native(VALUE self, VALUE str, VALUE len) { + return rb_str_new(NATIVE_RSTRING_PTR(str), FIX2INT(len)); +} + VALUE string_spec_rb_str_new_offset(VALUE self, VALUE str, VALUE offset, VALUE len) { return rb_str_new(RSTRING_PTR(str) + FIX2INT(offset), FIX2INT(len)); } @@ -358,6 +379,11 @@ VALUE string_spec_RSTRING_PTR_assign(VALUE self, VALUE str, VALUE chr) { return Qnil; } +VALUE string_spec_RSTRING_PTR_set(VALUE self, VALUE str, VALUE i, VALUE chr) { + RSTRING_PTR(str)[FIX2INT(i)] = (char) FIX2INT(chr); + return str; +} + VALUE string_spec_RSTRING_PTR_after_funcall(VALUE self, VALUE str, VALUE cb) { /* Silence gcc 4.3.2 warning about computed value not used */ if(RSTRING_PTR(str)) { /* force it out */ @@ -366,6 +392,19 @@ VALUE string_spec_RSTRING_PTR_after_funcall(VALUE self, VALUE str, VALUE cb) { return rb_str_new2(RSTRING_PTR(str)); } + +VALUE string_spec_RSTRING_PTR_after_yield(VALUE self, VALUE str) { + char* ptr = NATIVE_RSTRING_PTR(str); + long len = RSTRING_LEN(str); + VALUE from_rstring_ptr; + + ptr[0] = '1'; + rb_yield(str); + ptr[2] = '2'; + + from_rstring_ptr = rb_str_new(ptr, len); + return from_rstring_ptr; +} #endif #ifdef HAVE_STRINGVALUE @@ -480,6 +519,7 @@ void Init_string_spec(void) { #ifdef HAVE_RB_STR_BUF_NEW rb_define_method(cls, "rb_str_buf_new", string_spec_rb_str_buf_new, 2); + rb_define_method(cls, "rb_str_capacity", string_spec_rb_str_capacity, 1); #endif #ifdef HAVE_RB_STR_BUF_NEW2 @@ -540,6 +580,7 @@ void Init_string_spec(void) { #ifdef HAVE_RB_STR_NEW rb_define_method(cls, "rb_str_new", string_spec_rb_str_new, 2); + rb_define_method(cls, "rb_str_new_native", string_spec_rb_str_new_native, 2); rb_define_method(cls, "rb_str_new_offset", string_spec_rb_str_new_offset, 3); #endif @@ -643,8 +684,9 @@ void Init_string_spec(void) { #ifdef HAVE_RSTRING_PTR rb_define_method(cls, "RSTRING_PTR_iterate", string_spec_RSTRING_PTR_iterate, 1); rb_define_method(cls, "RSTRING_PTR_assign", string_spec_RSTRING_PTR_assign, 2); - rb_define_method(cls, "RSTRING_PTR_after_funcall", - string_spec_RSTRING_PTR_after_funcall, 2); + rb_define_method(cls, "RSTRING_PTR_set", string_spec_RSTRING_PTR_set, 3); + rb_define_method(cls, "RSTRING_PTR_after_funcall", string_spec_RSTRING_PTR_after_funcall, 2); + rb_define_method(cls, "RSTRING_PTR_after_yield", string_spec_RSTRING_PTR_after_yield, 1); #endif #ifdef HAVE_STRINGVALUE diff --git a/spec/ruby/optional/capi/fixtures/class.rb b/spec/ruby/optional/capi/fixtures/class.rb index de824b3ab0..dbb0b69967 100644 --- a/spec/ruby/optional/capi/fixtures/class.rb +++ b/spec/ruby/optional/capi/fixtures/class.rb @@ -68,10 +68,19 @@ class CApiClassSpecs class SubSub < Sub def call_super_method - :subclass_method + :subsubclass_method end end + class SuperSelf + def call_super_method + self + end + end + + class SubSelf < SuperSelf + end + class A C = 1 autoload :D, File.expand_path('../path_to_class.rb', __FILE__) diff --git a/spec/ruby/optional/capi/string_spec.rb b/spec/ruby/optional/capi/string_spec.rb index c042d5edad..cc6ac2e0cb 100644 --- a/spec/ruby/optional/capi/string_spec.rb +++ b/spec/ruby/optional/capi/string_spec.rb @@ -48,14 +48,43 @@ describe "C-API String function" do @s.rb_str_set_len(@str, 8).should == "abcde\x00gh" end + it "updates the byte size and character size" do + @s.rb_str_set_len(@str, 4) + @str.bytesize.should == 4 + @str.size.should == 4 + @str.should == "abcd" + end + it "updates the string's attributes visible in C code" do @s.rb_str_set_len_RSTRING_LEN(@str, 4).should == 4 end + + it "can reveal characters written from C with RSTRING_PTR" do + @s.rb_str_set_len(@str, 1) + @str.should == "a" + + @str.force_encoding(Encoding::UTF_8) + @s.RSTRING_PTR_set(@str, 1, 'B'.ord) + @s.RSTRING_PTR_set(@str, 2, 'C'.ord) + @s.rb_str_set_len(@str, 3) + + @str.bytesize.should == 3 + @str.should == "aBC" + end end describe "rb_str_buf_new" do it "returns the equivalent of an empty string" do - @s.rb_str_buf_new(10, nil).should == "" + buf = @s.rb_str_buf_new(10, nil) + buf.should == "" + buf.bytesize.should == 0 + buf.size.should == 0 + @s.RSTRING_LEN(buf).should == 0 + end + + it "returns a string with the given capacity" do + buf = @s.rb_str_buf_new(256, nil) + @s.rb_str_capacity(buf).should == 256 end it "returns a string that can be appended to" do @@ -83,6 +112,19 @@ describe "C-API String function" do str[0, 6].should == "abcd\x00f" @s.RSTRING_LEN(str).should == 8 end + + it "can be used as a general buffer and reveal characters with rb_str_set_len" do + str = @s.rb_str_buf_new(10, "abcdef") + + @s.RSTRING_PTR_set(str, 0, 195) + @s.RSTRING_PTR_set(str, 1, 169) + @s.rb_str_set_len(str, 2) + + str.force_encoding(Encoding::UTF_8) + str.bytesize.should == 2 + str.size.should == 1 + str.should == "é" + end end describe "rb_str_buf_new2" do @@ -93,6 +135,10 @@ describe "C-API String function" do end describe "rb_str_new" do + it "creates a new String with ASCII-8BIT Encoding" do + @s.rb_str_new("", 0).encoding.should == Encoding::ASCII_8BIT + end + it "returns a new string object from a char buffer of len characters" do @s.rb_str_new("hello", 3).should == "hel" end @@ -101,6 +147,11 @@ describe "C-API String function" do @s.rb_str_new("hello", 0).should == "" end + it "copy length bytes and does not stop at the first \\0 byte" do + @s.rb_str_new("he\x00llo", 6).should == "he\x00llo" + @s.rb_str_new_native("he\x00llo", 6).should == "he\x00llo" + end + it "returns a string from an offset char buffer" do @s.rb_str_new_offset("hello", 1, 3).should == "ell" end @@ -110,12 +161,6 @@ describe "C-API String function" do it_behaves_like :rb_str_new2, :rb_str_new2 end - describe "rb_str_new" do - it "creates a new String with ASCII-8BIT Encoding" do - @s.rb_str_new("", 0).encoding.should == Encoding::ASCII_8BIT - end - end - describe "rb_str_new_cstr" do it_behaves_like :rb_str_new2, :rb_str_new_cstr end @@ -402,7 +447,7 @@ describe "C-API String function" do it "allows changing the characters in the string" do str = "abc" - @s.RSTRING_PTR_assign(str, 65) + @s.RSTRING_PTR_assign(str, 'A'.ord) str.should == "AAA" end @@ -417,6 +462,13 @@ describe "C-API String function" do ret.should == str end + it "reflects changes from native memory and from String#setbyte in bounds" do + str = "abc" + from_rstring_ptr = @s.RSTRING_PTR_after_yield(str) { str.setbyte(1, 'B'.ord) } + from_rstring_ptr.should == "1B2" + str.should == "1B2" + end + it "returns a pointer to the contents of encoded pointer-sized string" do s = "70パク". encode(Encoding::UTF_16LE). -- cgit v1.2.3