diff options
Diffstat (limited to 'spec/ruby/core/string')
128 files changed, 2972 insertions, 2213 deletions
diff --git a/spec/ruby/core/string/allocate_spec.rb b/spec/ruby/core/string/allocate_spec.rb index 30d5f60594..00dadaf076 100644 --- a/spec/ruby/core/string/allocate_spec.rb +++ b/spec/ruby/core/string/allocate_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "String.allocate" do it "returns an instance of String" do str = String.allocate - str.should be_an_instance_of(String) + str.should.instance_of?(String) end it "returns a fully-formed String" do diff --git a/spec/ruby/core/string/append_as_bytes_spec.rb b/spec/ruby/core/string/append_as_bytes_spec.rb new file mode 100644 index 0000000000..feead64615 --- /dev/null +++ b/spec/ruby/core/string/append_as_bytes_spec.rb @@ -0,0 +1,60 @@ +require_relative '../../spec_helper' + +describe "String#append_bytes" do + ruby_version_is "3.4" do + it "doesn't allow to mutate frozen strings" do + str = "hello".freeze + -> { str.append_as_bytes("\xE2\x82") }.should.raise(FrozenError) + end + + it "allows creating broken strings in UTF8" do + str = +"hello" + str.append_as_bytes("\xE2\x82") + str.valid_encoding?.should == false + + str.append_as_bytes("\xAC") + str.valid_encoding?.should == true + end + + it "allows creating broken strings in UTF_32" do + str = "abc".encode(Encoding::UTF_32LE) + str.append_as_bytes("def") + str.encoding.should == Encoding::UTF_32LE + str.valid_encoding?.should == false + end + + it "never changes the receiver encoding" do + str = "".b + str.append_as_bytes("€") + str.encoding.should == Encoding::BINARY + end + + it "accepts variadic String or Integer arguments" do + str = "hello".b + str.append_as_bytes("\xE2\x82", 12, 43, "\xAC") + str.encoding.should == Encoding::BINARY + str.should == "hello\xE2\x82\f+\xAC".b + end + + it "truncates integers to the least significant byte" do + str = +"" + str.append_as_bytes(0x131, 0x232, 0x333, bignum_value, bignum_value(1)) + str.bytes.should == [0x31, 0x32, 0x33, 0, 1] + end + + it "wraps negative integers" do + str = "".b + str.append_as_bytes(-1, -bignum_value, -bignum_value(1)) + str.bytes.should == [0xFF, 0, 0xFF] + end + + it "only accepts strings or integers, and doesn't attempt to cast with #to_str or #to_int" do + to_str = mock("to_str") + to_str.should_not_receive(:to_str) + to_str.should_not_receive(:to_int) + + str = +"hello" + -> { str.append_as_bytes(to_str) }.should.raise(TypeError, "wrong argument type MockObject (expected String or Integer)") + end + end +end diff --git a/spec/ruby/core/string/append_spec.rb b/spec/ruby/core/string/append_spec.rb index 8497ce8262..e0f71b7c97 100644 --- a/spec/ruby/core/string/append_spec.rb +++ b/spec/ruby/core/string/append_spec.rb @@ -8,7 +8,7 @@ describe "String#<<" do it_behaves_like :string_concat_type_coercion, :<< it "raises an ArgumentError when given the incorrect number of arguments" do - -> { "hello".send(:<<) }.should raise_error(ArgumentError) - -> { "hello".send(:<<, "one", "two") }.should raise_error(ArgumentError) + -> { "hello".send(:<<) }.should.raise(ArgumentError) + -> { "hello".send(:<<, "one", "two") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/ascii_only_spec.rb b/spec/ruby/core/string/ascii_only_spec.rb index c7e02fd874..9af663beb8 100644 --- a/spec/ruby/core/string/ascii_only_spec.rb +++ b/spec/ruby/core/string/ascii_only_spec.rb @@ -7,18 +7,18 @@ describe "String#ascii_only?" do it "returns true if the encoding is UTF-8" do [ ["hello", true], ["hello".encode('UTF-8'), true], - ["hello".force_encoding('UTF-8'), true], + ["hello".dup.force_encoding('UTF-8'), true], ].should be_computed_by(:ascii_only?) end it "returns true if the encoding is US-ASCII" do - "hello".force_encoding(Encoding::US_ASCII).ascii_only?.should be_true - "hello".encode(Encoding::US_ASCII).ascii_only?.should be_true + "hello".dup.force_encoding(Encoding::US_ASCII).ascii_only?.should == true + "hello".encode(Encoding::US_ASCII).ascii_only?.should == true end it "returns true for all single-character UTF-8 Strings" do 0.upto(127) do |n| - n.chr.ascii_only?.should be_true + n.chr.ascii_only?.should == true end end end @@ -27,57 +27,56 @@ describe "String#ascii_only?" do it "returns false if the encoding is BINARY" do chr = 128.chr chr.encoding.should == Encoding::BINARY - chr.ascii_only?.should be_false + chr.ascii_only?.should == false end it "returns false if the String contains any non-ASCII characters" do [ ["\u{6666}", false], ["hello, \u{6666}", false], ["\u{6666}".encode('UTF-8'), false], - ["\u{6666}".force_encoding('UTF-8'), false], + ["\u{6666}".dup.force_encoding('UTF-8'), false], ].should be_computed_by(:ascii_only?) end it "returns false if the encoding is US-ASCII" do - [ ["\u{6666}".force_encoding(Encoding::US_ASCII), false], - ["hello, \u{6666}".force_encoding(Encoding::US_ASCII), false], + [ ["\u{6666}".dup.force_encoding(Encoding::US_ASCII), false], + ["hello, \u{6666}".dup.force_encoding(Encoding::US_ASCII), false], ].should be_computed_by(:ascii_only?) end end it "returns true for the empty String with an ASCII-compatible encoding" do - "".ascii_only?.should be_true - "".encode('UTF-8').ascii_only?.should be_true + "".ascii_only?.should == true + "".encode('UTF-8').ascii_only?.should == true end it "returns false for the empty String with a non-ASCII-compatible encoding" do - "".force_encoding('UTF-16LE').ascii_only?.should be_false - "".encode('UTF-16BE').ascii_only?.should be_false + "".dup.force_encoding('UTF-16LE').ascii_only?.should == false + "".encode('UTF-16BE').ascii_only?.should == false end it "returns false for a non-empty String with non-ASCII-compatible encoding" do - "\x78\x00".force_encoding("UTF-16LE").ascii_only?.should be_false + "\x78\x00".dup.force_encoding("UTF-16LE").ascii_only?.should == false end it "returns false when interpolating non ascii strings" do - base = "EU currency is" - base.force_encoding(Encoding::US_ASCII) + base = "EU currency is".dup.force_encoding(Encoding::US_ASCII) euro = "\u20AC" interp = "#{base} #{euro}" - euro.ascii_only?.should be_false - base.ascii_only?.should be_true - interp.ascii_only?.should be_false + euro.ascii_only?.should == false + base.ascii_only?.should == true + interp.ascii_only?.should == false end it "returns false after appending non ASCII characters to an empty String" do - ("" << "λ").ascii_only?.should be_false + ("".dup << "λ").ascii_only?.should == false end it "returns false when concatenating an ASCII and non-ASCII String" do - "".concat("λ").ascii_only?.should be_false + "".dup.concat("λ").ascii_only?.should == false end it "returns false when replacing an ASCII String with a non-ASCII String" do - "".replace("λ").ascii_only?.should be_false + "".dup.replace("λ").ascii_only?.should == false end end diff --git a/spec/ruby/core/string/b_spec.rb b/spec/ruby/core/string/b_spec.rb index 37c7994700..d181447709 100644 --- a/spec/ruby/core/string/b_spec.rb +++ b/spec/ruby/core/string/b_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' describe "String#b" do @@ -9,7 +10,7 @@ describe "String#b" do it "returns new string without modifying self" do str = "こんちには" - str.b.should_not equal(str) + str.b.should_not.equal?(str) str.should == "こんちには" end end diff --git a/spec/ruby/core/string/byteindex_spec.rb b/spec/ruby/core/string/byteindex_spec.rb index 7be0c7ec1e..f4c6408790 100644 --- a/spec/ruby/core/string/byteindex_spec.rb +++ b/spec/ruby/core/string/byteindex_spec.rb @@ -4,301 +4,295 @@ require_relative 'fixtures/classes' require_relative 'shared/byte_index_common.rb' describe "String#byteindex" do - ruby_version_is "3.2" do - it "calls #to_str to convert the first argument" do - char = mock("string index char") - char.should_receive(:to_str).and_return("b") - "abc".byteindex(char).should == 1 - end - - it "calls #to_int to convert the second argument" do - offset = mock("string index offset") - offset.should_receive(:to_int).and_return(1) - "abc".byteindex("c", offset).should == 2 - end + it "calls #to_str to convert the first argument" do + char = mock("string index char") + char.should_receive(:to_str).and_return("b") + "abc".byteindex(char).should == 1 + end - it "does not raise IndexError when byte offset is correct or on string boundary" do - "わ".byteindex("").should == 0 - "わ".byteindex("", 0).should == 0 - "わ".byteindex("", 3).should == 3 - end + it "calls #to_int to convert the second argument" do + offset = mock("string index offset") + offset.should_receive(:to_int).and_return(1) + "abc".byteindex("c", offset).should == 2 + end - it_behaves_like :byte_index_common, :byteindex + it "does not raise IndexError when byte offset is correct or on string boundary" do + "わ".byteindex("").should == 0 + "わ".byteindex("", 0).should == 0 + "わ".byteindex("", 3).should == 3 end + + it_behaves_like :byte_index_common, :byteindex end describe "String#byteindex with String" do - ruby_version_is "3.2" do - it "behaves the same as String#byteindex(char) for one-character strings" do - "blablabla hello cruel world...!".split("").uniq.each do |str| - chr = str[0] - str.byteindex(str).should == str.byteindex(chr) + it "behaves the same as String#byteindex(char) for one-character strings" do + "blablabla hello cruel world...!".split("").uniq.each do |str| + chr = str[0] + str.byteindex(str).should == str.byteindex(chr) - 0.upto(str.size + 1) do |start| - str.byteindex(str, start).should == str.byteindex(chr, start) - end + 0.upto(str.size + 1) do |start| + str.byteindex(str, start).should == str.byteindex(chr, start) + end - (-str.size - 1).upto(-1) do |start| - str.byteindex(str, start).should == str.byteindex(chr, start) - end + (-str.size - 1).upto(-1) do |start| + str.byteindex(str, start).should == str.byteindex(chr, start) end end + end - it "returns the byteindex of the first occurrence of the given substring" do - "blablabla".byteindex("").should == 0 - "blablabla".byteindex("b").should == 0 - "blablabla".byteindex("bla").should == 0 - "blablabla".byteindex("blabla").should == 0 - "blablabla".byteindex("blablabla").should == 0 - - "blablabla".byteindex("l").should == 1 - "blablabla".byteindex("la").should == 1 - "blablabla".byteindex("labla").should == 1 - "blablabla".byteindex("lablabla").should == 1 - - "blablabla".byteindex("a").should == 2 - "blablabla".byteindex("abla").should == 2 - "blablabla".byteindex("ablabla").should == 2 - end + it "returns the byteindex of the first occurrence of the given substring" do + "blablabla".byteindex("").should == 0 + "blablabla".byteindex("b").should == 0 + "blablabla".byteindex("bla").should == 0 + "blablabla".byteindex("blabla").should == 0 + "blablabla".byteindex("blablabla").should == 0 + + "blablabla".byteindex("l").should == 1 + "blablabla".byteindex("la").should == 1 + "blablabla".byteindex("labla").should == 1 + "blablabla".byteindex("lablabla").should == 1 + + "blablabla".byteindex("a").should == 2 + "blablabla".byteindex("abla").should == 2 + "blablabla".byteindex("ablabla").should == 2 + end - it "treats the offset as a byteindex" do - "aaaaa".byteindex("a", 0).should == 0 - "aaaaa".byteindex("a", 2).should == 2 - "aaaaa".byteindex("a", 4).should == 4 - end + it "treats the offset as a byteindex" do + "aaaaa".byteindex("a", 0).should == 0 + "aaaaa".byteindex("a", 2).should == 2 + "aaaaa".byteindex("a", 4).should == 4 + end - it "ignores string subclasses" do - "blablabla".byteindex(StringSpecs::MyString.new("bla")).should == 0 - StringSpecs::MyString.new("blablabla").byteindex("bla").should == 0 - StringSpecs::MyString.new("blablabla").byteindex(StringSpecs::MyString.new("bla")).should == 0 - end + it "ignores string subclasses" do + "blablabla".byteindex(StringSpecs::MyString.new("bla")).should == 0 + StringSpecs::MyString.new("blablabla").byteindex("bla").should == 0 + StringSpecs::MyString.new("blablabla").byteindex(StringSpecs::MyString.new("bla")).should == 0 + end - it "starts the search at the given offset" do - "blablabla".byteindex("bl", 0).should == 0 - "blablabla".byteindex("bl", 1).should == 3 - "blablabla".byteindex("bl", 2).should == 3 - "blablabla".byteindex("bl", 3).should == 3 - - "blablabla".byteindex("bla", 0).should == 0 - "blablabla".byteindex("bla", 1).should == 3 - "blablabla".byteindex("bla", 2).should == 3 - "blablabla".byteindex("bla", 3).should == 3 - - "blablabla".byteindex("blab", 0).should == 0 - "blablabla".byteindex("blab", 1).should == 3 - "blablabla".byteindex("blab", 2).should == 3 - "blablabla".byteindex("blab", 3).should == 3 - - "blablabla".byteindex("la", 1).should == 1 - "blablabla".byteindex("la", 2).should == 4 - "blablabla".byteindex("la", 3).should == 4 - "blablabla".byteindex("la", 4).should == 4 - - "blablabla".byteindex("lab", 1).should == 1 - "blablabla".byteindex("lab", 2).should == 4 - "blablabla".byteindex("lab", 3).should == 4 - "blablabla".byteindex("lab", 4).should == 4 - - "blablabla".byteindex("ab", 2).should == 2 - "blablabla".byteindex("ab", 3).should == 5 - "blablabla".byteindex("ab", 4).should == 5 - "blablabla".byteindex("ab", 5).should == 5 - - "blablabla".byteindex("", 0).should == 0 - "blablabla".byteindex("", 1).should == 1 - "blablabla".byteindex("", 2).should == 2 - "blablabla".byteindex("", 7).should == 7 - "blablabla".byteindex("", 8).should == 8 - "blablabla".byteindex("", 9).should == 9 - end + it "starts the search at the given offset" do + "blablabla".byteindex("bl", 0).should == 0 + "blablabla".byteindex("bl", 1).should == 3 + "blablabla".byteindex("bl", 2).should == 3 + "blablabla".byteindex("bl", 3).should == 3 + + "blablabla".byteindex("bla", 0).should == 0 + "blablabla".byteindex("bla", 1).should == 3 + "blablabla".byteindex("bla", 2).should == 3 + "blablabla".byteindex("bla", 3).should == 3 + + "blablabla".byteindex("blab", 0).should == 0 + "blablabla".byteindex("blab", 1).should == 3 + "blablabla".byteindex("blab", 2).should == 3 + "blablabla".byteindex("blab", 3).should == 3 + + "blablabla".byteindex("la", 1).should == 1 + "blablabla".byteindex("la", 2).should == 4 + "blablabla".byteindex("la", 3).should == 4 + "blablabla".byteindex("la", 4).should == 4 + + "blablabla".byteindex("lab", 1).should == 1 + "blablabla".byteindex("lab", 2).should == 4 + "blablabla".byteindex("lab", 3).should == 4 + "blablabla".byteindex("lab", 4).should == 4 + + "blablabla".byteindex("ab", 2).should == 2 + "blablabla".byteindex("ab", 3).should == 5 + "blablabla".byteindex("ab", 4).should == 5 + "blablabla".byteindex("ab", 5).should == 5 + + "blablabla".byteindex("", 0).should == 0 + "blablabla".byteindex("", 1).should == 1 + "blablabla".byteindex("", 2).should == 2 + "blablabla".byteindex("", 7).should == 7 + "blablabla".byteindex("", 8).should == 8 + "blablabla".byteindex("", 9).should == 9 + end - it "starts the search at offset + self.length if offset is negative" do - str = "blablabla" + it "starts the search at offset + self.length if offset is negative" do + str = "blablabla" - ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| - (-str.length .. -1).each do |offset| - str.byteindex(needle, offset).should == - str.byteindex(needle, offset + str.length) - end + ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| + (-str.length .. -1).each do |offset| + str.byteindex(needle, offset).should == + str.byteindex(needle, offset + str.length) end end + end - it "returns nil if the substring isn't found" do - "blablabla".byteindex("B").should == nil - "blablabla".byteindex("z").should == nil - "blablabla".byteindex("BLA").should == nil - "blablabla".byteindex("blablablabla").should == nil - "blablabla".byteindex("", 10).should == nil + it "returns nil if the substring isn't found" do + "blablabla".byteindex("B").should == nil + "blablabla".byteindex("z").should == nil + "blablabla".byteindex("BLA").should == nil + "blablabla".byteindex("blablablabla").should == nil + "blablabla".byteindex("", 10).should == nil - "hello".byteindex("he", 1).should == nil - "hello".byteindex("he", 2).should == nil - "I’ve got a multibyte character.\n".byteindex("\n\n").should == nil - end + "hello".byteindex("he", 1).should == nil + "hello".byteindex("he", 2).should == nil + "I’ve got a multibyte character.\n".byteindex("\n\n").should == nil + end - it "returns the character byteindex of a multibyte character" do - "ありがとう".byteindex("が").should == 6 - end + it "returns the character byteindex of a multibyte character" do + "ありがとう".byteindex("が").should == 6 + end - it "returns the character byteindex after offset" do - "われわれ".byteindex("わ", 3).should == 6 - "ありがとうありがとう".byteindex("が", 9).should == 21 - end + it "returns the character byteindex after offset" do + "われわれ".byteindex("わ", 3).should == 6 + "ありがとうありがとう".byteindex("が", 9).should == 21 + end - it "returns the character byteindex after a partial first match" do - "</</h".byteindex("</h").should == 2 - end + it "returns the character byteindex after a partial first match" do + "</</h".byteindex("</h").should == 2 + end - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - char = "れ".encode Encoding::EUC_JP - -> do - "あれ".byteindex(char) - end.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + char = "れ".encode Encoding::EUC_JP + -> do + "あれ".byteindex(char) + end.should.raise(Encoding::CompatibilityError) + end - it "handles a substring in a superset encoding" do - 'abc'.force_encoding(Encoding::US_ASCII).byteindex('é').should == nil - end + it "handles a substring in a superset encoding" do + 'abc'.dup.force_encoding(Encoding::US_ASCII).byteindex('é').should == nil + end - it "handles a substring in a subset encoding" do - 'été'.byteindex('t'.force_encoding(Encoding::US_ASCII)).should == 2 - end + it "handles a substring in a subset encoding" do + 'été'.byteindex('t'.dup.force_encoding(Encoding::US_ASCII)).should == 2 end end describe "String#byteindex with Regexp" do - ruby_version_is "3.2" do - it "behaves the same as String#byteindex(string) for escaped string regexps" do - ["blablabla", "hello cruel world...!"].each do |str| - ["", "b", "bla", "lab", "o c", "d."].each do |needle| - regexp = Regexp.new(Regexp.escape(needle)) - str.byteindex(regexp).should == str.byteindex(needle) - - 0.upto(str.size + 1) do |start| - str.byteindex(regexp, start).should == str.byteindex(needle, start) - end - - (-str.size - 1).upto(-1) do |start| - str.byteindex(regexp, start).should == str.byteindex(needle, start) - end + it "behaves the same as String#byteindex(string) for escaped string regexps" do + ["blablabla", "hello cruel world...!"].each do |str| + ["", "b", "bla", "lab", "o c", "d."].each do |needle| + regexp = Regexp.new(Regexp.escape(needle)) + str.byteindex(regexp).should == str.byteindex(needle) + + 0.upto(str.size + 1) do |start| + str.byteindex(regexp, start).should == str.byteindex(needle, start) + end + + (-str.size - 1).upto(-1) do |start| + str.byteindex(regexp, start).should == str.byteindex(needle, start) end end end + end - it "returns the byteindex of the first match of regexp" do - "blablabla".byteindex(/bla/).should == 0 - "blablabla".byteindex(/BLA/i).should == 0 + it "returns the byteindex of the first match of regexp" do + "blablabla".byteindex(/bla/).should == 0 + "blablabla".byteindex(/BLA/i).should == 0 - "blablabla".byteindex(/.{0}/).should == 0 - "blablabla".byteindex(/.{6}/).should == 0 - "blablabla".byteindex(/.{9}/).should == 0 + "blablabla".byteindex(/.{0}/).should == 0 + "blablabla".byteindex(/.{6}/).should == 0 + "blablabla".byteindex(/.{9}/).should == 0 - "blablabla".byteindex(/.*/).should == 0 - "blablabla".byteindex(/.+/).should == 0 + "blablabla".byteindex(/.*/).should == 0 + "blablabla".byteindex(/.+/).should == 0 - "blablabla".byteindex(/lab|b/).should == 0 + "blablabla".byteindex(/lab|b/).should == 0 - not_supported_on :opal do - "blablabla".byteindex(/\A/).should == 0 - "blablabla".byteindex(/\Z/).should == 9 - "blablabla".byteindex(/\z/).should == 9 - "blablabla\n".byteindex(/\Z/).should == 9 - "blablabla\n".byteindex(/\z/).should == 10 - end + not_supported_on :opal do + "blablabla".byteindex(/\A/).should == 0 + "blablabla".byteindex(/\Z/).should == 9 + "blablabla".byteindex(/\z/).should == 9 + "blablabla\n".byteindex(/\Z/).should == 9 + "blablabla\n".byteindex(/\z/).should == 10 + end - "blablabla".byteindex(/^/).should == 0 - "\nblablabla".byteindex(/^/).should == 0 - "b\nablabla".byteindex(/$/).should == 1 - "bl\nablabla".byteindex(/$/).should == 2 + "blablabla".byteindex(/^/).should == 0 + "\nblablabla".byteindex(/^/).should == 0 + "b\nablabla".byteindex(/$/).should == 1 + "bl\nablabla".byteindex(/$/).should == 2 - "blablabla".byteindex(/.l./).should == 0 - end + "blablabla".byteindex(/.l./).should == 0 + end - it "starts the search at the given offset" do - "blablabla".byteindex(/.{0}/, 5).should == 5 - "blablabla".byteindex(/.{1}/, 5).should == 5 - "blablabla".byteindex(/.{2}/, 5).should == 5 - "blablabla".byteindex(/.{3}/, 5).should == 5 - "blablabla".byteindex(/.{4}/, 5).should == 5 - - "blablabla".byteindex(/.{0}/, 3).should == 3 - "blablabla".byteindex(/.{1}/, 3).should == 3 - "blablabla".byteindex(/.{2}/, 3).should == 3 - "blablabla".byteindex(/.{5}/, 3).should == 3 - "blablabla".byteindex(/.{6}/, 3).should == 3 - - "blablabla".byteindex(/.l./, 0).should == 0 - "blablabla".byteindex(/.l./, 1).should == 3 - "blablabla".byteindex(/.l./, 2).should == 3 - "blablabla".byteindex(/.l./, 3).should == 3 - - "xblaxbla".byteindex(/x./, 0).should == 0 - "xblaxbla".byteindex(/x./, 1).should == 4 - "xblaxbla".byteindex(/x./, 2).should == 4 - - not_supported_on :opal do - "blablabla\n".byteindex(/\Z/, 9).should == 9 - end + it "starts the search at the given offset" do + "blablabla".byteindex(/.{0}/, 5).should == 5 + "blablabla".byteindex(/.{1}/, 5).should == 5 + "blablabla".byteindex(/.{2}/, 5).should == 5 + "blablabla".byteindex(/.{3}/, 5).should == 5 + "blablabla".byteindex(/.{4}/, 5).should == 5 + + "blablabla".byteindex(/.{0}/, 3).should == 3 + "blablabla".byteindex(/.{1}/, 3).should == 3 + "blablabla".byteindex(/.{2}/, 3).should == 3 + "blablabla".byteindex(/.{5}/, 3).should == 3 + "blablabla".byteindex(/.{6}/, 3).should == 3 + + "blablabla".byteindex(/.l./, 0).should == 0 + "blablabla".byteindex(/.l./, 1).should == 3 + "blablabla".byteindex(/.l./, 2).should == 3 + "blablabla".byteindex(/.l./, 3).should == 3 + + "xblaxbla".byteindex(/x./, 0).should == 0 + "xblaxbla".byteindex(/x./, 1).should == 4 + "xblaxbla".byteindex(/x./, 2).should == 4 + + not_supported_on :opal do + "blablabla\n".byteindex(/\Z/, 9).should == 9 end + end - it "starts the search at offset + self.length if offset is negative" do - str = "blablabla" + it "starts the search at offset + self.length if offset is negative" do + str = "blablabla" - ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| - (-str.length .. -1).each do |offset| - str.byteindex(needle, offset).should == - str.byteindex(needle, offset + str.length) - end + ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| + (-str.length .. -1).each do |offset| + str.byteindex(needle, offset).should == + str.byteindex(needle, offset + str.length) end end + end - it "returns nil if the substring isn't found" do - "blablabla".byteindex(/BLA/).should == nil + it "returns nil if the substring isn't found" do + "blablabla".byteindex(/BLA/).should == nil - "blablabla".byteindex(/.{10}/).should == nil - "blaxbla".byteindex(/.x/, 3).should == nil - "blaxbla".byteindex(/..x/, 2).should == nil - end + "blablabla".byteindex(/.{10}/).should == nil + "blaxbla".byteindex(/.x/, 3).should == nil + "blaxbla".byteindex(/..x/, 2).should == nil + end - it "returns nil if the Regexp matches the empty string and the offset is out of range" do - "ruby".byteindex(//, 12).should be_nil - end + it "returns nil if the Regexp matches the empty string and the offset is out of range" do + "ruby".byteindex(//, 12).should == nil + end - it "supports \\G which matches at the given start offset" do - "helloYOU.".byteindex(/\GYOU/, 5).should == 5 - "helloYOU.".byteindex(/\GYOU/).should == nil + it "supports \\G which matches at the given start offset" do + "helloYOU.".byteindex(/\GYOU/, 5).should == 5 + "helloYOU.".byteindex(/\GYOU/).should == nil - re = /\G.+YOU/ - # The # marks where \G will match. - [ - ["#hi!YOUall.", 0], - ["h#i!YOUall.", 1], - ["hi#!YOUall.", 2], - ["hi!#YOUall.", nil] - ].each do |spec| + re = /\G.+YOU/ + # The # marks where \G will match. + [ + ["#hi!YOUall.", 0], + ["h#i!YOUall.", 1], + ["hi#!YOUall.", 2], + ["hi!#YOUall.", nil] + ].each do |spec| - start = spec[0].byteindex("#") - str = spec[0].delete("#") + start = spec[0].byteindex("#") + str = spec[0].delete("#") - str.byteindex(re, start).should == spec[1] - end + str.byteindex(re, start).should == spec[1] end + end - it "converts start_offset to an integer via to_int" do - obj = mock('1') - obj.should_receive(:to_int).and_return(1) - "RWOARW".byteindex(/R./, obj).should == 4 - end + it "converts start_offset to an integer via to_int" do + obj = mock('1') + obj.should_receive(:to_int).and_return(1) + "RWOARW".byteindex(/R./, obj).should == 4 + end - it "returns the character byteindex of a multibyte character" do - "ありがとう".byteindex(/が/).should == 6 - end + it "returns the character byteindex of a multibyte character" do + "ありがとう".byteindex(/が/).should == 6 + end - it "returns the character byteindex after offset" do - "われわれ".byteindex(/わ/, 3).should == 6 - end + it "returns the character byteindex after offset" do + "われわれ".byteindex(/わ/, 3).should == 6 + end - it "treats the offset as a byteindex" do - "われわわれ".byteindex(/わ/, 6).should == 6 - end + it "treats the offset as a byteindex" do + "われわわれ".byteindex(/わ/, 6).should == 6 end end diff --git a/spec/ruby/core/string/byterindex_spec.rb b/spec/ruby/core/string/byterindex_spec.rb index 717708c97d..569820463d 100644 --- a/spec/ruby/core/string/byterindex_spec.rb +++ b/spec/ruby/core/string/byterindex_spec.rb @@ -4,356 +4,350 @@ require_relative 'fixtures/classes' require_relative 'shared/byte_index_common.rb' describe "String#byterindex with object" do - ruby_version_is "3.2" do - it "tries to convert obj to a string via to_str" do - obj = mock('lo') - def obj.to_str() "lo" end - "hello".byterindex(obj).should == "hello".byterindex("lo") - - obj = mock('o') - def obj.respond_to?(arg, *) true end - def obj.method_missing(*args) "o" end - "hello".byterindex(obj).should == "hello".byterindex("o") - end - - it "calls #to_int to convert the second argument" do - offset = mock("string index offset") - offset.should_receive(:to_int).and_return(3) - "abc".byterindex("c", offset).should == 2 - end + it "tries to convert obj to a string via to_str" do + obj = mock('lo') + def obj.to_str() "lo" end + "hello".byterindex(obj).should == "hello".byterindex("lo") + + obj = mock('o') + def obj.respond_to?(arg, *) true end + def obj.method_missing(*args) "o" end + "hello".byterindex(obj).should == "hello".byterindex("o") + end - it "does not raise IndexError when byte offset is correct or on string boundary" do - "わ".byterindex("", 0).should == 0 - "わ".byterindex("", 3).should == 3 - "わ".byterindex("").should == 3 - end + it "calls #to_int to convert the second argument" do + offset = mock("string index offset") + offset.should_receive(:to_int).and_return(3) + "abc".byterindex("c", offset).should == 2 + end - it_behaves_like :byte_index_common, :byterindex + it "does not raise IndexError when byte offset is correct or on string boundary" do + "わ".byterindex("", 0).should == 0 + "わ".byterindex("", 3).should == 3 + "わ".byterindex("").should == 3 end + + it_behaves_like :byte_index_common, :byterindex end describe "String#byterindex with String" do - ruby_version_is "3.2" do - it "behaves the same as String#byterindex(char) for one-character strings" do - "blablabla hello cruel world...!".split("").uniq.each do |str| - chr = str[0] - str.byterindex(str).should == str.byterindex(chr) + it "behaves the same as String#byterindex(char) for one-character strings" do + "blablabla hello cruel world...!".split("").uniq.each do |str| + chr = str[0] + str.byterindex(str).should == str.byterindex(chr) - 0.upto(str.size + 1) do |start| - str.byterindex(str, start).should == str.byterindex(chr, start) - end + 0.upto(str.size + 1) do |start| + str.byterindex(str, start).should == str.byterindex(chr, start) + end - (-str.size - 1).upto(-1) do |start| - str.byterindex(str, start).should == str.byterindex(chr, start) - end + (-str.size - 1).upto(-1) do |start| + str.byterindex(str, start).should == str.byterindex(chr, start) end end + end - it "behaves the same as String#byterindex(?char) for one-character strings" do - "blablabla hello cruel world...!".split("").uniq.each do |str| - chr = str[0] =~ / / ? str[0] : eval("?#{str[0]}") - str.byterindex(str).should == str.byterindex(chr) + it "behaves the same as String#byterindex(?char) for one-character strings" do + "blablabla hello cruel world...!".split("").uniq.each do |str| + chr = str[0] =~ / / ? str[0] : eval("?#{str[0]}") + str.byterindex(str).should == str.byterindex(chr) - 0.upto(str.size + 1) do |start| - str.byterindex(str, start).should == str.byterindex(chr, start) - end + 0.upto(str.size + 1) do |start| + str.byterindex(str, start).should == str.byterindex(chr, start) + end - (-str.size - 1).upto(-1) do |start| - str.byterindex(str, start).should == str.byterindex(chr, start) - end + (-str.size - 1).upto(-1) do |start| + str.byterindex(str, start).should == str.byterindex(chr, start) end end + end - it "returns the index of the last occurrence of the given substring" do - "blablabla".byterindex("").should == 9 - "blablabla".byterindex("a").should == 8 - "blablabla".byterindex("la").should == 7 - "blablabla".byterindex("bla").should == 6 - "blablabla".byterindex("abla").should == 5 - "blablabla".byterindex("labla").should == 4 - "blablabla".byterindex("blabla").should == 3 - "blablabla".byterindex("ablabla").should == 2 - "blablabla".byterindex("lablabla").should == 1 - "blablabla".byterindex("blablabla").should == 0 - - "blablabla".byterindex("l").should == 7 - "blablabla".byterindex("bl").should == 6 - "blablabla".byterindex("abl").should == 5 - "blablabla".byterindex("labl").should == 4 - "blablabla".byterindex("blabl").should == 3 - "blablabla".byterindex("ablabl").should == 2 - "blablabla".byterindex("lablabl").should == 1 - "blablabla".byterindex("blablabl").should == 0 - - "blablabla".byterindex("b").should == 6 - "blablabla".byterindex("ab").should == 5 - "blablabla".byterindex("lab").should == 4 - "blablabla".byterindex("blab").should == 3 - "blablabla".byterindex("ablab").should == 2 - "blablabla".byterindex("lablab").should == 1 - "blablabla".byterindex("blablab").should == 0 - end + it "returns the index of the last occurrence of the given substring" do + "blablabla".byterindex("").should == 9 + "blablabla".byterindex("a").should == 8 + "blablabla".byterindex("la").should == 7 + "blablabla".byterindex("bla").should == 6 + "blablabla".byterindex("abla").should == 5 + "blablabla".byterindex("labla").should == 4 + "blablabla".byterindex("blabla").should == 3 + "blablabla".byterindex("ablabla").should == 2 + "blablabla".byterindex("lablabla").should == 1 + "blablabla".byterindex("blablabla").should == 0 + + "blablabla".byterindex("l").should == 7 + "blablabla".byterindex("bl").should == 6 + "blablabla".byterindex("abl").should == 5 + "blablabla".byterindex("labl").should == 4 + "blablabla".byterindex("blabl").should == 3 + "blablabla".byterindex("ablabl").should == 2 + "blablabla".byterindex("lablabl").should == 1 + "blablabla".byterindex("blablabl").should == 0 + + "blablabla".byterindex("b").should == 6 + "blablabla".byterindex("ab").should == 5 + "blablabla".byterindex("lab").should == 4 + "blablabla".byterindex("blab").should == 3 + "blablabla".byterindex("ablab").should == 2 + "blablabla".byterindex("lablab").should == 1 + "blablabla".byterindex("blablab").should == 0 + end - it "ignores string subclasses" do - "blablabla".byterindex(StringSpecs::MyString.new("bla")).should == 6 - StringSpecs::MyString.new("blablabla").byterindex("bla").should == 6 - StringSpecs::MyString.new("blablabla").byterindex(StringSpecs::MyString.new("bla")).should == 6 - end + it "ignores string subclasses" do + "blablabla".byterindex(StringSpecs::MyString.new("bla")).should == 6 + StringSpecs::MyString.new("blablabla").byterindex("bla").should == 6 + StringSpecs::MyString.new("blablabla").byterindex(StringSpecs::MyString.new("bla")).should == 6 + end - it "starts the search at the given offset" do - "blablabla".byterindex("bl", 0).should == 0 - "blablabla".byterindex("bl", 1).should == 0 - "blablabla".byterindex("bl", 2).should == 0 - "blablabla".byterindex("bl", 3).should == 3 - - "blablabla".byterindex("bla", 0).should == 0 - "blablabla".byterindex("bla", 1).should == 0 - "blablabla".byterindex("bla", 2).should == 0 - "blablabla".byterindex("bla", 3).should == 3 - - "blablabla".byterindex("blab", 0).should == 0 - "blablabla".byterindex("blab", 1).should == 0 - "blablabla".byterindex("blab", 2).should == 0 - "blablabla".byterindex("blab", 3).should == 3 - "blablabla".byterindex("blab", 6).should == 3 - "blablablax".byterindex("blab", 6).should == 3 - - "blablabla".byterindex("la", 1).should == 1 - "blablabla".byterindex("la", 2).should == 1 - "blablabla".byterindex("la", 3).should == 1 - "blablabla".byterindex("la", 4).should == 4 - - "blablabla".byterindex("lab", 1).should == 1 - "blablabla".byterindex("lab", 2).should == 1 - "blablabla".byterindex("lab", 3).should == 1 - "blablabla".byterindex("lab", 4).should == 4 - - "blablabla".byterindex("ab", 2).should == 2 - "blablabla".byterindex("ab", 3).should == 2 - "blablabla".byterindex("ab", 4).should == 2 - "blablabla".byterindex("ab", 5).should == 5 - - "blablabla".byterindex("", 0).should == 0 - "blablabla".byterindex("", 1).should == 1 - "blablabla".byterindex("", 2).should == 2 - "blablabla".byterindex("", 7).should == 7 - "blablabla".byterindex("", 8).should == 8 - "blablabla".byterindex("", 9).should == 9 - "blablabla".byterindex("", 10).should == 9 - end + it "starts the search at the given offset" do + "blablabla".byterindex("bl", 0).should == 0 + "blablabla".byterindex("bl", 1).should == 0 + "blablabla".byterindex("bl", 2).should == 0 + "blablabla".byterindex("bl", 3).should == 3 + + "blablabla".byterindex("bla", 0).should == 0 + "blablabla".byterindex("bla", 1).should == 0 + "blablabla".byterindex("bla", 2).should == 0 + "blablabla".byterindex("bla", 3).should == 3 + + "blablabla".byterindex("blab", 0).should == 0 + "blablabla".byterindex("blab", 1).should == 0 + "blablabla".byterindex("blab", 2).should == 0 + "blablabla".byterindex("blab", 3).should == 3 + "blablabla".byterindex("blab", 6).should == 3 + "blablablax".byterindex("blab", 6).should == 3 + + "blablabla".byterindex("la", 1).should == 1 + "blablabla".byterindex("la", 2).should == 1 + "blablabla".byterindex("la", 3).should == 1 + "blablabla".byterindex("la", 4).should == 4 + + "blablabla".byterindex("lab", 1).should == 1 + "blablabla".byterindex("lab", 2).should == 1 + "blablabla".byterindex("lab", 3).should == 1 + "blablabla".byterindex("lab", 4).should == 4 + + "blablabla".byterindex("ab", 2).should == 2 + "blablabla".byterindex("ab", 3).should == 2 + "blablabla".byterindex("ab", 4).should == 2 + "blablabla".byterindex("ab", 5).should == 5 + + "blablabla".byterindex("", 0).should == 0 + "blablabla".byterindex("", 1).should == 1 + "blablabla".byterindex("", 2).should == 2 + "blablabla".byterindex("", 7).should == 7 + "blablabla".byterindex("", 8).should == 8 + "blablabla".byterindex("", 9).should == 9 + "blablabla".byterindex("", 10).should == 9 + end - it "starts the search at offset + self.length if offset is negative" do - str = "blablabla" + it "starts the search at offset + self.length if offset is negative" do + str = "blablabla" - ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| - (-str.length .. -1).each do |offset| - str.byterindex(needle, offset).should == - str.byterindex(needle, offset + str.length) - end + ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| + (-str.length .. -1).each do |offset| + str.byterindex(needle, offset).should == + str.byterindex(needle, offset + str.length) end end + end - it "returns nil if the substring isn't found" do - "blablabla".byterindex("B").should == nil - "blablabla".byterindex("z").should == nil - "blablabla".byterindex("BLA").should == nil - "blablabla".byterindex("blablablabla").should == nil + it "returns nil if the substring isn't found" do + "blablabla".byterindex("B").should == nil + "blablabla".byterindex("z").should == nil + "blablabla".byterindex("BLA").should == nil + "blablabla".byterindex("blablablabla").should == nil - "hello".byterindex("lo", 0).should == nil - "hello".byterindex("lo", 1).should == nil - "hello".byterindex("lo", 2).should == nil + "hello".byterindex("lo", 0).should == nil + "hello".byterindex("lo", 1).should == nil + "hello".byterindex("lo", 2).should == nil - "hello".byterindex("llo", 0).should == nil - "hello".byterindex("llo", 1).should == nil + "hello".byterindex("llo", 0).should == nil + "hello".byterindex("llo", 1).should == nil - "hello".byterindex("el", 0).should == nil - "hello".byterindex("ello", 0).should == nil + "hello".byterindex("el", 0).should == nil + "hello".byterindex("ello", 0).should == nil - "hello".byterindex("", -6).should == nil - "hello".byterindex("", -7).should == nil + "hello".byterindex("", -6).should == nil + "hello".byterindex("", -7).should == nil - "hello".byterindex("h", -6).should == nil - end + "hello".byterindex("h", -6).should == nil + end - it "tries to convert start_offset to an integer via to_int" do - obj = mock('5') - def obj.to_int() 5 end - "str".byterindex("st", obj).should == 0 + it "tries to convert start_offset to an integer via to_int" do + obj = mock('5') + def obj.to_int() 5 end + "str".byterindex("st", obj).should == 0 - obj = mock('5') - def obj.respond_to?(arg, *) true end - def obj.method_missing(*args) 5 end - "str".byterindex("st", obj).should == 0 - end + obj = mock('5') + def obj.respond_to?(arg, *) true end + def obj.method_missing(*args) 5 end + "str".byterindex("st", obj).should == 0 + end - it "raises a TypeError when given offset is nil" do - -> { "str".byterindex("st", nil) }.should raise_error(TypeError) - end + it "raises a TypeError when given offset is nil" do + -> { "str".byterindex("st", nil) }.should.raise(TypeError) + end - it "handles a substring in a superset encoding" do - 'abc'.force_encoding(Encoding::US_ASCII).byterindex('é').should == nil - end + it "handles a substring in a superset encoding" do + 'abc'.dup.force_encoding(Encoding::US_ASCII).byterindex('é').should == nil + end - it "handles a substring in a subset encoding" do - 'été'.byterindex('t'.force_encoding(Encoding::US_ASCII)).should == 2 - end + it "handles a substring in a subset encoding" do + 'été'.byterindex('t'.dup.force_encoding(Encoding::US_ASCII)).should == 2 end end describe "String#byterindex with Regexp" do - ruby_version_is "3.2" do - it "behaves the same as String#byterindex(string) for escaped string regexps" do - ["blablabla", "hello cruel world...!"].each do |str| - ["", "b", "bla", "lab", "o c", "d."].each do |needle| - regexp = Regexp.new(Regexp.escape(needle)) - str.byterindex(regexp).should == str.byterindex(needle) - - 0.upto(str.size + 1) do |start| - str.byterindex(regexp, start).should == str.byterindex(needle, start) - end - - (-str.size - 1).upto(-1) do |start| - str.byterindex(regexp, start).should == str.byterindex(needle, start) - end + it "behaves the same as String#byterindex(string) for escaped string regexps" do + ["blablabla", "hello cruel world...!"].each do |str| + ["", "b", "bla", "lab", "o c", "d."].each do |needle| + regexp = Regexp.new(Regexp.escape(needle)) + str.byterindex(regexp).should == str.byterindex(needle) + + 0.upto(str.size + 1) do |start| + str.byterindex(regexp, start).should == str.byterindex(needle, start) + end + + (-str.size - 1).upto(-1) do |start| + str.byterindex(regexp, start).should == str.byterindex(needle, start) end end end + end - it "returns the index of the first match from the end of string of regexp" do - "blablabla".byterindex(/bla/).should == 6 - "blablabla".byterindex(/BLA/i).should == 6 - - "blablabla".byterindex(/.{0}/).should == 9 - "blablabla".byterindex(/.{1}/).should == 8 - "blablabla".byterindex(/.{2}/).should == 7 - "blablabla".byterindex(/.{6}/).should == 3 - "blablabla".byterindex(/.{9}/).should == 0 + it "returns the index of the first match from the end of string of regexp" do + "blablabla".byterindex(/bla/).should == 6 + "blablabla".byterindex(/BLA/i).should == 6 - "blablabla".byterindex(/.*/).should == 9 - "blablabla".byterindex(/.+/).should == 8 + "blablabla".byterindex(/.{0}/).should == 9 + "blablabla".byterindex(/.{1}/).should == 8 + "blablabla".byterindex(/.{2}/).should == 7 + "blablabla".byterindex(/.{6}/).should == 3 + "blablabla".byterindex(/.{9}/).should == 0 - "blablabla".byterindex(/bla|a/).should == 8 + "blablabla".byterindex(/.*/).should == 9 + "blablabla".byterindex(/.+/).should == 8 - not_supported_on :opal do - "blablabla".byterindex(/\A/).should == 0 - "blablabla".byterindex(/\Z/).should == 9 - "blablabla".byterindex(/\z/).should == 9 - "blablabla\n".byterindex(/\Z/).should == 10 - "blablabla\n".byterindex(/\z/).should == 10 - end + "blablabla".byterindex(/bla|a/).should == 8 - "blablabla".byterindex(/^/).should == 0 - not_supported_on :opal do - "\nblablabla".byterindex(/^/).should == 1 - "b\nlablabla".byterindex(/^/).should == 2 - end - "blablabla".byterindex(/$/).should == 9 - - "blablabla".byterindex(/.l./).should == 6 + not_supported_on :opal do + "blablabla".byterindex(/\A/).should == 0 + "blablabla".byterindex(/\Z/).should == 9 + "blablabla".byterindex(/\z/).should == 9 + "blablabla\n".byterindex(/\Z/).should == 10 + "blablabla\n".byterindex(/\z/).should == 10 end - it "starts the search at the given offset" do - "blablabla".byterindex(/.{0}/, 5).should == 5 - "blablabla".byterindex(/.{1}/, 5).should == 5 - "blablabla".byterindex(/.{2}/, 5).should == 5 - "blablabla".byterindex(/.{3}/, 5).should == 5 - "blablabla".byterindex(/.{4}/, 5).should == 5 - - "blablabla".byterindex(/.{0}/, 3).should == 3 - "blablabla".byterindex(/.{1}/, 3).should == 3 - "blablabla".byterindex(/.{2}/, 3).should == 3 - "blablabla".byterindex(/.{5}/, 3).should == 3 - "blablabla".byterindex(/.{6}/, 3).should == 3 - - "blablabla".byterindex(/.l./, 0).should == 0 - "blablabla".byterindex(/.l./, 1).should == 0 - "blablabla".byterindex(/.l./, 2).should == 0 - "blablabla".byterindex(/.l./, 3).should == 3 - - "blablablax".byterindex(/.x/, 10).should == 8 - "blablablax".byterindex(/.x/, 9).should == 8 - "blablablax".byterindex(/.x/, 8).should == 8 - - "blablablax".byterindex(/..x/, 10).should == 7 - "blablablax".byterindex(/..x/, 9).should == 7 - "blablablax".byterindex(/..x/, 8).should == 7 - "blablablax".byterindex(/..x/, 7).should == 7 - - not_supported_on :opal do - "blablabla\n".byterindex(/\Z/, 9).should == 9 - end + "blablabla".byterindex(/^/).should == 0 + not_supported_on :opal do + "\nblablabla".byterindex(/^/).should == 1 + "b\nlablabla".byterindex(/^/).should == 2 end + "blablabla".byterindex(/$/).should == 9 - it "starts the search at offset + self.length if offset is negative" do - str = "blablabla" + "blablabla".byterindex(/.l./).should == 6 + end - ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| - (-str.length .. -1).each do |offset| - str.byterindex(needle, offset).should == - str.byterindex(needle, offset + str.length) - end - end + it "starts the search at the given offset" do + "blablabla".byterindex(/.{0}/, 5).should == 5 + "blablabla".byterindex(/.{1}/, 5).should == 5 + "blablabla".byterindex(/.{2}/, 5).should == 5 + "blablabla".byterindex(/.{3}/, 5).should == 5 + "blablabla".byterindex(/.{4}/, 5).should == 5 + + "blablabla".byterindex(/.{0}/, 3).should == 3 + "blablabla".byterindex(/.{1}/, 3).should == 3 + "blablabla".byterindex(/.{2}/, 3).should == 3 + "blablabla".byterindex(/.{5}/, 3).should == 3 + "blablabla".byterindex(/.{6}/, 3).should == 3 + + "blablabla".byterindex(/.l./, 0).should == 0 + "blablabla".byterindex(/.l./, 1).should == 0 + "blablabla".byterindex(/.l./, 2).should == 0 + "blablabla".byterindex(/.l./, 3).should == 3 + + "blablablax".byterindex(/.x/, 10).should == 8 + "blablablax".byterindex(/.x/, 9).should == 8 + "blablablax".byterindex(/.x/, 8).should == 8 + + "blablablax".byterindex(/..x/, 10).should == 7 + "blablablax".byterindex(/..x/, 9).should == 7 + "blablablax".byterindex(/..x/, 8).should == 7 + "blablablax".byterindex(/..x/, 7).should == 7 + + not_supported_on :opal do + "blablabla\n".byterindex(/\Z/, 9).should == 9 end + end - it "returns nil if the substring isn't found" do - "blablabla".byterindex(/BLA/).should == nil - "blablabla".byterindex(/.{10}/).should == nil - "blablablax".byterindex(/.x/, 7).should == nil - "blablablax".byterindex(/..x/, 6).should == nil + it "starts the search at offset + self.length if offset is negative" do + str = "blablabla" - not_supported_on :opal do - "blablabla".byterindex(/\Z/, 5).should == nil - "blablabla".byterindex(/\z/, 5).should == nil - "blablabla\n".byterindex(/\z/, 9).should == nil + ["bl", "bla", "blab", "la", "lab", "ab", ""].each do |needle| + (-str.length .. -1).each do |offset| + str.byterindex(needle, offset).should == + str.byterindex(needle, offset + str.length) end end + end + + it "returns nil if the substring isn't found" do + "blablabla".byterindex(/BLA/).should == nil + "blablabla".byterindex(/.{10}/).should == nil + "blablablax".byterindex(/.x/, 7).should == nil + "blablablax".byterindex(/..x/, 6).should == nil not_supported_on :opal do - it "supports \\G which matches at the given start offset" do - "helloYOU.".byterindex(/YOU\G/, 8).should == 5 - "helloYOU.".byterindex(/YOU\G/).should == nil - - idx = "helloYOUall!".index("YOU") - re = /YOU.+\G.+/ - # The # marks where \G will match. - [ - ["helloYOU#all.", nil], - ["helloYOUa#ll.", idx], - ["helloYOUal#l.", idx], - ["helloYOUall#.", idx], - ["helloYOUall.#", nil] - ].each do |i| - start = i[0].index("#") - str = i[0].delete("#") - - str.byterindex(re, start).should == i[1] - end + "blablabla".byterindex(/\Z/, 5).should == nil + "blablabla".byterindex(/\z/, 5).should == nil + "blablabla\n".byterindex(/\z/, 9).should == nil + end + end + + not_supported_on :opal do + it "supports \\G which matches at the given start offset" do + "helloYOU.".byterindex(/YOU\G/, 8).should == 5 + "helloYOU.".byterindex(/YOU\G/).should == nil + + idx = "helloYOUall!".index("YOU") + re = /YOU.+\G.+/ + # The # marks where \G will match. + [ + ["helloYOU#all.", nil], + ["helloYOUa#ll.", idx], + ["helloYOUal#l.", idx], + ["helloYOUall#.", idx], + ["helloYOUall.#", nil] + ].each do |i| + start = i[0].index("#") + str = i[0].delete("#") + + str.byterindex(re, start).should == i[1] end end + end - it "tries to convert start_offset to an integer" do - obj = mock('5') - def obj.to_int() 5 end - "str".byterindex(/../, obj).should == 1 + it "tries to convert start_offset to an integer" do + obj = mock('5') + def obj.to_int() 5 end + "str".byterindex(/../, obj).should == 1 - obj = mock('5') - def obj.respond_to?(arg, *) true end - def obj.method_missing(*args); 5; end - "str".byterindex(/../, obj).should == 1 - end + obj = mock('5') + def obj.respond_to?(arg, *) true end + def obj.method_missing(*args); 5; end + "str".byterindex(/../, obj).should == 1 + end - it "raises a TypeError when given offset is nil" do - -> { "str".byterindex(/../, nil) }.should raise_error(TypeError) - end + it "raises a TypeError when given offset is nil" do + -> { "str".byterindex(/../, nil) }.should.raise(TypeError) + end - it "returns the reverse byte index of a multibyte character" do - "ありがりがとう".byterindex("が").should == 12 - "ありがりがとう".byterindex(/が/).should == 12 - end + it "returns the reverse byte index of a multibyte character" do + "ありがりがとう".byterindex("が").should == 12 + "ありがりがとう".byterindex(/が/).should == 12 + end - it "returns the character index before the finish" do - "ありがりがとう".byterindex("が", 9).should == 6 - "ありがりがとう".byterindex(/が/, 9).should == 6 - end + it "returns the character index before the finish" do + "ありがりがとう".byterindex("が", 9).should == 6 + "ありがりがとう".byterindex(/が/, 9).should == 6 end end diff --git a/spec/ruby/core/string/bytes_spec.rb b/spec/ruby/core/string/bytes_spec.rb index 859b346550..e6019fb987 100644 --- a/spec/ruby/core/string/bytes_spec.rb +++ b/spec/ruby/core/string/bytes_spec.rb @@ -9,7 +9,7 @@ describe "String#bytes" do end it "returns an Array when no block is given" do - @utf8.bytes.should be_an_instance_of(Array) + @utf8.bytes.should.instance_of?(Array) end it "yields each byte to a block if one is given, returning self" do @@ -23,8 +23,8 @@ describe "String#bytes" do end it "returns bytes as Integers" do - @ascii.bytes.to_a.each {|b| b.should be_an_instance_of(Integer)} - @utf8_ascii.bytes { |b| b.should be_an_instance_of(Integer) } + @ascii.bytes.to_a.each {|b| b.should.instance_of?(Integer)} + @utf8_ascii.bytes { |b| b.should.instance_of?(Integer) } end it "agrees with #unpack('C*')" do @@ -50,6 +50,6 @@ describe "String#bytes" do end it "is unaffected by #force_encoding" do - @utf8.force_encoding('ASCII').bytes.to_a.should == @utf8.bytes.to_a + @utf8.dup.force_encoding('ASCII').bytes.to_a.should == @utf8.bytes.to_a end end diff --git a/spec/ruby/core/string/bytesize_spec.rb b/spec/ruby/core/string/bytesize_spec.rb index a31f3ae671..2bbefc0820 100644 --- a/spec/ruby/core/string/bytesize_spec.rb +++ b/spec/ruby/core/string/bytesize_spec.rb @@ -13,21 +13,21 @@ describe "String#bytesize" do end it "works with pseudo-ASCII strings containing single UTF-8 characters" do - "\u{6666}".force_encoding('ASCII').bytesize.should == 3 + "\u{6666}".dup.force_encoding('ASCII').bytesize.should == 3 end it "works with strings containing UTF-8 characters" do - "c \u{6666}".force_encoding('UTF-8').bytesize.should == 5 + "c \u{6666}".dup.force_encoding('UTF-8').bytesize.should == 5 "c \u{6666}".bytesize.should == 5 end it "works with pseudo-ASCII strings containing UTF-8 characters" do - "c \u{6666}".force_encoding('ASCII').bytesize.should == 5 + "c \u{6666}".dup.force_encoding('ASCII').bytesize.should == 5 end it "returns 0 for the empty string" do "".bytesize.should == 0 - "".force_encoding('ASCII').bytesize.should == 0 - "".force_encoding('UTF-8').bytesize.should == 0 + "".dup.force_encoding('ASCII').bytesize.should == 0 + "".dup.force_encoding('UTF-8').bytesize.should == 0 end end diff --git a/spec/ruby/core/string/byteslice_spec.rb b/spec/ruby/core/string/byteslice_spec.rb index 312229523d..4ad9e8d8f1 100644 --- a/spec/ruby/core/string/byteslice_spec.rb +++ b/spec/ruby/core/string/byteslice_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/slice' @@ -17,12 +17,12 @@ describe "String#byteslice with Range" do it_behaves_like :string_slice_range, :byteslice end -describe "String#byteslice on on non ASCII strings" do +describe "String#byteslice on non ASCII strings" do it "returns byteslice of unicode strings" do - "\u3042".byteslice(1).should == "\x81".force_encoding("UTF-8") - "\u3042".byteslice(1, 2).should == "\x81\x82".force_encoding("UTF-8") - "\u3042".byteslice(1..2).should == "\x81\x82".force_encoding("UTF-8") - "\u3042".byteslice(-1).should == "\x82".force_encoding("UTF-8") + "\u3042".byteslice(1).should == "\x81".dup.force_encoding("UTF-8") + "\u3042".byteslice(1, 2).should == "\x81\x82".dup.force_encoding("UTF-8") + "\u3042".byteslice(1..2).should == "\x81\x82".dup.force_encoding("UTF-8") + "\u3042".byteslice(-1).should == "\x82".dup.force_encoding("UTF-8") end it "returns a String in the same encoding as self" do diff --git a/spec/ruby/core/string/bytesplice_spec.rb b/spec/ruby/core/string/bytesplice_spec.rb index f13024a79b..3e5e6fe1ee 100644 --- a/spec/ruby/core/string/bytesplice_spec.rb +++ b/spec/ruby/core/string/bytesplice_spec.rb @@ -1,133 +1,290 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' describe "String#bytesplice" do - ruby_version_is "3.2" do - it "raises IndexError when index is less than -bytesize" do - -> { "hello".bytesplice(-6, 0, "xxx") }.should raise_error(IndexError, "index -6 out of string") - end - - it "raises IndexError when index is greater than bytesize" do - -> { "hello".bytesplice(6, 0, "xxx") }.should raise_error(IndexError, "index 6 out of string") - end - - it "raises IndexError for negative length" do - -> { "abc".bytesplice(0, -2, "") }.should raise_error(IndexError, "negative length -2") - end - - it "replaces with integer indices" do - "hello".bytesplice(-5, 0, "xxx").should == "xxxhello" - "hello".bytesplice(0, 0, "xxx").should == "xxxhello" - "hello".bytesplice(0, 1, "xxx").should == "xxxello" - "hello".bytesplice(0, 5, "xxx").should == "xxx" - "hello".bytesplice(0, 6, "xxx").should == "xxx" - end - - it "raises RangeError when range left boundary is less than -bytesize" do - -> { "hello".bytesplice(-6...-6, "xxx") }.should raise_error(RangeError, "-6...-6 out of range") - end - - it "replaces with ranges" do - "hello".bytesplice(-5...-5, "xxx").should == "xxxhello" - "hello".bytesplice(0...0, "xxx").should == "xxxhello" - "hello".bytesplice(0..0, "xxx").should == "xxxello" - "hello".bytesplice(0...1, "xxx").should == "xxxello" - "hello".bytesplice(0..1, "xxx").should == "xxxllo" - "hello".bytesplice(0..-1, "xxx").should == "xxx" - "hello".bytesplice(0...5, "xxx").should == "xxx" - "hello".bytesplice(0...6, "xxx").should == "xxx" - end - - it "raises TypeError when integer index is provided without length argument" do - -> { "hello".bytesplice(0, "xxx") }.should raise_error(TypeError, "wrong argument type Integer (expected Range)") - end - - it "replaces on an empty string" do - "".bytesplice(0, 0, "").should == "" - "".bytesplice(0, 0, "xxx").should == "xxx" - end - - it "mutates self" do - s = "hello" - s.bytesplice(2, 1, "xxx").should.equal?(s) - end - - it "raises when string is frozen" do - s = "hello".freeze - -> { s.bytesplice(2, 1, "xxx") }.should raise_error(FrozenError, "can't modify frozen String: \"hello\"") - end + it "raises IndexError when index is less than -bytesize" do + -> { "hello".bytesplice(-6, 0, "xxx") }.should.raise(IndexError, "index -6 out of string") + end + + it "raises IndexError when index is greater than bytesize" do + -> { "hello".bytesplice(6, 0, "xxx") }.should.raise(IndexError, "index 6 out of string") + end + + it "raises IndexError for negative length" do + -> { "abc".bytesplice(0, -2, "") }.should.raise(IndexError, "negative length -2") + end + + it "replaces with integer indices" do + "hello".bytesplice(-5, 0, "xxx").should == "xxxhello" + "hello".bytesplice(0, 0, "xxx").should == "xxxhello" + "hello".bytesplice(0, 1, "xxx").should == "xxxello" + "hello".bytesplice(0, 5, "xxx").should == "xxx" + "hello".bytesplice(0, 6, "xxx").should == "xxx" + end + + it "raises RangeError when range left boundary is less than -bytesize" do + -> { "hello".bytesplice(-6...-6, "xxx") }.should.raise(RangeError, "-6...-6 out of range") + end + + it "replaces with ranges" do + "hello".bytesplice(-5...-5, "xxx").should == "xxxhello" + "hello".bytesplice(0...0, "xxx").should == "xxxhello" + "hello".bytesplice(0..0, "xxx").should == "xxxello" + "hello".bytesplice(0...1, "xxx").should == "xxxello" + "hello".bytesplice(0..1, "xxx").should == "xxxllo" + "hello".bytesplice(0..-1, "xxx").should == "xxx" + "hello".bytesplice(0...5, "xxx").should == "xxx" + "hello".bytesplice(0...6, "xxx").should == "xxx" + end + + it "raises TypeError when integer index is provided without length argument" do + -> { "hello".bytesplice(0, "xxx") }.should.raise(TypeError, "wrong argument type Integer (expected Range)") + end + + it "replaces on an empty string" do + "".bytesplice(0, 0, "").should == "" + "".bytesplice(0, 0, "xxx").should == "xxx" + end + + it "mutates self" do + s = "hello" + s.bytesplice(2, 1, "xxx").should.equal?(s) + end + + it "raises when string is frozen" do + s = "hello".freeze + -> { s.bytesplice(2, 1, "xxx") }.should.raise(FrozenError, "can't modify frozen String: \"hello\"") + end + + it "raises IndexError when str_index is less than -bytesize" do + -> { "hello".bytesplice(2, 1, "HELLO", -6, 0) }.should.raise(IndexError, "index -6 out of string") + end + + it "raises IndexError when str_index is greater than bytesize" do + -> { "hello".bytesplice(2, 1, "HELLO", 6, 0) }.should.raise(IndexError, "index 6 out of string") + end + + it "raises IndexError for negative str length" do + -> { "abc".bytesplice(0, 1, "", 0, -2) }.should.raise(IndexError, "negative length -2") + end + + it "replaces with integer str indices" do + "hello".bytesplice(1, 2, "HELLO", -5, 0).should == "hlo" + "hello".bytesplice(1, 2, "HELLO", 0, 0).should == "hlo" + "hello".bytesplice(1, 2, "HELLO", 0, 1).should == "hHlo" + "hello".bytesplice(1, 2, "HELLO", 0, 5).should == "hHELLOlo" + "hello".bytesplice(1, 2, "HELLO", 0, 6).should == "hHELLOlo" + end + + it "raises RangeError when str range left boundary is less than -bytesize" do + -> { "hello".bytesplice(0..1, "HELLO", -6...-6) }.should.raise(RangeError, "-6...-6 out of range") + end + + it "replaces with str ranges" do + "hello".bytesplice(1..2, "HELLO", -5...-5).should == "hlo" + "hello".bytesplice(1..2, "HELLO", 0...0).should == "hlo" + "hello".bytesplice(1..2, "HELLO", 0..0).should == "hHlo" + "hello".bytesplice(1..2, "HELLO", 0...1).should == "hHlo" + "hello".bytesplice(1..2, "HELLO", 0..1).should == "hHElo" + "hello".bytesplice(1..2, "HELLO", 0..-1).should == "hHELLOlo" + "hello".bytesplice(1..2, "HELLO", 0...5).should == "hHELLOlo" + "hello".bytesplice(1..2, "HELLO", 0...6).should == "hHELLOlo" + end + + it "raises ArgumentError when integer str index is provided without str length argument" do + -> { "hello".bytesplice(0, 1, "xxx", 0) }.should.raise(ArgumentError, "wrong number of arguments (given 4, expected 2, 3, or 5)") + end + + it "replaces on an empty string with str index/length" do + "".bytesplice(0, 0, "", 0, 0).should == "" + "".bytesplice(0, 0, "xxx", 0, 1).should == "x" + end + + it "mutates self with substring and str index/length" do + s = "hello" + s.bytesplice(2, 1, "xxx", 1, 2).should.equal?(s) + s.should.eql?("hexxlo") + end + + it "raises when string is frozen and str index/length" do + s = "hello".freeze + -> { s.bytesplice(2, 1, "xxx", 0, 1) }.should.raise(FrozenError, "can't modify frozen String: \"hello\"") + end + + it "replaces on an empty string with str range" do + "".bytesplice(0..0, "", 0..0).should == "" + "".bytesplice(0..0, "xyz", 0..1).should == "xy" + end + + it "mutates self with substring and str range" do + s = "hello" + s.bytesplice(2..2, "xyz", 1..2).should.equal?(s) + s.should.eql?("heyzlo") + end + + it "raises when string is frozen and str range" do + s = "hello".freeze + -> { s.bytesplice(2..2, "yzx", 0..1) }.should.raise(FrozenError, "can't modify frozen String: \"hello\"") end end describe "String#bytesplice with multibyte characters" do - ruby_version_is "3.2" do - it "raises IndexError when index is out of byte size boundary" do - -> { "こんにちは".bytesplice(-16, 0, "xxx") }.should raise_error(IndexError, "index -16 out of string") - end - - it "raises IndexError when index is not on a codepoint boundary" do - -> { "こんにちは".bytesplice(1, 0, "xxx") }.should raise_error(IndexError, "offset 1 does not land on character boundary") - end - - it "raises IndexError when length is not matching the codepoint boundary" do - -> { "こんにちは".bytesplice(0, 1, "xxx") }.should raise_error(IndexError, "offset 1 does not land on character boundary") - -> { "こんにちは".bytesplice(0, 2, "xxx") }.should raise_error(IndexError, "offset 2 does not land on character boundary") - end - - it "replaces with integer indices" do - "こんにちは".bytesplice(-15, 0, "xxx").should == "xxxこんにちは" - "こんにちは".bytesplice(0, 0, "xxx").should == "xxxこんにちは" - "こんにちは".bytesplice(0, 3, "xxx").should == "xxxんにちは" - "こんにちは".bytesplice(3, 3, "はは").should == "こははにちは" - "こんにちは".bytesplice(15, 0, "xxx").should == "こんにちはxxx" - end - - it "replaces with range" do - "こんにちは".bytesplice(-15...-16, "xxx").should == "xxxこんにちは" - "こんにちは".bytesplice(0...0, "xxx").should == "xxxこんにちは" - "こんにちは".bytesplice(0..2, "xxx").should == "xxxんにちは" - "こんにちは".bytesplice(0...3, "xxx").should == "xxxんにちは" - "こんにちは".bytesplice(0..5, "xxx").should == "xxxにちは" - "こんにちは".bytesplice(0..-1, "xxx").should == "xxx" - "こんにちは".bytesplice(0...15, "xxx").should == "xxx" - "こんにちは".bytesplice(0...18, "xxx").should == "xxx" - end - - it "treats negative length for range as 0" do - "こんにちは".bytesplice(0...-100, "xxx").should == "xxxこんにちは" - "こんにちは".bytesplice(3...-100, "xxx").should == "こxxxんにちは" - "こんにちは".bytesplice(-15...-100, "xxx").should == "xxxこんにちは" - end - - it "raises when ranges not match codepoint boundaries" do - -> { "こんにちは".bytesplice(0..0, "x") }.should raise_error(IndexError, "offset 1 does not land on character boundary") - -> { "こんにちは".bytesplice(0..1, "x") }.should raise_error(IndexError, "offset 2 does not land on character boundary") - # Begin is incorrect - -> { "こんにちは".bytesplice(-4..-1, "x") }.should raise_error(IndexError, "offset 11 does not land on character boundary") - -> { "こんにちは".bytesplice(-5..-1, "x") }.should raise_error(IndexError, "offset 10 does not land on character boundary") - # End is incorrect - -> { "こんにちは".bytesplice(-3..-2, "x") }.should raise_error(IndexError, "offset 14 does not land on character boundary") - -> { "こんにちは".bytesplice(-3..-3, "x") }.should raise_error(IndexError, "offset 13 does not land on character boundary") - end - - it "deals with a different encoded argument" do - s = "こんにちは" - s.encoding.should == Encoding::UTF_8 - sub = "xxxxxx" - sub.force_encoding(Encoding::US_ASCII) - - result = s.bytesplice(0, 3, sub) - result.should == "xxxxxxんにちは" - result.encoding.should == Encoding::UTF_8 - - s = "xxxxxx" - s.force_encoding(Encoding::US_ASCII) - sub = "こんにちは" - sub.encoding.should == Encoding::UTF_8 - - result = s.bytesplice(0, 3, sub) - result.should == "こんにちはxxx" - result.encoding.should == Encoding::UTF_8 - end + it "raises IndexError when index is out of byte size boundary" do + -> { "こんにちは".bytesplice(-16, 0, "xxx") }.should.raise(IndexError, "index -16 out of string") + end + + it "raises IndexError when index is not on a codepoint boundary" do + -> { "こんにちは".bytesplice(1, 0, "xxx") }.should.raise(IndexError, "offset 1 does not land on character boundary") + end + + it "raises IndexError when length is not matching the codepoint boundary" do + -> { "こんにちは".bytesplice(0, 1, "xxx") }.should.raise(IndexError, "offset 1 does not land on character boundary") + -> { "こんにちは".bytesplice(0, 2, "xxx") }.should.raise(IndexError, "offset 2 does not land on character boundary") + end + + it "replaces with integer indices" do + "こんにちは".bytesplice(-15, 0, "xxx").should == "xxxこんにちは" + "こんにちは".bytesplice(0, 0, "xxx").should == "xxxこんにちは" + "こんにちは".bytesplice(0, 3, "xxx").should == "xxxんにちは" + "こんにちは".bytesplice(3, 3, "はは").should == "こははにちは" + "こんにちは".bytesplice(15, 0, "xxx").should == "こんにちはxxx" + end + + it "replaces with range" do + "こんにちは".bytesplice(-15...-16, "xxx").should == "xxxこんにちは" + "こんにちは".bytesplice(0...0, "xxx").should == "xxxこんにちは" + "こんにちは".bytesplice(0..2, "xxx").should == "xxxんにちは" + "こんにちは".bytesplice(0...3, "xxx").should == "xxxんにちは" + "こんにちは".bytesplice(0..5, "xxx").should == "xxxにちは" + "こんにちは".bytesplice(0..-1, "xxx").should == "xxx" + "こんにちは".bytesplice(0...15, "xxx").should == "xxx" + "こんにちは".bytesplice(0...18, "xxx").should == "xxx" + end + + it "treats negative length for range as 0" do + "こんにちは".bytesplice(0...-100, "xxx").should == "xxxこんにちは" + "こんにちは".bytesplice(3...-100, "xxx").should == "こxxxんにちは" + "こんにちは".bytesplice(-15...-100, "xxx").should == "xxxこんにちは" + end + + it "raises when ranges not match codepoint boundaries" do + -> { "こんにちは".bytesplice(0..0, "x") }.should.raise(IndexError, "offset 1 does not land on character boundary") + -> { "こんにちは".bytesplice(0..1, "x") }.should.raise(IndexError, "offset 2 does not land on character boundary") + # Begin is incorrect + -> { "こんにちは".bytesplice(-4..-1, "x") }.should.raise(IndexError, "offset 11 does not land on character boundary") + -> { "こんにちは".bytesplice(-5..-1, "x") }.should.raise(IndexError, "offset 10 does not land on character boundary") + # End is incorrect + -> { "こんにちは".bytesplice(-3..-2, "x") }.should.raise(IndexError, "offset 14 does not land on character boundary") + -> { "こんにちは".bytesplice(-3..-3, "x") }.should.raise(IndexError, "offset 13 does not land on character boundary") + end + + it "deals with a different encoded argument" do + s = "こんにちは" + s.encoding.should == Encoding::UTF_8 + sub = "xxxxxx" + sub.force_encoding(Encoding::US_ASCII) + + result = s.bytesplice(0, 3, sub) + result.should == "xxxxxxんにちは" + result.encoding.should == Encoding::UTF_8 + + s = "xxxxxx" + s.force_encoding(Encoding::US_ASCII) + sub = "こんにちは" + sub.encoding.should == Encoding::UTF_8 + + result = s.bytesplice(0, 3, sub) + result.should == "こんにちはxxx" + result.encoding.should == Encoding::UTF_8 + end + + it "raises IndexError when str_index is out of byte size boundary" do + -> { "こんにちは".bytesplice(3, 3, "こんにちは", -16, 0) }.should.raise(IndexError, "index -16 out of string") + end + + it "raises IndexError when str_index is not on a codepoint boundary" do + -> { "こんにちは".bytesplice(3, 3, "こんにちは", 1, 0) }.should.raise(IndexError, "offset 1 does not land on character boundary") + end + + it "raises IndexError when str_length is not matching the codepoint boundary" do + -> { "こんにちは".bytesplice(3, 3, "こんにちは", 0, 1) }.should.raise(IndexError, "offset 1 does not land on character boundary") + -> { "こんにちは".bytesplice(3, 3, "こんにちは", 0, 2) }.should.raise(IndexError, "offset 2 does not land on character boundary") + end + + it "replaces with integer str indices" do + "こんにちは".bytesplice(3, 3, "こんにちは", -15, 0).should == "こにちは" + "こんにちは".bytesplice(3, 3, "こんにちは", 0, 0).should == "こにちは" + "こんにちは".bytesplice(3, 3, "こんにちは", 0, 3).should == "ここにちは" + "こんにちは".bytesplice(3, 3, "はは", 3, 3).should == "こはにちは" + "こんにちは".bytesplice(3, 3, "こんにちは", 15, 0).should == "こにちは" + end + + it "replaces with str range" do + "こんにちは".bytesplice(0..2, "こんにちは", -15...-16).should == "んにちは" + "こんにちは".bytesplice(0..2, "こんにちは", 0...0).should == "んにちは" + "こんにちは".bytesplice(0..2, "こんにちは", 3..5).should == "んんにちは" + "こんにちは".bytesplice(0..2, "こんにちは", 3...6).should == "んんにちは" + "こんにちは".bytesplice(0..2, "こんにちは", 3..8).should == "んにんにちは" + "こんにちは".bytesplice(0..2, "こんにちは", 0..-1).should == "こんにちはんにちは" + "こんにちは".bytesplice(0..2, "こんにちは", 0...15).should == "こんにちはんにちは" + "こんにちは".bytesplice(0..2, "こんにちは", 0...18).should == "こんにちはんにちは" + end + + it "treats negative length for str range as 0" do + "こんにちは".bytesplice(0..2, "こんにちは", 0...-100).should == "んにちは" + "こんにちは".bytesplice(0..2, "こんにちは", 3...-100).should == "んにちは" + "こんにちは".bytesplice(0..2, "こんにちは", -15...-100).should == "んにちは" + end + + it "raises when ranges not match codepoint boundaries in str" do + -> { "こんにちは".bytesplice(3...3, "こ", 0..0) }.should.raise(IndexError, "offset 1 does not land on character boundary") + -> { "こんにちは".bytesplice(3...3, "こ", 0..1) }.should.raise(IndexError, "offset 2 does not land on character boundary") + # Begin is incorrect + -> { "こんにちは".bytesplice(3...3, "こんにちは", -4..-1) }.should.raise(IndexError, "offset 11 does not land on character boundary") + -> { "こんにちは".bytesplice(3...3, "こんにちは", -5..-1) }.should.raise(IndexError, "offset 10 does not land on character boundary") + # End is incorrect + -> { "こんにちは".bytesplice(3...3, "こんにちは", -3..-2) }.should.raise(IndexError, "offset 14 does not land on character boundary") + -> { "こんにちは".bytesplice(3...3, "こんにちは", -3..-3) }.should.raise(IndexError, "offset 13 does not land on character boundary") + end + + it "deals with a different encoded argument with str index/length" do + s = "こんにちは" + s.encoding.should == Encoding::UTF_8 + sub = "goodbye" + sub.force_encoding(Encoding::US_ASCII) + + result = s.bytesplice(3, 3, sub, 0, 3) + result.should == "こgooにちは" + result.encoding.should == Encoding::UTF_8 + + s = "hello" + s.force_encoding(Encoding::US_ASCII) + sub = "こんにちは" + sub.encoding.should == Encoding::UTF_8 + + result = s.bytesplice(1, 2, sub, 3, 3) + result.should == "hんlo" + result.encoding.should == Encoding::UTF_8 + end + + it "deals with a different encoded argument with str range" do + s = "こんにちは" + s.encoding.should == Encoding::UTF_8 + sub = "goodbye" + sub.force_encoding(Encoding::US_ASCII) + + result = s.bytesplice(3..5, sub, 0..2) + result.should == "こgooにちは" + result.encoding.should == Encoding::UTF_8 + + s = "hello" + s.force_encoding(Encoding::US_ASCII) + sub = "こんにちは" + sub.encoding.should == Encoding::UTF_8 + + result = s.bytesplice(1..2, sub, 3..5) + result.should == "hんlo" + result.encoding.should == Encoding::UTF_8 end end diff --git a/spec/ruby/core/string/capitalize_spec.rb b/spec/ruby/core/string/capitalize_spec.rb index b79e9cfdbd..12b1675c2e 100644 --- a/spec/ruby/core/string/capitalize_spec.rb +++ b/spec/ruby/core/string/capitalize_spec.rb @@ -28,7 +28,7 @@ describe "String#capitalize" do capitalized.should == "Sset" capitalized.size.should == 4 capitalized.bytesize.should == 4 - capitalized.ascii_only?.should be_true + capitalized.ascii_only?.should == true end end @@ -52,7 +52,7 @@ describe "String#capitalize" do end it "does not allow any other additional option" do - -> { "iSa".capitalize(:turkic, :ascii) }.should raise_error(ArgumentError) + -> { "iSa".capitalize(:turkic, :ascii) }.should.raise(ArgumentError) end end @@ -66,21 +66,21 @@ describe "String#capitalize" do end it "does not allow any other additional option" do - -> { "iß".capitalize(:lithuanian, :ascii) }.should raise_error(ArgumentError) + -> { "iß".capitalize(:lithuanian, :ascii) }.should.raise(ArgumentError) end end it "does not allow the :fold option for upcasing" do - -> { "abc".capitalize(:fold) }.should raise_error(ArgumentError) + -> { "abc".capitalize(:fold) }.should.raise(ArgumentError) end it "does not allow invalid options" do - -> { "abc".capitalize(:invalid_option) }.should raise_error(ArgumentError) + -> { "abc".capitalize(:invalid_option) }.should.raise(ArgumentError) end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("hello").capitalize.should be_an_instance_of(String) - StringSpecs::MyString.new("Hello").capitalize.should be_an_instance_of(String) + StringSpecs::MyString.new("hello").capitalize.should.instance_of?(String) + StringSpecs::MyString.new("Hello").capitalize.should.instance_of?(String) end it "returns a String in the same encoding as self" do @@ -90,8 +90,8 @@ end describe "String#capitalize!" do it "capitalizes self in place" do - a = "hello" - a.capitalize!.should equal(a) + a = +"hello" + a.capitalize!.should.equal?(a) a.should == "Hello" end @@ -103,13 +103,13 @@ describe "String#capitalize!" do describe "full Unicode case mapping" do it "modifies self in place for all of Unicode with no option" do - a = "äöÜ" + 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 = +"ß" a.capitalize! a.should == "Ss" end @@ -121,19 +121,19 @@ describe "String#capitalize!" do end it "updates string metadata" do - capitalized = "ßeT" + capitalized = +"ßeT" capitalized.capitalize! capitalized.should == "Sset" capitalized.size.should == 4 capitalized.bytesize.should == 4 - capitalized.ascii_only?.should be_true + capitalized.ascii_only?.should == 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 = +"ßet" a.capitalize!(:ascii) a.should == "ßet" end @@ -147,61 +147,61 @@ describe "String#capitalize!" do 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 = +"iSa" a.capitalize!(:turkic) a.should == "İsa" end it "allows Lithuanian as an extra option" do - a = "iSa" + a = +"iSa" a.capitalize!(:turkic, :lithuanian) a.should == "İsa" end it "does not allow any other additional option" do - -> { a = "iSa"; a.capitalize!(:turkic, :ascii) }.should raise_error(ArgumentError) + -> { a = "iSa"; a.capitalize!(:turkic, :ascii) }.should.raise(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 = +"iß" a.capitalize!(:lithuanian) a.should == "Iß" end it "allows Turkic as an extra option (and applies Turkic semantics)" do - a = "iß" + a = +"iß" a.capitalize!(:lithuanian, :turkic) a.should == "İß" end it "does not allow any other additional option" do - -> { a = "iß"; a.capitalize!(:lithuanian, :ascii) }.should raise_error(ArgumentError) + -> { a = "iß"; a.capitalize!(:lithuanian, :ascii) }.should.raise(ArgumentError) end end it "does not allow the :fold option for upcasing" do - -> { a = "abc"; a.capitalize!(:fold) }.should raise_error(ArgumentError) + -> { a = "abc"; a.capitalize!(:fold) }.should.raise(ArgumentError) end it "does not allow invalid options" do - -> { a = "abc"; a.capitalize!(:invalid_option) }.should raise_error(ArgumentError) + -> { a = "abc"; a.capitalize!(:invalid_option) }.should.raise(ArgumentError) end it "returns nil when no changes are made" do - a = "Hello" + a = +"Hello" a.capitalize!.should == nil a.should == "Hello" - "".capitalize!.should == nil - "H".capitalize!.should == nil + (+"").capitalize!.should == nil + (+"H").capitalize!.should == nil end it "raises a FrozenError when self is frozen" do ["", "Hello", "hello"].each do |a| a.freeze - -> { a.capitalize! }.should raise_error(FrozenError) + -> { a.capitalize! }.should.raise(FrozenError) end end end diff --git a/spec/ruby/core/string/casecmp_spec.rb b/spec/ruby/core/string/casecmp_spec.rb index 81ebea557c..90577aaac0 100644 --- a/spec/ruby/core/string/casecmp_spec.rb +++ b/spec/ruby/core/string/casecmp_spec.rb @@ -26,11 +26,11 @@ describe "String#casecmp independent of case" do end it "returns nil if other can't be converted to a string" do - "abc".casecmp(mock('abc')).should be_nil + "abc".casecmp(mock('abc')).should == nil end it "returns nil if incompatible encodings" do - "あれ".casecmp("れ".encode(Encoding::EUC_JP)).should be_nil + "あれ".casecmp("れ".encode(Encoding::EUC_JP)).should == nil end describe "in UTF-8 mode" do @@ -143,7 +143,7 @@ describe 'String#casecmp? independent of case' do end it "returns nil if incompatible encodings" do - "あれ".casecmp?("れ".encode(Encoding::EUC_JP)).should be_nil + "あれ".casecmp?("れ".encode(Encoding::EUC_JP)).should == nil end describe 'for UNICODE characters' do @@ -190,11 +190,11 @@ describe 'String#casecmp? independent of case' do end it "case folds" do - "ß".casecmp?("ss").should be_true + "ß".casecmp?("ss").should == true end it "returns nil if other can't be converted to a string" do - "abc".casecmp?(mock('abc')).should be_nil + "abc".casecmp?(mock('abc')).should == nil end it "returns true for empty strings in different encodings" do diff --git a/spec/ruby/core/string/center_spec.rb b/spec/ruby/core/string/center_spec.rb index a59dd2a91b..ac5b8a2ff3 100644 --- a/spec/ruby/core/string/center_spec.rb +++ b/spec/ruby/core/string/center_spec.rb @@ -57,10 +57,10 @@ describe "String#center with length, padding" do end it "raises a TypeError when length can't be converted to an integer" do - -> { "hello".center("x") }.should raise_error(TypeError) - -> { "hello".center("x", "y") }.should raise_error(TypeError) - -> { "hello".center([]) }.should raise_error(TypeError) - -> { "hello".center(mock('x')) }.should raise_error(TypeError) + -> { "hello".center("x") }.should.raise(TypeError) + -> { "hello".center("x", "y") }.should.raise(TypeError) + -> { "hello".center([]) }.should.raise(TypeError) + -> { "hello".center(mock('x')) }.should.raise(TypeError) end it "calls #to_str to convert padstr to a String" do @@ -71,47 +71,47 @@ describe "String#center with length, padding" do end it "raises a TypeError when padstr can't be converted to a string" do - -> { "hello".center(20, 100) }.should raise_error(TypeError) - -> { "hello".center(20, []) }.should raise_error(TypeError) - -> { "hello".center(20, mock('x')) }.should raise_error(TypeError) + -> { "hello".center(20, 100) }.should.raise(TypeError) + -> { "hello".center(20, []) }.should.raise(TypeError) + -> { "hello".center(20, mock('x')) }.should.raise(TypeError) end it "raises an ArgumentError if padstr is empty" do - -> { "hello".center(10, "") }.should raise_error(ArgumentError) - -> { "hello".center(0, "") }.should raise_error(ArgumentError) + -> { "hello".center(10, "") }.should.raise(ArgumentError) + -> { "hello".center(0, "") }.should.raise(ArgumentError) end it "returns String instances when called on subclasses" do - StringSpecs::MyString.new("").center(10).should be_an_instance_of(String) - StringSpecs::MyString.new("foo").center(10).should be_an_instance_of(String) - StringSpecs::MyString.new("foo").center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) + StringSpecs::MyString.new("").center(10).should.instance_of?(String) + StringSpecs::MyString.new("foo").center(10).should.instance_of?(String) + StringSpecs::MyString.new("foo").center(10, StringSpecs::MyString.new("x")).should.instance_of?(String) - "".center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) - "foo".center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) + "".center(10, StringSpecs::MyString.new("x")).should.instance_of?(String) + "foo".center(10, StringSpecs::MyString.new("x")).should.instance_of?(String) end describe "with width" do it "returns a String in the same encoding as the original" do - str = "abc".force_encoding Encoding::IBM437 + str = "abc".dup.force_encoding Encoding::IBM437 result = str.center 6 result.should == " abc " - result.encoding.should equal(Encoding::IBM437) + result.encoding.should.equal?(Encoding::IBM437) end end describe "with width, pattern" do it "returns a String in the compatible encoding" do - str = "abc".force_encoding Encoding::IBM437 + str = "abc".dup.force_encoding Encoding::IBM437 result = str.center 6, "あ" result.should == "あabcああ" - result.encoding.should equal(Encoding::UTF_8) + result.encoding.should.equal?(Encoding::UTF_8) end it "raises an Encoding::CompatibilityError if the encodings are incompatible" do pat = "ア".encode Encoding::EUC_JP -> do "あれ".center 5, pat - end.should raise_error(Encoding::CompatibilityError) + end.should.raise(Encoding::CompatibilityError) end end end diff --git a/spec/ruby/core/string/chilled_string_spec.rb b/spec/ruby/core/string/chilled_string_spec.rb new file mode 100644 index 0000000000..6a5e846db2 --- /dev/null +++ b/spec/ruby/core/string/chilled_string_spec.rb @@ -0,0 +1,151 @@ +require_relative '../../spec_helper' + +describe "chilled String" do + guard -> { ruby_version_is "3.4" and !"test".equal?("test") } do + describe "chilled string literals" do + + describe "#frozen?" do + it "returns false" do + "chilled".frozen?.should == false + end + end + + describe "#-@" do + it "returns a different instance" do + input = "chilled" + interned = (-input) + interned.frozen?.should == true + interned.object_id.should_not == input.object_id + end + end + + describe "#+@" do + it "returns a different instance" do + input = "chilled" + duped = (+input) + duped.frozen?.should == false + duped.object_id.should_not == input.object_id + end + end + + describe "#clone" do + it "preserves chilled status" do + input = "chilled".clone + -> { + input << "-mutated" + }.should complain(/literal string will be frozen in the future/) + input.should == "chilled-mutated" + end + end + + describe "mutation" do + it "emits a warning" do + input = "chilled" + -> { + input << "-mutated" + }.should complain(/literal string will be frozen in the future/) + input.should == "chilled-mutated" + end + + it "emits a warning for concatenated strings" do + input = "still" "+chilled" + -> { + input << "-mutated" + }.should complain(/literal string will be frozen in the future/) + input.should == "still+chilled-mutated" + end + + it "emits a warning on singleton_class creation" do + -> { + "chilled".singleton_class + }.should complain(/literal string will be frozen in the future/) + end + + it "emits a warning on instance variable assignment" do + -> { + "chilled".instance_variable_set(:@ivar, 42) + }.should complain(/literal string will be frozen in the future/) + end + + it "raises FrozenError after the string was explicitly frozen" do + input = "chilled" + input.freeze + -> { + -> { + input << "mutated" + }.should.raise(FrozenError) + }.should_not complain(/literal string will be frozen in the future/) + end + end + end + + describe "chilled strings returned by Symbol#to_s" do + + describe "#frozen?" do + it "returns false" do + :chilled.to_s.frozen?.should == false + end + end + + describe "#-@" do + it "returns a different instance" do + input = :chilled.to_s + interned = (-input) + interned.frozen?.should == true + interned.object_id.should_not == input.object_id + end + end + + describe "#+@" do + it "returns a different instance" do + input = :chilled.to_s + duped = (+input) + duped.frozen?.should == false + duped.object_id.should_not == input.object_id + end + end + + describe "#clone" do + it "preserves chilled status" do + input = :chilled.to_s.clone + -> { + input << "-mutated" + }.should complain(/string returned by :chilled\.to_s will be frozen in the future/) + input.should == "chilled-mutated" + end + end + + describe "mutation" do + it "emits a warning" do + input = :chilled.to_s + -> { + input << "-mutated" + }.should complain(/string returned by :chilled\.to_s will be frozen in the future/) + input.should == "chilled-mutated" + end + + it "emits a warning on singleton_class creation" do + -> { + :chilled.to_s.singleton_class + }.should complain(/string returned by :chilled\.to_s will be frozen in the future/) + end + + it "emits a warning on instance variable assignment" do + -> { + :chilled.to_s.instance_variable_set(:@ivar, 42) + }.should complain(/string returned by :chilled\.to_s will be frozen in the future/) + end + + it "raises FrozenError after the string was explicitly frozen" do + input = :chilled.to_s + input.freeze + -> { + -> { + input << "mutated" + }.should.raise(FrozenError) + }.should_not complain(/string returned by :chilled\.to_s will be frozen in the future/) + end + end + end + end +end diff --git a/spec/ruby/core/string/chomp_spec.rb b/spec/ruby/core/string/chomp_spec.rb index ec0490220b..3a8550892f 100644 --- a/spec/ruby/core/string/chomp_spec.rb +++ b/spec/ruby/core/string/chomp_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -21,7 +22,7 @@ describe "String#chomp" do it "returns a copy of the String when it is not modified" do str = "abc" - str.chomp.should_not equal(str) + str.chomp.should_not.equal?(str) end it "removes one trailing newline" do @@ -46,7 +47,7 @@ describe "String#chomp" do it "returns String instances when called on a subclass" do str = StringSpecs::MyString.new("hello\n").chomp - str.should be_an_instance_of(String) + str.should.instance_of?(String) end it "removes trailing characters that match $/ when it has been assigned a value" do @@ -66,7 +67,7 @@ describe "String#chomp" do it "returns a copy of the String" do str = "abc" - str.chomp(nil).should_not equal(str) + str.chomp(nil).should_not.equal?(str) end it "returns an empty String when self is empty" do @@ -132,7 +133,7 @@ describe "String#chomp" do it "raises a TypeError if #to_str does not return a String" do arg = mock("string chomp") arg.should_receive(:to_str).and_return(1) - -> { "abc".chomp(arg) }.should raise_error(TypeError) + -> { "abc".chomp(arg) }.should.raise(TypeError) end end @@ -170,11 +171,11 @@ describe "String#chomp!" do it "modifies self" do str = "abc\n" - str.chomp!.should equal(str) + str.chomp!.should.equal?(str) end it "returns nil if self is not modified" do - "abc".chomp!.should be_nil + "abc".chomp!.should == nil end it "removes one trailing newline" do @@ -190,12 +191,12 @@ describe "String#chomp!" do end it "returns nil when self is empty" do - "".chomp!.should be_nil + "".chomp!.should == nil end it "returns subclass instances when called on a subclass" do str = StringSpecs::MyString.new("hello\n").chomp! - str.should be_an_instance_of(StringSpecs::MyString) + str.should.instance_of?(StringSpecs::MyString) end it "removes trailing characters that match $/ when it has been assigned a value" do @@ -206,11 +207,11 @@ describe "String#chomp!" do describe "when passed nil" do it "returns nil" do - "abc\r\n".chomp!(nil).should be_nil + "abc\r\n".chomp!(nil).should == nil end it "returns nil when self is empty" do - "".chomp!(nil).should be_nil + "".chomp!(nil).should == nil end end @@ -224,7 +225,7 @@ describe "String#chomp!" do end it "does not remove a final carriage return" do - "abc\r".chomp!("").should be_nil + "abc\r".chomp!("").should == nil end it "removes more than one trailing newlines" do @@ -236,7 +237,7 @@ describe "String#chomp!" do end it "returns nil when self is empty" do - "".chomp!("").should be_nil + "".chomp!("").should == nil end end @@ -254,7 +255,7 @@ describe "String#chomp!" do end it "returns nil when self is empty" do - "".chomp!("\n").should be_nil + "".chomp!("\n").should == nil end end @@ -268,7 +269,7 @@ describe "String#chomp!" do it "raises a TypeError if #to_str does not return a String" do arg = mock("string chomp") arg.should_receive(:to_str).and_return(1) - -> { "abc".chomp!(arg) }.should raise_error(TypeError) + -> { "abc".chomp!(arg) }.should.raise(TypeError) end end @@ -278,11 +279,11 @@ describe "String#chomp!" do end it "returns nil if the argument does not match the trailing characters" do - "abc".chomp!("def").should be_nil + "abc".chomp!("def").should == nil end it "returns nil when self is empty" do - "".chomp!("abc").should be_nil + "".chomp!("abc").should == nil end end @@ -290,15 +291,15 @@ describe "String#chomp!" do a = "string\n\r" a.freeze - -> { a.chomp! }.should raise_error(FrozenError) + -> { a.chomp! }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on a frozen instance when it would not be modified" do a = "string\n\r" a.freeze - -> { a.chomp!(nil) }.should raise_error(FrozenError) - -> { a.chomp!("x") }.should raise_error(FrozenError) + -> { a.chomp!(nil) }.should.raise(FrozenError) + -> { a.chomp!("x") }.should.raise(FrozenError) end end @@ -345,7 +346,7 @@ describe "String#chomp!" do end it "returns nil when the String is not modified" do - "あれ".chomp!.should be_nil + "あれ".chomp!.should == nil end it "removes the final carriage return, newline from a multibyte String" do diff --git a/spec/ruby/core/string/chop_spec.rb b/spec/ruby/core/string/chop_spec.rb index 75f25b39cd..2113da543b 100644 --- a/spec/ruby/core/string/chop_spec.rb +++ b/spec/ruby/core/string/chop_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -46,11 +47,11 @@ describe "String#chop" do it "returns a new string when applied to an empty string" do s = "" - s.chop.should_not equal(s) + s.chop.should_not.equal?(s) end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("hello\n").chop.should be_an_instance_of(String) + StringSpecs::MyString.new("hello\n").chop.should.instance_of?(String) end it "returns a String in the same encoding as self" do @@ -98,21 +99,21 @@ describe "String#chop!" do it "returns self if modifications were made" do str = "hello" - str.chop!.should equal(str) + str.chop!.should.equal?(str) end it "returns nil when called on an empty string" do - "".chop!.should be_nil + "".chop!.should == nil end it "raises a FrozenError on a frozen instance that is modified" do - -> { "string\n\r".freeze.chop! }.should raise_error(FrozenError) + -> { "string\n\r".freeze.chop! }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on a frozen instance that would not be modified" do a = "" a.freeze - -> { a.chop! }.should raise_error(FrozenError) + -> { a.chop! }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/chr_spec.rb b/spec/ruby/core/string/chr_spec.rb index 9ed29542e6..26c7d51202 100644 --- a/spec/ruby/core/string/chr_spec.rb +++ b/spec/ruby/core/string/chr_spec.rb @@ -3,11 +3,11 @@ require_relative '../../spec_helper' describe "String#chr" do it "returns a copy of self" do s = 'e' - s.should_not equal s.chr + s.should_not.equal? s.chr end it "returns a String" do - 'glark'.chr.should be_an_instance_of(String) + 'glark'.chr.should.instance_of?(String) end it "returns an empty String if self is an empty String" do diff --git a/spec/ruby/core/string/clear_spec.rb b/spec/ruby/core/string/clear_spec.rb index e1d68e03bd..d4688d3689 100644 --- a/spec/ruby/core/string/clear_spec.rb +++ b/spec/ruby/core/string/clear_spec.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require_relative '../../spec_helper' describe "String#clear" do @@ -13,7 +14,7 @@ describe "String#clear" do it "returns self after emptying it" do cleared = @s.clear cleared.should == "" - cleared.should equal @s + cleared.should.equal? @s end it "preserves its encoding" do @@ -31,7 +32,7 @@ describe "String#clear" do it "raises a FrozenError if self is frozen" do @s.freeze - -> { @s.clear }.should raise_error(FrozenError) - -> { "".freeze.clear }.should raise_error(FrozenError) + -> { @s.clear }.should.raise(FrozenError) + -> { "".freeze.clear }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/clone_spec.rb b/spec/ruby/core/string/clone_spec.rb index a2ba2f9877..2cb289d2c1 100644 --- a/spec/ruby/core/string/clone_spec.rb +++ b/spec/ruby/core/string/clone_spec.rb @@ -43,8 +43,8 @@ describe "String#clone" do end it "copies frozen state" do - @obj.freeze.clone.frozen?.should be_true - "".freeze.clone.frozen?.should be_true + @obj.freeze.clone.frozen?.should == true + "".freeze.clone.frozen?.should == true end it "does not modify the original string when changing cloned string" do diff --git a/spec/ruby/core/string/codepoints_spec.rb b/spec/ruby/core/string/codepoints_spec.rb index 0b6cde82f7..51bd57d127 100644 --- a/spec/ruby/core/string/codepoints_spec.rb +++ b/spec/ruby/core/string/codepoints_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' require_relative 'shared/codepoints' require_relative 'shared/each_codepoint_without_block' @@ -11,8 +11,8 @@ describe "String#codepoints" do end it "raises an ArgumentError when no block is given if self has an invalid encoding" do - s = "\xDF".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.codepoints }.should raise_error(ArgumentError) + s = "\xDF".dup.force_encoding(Encoding::UTF_8) + s.valid_encoding?.should == false + -> { s.codepoints }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/comparison_spec.rb b/spec/ruby/core/string/comparison_spec.rb index 91cfdca25a..0737c131bb 100644 --- a/spec/ruby/core/string/comparison_spec.rb +++ b/spec/ruby/core/string/comparison_spec.rb @@ -61,12 +61,12 @@ describe "String#<=> with String" do end it "ignores encoding difference" do - ("ÄÖÛ".force_encoding("utf-8") <=> "ÄÖÜ".force_encoding("iso-8859-1")).should == -1 - ("ÄÖÜ".force_encoding("utf-8") <=> "ÄÖÛ".force_encoding("iso-8859-1")).should == 1 + ("ÄÖÛ".dup.force_encoding("utf-8") <=> "ÄÖÜ".dup.force_encoding("iso-8859-1")).should == -1 + ("ÄÖÜ".dup.force_encoding("utf-8") <=> "ÄÖÛ".dup.force_encoding("iso-8859-1")).should == 1 end it "returns 0 with identical ASCII-compatible bytes of different encodings" do - ("abc".force_encoding("utf-8") <=> "abc".force_encoding("iso-8859-1")).should == 0 + ("abc".dup.force_encoding("utf-8") <=> "abc".dup.force_encoding("iso-8859-1")).should == 0 end it "compares the indices of the encodings when the strings have identical non-ASCII-compatible bytes" do @@ -77,7 +77,7 @@ describe "String#<=> with String" do end it "returns 0 when comparing 2 empty strings but one is not ASCII-compatible" do - ("" <=> "".force_encoding('iso-2022-jp')).should == 0 + ("" <=> "".dup.force_encoding('iso-2022-jp')).should == 0 end end @@ -85,7 +85,7 @@ end # just using it as an indicator. describe "String#<=>" do it "returns nil if its argument provides neither #to_str nor #<=>" do - ("abc" <=> mock('x')).should be_nil + ("abc" <=> mock('x')).should == nil end it "uses the result of calling #to_str for comparison when #to_str is defined" do @@ -107,6 +107,6 @@ describe "String#<=>" do def obj.<=>(other); other <=> self; end obj.should_receive(:<=>).once - ("abc" <=> obj).should be_nil + ("abc" <=> obj).should == nil end end diff --git a/spec/ruby/core/string/concat_spec.rb b/spec/ruby/core/string/concat_spec.rb index 6f487eaa3a..0194a357a0 100644 --- a/spec/ruby/core/string/concat_spec.rb +++ b/spec/ruby/core/string/concat_spec.rb @@ -8,20 +8,20 @@ describe "String#concat" do it_behaves_like :string_concat_type_coercion, :concat it "takes multiple arguments" do - str = "hello " + str = +"hello " str.concat "wo", "", "rld" str.should == "hello world" end it "concatenates the initial value when given arguments contain 2 self" do - str = "hello" + str = +"hello" str.concat str, str str.should == "hellohellohello" end it "returns self when given no arguments" do - str = "hello" - str.concat.should equal(str) + str = +"hello" + str.concat.should.equal?(str) str.should == "hello" end end diff --git a/spec/ruby/core/string/count_spec.rb b/spec/ruby/core/string/count_spec.rb index 06ba5a4f0e..fd127c6ff2 100644 --- a/spec/ruby/core/string/count_spec.rb +++ b/spec/ruby/core/string/count_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -23,7 +23,7 @@ describe "String#count" do end it "raises an ArgumentError when given no arguments" do - -> { "hell yeah".count }.should raise_error(ArgumentError) + -> { "hell yeah".count }.should.raise(ArgumentError) end it "negates sets starting with ^" do @@ -76,8 +76,8 @@ describe "String#count" do it "raises if the given sequences are invalid" do s = "hel-[()]-lo012^" - -> { s.count("h-e") }.should raise_error(ArgumentError) - -> { s.count("^h-e") }.should raise_error(ArgumentError) + -> { s.count("h-e") }.should.raise(ArgumentError) + -> { s.count("^h-e") }.should.raise(ArgumentError) end it 'returns the number of occurrences of a multi-byte character' do @@ -98,8 +98,8 @@ describe "String#count" do end it "raises a TypeError when a set arg can't be converted to a string" do - -> { "hello world".count(100) }.should raise_error(TypeError) - -> { "hello world".count([]) }.should raise_error(TypeError) - -> { "hello world".count(mock('x')) }.should raise_error(TypeError) + -> { "hello world".count(100) }.should.raise(TypeError) + -> { "hello world".count([]) }.should.raise(TypeError) + -> { "hello world".count(mock('x')) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/string/crypt_spec.rb b/spec/ruby/core/string/crypt_spec.rb index 06f84c70a4..924b4d48cf 100644 --- a/spec/ruby/core/string/crypt_spec.rb +++ b/spec/ruby/core/string/crypt_spec.rb @@ -15,7 +15,7 @@ describe "String#crypt" do end it "raises Errno::EINVAL when the salt is shorter than 29 characters" do - -> { "mypassword".crypt("$2a$04$0WVaz0pV3jzfZ5G5tpmHW") }.should raise_error(Errno::EINVAL) + -> { "mypassword".crypt("$2a$04$0WVaz0pV3jzfZ5G5tpmHW") }.should.raise(Errno::EINVAL) end it "calls #to_str to converts the salt arg to a String" do @@ -26,9 +26,9 @@ describe "String#crypt" do end it "doesn't return subclass instances" do - StringSpecs::MyString.new("mypassword").crypt("$2a$04$0WVaz0pV3jzfZ5G5tpmHWu").should be_an_instance_of(String) - "mypassword".crypt(StringSpecs::MyString.new("$2a$04$0WVaz0pV3jzfZ5G5tpmHWu")).should be_an_instance_of(String) - StringSpecs::MyString.new("mypassword").crypt(StringSpecs::MyString.new("$2a$04$0WVaz0pV3jzfZ5G5tpmHWu")).should be_an_instance_of(String) + StringSpecs::MyString.new("mypassword").crypt("$2a$04$0WVaz0pV3jzfZ5G5tpmHWu").should.instance_of?(String) + "mypassword".crypt(StringSpecs::MyString.new("$2a$04$0WVaz0pV3jzfZ5G5tpmHWu")).should.instance_of?(String) + StringSpecs::MyString.new("mypassword").crypt(StringSpecs::MyString.new("$2a$04$0WVaz0pV3jzfZ5G5tpmHWu")).should.instance_of?(String) end end @@ -60,7 +60,7 @@ describe "String#crypt" do end it "raises an ArgumentError when the string contains NUL character" do - -> { "poison\0null".crypt("aa") }.should raise_error(ArgumentError) + -> { "poison\0null".crypt("aa") }.should.raise(ArgumentError) end it "calls #to_str to converts the salt arg to a String" do @@ -71,22 +71,22 @@ describe "String#crypt" do end it "doesn't return subclass instances" do - StringSpecs::MyString.new("hello").crypt("aa").should be_an_instance_of(String) - "hello".crypt(StringSpecs::MyString.new("aa")).should be_an_instance_of(String) - StringSpecs::MyString.new("hello").crypt(StringSpecs::MyString.new("aa")).should be_an_instance_of(String) + StringSpecs::MyString.new("hello").crypt("aa").should.instance_of?(String) + "hello".crypt(StringSpecs::MyString.new("aa")).should.instance_of?(String) + StringSpecs::MyString.new("hello").crypt(StringSpecs::MyString.new("aa")).should.instance_of?(String) end it "raises an ArgumentError when the salt is shorter than two characters" do - -> { "hello".crypt("") }.should raise_error(ArgumentError) - -> { "hello".crypt("f") }.should raise_error(ArgumentError) - -> { "hello".crypt("\x00\x00") }.should raise_error(ArgumentError) - -> { "hello".crypt("\x00a") }.should raise_error(ArgumentError) - -> { "hello".crypt("a\x00") }.should raise_error(ArgumentError) + -> { "hello".crypt("") }.should.raise(ArgumentError) + -> { "hello".crypt("f") }.should.raise(ArgumentError) + -> { "hello".crypt("\x00\x00") }.should.raise(ArgumentError) + -> { "hello".crypt("\x00a") }.should.raise(ArgumentError) + -> { "hello".crypt("a\x00") }.should.raise(ArgumentError) end end it "raises a type error when the salt arg can't be converted to a string" do - -> { "".crypt(5) }.should raise_error(TypeError) - -> { "".crypt(mock('x')) }.should raise_error(TypeError) + -> { "".crypt(5) }.should.raise(TypeError) + -> { "".crypt(mock('x')) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/string/dedup_spec.rb b/spec/ruby/core/string/dedup_spec.rb index 57d2be2cfd..2b31d80708 100644 --- a/spec/ruby/core/string/dedup_spec.rb +++ b/spec/ruby/core/string/dedup_spec.rb @@ -2,7 +2,5 @@ require_relative '../../spec_helper' require_relative 'shared/dedup' describe 'String#dedup' do - ruby_version_is '3.2' do - it_behaves_like :string_dedup, :dedup - end + it_behaves_like :string_dedup, :dedup end diff --git a/spec/ruby/core/string/delete_prefix_spec.rb b/spec/ruby/core/string/delete_prefix_spec.rb index 4214fdecce..fc69adcbad 100644 --- a/spec/ruby/core/string/delete_prefix_spec.rb +++ b/spec/ruby/core/string/delete_prefix_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -11,13 +12,13 @@ describe "String#delete_prefix" do it "returns a copy of the string, when the prefix isn't found" do s = 'hello' r = s.delete_prefix('hello!') - r.should_not equal s + r.should_not.equal? s r.should == s r = s.delete_prefix('ell') - r.should_not equal s + r.should_not.equal? s r.should == s r = s.delete_prefix('') - r.should_not equal s + r.should_not.equal? s r.should == s end @@ -40,7 +41,7 @@ describe "String#delete_prefix" do it "returns a String instance when called on a subclass instance" do s = StringSpecs::MyString.new('hello') - s.delete_prefix('hell').should be_an_instance_of(String) + s.delete_prefix('hell').should.instance_of?(String) end it "returns a String in the same encoding as self" do @@ -51,7 +52,7 @@ end describe "String#delete_prefix!" do it "removes the found prefix" do s = 'hello' - s.delete_prefix!('hell').should equal(s) + s.delete_prefix!('hell').should.equal?(s) s.should == 'o' end @@ -75,8 +76,8 @@ describe "String#delete_prefix!" do end it "raises a FrozenError when self is frozen" do - -> { 'hello'.freeze.delete_prefix!('hell') }.should raise_error(FrozenError) - -> { 'hello'.freeze.delete_prefix!('') }.should raise_error(FrozenError) - -> { ''.freeze.delete_prefix!('') }.should raise_error(FrozenError) + -> { 'hello'.freeze.delete_prefix!('hell') }.should.raise(FrozenError) + -> { 'hello'.freeze.delete_prefix!('') }.should.raise(FrozenError) + -> { ''.freeze.delete_prefix!('') }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/delete_spec.rb b/spec/ruby/core/string/delete_spec.rb index 3b9aa4fb75..adb5150cff 100644 --- a/spec/ruby/core/string/delete_spec.rb +++ b/spec/ruby/core/string/delete_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -14,7 +15,7 @@ describe "String#delete" do end it "raises an ArgumentError when given no arguments" do - -> { "hell yeah".delete }.should raise_error(ArgumentError) + -> { "hell yeah".delete }.should.raise(ArgumentError) end it "negates sets starting with ^" do @@ -62,10 +63,10 @@ describe "String#delete" do not_supported_on :opal do xFF = [0xFF].pack('C') range = "\x00 - #{xFF}".force_encoding('utf-8') - -> { "hello".delete(range).should == "" }.should raise_error(ArgumentError) + -> { "hello".delete(range).should == "" }.should.raise(ArgumentError) end - -> { "hello".delete("h-e") }.should raise_error(ArgumentError) - -> { "hello".delete("^h-e") }.should raise_error(ArgumentError) + -> { "hello".delete("h-e") }.should.raise(ArgumentError) + -> { "hello".delete("^h-e") }.should.raise(ArgumentError) end it "tries to convert each set arg to a string using to_str" do @@ -79,13 +80,13 @@ describe "String#delete" do end it "raises a TypeError when one set arg can't be converted to a string" do - -> { "hello world".delete(100) }.should raise_error(TypeError) - -> { "hello world".delete([]) }.should raise_error(TypeError) - -> { "hello world".delete(mock('x')) }.should raise_error(TypeError) + -> { "hello world".delete(100) }.should.raise(TypeError) + -> { "hello world".delete([]) }.should.raise(TypeError) + -> { "hello world".delete(mock('x')) }.should.raise(TypeError) end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("oh no!!!").delete("!").should be_an_instance_of(String) + StringSpecs::MyString.new("oh no!!!").delete("!").should.instance_of?(String) end it "returns a String in the same encoding as self" do @@ -96,7 +97,7 @@ end describe "String#delete!" do it "modifies self in place and returns self" do a = "hello" - a.delete!("aeiou", "^e").should equal(a) + a.delete!("aeiou", "^e").should.equal?(a) a.should == "hell" end @@ -110,7 +111,7 @@ describe "String#delete!" do a = "hello" a.freeze - -> { a.delete!("") }.should raise_error(FrozenError) - -> { a.delete!("aeiou", "^e") }.should raise_error(FrozenError) + -> { a.delete!("") }.should.raise(FrozenError) + -> { a.delete!("aeiou", "^e") }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/delete_suffix_spec.rb b/spec/ruby/core/string/delete_suffix_spec.rb index 9381f4cee7..11d8fbbac1 100644 --- a/spec/ruby/core/string/delete_suffix_spec.rb +++ b/spec/ruby/core/string/delete_suffix_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -11,13 +12,13 @@ describe "String#delete_suffix" do it "returns a copy of the string, when the suffix isn't found" do s = 'hello' r = s.delete_suffix('!hello') - r.should_not equal s + r.should_not.equal? s r.should == s r = s.delete_suffix('ell') - r.should_not equal s + r.should_not.equal? s r.should == s r = s.delete_suffix('') - r.should_not equal s + r.should_not.equal? s r.should == s end @@ -40,7 +41,7 @@ describe "String#delete_suffix" do it "returns a String instance when called on a subclass instance" do s = StringSpecs::MyString.new('hello') - s.delete_suffix('ello').should be_an_instance_of(String) + s.delete_suffix('ello').should.instance_of?(String) end it "returns a String in the same encoding as self" do @@ -51,7 +52,7 @@ end describe "String#delete_suffix!" do it "removes the found prefix" do s = 'hello' - s.delete_suffix!('ello').should equal(s) + s.delete_suffix!('ello').should.equal?(s) s.should == 'h' end @@ -75,8 +76,8 @@ describe "String#delete_suffix!" do end it "raises a FrozenError when self is frozen" do - -> { 'hello'.freeze.delete_suffix!('ello') }.should raise_error(FrozenError) - -> { 'hello'.freeze.delete_suffix!('') }.should raise_error(FrozenError) - -> { ''.freeze.delete_suffix!('') }.should raise_error(FrozenError) + -> { 'hello'.freeze.delete_suffix!('ello') }.should.raise(FrozenError) + -> { 'hello'.freeze.delete_suffix!('') }.should.raise(FrozenError) + -> { ''.freeze.delete_suffix!('') }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/downcase_spec.rb b/spec/ruby/core/string/downcase_spec.rb index 7ee9d6df1d..4f7f44e5d4 100644 --- a/spec/ruby/core/string/downcase_spec.rb +++ b/spec/ruby/core/string/downcase_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -23,7 +24,7 @@ describe "String#downcase" do downcased.should == "king" downcased.size.should == 4 downcased.bytesize.should == 4 - downcased.ascii_only?.should be_true + downcased.ascii_only?.should == true end end @@ -47,7 +48,7 @@ describe "String#downcase" do end it "does not allow any other additional option" do - -> { "İ".downcase(:turkic, :ascii) }.should raise_error(ArgumentError) + -> { "İ".downcase(:turkic, :ascii) }.should.raise(ArgumentError) end end @@ -61,7 +62,7 @@ describe "String#downcase" do end it "does not allow any other additional option" do - -> { "İS".downcase(:lithuanian, :ascii) }.should raise_error(ArgumentError) + -> { "İS".downcase(:lithuanian, :ascii) }.should.raise(ArgumentError) end end @@ -73,18 +74,18 @@ describe "String#downcase" do end it "does not allow invalid options" do - -> { "ABC".downcase(:invalid_option) }.should raise_error(ArgumentError) + -> { "ABC".downcase(:invalid_option) }.should.raise(ArgumentError) end it "returns a String instance for subclasses" do - StringSpecs::MyString.new("FOObar").downcase.should be_an_instance_of(String) + StringSpecs::MyString.new("FOObar").downcase.should.instance_of?(String) end end describe "String#downcase!" do it "modifies self in place" do a = "HeLlO" - a.downcase!.should equal(a) + a.downcase!.should.equal?(a) a.should == "hello" end @@ -108,7 +109,7 @@ describe "String#downcase!" do downcased.should == "king" downcased.size.should == 4 downcased.bytesize.should == 4 - downcased.ascii_only?.should be_true + downcased.ascii_only?.should == true end end @@ -140,7 +141,7 @@ describe "String#downcase!" do end it "does not allow any other additional option" do - -> { a = "İ"; a.downcase!(:turkic, :ascii) }.should raise_error(ArgumentError) + -> { a = "İ"; a.downcase!(:turkic, :ascii) }.should.raise(ArgumentError) end end @@ -158,7 +159,7 @@ describe "String#downcase!" do end it "does not allow any other additional option" do - -> { a = "İS"; a.downcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError) + -> { a = "İS"; a.downcase!(:lithuanian, :ascii) }.should.raise(ArgumentError) end end @@ -174,7 +175,7 @@ describe "String#downcase!" do end it "does not allow invalid options" do - -> { a = "ABC"; a.downcase!(:invalid_option) }.should raise_error(ArgumentError) + -> { a = "ABC"; a.downcase!(:invalid_option) }.should.raise(ArgumentError) end it "returns nil if no modifications were made" do @@ -184,11 +185,11 @@ describe "String#downcase!" do end it "raises a FrozenError when self is frozen" do - -> { "HeLlo".freeze.downcase! }.should raise_error(FrozenError) - -> { "hello".freeze.downcase! }.should raise_error(FrozenError) + -> { "HeLlo".freeze.downcase! }.should.raise(FrozenError) + -> { "hello".freeze.downcase! }.should.raise(FrozenError) end it "sets the result String encoding to the source String encoding" do - "ABC".downcase.encoding.should equal(Encoding::UTF_8) + "ABC".downcase.encoding.should.equal?(Encoding::UTF_8) end end diff --git a/spec/ruby/core/string/dump_spec.rb b/spec/ruby/core/string/dump_spec.rb index cab8beff5a..176be79db2 100644 --- a/spec/ruby/core/string/dump_spec.rb +++ b/spec/ruby/core/string/dump_spec.rb @@ -8,7 +8,7 @@ describe "String#dump" do end it "returns a String instance" do - StringSpecs::MyString.new.dump.should be_an_instance_of(String) + StringSpecs::MyString.new.dump.should.instance_of?(String) end it "wraps string with \"" do diff --git a/spec/ruby/core/string/dup_spec.rb b/spec/ruby/core/string/dup_spec.rb index 73f71b8ffc..e367f7a311 100644 --- a/spec/ruby/core/string/dup_spec.rb +++ b/spec/ruby/core/string/dup_spec.rb @@ -21,7 +21,7 @@ describe "String#dup" do it "does not copy singleton methods" do def @obj.special() :the_one end dup = @obj.dup - -> { dup.special }.should raise_error(NameError) + -> { dup.special }.should.raise(NameError) end it "does not copy modules included in the singleton class" do @@ -30,7 +30,7 @@ describe "String#dup" do end dup = @obj.dup - -> { dup.repr }.should raise_error(NameError) + -> { dup.repr }.should.raise(NameError) end it "does not copy constants defined in the singleton class" do @@ -39,7 +39,7 @@ describe "String#dup" do end dup = @obj.dup - -> { class << dup; CLONE; end }.should raise_error(NameError) + -> { class << dup; CLONE; end }.should.raise(NameError) end it "does not modify the original string when changing dupped string" do @@ -51,7 +51,7 @@ describe "String#dup" do end it "does not modify the original setbyte-mutated string when changing dupped string" do - orig = "a" + orig = +"a" orig.setbyte 0, "b".ord copy = orig.dup orig.setbyte 0, "c".ord diff --git a/spec/ruby/core/string/each_byte_spec.rb b/spec/ruby/core/string/each_byte_spec.rb index e04dca807f..1884df416b 100644 --- a/spec/ruby/core/string/each_byte_spec.rb +++ b/spec/ruby/core/string/each_byte_spec.rb @@ -9,39 +9,39 @@ describe "String#each_byte" do end it "keeps iterating from the old position (to new string end) when self changes" do - r = "" - s = "hello world" + r = +"" + s = +"hello world" s.each_byte do |c| r << c s.insert(0, "<>") if r.size < 3 end r.should == "h><>hello world" - r = "" - s = "hello world" + r = +"" + s = +"hello world" s.each_byte { |c| s.slice!(-1); r << c } r.should == "hello " - r = "" - s = "hello world" + r = +"" + s = +"hello world" s.each_byte { |c| s.slice!(0); r << c } r.should == "hlowrd" - r = "" - s = "hello world" + r = +"" + s = +"hello world" s.each_byte { |c| s.slice!(0..-1); r << c } r.should == "h" end it "returns self" do s = "hello" - (s.each_byte {}).should equal(s) + (s.each_byte {}).should.equal?(s) end describe "when no block is given" do it "returns an enumerator" do enum = "hello".each_byte - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == [104, 101, 108, 108, 111] end diff --git a/spec/ruby/core/string/element_set_spec.rb b/spec/ruby/core/string/element_set_spec.rb index fa041fa31d..2abc5117aa 100644 --- a/spec/ruby/core/string/element_set_spec.rb +++ b/spec/ruby/core/string/element_set_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -17,13 +18,13 @@ describe "String#[]= with Integer index" do it "raises an IndexError without changing self if idx is outside of self" do str = "hello" - -> { str[20] = "bam" }.should raise_error(IndexError) + -> { str[20] = "bam" }.should.raise(IndexError) str.should == "hello" - -> { str[-20] = "bam" }.should raise_error(IndexError) + -> { str[-20] = "bam" }.should.raise(IndexError) str.should == "hello" - -> { ""[-1] = "bam" }.should raise_error(IndexError) + -> { ""[-1] = "bam" }.should.raise(IndexError) end # Behaviour is verified by matz in @@ -36,7 +37,7 @@ describe "String#[]= with Integer index" do it "raises IndexError if the string index doesn't match a position in the string" do str = "hello" - -> { str['y'] = "bam" }.should raise_error(IndexError) + -> { str['y'] = "bam" }.should.raise(IndexError) str.should == "hello" end @@ -44,7 +45,7 @@ describe "String#[]= with Integer index" do a = "hello" a.freeze - -> { a[0] = "bam" }.should raise_error(FrozenError) + -> { a[0] = "bam" }.should.raise(FrozenError) end it "calls to_int on index" do @@ -68,17 +69,17 @@ describe "String#[]= with Integer index" do end it "raises a TypeError if other_str can't be converted to a String" do - -> { "test"[1] = [] }.should raise_error(TypeError) - -> { "test"[1] = mock('x') }.should raise_error(TypeError) - -> { "test"[1] = nil }.should raise_error(TypeError) + -> { "test"[1] = [] }.should.raise(TypeError) + -> { "test"[1] = mock('x') }.should.raise(TypeError) + -> { "test"[1] = nil }.should.raise(TypeError) end it "raises a TypeError if passed an Integer replacement" do - -> { "abc"[1] = 65 }.should raise_error(TypeError) + -> { "abc"[1] = 65 }.should.raise(TypeError) end it "raises an IndexError if the index is greater than character size" do - -> { "あれ"[4] = "a" }.should raise_error(IndexError) + -> { "あれ"[4] = "a" }.should.raise(IndexError) end it "calls #to_int to convert the index" do @@ -94,14 +95,14 @@ describe "String#[]= with Integer index" do index = mock("string element set") index.should_receive(:to_int).and_return('1') - -> { "abc"[index] = "d" }.should raise_error(TypeError) + -> { "abc"[index] = "d" }.should.raise(TypeError) end it "raises an IndexError if #to_int returns a value out of range" do index = mock("string element set") index.should_receive(:to_int).and_return(4) - -> { "ab"[index] = "c" }.should raise_error(IndexError) + -> { "ab"[index] = "c" }.should.raise(IndexError) end it "replaces a character with a multibyte character" do @@ -126,7 +127,7 @@ describe "String#[]= with Integer index" do str = " ".force_encoding Encoding::US_ASCII rep = [160].pack('C').force_encoding Encoding::BINARY str[0] = rep - str.encoding.should equal(Encoding::BINARY) + str.encoding.should.equal?(Encoding::BINARY) end it "updates the string to a compatible encoding" do @@ -138,7 +139,7 @@ describe "String#[]= with Integer index" do it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do str = "あれ" rep = "が".encode Encoding::EUC_JP - -> { str[0] = rep }.should raise_error(Encoding::CompatibilityError) + -> { str[0] = rep }.should.raise(Encoding::CompatibilityError) end end @@ -163,7 +164,7 @@ describe "String#[]= with String index" do it "raises an IndexError if the search String is not found" do str = "abcde" - -> { str["g"] = "h" }.should raise_error(IndexError) + -> { str["g"] = "h" }.should.raise(IndexError) end it "replaces characters with a multibyte character" do @@ -188,13 +189,13 @@ describe "String#[]= with String index" do str = " ".force_encoding Encoding::US_ASCII rep = [160].pack('C').force_encoding Encoding::BINARY str[" "] = rep - str.encoding.should equal(Encoding::BINARY) + str.encoding.should.equal?(Encoding::BINARY) end it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do str = "あれ" rep = "が".encode Encoding::EUC_JP - -> { str["れ"] = rep }.should raise_error(Encoding::CompatibilityError) + -> { str["れ"] = rep }.should.raise(Encoding::CompatibilityError) end end @@ -207,7 +208,7 @@ describe "String#[]= with a Regexp index" do it "raises IndexError if the regexp index doesn't match a position in the string" do str = "hello" - -> { str[/y/] = "bam" }.should raise_error(IndexError) + -> { str[/y/] = "bam" }.should.raise(IndexError) str.should == "hello" end @@ -224,7 +225,7 @@ describe "String#[]= with a Regexp index" do rep = mock("string element set regexp") rep.should_not_receive(:to_str) - -> { "abc"[/def/] = rep }.should raise_error(IndexError) + -> { "abc"[/def/] = rep }.should.raise(IndexError) end describe "with 3 arguments" do @@ -241,7 +242,7 @@ describe "String#[]= with a Regexp index" do ref = mock("string element set regexp ref") ref.should_receive(:to_int).and_return(nil) - -> { "abc"[/a(b)/, ref] = "x" }.should raise_error(TypeError) + -> { "abc"[/a(b)/, ref] = "x" }.should.raise(TypeError) end it "uses the 2nd of 3 arguments as which capture should be replaced" do @@ -260,20 +261,20 @@ describe "String#[]= with a Regexp index" do rep = mock("string element set regexp") rep.should_not_receive(:to_str) - -> { "abc"[/a(b)/, 2] = rep }.should raise_error(IndexError) + -> { "abc"[/a(b)/, 2] = rep }.should.raise(IndexError) end it "raises IndexError if the specified capture isn't available" do str = "aaa bbb ccc" - -> { str[/a (bbb) c/, 2] = "ddd" }.should raise_error(IndexError) - -> { str[/a (bbb) c/, -2] = "ddd" }.should raise_error(IndexError) + -> { str[/a (bbb) c/, 2] = "ddd" }.should.raise(IndexError) + -> { str[/a (bbb) c/, -2] = "ddd" }.should.raise(IndexError) end describe "when the optional capture does not match" do it "raises an IndexError before setting the replacement" do str1 = "a b c" str2 = str1.dup - -> { str2[/a (b) (Z)?/, 2] = "d" }.should raise_error(IndexError) + -> { str2[/a (b) (Z)?/, 2] = "d" }.should.raise(IndexError) str2.should == str1 end end @@ -301,13 +302,13 @@ describe "String#[]= with a Regexp index" do str = " ".force_encoding Encoding::US_ASCII rep = [160].pack('C').force_encoding Encoding::BINARY str[/ /] = rep - str.encoding.should equal(Encoding::BINARY) + str.encoding.should.equal?(Encoding::BINARY) end it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do str = "あれ" rep = "が".encode Encoding::EUC_JP - -> { str[/れ/] = rep }.should raise_error(Encoding::CompatibilityError) + -> { str[/れ/] = rep }.should.raise(Encoding::CompatibilityError) end end @@ -357,11 +358,11 @@ describe "String#[]= with a Range index" do end it "raises a RangeError if negative Range begin is out of range" do - -> { "abc"[-4..-2] = "x" }.should raise_error(RangeError, "-4..-2 out of range") + -> { "abc"[-4..-2] = "x" }.should.raise(RangeError, "-4..-2 out of range") end it "raises a RangeError if positive Range begin is greater than String size" do - -> { "abc"[4..2] = "x" }.should raise_error(RangeError, "4..2 out of range") + -> { "abc"[4..2] = "x" }.should.raise(RangeError, "4..2 out of range") end it "uses the Range end as an index rather than a count" do @@ -422,13 +423,13 @@ describe "String#[]= with a Range index" do str = " ".force_encoding Encoding::US_ASCII rep = [160].pack('C').force_encoding Encoding::BINARY str[0..1] = rep - str.encoding.should equal(Encoding::BINARY) + str.encoding.should.equal?(Encoding::BINARY) end it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do str = "あれ" rep = "が".encode Encoding::EUC_JP - -> { str[0..1] = rep }.should raise_error(Encoding::CompatibilityError) + -> { str[0..1] = rep }.should.raise(Encoding::CompatibilityError) end end @@ -497,14 +498,14 @@ describe "String#[]= with Integer index, count" do index = mock("string element set index") index.should_receive(:to_int).and_return("1") - -> { "abc"[index, 2] = "xyz" }.should raise_error(TypeError) + -> { "abc"[index, 2] = "xyz" }.should.raise(TypeError) end it "raises a TypeError if #to_int for count does not return an Integer" do count = mock("string element set count") count.should_receive(:to_int).and_return("1") - -> { "abc"[1, count] = "xyz" }.should raise_error(TypeError) + -> { "abc"[1, count] = "xyz" }.should.raise(TypeError) end it "calls #to_str to convert the replacement object" do @@ -520,23 +521,23 @@ describe "String#[]= with Integer index, count" do r = mock("string element set replacement") r.should_receive(:to_str).and_return(nil) - -> { "abc"[1, 1] = r }.should raise_error(TypeError) + -> { "abc"[1, 1] = r }.should.raise(TypeError) end it "raises an IndexError if |idx| is greater than the length of the string" do - -> { "hello"[6, 0] = "bob" }.should raise_error(IndexError) - -> { "hello"[-6, 0] = "bob" }.should raise_error(IndexError) + -> { "hello"[6, 0] = "bob" }.should.raise(IndexError) + -> { "hello"[-6, 0] = "bob" }.should.raise(IndexError) end it "raises an IndexError if count < 0" do - -> { "hello"[0, -1] = "bob" }.should raise_error(IndexError) - -> { "hello"[1, -1] = "bob" }.should raise_error(IndexError) + -> { "hello"[0, -1] = "bob" }.should.raise(IndexError) + -> { "hello"[1, -1] = "bob" }.should.raise(IndexError) end it "raises a TypeError if other_str is a type other than String" do - -> { "hello"[0, 2] = nil }.should raise_error(TypeError) - -> { "hello"[0, 2] = [] }.should raise_error(TypeError) - -> { "hello"[0, 2] = 33 }.should raise_error(TypeError) + -> { "hello"[0, 2] = nil }.should.raise(TypeError) + -> { "hello"[0, 2] = [] }.should.raise(TypeError) + -> { "hello"[0, 2] = 33 }.should.raise(TypeError) end it "replaces characters with a multibyte character" do @@ -570,19 +571,19 @@ describe "String#[]= with Integer index, count" do end it "raises an IndexError if the character index is out of range of a multibyte String" do - -> { "あれ"[3, 0] = "り" }.should raise_error(IndexError) + -> { "あれ"[3, 0] = "り" }.should.raise(IndexError) end it "encodes the String in an encoding compatible with the replacement" do str = " ".force_encoding Encoding::US_ASCII rep = [160].pack('C').force_encoding Encoding::BINARY str[0, 1] = rep - str.encoding.should equal(Encoding::BINARY) + str.encoding.should.equal?(Encoding::BINARY) end it "raises an Encoding::CompatibilityError if the replacement encoding is incompatible" do str = "あれ" rep = "が".encode Encoding::EUC_JP - -> { str[0, 1] = rep }.should raise_error(Encoding::CompatibilityError) + -> { str[0, 1] = rep }.should.raise(Encoding::CompatibilityError) end end diff --git a/spec/ruby/core/string/encode_spec.rb b/spec/ruby/core/string/encode_spec.rb index 35ed27bb40..ab096f4041 100644 --- a/spec/ruby/core/string/encode_spec.rb +++ b/spec/ruby/core/string/encode_spec.rb @@ -20,7 +20,7 @@ describe "String#encode" do Encoding.default_internal = nil str = "あ" encoded = str.encode - encoded.should_not equal(str) + encoded.should_not.equal?(str) encoded.should == str end @@ -28,15 +28,15 @@ describe "String#encode" do Encoding.default_internal = nil str = "abc" encoded = str.encode - encoded.should_not equal(str) + encoded.should_not.equal?(str) encoded.should == str end it "encodes an ascii substring of a binary string to UTF-8" do x82 = [0x82].pack('C') - str = "#{x82}foo".force_encoding("binary")[1..-1].encode("utf-8") - str.should == "foo".force_encoding("utf-8") - str.encoding.should equal(Encoding::UTF_8) + str = "#{x82}foo".dup.force_encoding("binary")[1..-1].encode("utf-8") + str.should == "foo".dup.force_encoding("utf-8") + str.encoding.should.equal?(Encoding::UTF_8) end end @@ -44,12 +44,12 @@ describe "String#encode" do it "returns a copy when passed the same encoding as the String" do str = "あ" encoded = str.encode(Encoding::UTF_8) - encoded.should_not equal(str) + encoded.should_not.equal?(str) encoded.should == str end it "round trips a String" do - str = "abc def".force_encoding Encoding::US_ASCII + str = "abc def".dup.force_encoding Encoding::US_ASCII str.encode("utf-32be").encode("ascii").should == "abc def" end end @@ -58,13 +58,25 @@ describe "String#encode" do it "returns a copy when Encoding.default_internal is nil" do Encoding.default_internal = nil str = "あ" - str.encode(invalid: :replace).should_not equal(str) + str.encode(invalid: :replace).should_not.equal?(str) end - it "normalizes newlines" do - "\r\nfoo".encode(universal_newline: true).should == "\nfoo" + it "normalizes newlines with cr_newline option" do + "\r\nfoo".encode(cr_newline: true).should == "\r\rfoo" + "\rfoo".encode(cr_newline: true).should == "\rfoo" + "\nfoo".encode(cr_newline: true).should == "\rfoo" + end + + it "normalizes newlines with crlf_newline option" do + "\r\nfoo".encode(crlf_newline: true).should == "\r\r\nfoo" + "\rfoo".encode(crlf_newline: true).should == "\rfoo" + "\nfoo".encode(crlf_newline: true).should == "\r\nfoo" + end + it "normalizes newlines with universal_newline option" do + "\r\nfoo".encode(universal_newline: true).should == "\nfoo" "\rfoo".encode(universal_newline: true).should == "\nfoo" + "\nfoo".encode(universal_newline: true).should == "\nfoo" end it "replaces invalid encoding in source with default replacement" do @@ -122,11 +134,10 @@ describe "String#encode" do describe "when passed to, from" do it "returns a copy in the destination encoding when both encodings are the same" do - str = "あ" - str.force_encoding("binary") + str = "あ".dup.force_encoding("binary") encoded = str.encode("utf-8", "utf-8") - encoded.should_not equal(str) + encoded.should_not.equal?(str) encoded.should == str.force_encoding("utf-8") encoded.encoding.should == Encoding::UTF_8 end @@ -141,7 +152,7 @@ describe "String#encode" do it "returns a copy when the destination encoding is the same as the String encoding" do str = "あ" encoded = str.encode(Encoding::UTF_8, undef: :replace) - encoded.should_not equal(str) + encoded.should_not.equal?(str) encoded.should == str end end @@ -150,16 +161,15 @@ describe "String#encode" do it "returns a copy when both encodings are the same" do str = "あ" encoded = str.encode("utf-8", "utf-8", invalid: :replace) - encoded.should_not equal(str) + encoded.should_not.equal?(str) encoded.should == str end it "returns a copy in the destination encoding when both encodings are the same" do - str = "あ" - str.force_encoding("binary") + str = "あ".dup.force_encoding("binary") encoded = str.encode("utf-8", "utf-8", invalid: :replace) - encoded.should_not equal(str) + encoded.should_not.equal?(str) encoded.should == str.force_encoding("utf-8") encoded.encoding.should == Encoding::UTF_8 end @@ -180,51 +190,51 @@ describe "String#encode!" do it_behaves_like :string_encode, :encode! it "raises a FrozenError when called on a frozen String" do - -> { "foo".freeze.encode!("euc-jp") }.should raise_error(FrozenError) + -> { "foo".freeze.encode!("euc-jp") }.should.raise(FrozenError) end # http://redmine.ruby-lang.org/issues/show/1836 it "raises a FrozenError when called on a frozen String when it's a no-op" do - -> { "foo".freeze.encode!("utf-8") }.should raise_error(FrozenError) + -> { "foo".freeze.encode!("utf-8") }.should.raise(FrozenError) end describe "when passed no options" do it "returns self when Encoding.default_internal is nil" do Encoding.default_internal = nil - str = "あ" - str.encode!.should equal(str) + str = +"あ" + str.encode!.should.equal?(str) end it "returns self for a ASCII-only String when Encoding.default_internal is nil" do Encoding.default_internal = nil - str = "abc" - str.encode!.should equal(str) + str = +"abc" + str.encode!.should.equal?(str) end end describe "when passed options" do it "returns self for ASCII-only String when Encoding.default_internal is nil" do Encoding.default_internal = nil - str = "abc" - str.encode!(invalid: :replace).should equal(str) + str = +"abc" + str.encode!(invalid: :replace).should.equal?(str) end end describe "when passed to encoding" do it "returns self" do - str = "abc" + str = +"abc" result = str.encode!(Encoding::BINARY) - result.encoding.should equal(Encoding::BINARY) - result.should equal(str) + result.encoding.should.equal?(Encoding::BINARY) + result.should.equal?(str) end end describe "when passed to, from" do it "returns self" do - str = "ああ" + str = +"ああ" result = str.encode!("euc-jp", "utf-8") - result.encoding.should equal(Encoding::EUC_JP) - result.should equal(str) + result.encoding.should.equal?(Encoding::EUC_JP) + result.should.equal?(str) end end end diff --git a/spec/ruby/core/string/encoding_spec.rb b/spec/ruby/core/string/encoding_spec.rb index 574a1e2f92..aa0e4765ed 100644 --- a/spec/ruby/core/string/encoding_spec.rb +++ b/spec/ruby/core/string/encoding_spec.rb @@ -4,7 +4,7 @@ require_relative 'fixtures/iso-8859-9-encoding' describe "String#encoding" do it "returns an Encoding object" do - String.new.encoding.should be_an_instance_of(Encoding) + String.new.encoding.should.instance_of?(Encoding) end it "is equal to the source encoding by default" do @@ -14,11 +14,11 @@ describe "String#encoding" do end it "returns the given encoding if #force_encoding has been called" do - "a".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "a".dup.force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS end it "returns the given encoding if #encode!has been called" do - "a".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "a".dup.encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS end end @@ -70,13 +70,13 @@ describe "String#encoding for Strings with \\u escapes" do it "returns US-ASCII if self is US-ASCII only" do s = "\u{40}" - s.ascii_only?.should be_true + s.ascii_only?.should == true s.encoding.should == Encoding::US_ASCII end it "returns UTF-8 if self isn't US-ASCII only" do s = "\u{4076}\u{619}" - s.ascii_only?.should be_false + s.ascii_only?.should == false s.encoding.should == Encoding::UTF_8 end @@ -108,13 +108,13 @@ describe "String#encoding for Strings with \\u escapes" do end it "returns the given encoding if #force_encoding has been called" do - "\u{20}".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - "\u{2020}".force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "\u{20}".dup.force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "\u{2020}".dup.force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS end it "returns the given encoding if #encode!has been called" do - "\u{20}".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - "\u{2020}".encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "\u{20}".dup.encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "\u{2020}".dup.encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS end end @@ -122,7 +122,7 @@ describe "String#encoding for Strings with \\x escapes" do it "returns US-ASCII if self is US-ASCII only" do s = "\x61" - s.ascii_only?.should be_true + s.ascii_only?.should == true s.encoding.should == Encoding::US_ASCII end @@ -131,7 +131,7 @@ describe "String#encoding for Strings with \\x escapes" do str = " " str.encoding.should == Encoding::US_ASCII str += [0xDF].pack('C') - str.ascii_only?.should be_false + str.ascii_only?.should == false str.encoding.should == Encoding::BINARY end @@ -140,7 +140,7 @@ describe "String#encoding for Strings with \\x escapes" do it "returns the source encoding when an escape creates a byte with the 8th bit set if the source encoding isn't US-ASCII" do fixture = StringSpecs::ISO88599Encoding.new fixture.source_encoding.should == Encoding::ISO8859_9 - fixture.x_escape.ascii_only?.should be_false + fixture.x_escape.ascii_only?.should == false fixture.x_escape.encoding.should == Encoding::ISO8859_9 end @@ -173,16 +173,12 @@ describe "String#encoding for Strings with \\x escapes" do end it "returns the given encoding if #force_encoding has been called" do - x50 = "\x50" - x50.force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - xD4 = [212].pack('C') - xD4.force_encoding(Encoding::ISO_8859_9).encoding.should == Encoding::ISO_8859_9 + "\x50".dup.force_encoding(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + [212].pack('C').force_encoding(Encoding::ISO_8859_9).encoding.should == Encoding::ISO_8859_9 end it "returns the given encoding if #encode!has been called" do - x50 = "\x50" - x50.encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS - x00 = "x\00" - x00.encode!(Encoding::UTF_8).encoding.should == Encoding::UTF_8 + "\x50".dup.encode!(Encoding::SHIFT_JIS).encoding.should == Encoding::SHIFT_JIS + "x\00".dup.encode!(Encoding::UTF_8).encoding.should == Encoding::UTF_8 end end diff --git a/spec/ruby/core/string/eql_spec.rb b/spec/ruby/core/string/eql_spec.rb index 397974d9fb..46ebeda509 100644 --- a/spec/ruby/core/string/eql_spec.rb +++ b/spec/ruby/core/string/eql_spec.rb @@ -6,16 +6,16 @@ describe "String#eql?" do describe "when given a non-String" do it "returns false" do - 'hello'.should_not eql(5) + 'hello'.should_not.eql?(5) not_supported_on :opal do - 'hello'.should_not eql(:hello) + 'hello'.should_not.eql?(:hello) end - 'hello'.should_not eql(mock('x')) + 'hello'.should_not.eql?(mock('x')) end it "does not try to call #to_str on the given argument" do (obj = mock('x')).should_not_receive(:to_str) - 'hello'.should_not eql(obj) + 'hello'.should_not.eql?(obj) end end end diff --git a/spec/ruby/core/string/fixtures/utf-8-encoding.rb b/spec/ruby/core/string/fixtures/utf-8-encoding.rb deleted file mode 100644 index fd243ec522..0000000000 --- a/spec/ruby/core/string/fixtures/utf-8-encoding.rb +++ /dev/null @@ -1,7 +0,0 @@ -# -*- encoding: utf-8 -*- -module StringSpecs - class UTF8Encoding - def self.source_encoding; __ENCODING__; end - def self.egrave; "é"; end - end -end diff --git a/spec/ruby/core/string/force_encoding_spec.rb b/spec/ruby/core/string/force_encoding_spec.rb index f37aaf9eb4..fc6914213f 100644 --- a/spec/ruby/core/string/force_encoding_spec.rb +++ b/spec/ruby/core/string/force_encoding_spec.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require_relative '../../spec_helper' describe "String#force_encoding" do @@ -40,23 +41,23 @@ describe "String#force_encoding" do obj = mock("force_encoding") obj.should_receive(:to_str).and_return(1) - -> { "abc".force_encoding(obj) }.should raise_error(TypeError) + -> { "abc".force_encoding(obj) }.should.raise(TypeError) end it "raises a TypeError if passed nil" do - -> { "abc".force_encoding(nil) }.should raise_error(TypeError) + -> { "abc".force_encoding(nil) }.should.raise(TypeError) end it "returns self" do str = "abc" - str.force_encoding('utf-8').should equal(str) + str.force_encoding('utf-8').should.equal?(str) end it "sets the encoding even if the String contents are invalid in that encoding" do str = "\u{9765}" str.force_encoding('euc-jp') str.encoding.should == Encoding::EUC_JP - str.valid_encoding?.should be_false + str.valid_encoding?.should == false end it "does not transcode self" do @@ -66,6 +67,6 @@ describe "String#force_encoding" do it "raises a FrozenError if self is frozen" do str = "abcd".freeze - -> { str.force_encoding(str.encoding) }.should raise_error(FrozenError) + -> { str.force_encoding(str.encoding) }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/freeze_spec.rb b/spec/ruby/core/string/freeze_spec.rb index 04d1e9513c..8485e8de21 100644 --- a/spec/ruby/core/string/freeze_spec.rb +++ b/spec/ruby/core/string/freeze_spec.rb @@ -1,13 +1,14 @@ +# frozen_string_literal: false require_relative '../../spec_helper' describe "String#freeze" do it "produces the same object whenever called on an instance of a literal in the source" do - "abc".freeze.should equal "abc".freeze + "abc".freeze.should.equal? "abc".freeze end it "doesn't produce the same object for different instances of literals in the source" do - "abc".should_not equal "abc" + "abc".should_not.equal? "abc" end it "being a special form doesn't change the value of defined?" do diff --git a/spec/ruby/core/string/getbyte_spec.rb b/spec/ruby/core/string/getbyte_spec.rb index 27b7d826ea..d4619dca61 100644 --- a/spec/ruby/core/string/getbyte_spec.rb +++ b/spec/ruby/core/string/getbyte_spec.rb @@ -3,7 +3,7 @@ require_relative '../../spec_helper' describe "String#getbyte" do it "returns an Integer if given a valid index" do - "a".getbyte(0).should be_kind_of(Integer) + "a".getbyte(0).should.is_a?(Integer) end it "starts indexing at 0" do @@ -51,19 +51,19 @@ describe "String#getbyte" do end it "returns nil for out-of-bound indexes" do - "g".getbyte(1).should be_nil + "g".getbyte(1).should == nil end it "regards the empty String as containing no bytes" do - "".getbyte(0).should be_nil + "".getbyte(0).should == nil end it "raises an ArgumentError unless given one argument" do - -> { "glark".getbyte }.should raise_error(ArgumentError) - -> { "food".getbyte(0,0) }.should raise_error(ArgumentError) + -> { "glark".getbyte }.should.raise(ArgumentError) + -> { "food".getbyte(0,0) }.should.raise(ArgumentError) end it "raises a TypeError unless its argument can be coerced into an Integer" do - -> { "a".getbyte('a') }.should raise_error(TypeError) + -> { "a".getbyte('a') }.should.raise(TypeError) end end diff --git a/spec/ruby/core/string/gsub_spec.rb b/spec/ruby/core/string/gsub_spec.rb index 9e3b50322c..d0e1c30bd2 100644 --- a/spec/ruby/core/string/gsub_spec.rb +++ b/spec/ruby/core/string/gsub_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -174,9 +175,9 @@ describe "String#gsub with pattern and replacement" do end it "raises a TypeError when pattern can't be converted to a string" do - -> { "hello".gsub([], "x") }.should raise_error(TypeError) - -> { "hello".gsub(Object.new, "x") }.should raise_error(TypeError) - -> { "hello".gsub(nil, "x") }.should raise_error(TypeError) + -> { "hello".gsub([], "x") }.should.raise(TypeError) + -> { "hello".gsub(Object.new, "x") }.should.raise(TypeError) + -> { "hello".gsub(nil, "x") }.should.raise(TypeError) end it "tries to convert replacement to a string using to_str" do @@ -187,16 +188,16 @@ describe "String#gsub with pattern and replacement" do end it "raises a TypeError when replacement can't be converted to a string" do - -> { "hello".gsub(/[aeiou]/, []) }.should raise_error(TypeError) - -> { "hello".gsub(/[aeiou]/, Object.new) }.should raise_error(TypeError) - -> { "hello".gsub(/[aeiou]/, nil) }.should raise_error(TypeError) + -> { "hello".gsub(/[aeiou]/, []) }.should.raise(TypeError) + -> { "hello".gsub(/[aeiou]/, Object.new) }.should.raise(TypeError) + -> { "hello".gsub(/[aeiou]/, nil) }.should.raise(TypeError) end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("").gsub(//, "").should be_an_instance_of(String) - StringSpecs::MyString.new("").gsub(/foo/, "").should be_an_instance_of(String) - StringSpecs::MyString.new("foo").gsub(/foo/, "").should be_an_instance_of(String) - StringSpecs::MyString.new("foo").gsub("foo", "").should be_an_instance_of(String) + StringSpecs::MyString.new("").gsub(//, "").should.instance_of?(String) + StringSpecs::MyString.new("").gsub(/foo/, "").should.instance_of?(String) + StringSpecs::MyString.new("foo").gsub(/foo/, "").should.instance_of?(String) + StringSpecs::MyString.new("foo").gsub("foo", "").should.instance_of?(String) end it "sets $~ to MatchData of last match and nil when there's none" do @@ -449,8 +450,8 @@ describe "String#gsub with pattern and block" do s = "hllëllo" s2 = "hellö" - -> { s.gsub(/l/) { |bar| "Русский".force_encoding("iso-8859-5") } }.should raise_error(Encoding::CompatibilityError) - -> { s2.gsub(/l/) { |bar| "Русский".force_encoding("iso-8859-5") } }.should raise_error(Encoding::CompatibilityError) + -> { s.gsub(/l/) { |bar| "Русский".force_encoding("iso-8859-5") } }.should.raise(Encoding::CompatibilityError) + -> { s2.gsub(/l/) { |bar| "Русский".force_encoding("iso-8859-5") } }.should.raise(Encoding::CompatibilityError) end it "replaces the incompatible part properly even if the encodings are not compatible" do @@ -462,7 +463,7 @@ describe "String#gsub with pattern and block" do not_supported_on :opal do it "raises an ArgumentError if encoding is not valid" do x92 = [0x92].pack('C').force_encoding('utf-8') - -> { "a#{x92}b".gsub(/[^\x00-\x7f]/u, '') }.should raise_error(ArgumentError) + -> { "a#{x92}b".gsub(/[^\x00-\x7f]/u, '') }.should.raise(ArgumentError) end end end @@ -470,7 +471,7 @@ end describe "String#gsub with pattern and without replacement and block" do it "returns an enumerator" do enum = "abca".gsub(/a/) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == ["a", "a"] end @@ -494,13 +495,13 @@ end describe "String#gsub! with pattern and replacement" do it "modifies self in place and returns self" do a = "hello" - a.gsub!(/[aeiou]/, '*').should equal(a) + a.gsub!(/[aeiou]/, '*').should.equal?(a) a.should == "h*ll*" end it "modifies self in place with multi-byte characters and returns self" do a = "¿por qué?" - a.gsub!(/([a-z\d]*)/, "*").should equal(a) + a.gsub!(/([a-z\d]*)/, "*").should.equal?(a) a.should == "*¿** **é*?*" end @@ -516,9 +517,9 @@ describe "String#gsub! with pattern and replacement" do s = "hello" s.freeze - -> { s.gsub!(/ROAR/, "x") }.should raise_error(FrozenError) - -> { s.gsub!(/e/, "e") }.should raise_error(FrozenError) - -> { s.gsub!(/[aeiou]/, '*') }.should raise_error(FrozenError) + -> { s.gsub!(/ROAR/, "x") }.should.raise(FrozenError) + -> { s.gsub!(/e/, "e") }.should.raise(FrozenError) + -> { s.gsub!(/[aeiou]/, '*') }.should.raise(FrozenError) end it "handles a pattern in a superset encoding" do @@ -546,7 +547,7 @@ end describe "String#gsub! with pattern and block" do it "modifies self in place and returns self" do a = "hello" - a.gsub!(/[aeiou]/) { '*' }.should equal(a) + a.gsub!(/[aeiou]/) { '*' }.should.equal?(a) a.should == "h*ll*" end @@ -562,9 +563,9 @@ describe "String#gsub! with pattern and block" do s = "hello" s.freeze - -> { s.gsub!(/ROAR/) { "x" } }.should raise_error(FrozenError) - -> { s.gsub!(/e/) { "e" } }.should raise_error(FrozenError) - -> { s.gsub!(/[aeiou]/) { '*' } }.should raise_error(FrozenError) + -> { s.gsub!(/ROAR/) { "x" } }.should.raise(FrozenError) + -> { s.gsub!(/e/) { "e" } }.should.raise(FrozenError) + -> { s.gsub!(/[aeiou]/) { '*' } }.should.raise(FrozenError) end it "uses the compatible encoding if they are compatible" do @@ -579,8 +580,8 @@ describe "String#gsub! with pattern and block" do s = "hllëllo" s2 = "hellö" - -> { s.gsub!(/l/) { |bar| "Русский".force_encoding("iso-8859-5") } }.should raise_error(Encoding::CompatibilityError) - -> { s2.gsub!(/l/) { |bar| "Русский".force_encoding("iso-8859-5") } }.should raise_error(Encoding::CompatibilityError) + -> { s.gsub!(/l/) { |bar| "Русский".force_encoding("iso-8859-5") } }.should.raise(Encoding::CompatibilityError) + -> { s2.gsub!(/l/) { |bar| "Русский".force_encoding("iso-8859-5") } }.should.raise(Encoding::CompatibilityError) end it "replaces the incompatible part properly even if the encodings are not compatible" do @@ -592,7 +593,7 @@ describe "String#gsub! with pattern and block" do not_supported_on :opal do it "raises an ArgumentError if encoding is not valid" do x92 = [0x92].pack('C').force_encoding('utf-8') - -> { "a#{x92}b".gsub!(/[^\x00-\x7f]/u, '') }.should raise_error(ArgumentError) + -> { "a#{x92}b".gsub!(/[^\x00-\x7f]/u, '') }.should.raise(ArgumentError) end end end @@ -600,7 +601,7 @@ end describe "String#gsub! with pattern and without replacement and block" do it "returns an enumerator" do enum = "abca".gsub!(/a/) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == ["a", "a"] end diff --git a/spec/ruby/core/string/include_spec.rb b/spec/ruby/core/string/include_spec.rb index 23e1e134ec..d943430335 100644 --- a/spec/ruby/core/string/include_spec.rb +++ b/spec/ruby/core/string/include_spec.rb @@ -15,16 +15,16 @@ describe "String#include? with String" do it "returns true if both strings are empty" do "".should.include?("") - "".force_encoding("EUC-JP").should.include?("") - "".should.include?("".force_encoding("EUC-JP")) - "".force_encoding("EUC-JP").should.include?("".force_encoding("EUC-JP")) + "".dup.force_encoding("EUC-JP").should.include?("") + "".should.include?("".dup.force_encoding("EUC-JP")) + "".dup.force_encoding("EUC-JP").should.include?("".dup.force_encoding("EUC-JP")) end it "returns true if the RHS is empty" do "a".should.include?("") - "a".force_encoding("EUC-JP").should.include?("") - "a".should.include?("".force_encoding("EUC-JP")) - "a".force_encoding("EUC-JP").should.include?("".force_encoding("EUC-JP")) + "a".dup.force_encoding("EUC-JP").should.include?("") + "a".should.include?("".dup.force_encoding("EUC-JP")) + "a".dup.force_encoding("EUC-JP").should.include?("".dup.force_encoding("EUC-JP")) end it "tries to convert other to string using to_str" do @@ -35,15 +35,15 @@ describe "String#include? with String" do end it "raises a TypeError if other can't be converted to string" do - -> { "hello".include?([]) }.should raise_error(TypeError) - -> { "hello".include?('h'.ord) }.should raise_error(TypeError) - -> { "hello".include?(mock('x')) }.should raise_error(TypeError) + -> { "hello".include?([]) }.should.raise(TypeError) + -> { "hello".include?('h'.ord) }.should.raise(TypeError) + -> { "hello".include?(mock('x')) }.should.raise(TypeError) end it "raises an Encoding::CompatibilityError if the encodings are incompatible" do pat = "ア".encode Encoding::EUC_JP -> do "あれ".include?(pat) - end.should raise_error(Encoding::CompatibilityError) + end.should.raise(Encoding::CompatibilityError) end end diff --git a/spec/ruby/core/string/index_spec.rb b/spec/ruby/core/string/index_spec.rb index b500cf6ca7..3f82181b98 100644 --- a/spec/ruby/core/string/index_spec.rb +++ b/spec/ruby/core/string/index_spec.rb @@ -4,15 +4,15 @@ require_relative 'fixtures/classes' describe "String#index" do it "raises a TypeError if passed nil" do - -> { "abc".index nil }.should raise_error(TypeError) + -> { "abc".index nil }.should.raise(TypeError) end it "raises a TypeError if passed a boolean" do - -> { "abc".index true }.should raise_error(TypeError) + -> { "abc".index true }.should.raise(TypeError) end it "raises a TypeError if passed a Symbol" do - -> { "abc".index :a }.should raise_error(TypeError) + -> { "abc".index :a }.should.raise(TypeError) end it "calls #to_str to convert the first argument" do @@ -28,7 +28,7 @@ describe "String#index" do end it "raises a TypeError if passed an Integer" do - -> { "abc".index 97 }.should raise_error(TypeError) + -> { "abc".index 97 }.should.raise(TypeError) end end @@ -157,22 +157,22 @@ describe "String#index with String" do char = "れ".encode Encoding::EUC_JP -> do "あれ".index char - end.should raise_error(Encoding::CompatibilityError) + end.should.raise(Encoding::CompatibilityError) end it "handles a substring in a superset encoding" do - 'abc'.force_encoding(Encoding::US_ASCII).index('é').should == nil + 'abc'.dup.force_encoding(Encoding::US_ASCII).index('é').should == nil end it "handles a substring in a subset encoding" do - 'été'.index('t'.force_encoding(Encoding::US_ASCII)).should == 1 + 'été'.index('t'.dup.force_encoding(Encoding::US_ASCII)).should == 1 end it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - str = 'abc'.force_encoding("ISO-2022-JP") - pattern = 'b'.force_encoding("EUC-JP") + str = 'abc'.dup.force_encoding("ISO-2022-JP") + pattern = 'b'.dup.force_encoding("EUC-JP") - -> { str.index(pattern) }.should raise_error(Encoding::CompatibilityError, "incompatible character encodings: ISO-2022-JP and EUC-JP") + -> { str.index(pattern) }.should.raise(Encoding::CompatibilityError, "incompatible character encodings: ISO-2022-JP and EUC-JP") end end @@ -231,6 +231,15 @@ describe "String#index with Regexp" do $~.should == nil end + it "always clear $~" do + "a".index(/a/) + $~.should_not == nil + + string = "blablabla" + string.index(/bla/, string.length + 1) + $~.should == nil + end + it "starts the search at the given offset" do "blablabla".index(/.{0}/, 5).should == 5 "blablabla".index(/.{1}/, 5).should == 5 @@ -278,7 +287,7 @@ describe "String#index with Regexp" do end it "returns nil if the Regexp matches the empty string and the offset is out of range" do - "ruby".index(//,12).should be_nil + "ruby".index(//,12).should == nil end it "supports \\G which matches at the given start offset" do @@ -319,21 +328,10 @@ describe "String#index with Regexp" do "われわわれ".index(/わ/, 3).should == 3 end - ruby_bug "#19763", ""..."3.3.0" do - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - re = Regexp.new "れ".encode(Encoding::EUC_JP) - -> do - "あれ".index re - end.should raise_error(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)") - end - end - - # The exception message was incorrectly "incompatible character encodings: UTF-8 and EUC-JP" before 3.3.0 - # Still test that the right exception class is used before that. it "raises an Encoding::CompatibilityError if the encodings are incompatible" do re = Regexp.new "れ".encode(Encoding::EUC_JP) -> do "あれ".index re - end.should raise_error(Encoding::CompatibilityError) + end.should.raise(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)") end end diff --git a/spec/ruby/core/string/initialize_spec.rb b/spec/ruby/core/string/initialize_spec.rb index 08734cc916..b0c1e2e573 100644 --- a/spec/ruby/core/string/initialize_spec.rb +++ b/spec/ruby/core/string/initialize_spec.rb @@ -4,7 +4,7 @@ require_relative 'shared/replace' describe "String#initialize" do it "is a private method" do - String.should have_private_instance_method(:initialize) + String.private_instance_methods(false).should.include?(:initialize) end describe "with no arguments" do @@ -16,7 +16,7 @@ describe "String#initialize" do it "does not raise an exception when frozen" do a = "hello".freeze - a.send(:initialize).should equal(a) + a.send(:initialize).should.equal?(a) end end diff --git a/spec/ruby/core/string/insert_spec.rb b/spec/ruby/core/string/insert_spec.rb index 0c87df3a95..c89793a8ca 100644 --- a/spec/ruby/core/string/insert_spec.rb +++ b/spec/ruby/core/string/insert_spec.rb @@ -1,5 +1,5 @@ # -*- encoding: utf-8 -*- - +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -23,8 +23,8 @@ describe "String#insert with index, other" do end it "raises an IndexError if the index is beyond string" do - -> { "abcd".insert(5, 'X') }.should raise_error(IndexError) - -> { "abcd".insert(-6, 'X') }.should raise_error(IndexError) + -> { "abcd".insert(5, 'X') }.should.raise(IndexError) + -> { "abcd".insert(-6, 'X') }.should.raise(IndexError) end it "converts index to an integer using to_int" do @@ -42,15 +42,15 @@ describe "String#insert with index, other" do end it "raises a TypeError if other can't be converted to string" do - -> { "abcd".insert(-6, Object.new)}.should raise_error(TypeError) - -> { "abcd".insert(-6, []) }.should raise_error(TypeError) - -> { "abcd".insert(-6, mock('x')) }.should raise_error(TypeError) + -> { "abcd".insert(-6, Object.new)}.should.raise(TypeError) + -> { "abcd".insert(-6, []) }.should.raise(TypeError) + -> { "abcd".insert(-6, mock('x')) }.should.raise(TypeError) end it "raises a FrozenError if self is frozen" do str = "abcd".freeze - -> { str.insert(4, '') }.should raise_error(FrozenError) - -> { str.insert(4, 'X') }.should raise_error(FrozenError) + -> { str.insert(4, '') }.should.raise(FrozenError) + -> { str.insert(4, 'X') }.should.raise(FrozenError) end it "inserts a character into a multibyte encoded string" do @@ -67,7 +67,7 @@ describe "String#insert with index, other" do pat = "ア".encode Encoding::EUC_JP -> do "あれ".insert 0, pat - end.should raise_error(Encoding::CompatibilityError) + end.should.raise(Encoding::CompatibilityError) end it "should not call subclassed string methods" do diff --git a/spec/ruby/core/string/inspect_spec.rb b/spec/ruby/core/string/inspect_spec.rb index 8bf3d3161f..8b91ce2f84 100644 --- a/spec/ruby/core/string/inspect_spec.rb +++ b/spec/ruby/core/string/inspect_spec.rb @@ -4,7 +4,7 @@ require_relative 'fixtures/classes' describe "String#inspect" do it "does not return a subclass instance" do - StringSpecs::MyString.new.inspect.should be_an_instance_of(String) + StringSpecs::MyString.new.inspect.should.instance_of?(String) end it "returns a string with special characters replaced with \\<char> notation" do @@ -327,7 +327,7 @@ describe "String#inspect" do end it "works for broken US-ASCII strings" do - s = "©".force_encoding("US-ASCII") + s = "©".dup.force_encoding("US-ASCII") s.inspect.should == '"\xC2\xA9"' end diff --git a/spec/ruby/core/string/ljust_spec.rb b/spec/ruby/core/string/ljust_spec.rb index 9208ec5897..0b2aab2638 100644 --- a/spec/ruby/core/string/ljust_spec.rb +++ b/spec/ruby/core/string/ljust_spec.rb @@ -41,10 +41,10 @@ describe "String#ljust with length, padding" do end it "raises a TypeError when length can't be converted to an integer" do - -> { "hello".ljust("x") }.should raise_error(TypeError) - -> { "hello".ljust("x", "y") }.should raise_error(TypeError) - -> { "hello".ljust([]) }.should raise_error(TypeError) - -> { "hello".ljust(mock('x')) }.should raise_error(TypeError) + -> { "hello".ljust("x") }.should.raise(TypeError) + -> { "hello".ljust("x", "y") }.should.raise(TypeError) + -> { "hello".ljust([]) }.should.raise(TypeError) + -> { "hello".ljust(mock('x')) }.should.raise(TypeError) end it "tries to convert padstr to a string using to_str" do @@ -55,46 +55,46 @@ describe "String#ljust with length, padding" do end it "raises a TypeError when padstr can't be converted" do - -> { "hello".ljust(20, []) }.should raise_error(TypeError) - -> { "hello".ljust(20, Object.new)}.should raise_error(TypeError) - -> { "hello".ljust(20, mock('x')) }.should raise_error(TypeError) + -> { "hello".ljust(20, []) }.should.raise(TypeError) + -> { "hello".ljust(20, Object.new)}.should.raise(TypeError) + -> { "hello".ljust(20, mock('x')) }.should.raise(TypeError) end it "raises an ArgumentError when padstr is empty" do - -> { "hello".ljust(10, '') }.should raise_error(ArgumentError) + -> { "hello".ljust(10, '') }.should.raise(ArgumentError) end it "returns String instances when called on subclasses" do - StringSpecs::MyString.new("").ljust(10).should be_an_instance_of(String) - StringSpecs::MyString.new("foo").ljust(10).should be_an_instance_of(String) - StringSpecs::MyString.new("foo").ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) + StringSpecs::MyString.new("").ljust(10).should.instance_of?(String) + StringSpecs::MyString.new("foo").ljust(10).should.instance_of?(String) + StringSpecs::MyString.new("foo").ljust(10, StringSpecs::MyString.new("x")).should.instance_of?(String) - "".ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) - "foo".ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) + "".ljust(10, StringSpecs::MyString.new("x")).should.instance_of?(String) + "foo".ljust(10, StringSpecs::MyString.new("x")).should.instance_of?(String) end describe "with width" do it "returns a String in the same encoding as the original" do - str = "abc".force_encoding Encoding::IBM437 + str = "abc".dup.force_encoding Encoding::IBM437 result = str.ljust 5 result.should == "abc " - result.encoding.should equal(Encoding::IBM437) + result.encoding.should.equal?(Encoding::IBM437) end end describe "with width, pattern" do it "returns a String in the compatible encoding" do - str = "abc".force_encoding Encoding::IBM437 + str = "abc".dup.force_encoding Encoding::IBM437 result = str.ljust 5, "あ" result.should == "abcああ" - result.encoding.should equal(Encoding::UTF_8) + result.encoding.should.equal?(Encoding::UTF_8) end it "raises an Encoding::CompatibilityError if the encodings are incompatible" do pat = "ア".encode Encoding::EUC_JP -> do "あれ".ljust 5, pat - end.should raise_error(Encoding::CompatibilityError) + end.should.raise(Encoding::CompatibilityError) end end end diff --git a/spec/ruby/core/string/lstrip_spec.rb b/spec/ruby/core/string/lstrip_spec.rb index 85685deb0a..5896f8d7da 100644 --- a/spec/ruby/core/string/lstrip_spec.rb +++ b/spec/ruby/core/string/lstrip_spec.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/strip' @@ -6,11 +7,11 @@ describe "String#lstrip" do it_behaves_like :string_strip, :lstrip it "returns a copy of self with leading whitespace removed" do - " hello ".lstrip.should == "hello " - " hello world ".lstrip.should == "hello world " - "\n\r\t\n\v\r hello world ".lstrip.should == "hello world " - "hello".lstrip.should == "hello" - " こにちわ".lstrip.should == "こにちわ" + " hello ".lstrip.should == "hello " + " hello world ".lstrip.should == "hello world " + "\n\r\t\n\v\r hello world ".lstrip.should == "hello world " + "hello".lstrip.should == "hello" + " こにちわ".lstrip.should == "こにちわ" end it "works with lazy substrings" do @@ -21,15 +22,15 @@ describe "String#lstrip" do end it "strips leading \\0" do - "\x00hello".lstrip.should == "hello" - "\000 \000hello\000 \000".lstrip.should == "hello\000 \000" + "\x00hello".lstrip.should == "hello" + "\000 \000hello\000 \000".lstrip.should == "hello\000 \000" end end describe "String#lstrip!" do it "modifies self in place and returns self" do a = " hello " - a.lstrip!.should equal(a) + a.lstrip!.should.equal?(a) a.should == "hello " end @@ -52,22 +53,22 @@ describe "String#lstrip!" do end it "raises a FrozenError on a frozen instance that is modified" do - -> { " hello ".freeze.lstrip! }.should raise_error(FrozenError) + -> { " hello ".freeze.lstrip! }.should.raise(FrozenError) end # see [ruby-core:23657] it "raises a FrozenError on a frozen instance that would not be modified" do - -> { "hello".freeze.lstrip! }.should raise_error(FrozenError) - -> { "".freeze.lstrip! }.should raise_error(FrozenError) + -> { "hello".freeze.lstrip! }.should.raise(FrozenError) + -> { "".freeze.lstrip! }.should.raise(FrozenError) end it "raises an ArgumentError if the first non-space codepoint is invalid" do s = "\xDFabc".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.lstrip! }.should raise_error(ArgumentError) + s.valid_encoding?.should == false + -> { s.lstrip! }.should.raise(ArgumentError) s = " \xDFabc".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.lstrip! }.should raise_error(ArgumentError) + s.valid_encoding?.should == false + -> { s.lstrip! }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/match_spec.rb b/spec/ruby/core/string/match_spec.rb index 5e988f34ca..3ea8d90aa8 100644 --- a/spec/ruby/core/string/match_spec.rb +++ b/spec/ruby/core/string/match_spec.rb @@ -19,8 +19,8 @@ describe "String#=~" do end it "raises a TypeError if a obj is a string" do - -> { "some string" =~ "another string" }.should raise_error(TypeError) - -> { "a" =~ StringSpecs::MyString.new("b") }.should raise_error(TypeError) + -> { "some string" =~ "another string" }.should.raise(TypeError) + -> { "a" =~ StringSpecs::MyString.new("b") }.should.raise(TypeError) end it "invokes obj.=~ with self if obj is neither a string nor regexp" do @@ -81,7 +81,7 @@ describe "String#match" do describe "when passed a block" do it "yields the MatchData" do "abc".match(/./) {|m| ScratchPad.record m } - ScratchPad.recorded.should be_kind_of(MatchData) + ScratchPad.recorded.should.is_a?(MatchData) end it "returns the block result" do @@ -107,9 +107,9 @@ describe "String#match" do end it "raises a TypeError if pattern is not a regexp or a string" do - -> { 'hello'.match(10) }.should raise_error(TypeError) + -> { 'hello'.match(10) }.should.raise(TypeError) not_supported_on :opal do - -> { 'hello'.match(:ell) }.should raise_error(TypeError) + -> { 'hello'.match(:ell) }.should.raise(TypeError) end end @@ -137,9 +137,17 @@ describe "String#match" do end it "calls match on the regular expression" do - regexp = /./.dup - regexp.should_receive(:match).and_return(:foo) - 'hello'.match(regexp).should == :foo + # Can't use regexp.should_receive(:match).and_return(:foo) since regexps are frozen + ScratchPad.clear + regexp = Class.new(Regexp) { + def match(*args) + ScratchPad.record [:match, *args] + super(*args) + end + }.new('.') + + 'hello'.match(regexp) + ScratchPad.recorded.should == [:match, 'hello'] end end @@ -151,17 +159,17 @@ describe "String#match?" do context "when matches the given regex" do it "returns true but does not set Regexp.last_match" do - 'string'.match?(/string/i).should be_true - Regexp.last_match.should be_nil + 'string'.match?(/string/i).should == true + Regexp.last_match.should == nil end end it "returns false when does not match the given regex" do - 'string'.match?(/STRING/).should be_false + 'string'.match?(/STRING/).should == false end it "takes matching position as the 2nd argument" do - 'string'.match?(/str/i, 0).should be_true - 'string'.match?(/str/i, 1).should be_false + 'string'.match?(/str/i, 0).should == true + 'string'.match?(/str/i, 1).should == false end end diff --git a/spec/ruby/core/string/modulo_spec.rb b/spec/ruby/core/string/modulo_spec.rb index bf96a82874..f93ec4bcf8 100644 --- a/spec/ruby/core/string/modulo_spec.rb +++ b/spec/ruby/core/string/modulo_spec.rb @@ -46,42 +46,57 @@ describe "String#%" do end it "raises if a compatible encoding can't be found" do - -> { "hello %s".encode("utf-8") % "world".encode("UTF-16LE") }.should raise_error(Encoding::CompatibilityError) + -> { "hello %s".encode("utf-8") % "world".encode("UTF-16LE") }.should.raise(Encoding::CompatibilityError) end end it "raises an error if single % appears at the end" do - -> { ("%" % []) }.should raise_error(ArgumentError) - -> { ("foo%" % [])}.should raise_error(ArgumentError) + -> { ("%" % []) }.should.raise(ArgumentError) + -> { ("foo%" % [])}.should.raise(ArgumentError) end - it "formats single % character before a newline as literal %" do - ("%\n" % []).should == "%\n" - ("foo%\n" % []).should == "foo%\n" - ("%\n.3f" % 1.2).should == "%\n.3f" - end + ruby_version_is ""..."3.4" do + it "formats single % character before a newline as literal %" do + ("%\n" % []).should == "%\n" + ("foo%\n" % []).should == "foo%\n" + ("%\n.3f" % 1.2).should == "%\n.3f" + end - it "formats single % character before a NUL as literal %" do - ("%\0" % []).should == "%\0" - ("foo%\0" % []).should == "foo%\0" - ("%\0.3f" % 1.2).should == "%\0.3f" - end + it "formats single % character before a NUL as literal %" do + ("%\0" % []).should == "%\0" + ("foo%\0" % []).should == "foo%\0" + ("%\0.3f" % 1.2).should == "%\0.3f" + end - it "raises an error if single % appears anywhere else" do - -> { (" % " % []) }.should raise_error(ArgumentError) - -> { ("foo%quux" % []) }.should raise_error(ArgumentError) - end + it "raises an error if single % appears anywhere else" do + -> { (" % " % []) }.should.raise(ArgumentError) + -> { ("foo%quux" % []) }.should.raise(ArgumentError) + end - it "raises an error if NULL or \\n appear anywhere else in the format string" do - begin - old_debug, $DEBUG = $DEBUG, false + it "raises an error if NULL or \\n appear anywhere else in the format string" do + begin + old_debug, $DEBUG = $DEBUG, false - -> { "%.\n3f" % 1.2 }.should raise_error(ArgumentError) - -> { "%.3\nf" % 1.2 }.should raise_error(ArgumentError) - -> { "%.\03f" % 1.2 }.should raise_error(ArgumentError) - -> { "%.3\0f" % 1.2 }.should raise_error(ArgumentError) - ensure - $DEBUG = old_debug + -> { "%.\n3f" % 1.2 }.should.raise(ArgumentError) + -> { "%.3\nf" % 1.2 }.should.raise(ArgumentError) + -> { "%.\03f" % 1.2 }.should.raise(ArgumentError) + -> { "%.3\0f" % 1.2 }.should.raise(ArgumentError) + ensure + $DEBUG = old_debug + end + end + end + + ruby_version_is "3.4" do + it "raises an ArgumentError if % is not followed by a conversion specifier" do + -> { "%" % [] }.should.raise(ArgumentError) + -> { "%\n" % [] }.should.raise(ArgumentError) + -> { "%\0" % [] }.should.raise(ArgumentError) + -> { " % " % [] }.should.raise(ArgumentError) + -> { "%.\n3f" % 1.2 }.should.raise(ArgumentError) + -> { "%.3\nf" % 1.2 }.should.raise(ArgumentError) + -> { "%.\03f" % 1.2 }.should.raise(ArgumentError) + -> { "%.3\0f" % 1.2 }.should.raise(ArgumentError) end end @@ -104,8 +119,8 @@ describe "String#%" do s = $stderr $stderr = IOStub.new - -> { "" % [1, 2, 3] }.should raise_error(ArgumentError) - -> { "%s" % [1, 2, 3] }.should raise_error(ArgumentError) + -> { "" % [1, 2, 3] }.should.raise(ArgumentError) + -> { "%s" % [1, 2, 3] }.should.raise(ArgumentError) ensure $DEBUG = old_debug $stderr = s @@ -125,26 +140,34 @@ describe "String#%" do end end - it "replaces trailing absolute argument specifier without type with percent sign" do - ("hello %1$" % "foo").should == "hello %" + ruby_version_is ""..."3.4" do + it "replaces trailing absolute argument specifier without type with percent sign" do + ("hello %1$" % "foo").should == "hello %" + end + end + + ruby_version_is "3.4" do + it "raises an ArgumentError if absolute argument specifier is followed by a conversion specifier" do + -> { "hello %1$" % "foo" }.should.raise(ArgumentError) + end end it "raises an ArgumentError when given invalid argument specifiers" do - -> { "%1" % [] }.should raise_error(ArgumentError) - -> { "%+" % [] }.should raise_error(ArgumentError) - -> { "%-" % [] }.should raise_error(ArgumentError) - -> { "%#" % [] }.should raise_error(ArgumentError) - -> { "%0" % [] }.should raise_error(ArgumentError) - -> { "%*" % [] }.should raise_error(ArgumentError) - -> { "%." % [] }.should raise_error(ArgumentError) - -> { "%_" % [] }.should raise_error(ArgumentError) - -> { "%0$s" % "x" }.should raise_error(ArgumentError) - -> { "%*0$s" % [5, "x"] }.should raise_error(ArgumentError) - -> { "%*1$.*0$1$s" % [1, 2, 3] }.should raise_error(ArgumentError) + -> { "%1" % [] }.should.raise(ArgumentError) + -> { "%+" % [] }.should.raise(ArgumentError) + -> { "%-" % [] }.should.raise(ArgumentError) + -> { "%#" % [] }.should.raise(ArgumentError) + -> { "%0" % [] }.should.raise(ArgumentError) + -> { "%*" % [] }.should.raise(ArgumentError) + -> { "%." % [] }.should.raise(ArgumentError) + -> { "%_" % [] }.should.raise(ArgumentError) + -> { "%0$s" % "x" }.should.raise(ArgumentError) + -> { "%*0$s" % [5, "x"] }.should.raise(ArgumentError) + -> { "%*1$.*0$1$s" % [1, 2, 3] }.should.raise(ArgumentError) end it "raises an ArgumentError when multiple positional argument tokens are given for one format specifier" do - -> { "%1$1$s" % "foo" }.should raise_error(ArgumentError) + -> { "%1$1$s" % "foo" }.should.raise(ArgumentError) end it "respects positional arguments and precision tokens given for one format specifier" do @@ -160,36 +183,36 @@ describe "String#%" do end it "raises an ArgumentError when multiple width star tokens are given for one format specifier" do - -> { "%**s" % [5, 5, 5] }.should raise_error(ArgumentError) + -> { "%**s" % [5, 5, 5] }.should.raise(ArgumentError) end it "raises an ArgumentError when a width star token is seen after a width token" do - -> { "%5*s" % [5, 5] }.should raise_error(ArgumentError) + -> { "%5*s" % [5, 5] }.should.raise(ArgumentError) end it "raises an ArgumentError when multiple precision tokens are given" do - -> { "%.5.5s" % 5 }.should raise_error(ArgumentError) - -> { "%.5.*s" % [5, 5] }.should raise_error(ArgumentError) - -> { "%.*.5s" % [5, 5] }.should raise_error(ArgumentError) + -> { "%.5.5s" % 5 }.should.raise(ArgumentError) + -> { "%.5.*s" % [5, 5] }.should.raise(ArgumentError) + -> { "%.*.5s" % [5, 5] }.should.raise(ArgumentError) end it "raises an ArgumentError when there are less arguments than format specifiers" do ("foo" % []).should == "foo" - -> { "%s" % [] }.should raise_error(ArgumentError) - -> { "%s %s" % [1] }.should raise_error(ArgumentError) + -> { "%s" % [] }.should.raise(ArgumentError) + -> { "%s %s" % [1] }.should.raise(ArgumentError) end it "raises an ArgumentError when absolute and relative argument numbers are mixed" do - -> { "%s %1$s" % "foo" }.should raise_error(ArgumentError) - -> { "%1$s %s" % "foo" }.should raise_error(ArgumentError) + -> { "%s %1$s" % "foo" }.should.raise(ArgumentError) + -> { "%1$s %s" % "foo" }.should.raise(ArgumentError) - -> { "%s %2$s" % ["foo", "bar"] }.should raise_error(ArgumentError) - -> { "%2$s %s" % ["foo", "bar"] }.should raise_error(ArgumentError) + -> { "%s %2$s" % ["foo", "bar"] }.should.raise(ArgumentError) + -> { "%2$s %s" % ["foo", "bar"] }.should.raise(ArgumentError) - -> { "%*2$s" % [5, 5, 5] }.should raise_error(ArgumentError) - -> { "%*.*2$s" % [5, 5, 5] }.should raise_error(ArgumentError) - -> { "%*2$.*2$s" % [5, 5, 5] }.should raise_error(ArgumentError) - -> { "%*.*2$s" % [5, 5, 5] }.should raise_error(ArgumentError) + -> { "%*2$s" % [5, 5, 5] }.should.raise(ArgumentError) + -> { "%*.*2$s" % [5, 5, 5] }.should.raise(ArgumentError) + -> { "%*2$.*2$s" % [5, 5, 5] }.should.raise(ArgumentError) + -> { "%*.*2$s" % [5, 5, 5] }.should.raise(ArgumentError) end it "allows reuse of the one argument multiple via absolute argument numbers" do @@ -198,13 +221,13 @@ describe "String#%" do end it "always interprets an array argument as a list of argument parameters" do - -> { "%p" % [] }.should raise_error(ArgumentError) + -> { "%p" % [] }.should.raise(ArgumentError) ("%p" % [1]).should == "1" ("%p %p" % [1, 2]).should == "1 2" end it "always interprets an array subclass argument as a list of argument parameters" do - -> { "%p" % StringSpecs::MyArray[] }.should raise_error(ArgumentError) + -> { "%p" % StringSpecs::MyArray[] }.should.raise(ArgumentError) ("%p" % StringSpecs::MyArray[1]).should == "1" ("%p %p" % StringSpecs::MyArray[1, 2]).should == "1 2" end @@ -275,7 +298,7 @@ describe "String#%" do x = mock("string modulo to_ary") x.should_receive(:to_ary).and_return("x") - -> { "%s" % x }.should raise_error(TypeError) + -> { "%s" % x }.should.raise(TypeError) end it "tries to convert the argument to Array by calling #to_ary" do @@ -298,7 +321,7 @@ describe "String#%" do "%f", "%g", "%G", "%i", "%o", "%p", "%s", "%u", "%x", "%X" ].each do |format| - (StringSpecs::MyString.new(format) % universal).should be_an_instance_of(String) + (StringSpecs::MyString.new(format) % universal).should.instance_of?(String) end end @@ -361,23 +384,15 @@ describe "String#%" do ("%*c" % [10, 3]).should == " \003" ("%c" % 42).should == "*" - -> { "%c" % Object }.should raise_error(TypeError) + -> { "%c" % Object }.should.raise(TypeError) end it "supports single character strings as argument for %c" do ("%c" % 'A').should == "A" end - ruby_version_is ""..."3.2" do - it "raises an exception for multiple character strings as argument for %c" do - -> { "%c" % 'AA' }.should raise_error(ArgumentError) - end - end - - ruby_version_is "3.2" do - it "supports only the first character as argument for %c" do - ("%c" % 'AA').should == "A" - end + it "supports only the first character as argument for %c" do + ("%c" % 'AA').should == "A" end it "calls to_str on argument for %c formats" do @@ -547,7 +562,7 @@ describe "String#%" do ("%1$p" % [10, 5]).should == "10" ("%-22p" % 10).should == "10 " ("%*p" % [10, 10]).should == " 10" - ("%p" % {capture: 1}).should == "{:capture=>1}" + ("%p" % {capture: 1}).should == {capture: 1}.inspect ("%p" % "str").should == "\"str\"" end @@ -595,7 +610,7 @@ describe "String#%" do # See http://groups.google.com/group/ruby-core-google/t/c285c18cd94c216d it "raises an ArgumentError for huge precisions for %s" do block = -> { "%.25555555555555555555555555555555555555s" % "hello world" } - block.should raise_error(ArgumentError) + block.should.raise(ArgumentError) end # Note: %u has been changed to an alias for %d in 1.9. @@ -690,16 +705,16 @@ describe "String#%" do -> { # see [ruby-core:14139] for more details (format % "0777").should == (format % Kernel.Integer("0777")) - }.should_not raise_error(ArgumentError) + }.should_not.raise(ArgumentError) - -> { format % "0__7_7_7" }.should raise_error(ArgumentError) + -> { format % "0__7_7_7" }.should.raise(ArgumentError) - -> { format % "" }.should raise_error(ArgumentError) - -> { format % "x" }.should raise_error(ArgumentError) - -> { format % "5x" }.should raise_error(ArgumentError) - -> { format % "08" }.should raise_error(ArgumentError) - -> { format % "0b2" }.should raise_error(ArgumentError) - -> { format % "123__456" }.should raise_error(ArgumentError) + -> { format % "" }.should.raise(ArgumentError) + -> { format % "x" }.should.raise(ArgumentError) + -> { format % "5x" }.should.raise(ArgumentError) + -> { format % "08" }.should.raise(ArgumentError) + -> { format % "0b2" }.should.raise(ArgumentError) + -> { format % "123__456" }.should.raise(ArgumentError) obj = mock('5') obj.should_receive(:to_i).and_return(5) @@ -726,21 +741,25 @@ describe "String#%" do (format % "-10.4e-20").should == (format % -10.4e-20) (format % ".5").should == (format % 0.5) (format % "-.5").should == (format % -0.5) + + ruby_version_is "3.4" do + (format % "10.").should == (format % 10) + end + # Something's strange with this spec: # it works just fine in individual mode, but not when run as part of a group (format % "10_1_0.5_5_5").should == (format % 1010.555) (format % "0777").should == (format % 777) - -> { format % "" }.should raise_error(ArgumentError) - -> { format % "x" }.should raise_error(ArgumentError) - -> { format % "." }.should raise_error(ArgumentError) - -> { format % "10." }.should raise_error(ArgumentError) - -> { format % "5x" }.should raise_error(ArgumentError) - -> { format % "0b1" }.should raise_error(ArgumentError) - -> { format % "10e10.5" }.should raise_error(ArgumentError) - -> { format % "10__10" }.should raise_error(ArgumentError) - -> { format % "10.10__10" }.should raise_error(ArgumentError) + -> { format % "" }.should.raise(ArgumentError) + -> { format % "x" }.should.raise(ArgumentError) + -> { format % "." }.should.raise(ArgumentError) + -> { format % "5x" }.should.raise(ArgumentError) + -> { format % "0b1" }.should.raise(ArgumentError) + -> { format % "10e10.5" }.should.raise(ArgumentError) + -> { format % "10__10" }.should.raise(ArgumentError) + -> { format % "10.10__10" }.should.raise(ArgumentError) obj = mock('5.0') obj.should_receive(:to_f).and_return(5.0) @@ -758,7 +777,7 @@ describe "String#%" do end it "should raise ArgumentError if no hash given" do - -> {"%{foo}" % []}.should raise_error(ArgumentError) + -> {"%{foo}" % []}.should.raise(ArgumentError) end end @@ -768,11 +787,11 @@ describe "String#%" do end it "raises KeyError if key is missing from passed-in hash" do - -> {"%<foo>d" % {}}.should raise_error(KeyError) + -> {"%<foo>d" % {}}.should.raise(KeyError) end it "should raise ArgumentError if no hash given" do - -> {"%<foo>" % []}.should raise_error(ArgumentError) + -> {"%<foo>" % []}.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/string/new_spec.rb b/spec/ruby/core/string/new_spec.rb index ca678f5323..aadf1e0e46 100644 --- a/spec/ruby/core/string/new_spec.rb +++ b/spec/ruby/core/string/new_spec.rb @@ -4,7 +4,7 @@ require_relative 'fixtures/classes' describe "String.new" do it "returns an instance of String" do str = String.new - str.should be_an_instance_of(String) + str.should.instance_of?(String) end it "accepts an encoding argument" do @@ -28,7 +28,7 @@ describe "String.new" do it "returns a new string given a string argument" do str1 = "test" str = String.new(str1) - str.should be_an_instance_of(String) + str.should.instance_of?(String) str.should == str1 str << "more" str.should == "testmore" @@ -36,7 +36,7 @@ describe "String.new" do it "returns an instance of a subclass" do a = StringSpecs::MyString.new("blah") - a.should be_an_instance_of(StringSpecs::MyString) + a.should.instance_of?(StringSpecs::MyString) a.should == "blah" end @@ -51,8 +51,8 @@ describe "String.new" do end it "raises TypeError on inconvertible object" do - -> { String.new 5 }.should raise_error(TypeError) - -> { String.new nil }.should raise_error(TypeError) + -> { String.new 5 }.should.raise(TypeError) + -> { String.new nil }.should.raise(TypeError) end it "returns a binary String" do diff --git a/spec/ruby/core/string/ord_spec.rb b/spec/ruby/core/string/ord_spec.rb index 4cf26990fe..5a17fc1d87 100644 --- a/spec/ruby/core/string/ord_spec.rb +++ b/spec/ruby/core/string/ord_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "String#ord" do it "returns an Integer" do - 'a'.ord.should be_an_instance_of(Integer) + 'a'.ord.should.instance_of?(Integer) end it "returns the codepoint of the first character in the String" do @@ -23,11 +23,11 @@ describe "String#ord" do end it "raises an ArgumentError if called on an empty String" do - -> { ''.ord }.should raise_error(ArgumentError) + -> { ''.ord }.should.raise(ArgumentError) end it "raises ArgumentError if the character is broken" do - s = "©".force_encoding("US-ASCII") - -> { s.ord }.should raise_error(ArgumentError, "invalid byte sequence in US-ASCII") + s = "©".dup.force_encoding("US-ASCII") + -> { s.ord }.should.raise(ArgumentError, "invalid byte sequence in US-ASCII") end end diff --git a/spec/ruby/core/string/partition_spec.rb b/spec/ruby/core/string/partition_spec.rb index 9cb3672881..29fe910b39 100644 --- a/spec/ruby/core/string/partition_spec.rb +++ b/spec/ruby/core/string/partition_spec.rb @@ -31,8 +31,8 @@ describe "String#partition with String" do end it "raises an error if not convertible to string" do - ->{ "hello".partition(5) }.should raise_error(TypeError) - ->{ "hello".partition(nil) }.should raise_error(TypeError) + ->{ "hello".partition(5) }.should.raise(TypeError) + ->{ "hello".partition(nil) }.should.raise(TypeError) end it "takes precedence over a given block" do @@ -40,7 +40,7 @@ describe "String#partition with String" do end it "handles a pattern in a superset encoding" do - string = "hello".force_encoding(Encoding::US_ASCII) + string = "hello".dup.force_encoding(Encoding::US_ASCII) result = string.partition("é") @@ -51,7 +51,7 @@ describe "String#partition with String" do end it "handles a pattern in a subset encoding" do - pattern = "o".force_encoding(Encoding::US_ASCII) + pattern = "o".dup.force_encoding(Encoding::US_ASCII) result = "héllo world".partition(pattern) diff --git a/spec/ruby/core/string/plus_spec.rb b/spec/ruby/core/string/plus_spec.rb index 9da17451c6..0464141c37 100644 --- a/spec/ruby/core/string/plus_spec.rb +++ b/spec/ruby/core/string/plus_spec.rb @@ -21,17 +21,17 @@ describe "String#+" do end it "raises a TypeError when given any object that fails #to_str" do - -> { "" + Object.new }.should raise_error(TypeError) - -> { "" + 65 }.should raise_error(TypeError) + -> { "" + Object.new }.should.raise(TypeError) + -> { "" + 65 }.should.raise(TypeError) end it "doesn't return subclass instances" do - (StringSpecs::MyString.new("hello") + "").should be_an_instance_of(String) - (StringSpecs::MyString.new("hello") + "foo").should be_an_instance_of(String) - (StringSpecs::MyString.new("hello") + StringSpecs::MyString.new("foo")).should be_an_instance_of(String) - (StringSpecs::MyString.new("hello") + StringSpecs::MyString.new("")).should be_an_instance_of(String) - (StringSpecs::MyString.new("") + StringSpecs::MyString.new("")).should be_an_instance_of(String) - ("hello" + StringSpecs::MyString.new("foo")).should be_an_instance_of(String) - ("hello" + StringSpecs::MyString.new("")).should be_an_instance_of(String) + (StringSpecs::MyString.new("hello") + "").should.instance_of?(String) + (StringSpecs::MyString.new("hello") + "foo").should.instance_of?(String) + (StringSpecs::MyString.new("hello") + StringSpecs::MyString.new("foo")).should.instance_of?(String) + (StringSpecs::MyString.new("hello") + StringSpecs::MyString.new("")).should.instance_of?(String) + (StringSpecs::MyString.new("") + StringSpecs::MyString.new("")).should.instance_of?(String) + ("hello" + StringSpecs::MyString.new("foo")).should.instance_of?(String) + ("hello" + StringSpecs::MyString.new("")).should.instance_of?(String) end end diff --git a/spec/ruby/core/string/prepend_spec.rb b/spec/ruby/core/string/prepend_spec.rb index a0393d4760..a8da4e62cb 100644 --- a/spec/ruby/core/string/prepend_spec.rb +++ b/spec/ruby/core/string/prepend_spec.rb @@ -1,10 +1,11 @@ +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' describe "String#prepend" do it "prepends the given argument to self and returns self" do str = "world" - str.prepend("hello ").should equal(str) + str.prepend("hello ").should.equal?(str) str.should == "hello world" end @@ -16,16 +17,16 @@ describe "String#prepend" do end it "raises a TypeError if the given argument can't be converted to a String" do - -> { "hello ".prepend [] }.should raise_error(TypeError) - -> { 'hello '.prepend mock('x') }.should raise_error(TypeError) + -> { "hello ".prepend [] }.should.raise(TypeError) + -> { 'hello '.prepend mock('x') }.should.raise(TypeError) end it "raises a FrozenError when self is frozen" do a = "hello" a.freeze - -> { a.prepend "" }.should raise_error(FrozenError) - -> { a.prepend "test" }.should raise_error(FrozenError) + -> { a.prepend "" }.should.raise(FrozenError) + -> { a.prepend "test" }.should.raise(FrozenError) end it "works when given a subclass instance" do @@ -48,7 +49,7 @@ describe "String#prepend" do it "returns self when given no arguments" do str = "hello" - str.prepend.should equal(str) + str.prepend.should.equal?(str) str.should == "hello" end end diff --git a/spec/ruby/core/string/reverse_spec.rb b/spec/ruby/core/string/reverse_spec.rb index e67122c05c..e37c1125db 100644 --- a/spec/ruby/core/string/reverse_spec.rb +++ b/spec/ruby/core/string/reverse_spec.rb @@ -1,4 +1,5 @@ # encoding: utf-8 +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -11,9 +12,9 @@ describe "String#reverse" do end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("stressed").reverse.should be_an_instance_of(String) - StringSpecs::MyString.new("m").reverse.should be_an_instance_of(String) - StringSpecs::MyString.new("").reverse.should be_an_instance_of(String) + StringSpecs::MyString.new("stressed").reverse.should.instance_of?(String) + StringSpecs::MyString.new("m").reverse.should.instance_of?(String) + StringSpecs::MyString.new("").reverse.should.instance_of?(String) end it "reverses a string with multi byte characters" do @@ -23,7 +24,7 @@ describe "String#reverse" do it "works with a broken string" do str = "微軟\xDF\xDE正黑體".force_encoding(Encoding::UTF_8) - str.valid_encoding?.should be_false + str.valid_encoding?.should == false str.reverse.should == "體黑正\xDE\xDF軟微" end @@ -36,20 +37,20 @@ end describe "String#reverse!" do it "reverses self in place and always returns self" do a = "stressed" - a.reverse!.should equal(a) + a.reverse!.should.equal?(a) a.should == "desserts" "".reverse!.should == "" end it "raises a FrozenError on a frozen instance that is modified" do - -> { "anna".freeze.reverse! }.should raise_error(FrozenError) - -> { "hello".freeze.reverse! }.should raise_error(FrozenError) + -> { "anna".freeze.reverse! }.should.raise(FrozenError) + -> { "hello".freeze.reverse! }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on a frozen instance that would not be modified" do - -> { "".freeze.reverse! }.should raise_error(FrozenError) + -> { "".freeze.reverse! }.should.raise(FrozenError) end it "reverses a string with multi byte characters" do @@ -61,7 +62,7 @@ describe "String#reverse!" do it "works with a broken string" do str = "微軟\xDF\xDE正黑體".force_encoding(Encoding::UTF_8) - str.valid_encoding?.should be_false + str.valid_encoding?.should == false str.reverse! str.should == "體黑正\xDE\xDF軟微" diff --git a/spec/ruby/core/string/rindex_spec.rb b/spec/ruby/core/string/rindex_spec.rb index 45ff13a006..acecec224f 100644 --- a/spec/ruby/core/string/rindex_spec.rb +++ b/spec/ruby/core/string/rindex_spec.rb @@ -1,24 +1,23 @@ # -*- encoding: utf-8 -*- require_relative '../../spec_helper' require_relative 'fixtures/classes' -require_relative 'fixtures/utf-8-encoding' describe "String#rindex with object" do it "raises a TypeError if obj isn't a String or Regexp" do not_supported_on :opal do - -> { "hello".rindex(:sym) }.should raise_error(TypeError) + -> { "hello".rindex(:sym) }.should.raise(TypeError) end - -> { "hello".rindex(mock('x')) }.should raise_error(TypeError) + -> { "hello".rindex(mock('x')) }.should.raise(TypeError) end it "raises a TypeError if obj is an Integer" do - -> { "hello".rindex(42) }.should raise_error(TypeError) + -> { "hello".rindex(42) }.should.raise(TypeError) end it "doesn't try to convert obj to an integer via to_int" do obj = mock('x') obj.should_not_receive(:to_int) - -> { "hello".rindex(obj) }.should raise_error(TypeError) + -> { "hello".rindex(obj) }.should.raise(TypeError) end it "tries to convert obj to a string via to_str" do @@ -194,22 +193,22 @@ describe "String#rindex with String" do end it "raises a TypeError when given offset is nil" do - -> { "str".rindex("st", nil) }.should raise_error(TypeError) + -> { "str".rindex("st", nil) }.should.raise(TypeError) end it "handles a substring in a superset encoding" do - 'abc'.force_encoding(Encoding::US_ASCII).rindex('é').should == nil + 'abc'.dup.force_encoding(Encoding::US_ASCII).rindex('é').should == nil end it "handles a substring in a subset encoding" do - 'été'.rindex('t'.force_encoding(Encoding::US_ASCII)).should == 1 + 'été'.rindex('t'.dup.force_encoding(Encoding::US_ASCII)).should == 1 end it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - str = 'abc'.force_encoding("ISO-2022-JP") - pattern = 'b'.force_encoding("EUC-JP") + str = 'abc'.dup.force_encoding("ISO-2022-JP") + pattern = 'b'.dup.force_encoding("EUC-JP") - -> { str.rindex(pattern) }.should raise_error(Encoding::CompatibilityError, "incompatible character encodings: ISO-2022-JP and EUC-JP") + -> { str.rindex(pattern) }.should.raise(Encoding::CompatibilityError, "incompatible character encodings: ISO-2022-JP and EUC-JP") end end @@ -363,7 +362,7 @@ describe "String#rindex with Regexp" do end it "raises a TypeError when given offset is nil" do - -> { "str".rindex(/../, nil) }.should raise_error(TypeError) + -> { "str".rindex(/../, nil) }.should.raise(TypeError) end it "returns the reverse character index of a multibyte character" do @@ -372,14 +371,14 @@ describe "String#rindex with Regexp" do end it "returns the character index before the finish" do - "ありがりがとう".rindex("が", 3).should == 2 - "ありがりがとう".rindex(/が/, 3).should == 2 + "ありがりがとう".rindex("が", 3).should == 2 + "ありがりがとう".rindex(/が/, 3).should == 2 end it "raises an Encoding::CompatibilityError if the encodings are incompatible" do re = Regexp.new "れ".encode(Encoding::EUC_JP) -> do "あれ".rindex re - end.should raise_error(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)") + end.should.raise(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)") end end diff --git a/spec/ruby/core/string/rjust_spec.rb b/spec/ruby/core/string/rjust_spec.rb index fcbaf3b938..9f9c369745 100644 --- a/spec/ruby/core/string/rjust_spec.rb +++ b/spec/ruby/core/string/rjust_spec.rb @@ -41,10 +41,10 @@ describe "String#rjust with length, padding" do end it "raises a TypeError when length can't be converted to an integer" do - -> { "hello".rjust("x") }.should raise_error(TypeError) - -> { "hello".rjust("x", "y") }.should raise_error(TypeError) - -> { "hello".rjust([]) }.should raise_error(TypeError) - -> { "hello".rjust(mock('x')) }.should raise_error(TypeError) + -> { "hello".rjust("x") }.should.raise(TypeError) + -> { "hello".rjust("x", "y") }.should.raise(TypeError) + -> { "hello".rjust([]) }.should.raise(TypeError) + -> { "hello".rjust(mock('x')) }.should.raise(TypeError) end it "tries to convert padstr to a string using to_str" do @@ -55,46 +55,46 @@ describe "String#rjust with length, padding" do end it "raises a TypeError when padstr can't be converted" do - -> { "hello".rjust(20, []) }.should raise_error(TypeError) - -> { "hello".rjust(20, Object.new)}.should raise_error(TypeError) - -> { "hello".rjust(20, mock('x')) }.should raise_error(TypeError) + -> { "hello".rjust(20, []) }.should.raise(TypeError) + -> { "hello".rjust(20, Object.new)}.should.raise(TypeError) + -> { "hello".rjust(20, mock('x')) }.should.raise(TypeError) end it "raises an ArgumentError when padstr is empty" do - -> { "hello".rjust(10, '') }.should raise_error(ArgumentError) + -> { "hello".rjust(10, '') }.should.raise(ArgumentError) end it "returns String instances when called on subclasses" do - StringSpecs::MyString.new("").rjust(10).should be_an_instance_of(String) - StringSpecs::MyString.new("foo").rjust(10).should be_an_instance_of(String) - StringSpecs::MyString.new("foo").rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) + StringSpecs::MyString.new("").rjust(10).should.instance_of?(String) + StringSpecs::MyString.new("foo").rjust(10).should.instance_of?(String) + StringSpecs::MyString.new("foo").rjust(10, StringSpecs::MyString.new("x")).should.instance_of?(String) - "".rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) - "foo".rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) + "".rjust(10, StringSpecs::MyString.new("x")).should.instance_of?(String) + "foo".rjust(10, StringSpecs::MyString.new("x")).should.instance_of?(String) end describe "with width" do it "returns a String in the same encoding as the original" do - str = "abc".force_encoding Encoding::IBM437 + str = "abc".dup.force_encoding Encoding::IBM437 result = str.rjust 5 result.should == " abc" - result.encoding.should equal(Encoding::IBM437) + result.encoding.should.equal?(Encoding::IBM437) end end describe "with width, pattern" do it "returns a String in the compatible encoding" do - str = "abc".force_encoding Encoding::IBM437 + str = "abc".dup.force_encoding Encoding::IBM437 result = str.rjust 5, "あ" result.should == "ああabc" - result.encoding.should equal(Encoding::UTF_8) + result.encoding.should.equal?(Encoding::UTF_8) end it "raises an Encoding::CompatibilityError if the encodings are incompatible" do pat = "ア".encode Encoding::EUC_JP -> do "あれ".rjust 5, pat - end.should raise_error(Encoding::CompatibilityError) + end.should.raise(Encoding::CompatibilityError) end end end diff --git a/spec/ruby/core/string/rpartition_spec.rb b/spec/ruby/core/string/rpartition_spec.rb index 21e87f530a..a7dd7430b7 100644 --- a/spec/ruby/core/string/rpartition_spec.rb +++ b/spec/ruby/core/string/rpartition_spec.rb @@ -43,12 +43,12 @@ describe "String#rpartition with String" do end it "raises an error if not convertible to string" do - ->{ "hello".rpartition(5) }.should raise_error(TypeError) - ->{ "hello".rpartition(nil) }.should raise_error(TypeError) + ->{ "hello".rpartition(5) }.should.raise(TypeError) + ->{ "hello".rpartition(nil) }.should.raise(TypeError) end it "handles a pattern in a superset encoding" do - string = "hello".force_encoding(Encoding::US_ASCII) + string = "hello".dup.force_encoding(Encoding::US_ASCII) result = string.rpartition("é") @@ -59,7 +59,7 @@ describe "String#rpartition with String" do end it "handles a pattern in a subset encoding" do - pattern = "o".force_encoding(Encoding::US_ASCII) + pattern = "o".dup.force_encoding(Encoding::US_ASCII) result = "héllo world".rpartition(pattern) diff --git a/spec/ruby/core/string/rstrip_spec.rb b/spec/ruby/core/string/rstrip_spec.rb index e4cf93315e..1638ea375d 100644 --- a/spec/ruby/core/string/rstrip_spec.rb +++ b/spec/ruby/core/string/rstrip_spec.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/strip' @@ -29,7 +30,7 @@ end describe "String#rstrip!" do it "modifies self in place and returns self" do a = " hello " - a.rstrip!.should equal(a) + a.rstrip!.should.equal?(a) a.should == " hello" end @@ -58,36 +59,22 @@ describe "String#rstrip!" do end it "raises a FrozenError on a frozen instance that is modified" do - -> { " hello ".freeze.rstrip! }.should raise_error(FrozenError) + -> { " hello ".freeze.rstrip! }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on a frozen instance that would not be modified" do - -> { "hello".freeze.rstrip! }.should raise_error(FrozenError) - -> { "".freeze.rstrip! }.should raise_error(FrozenError) + -> { "hello".freeze.rstrip! }.should.raise(FrozenError) + -> { "".freeze.rstrip! }.should.raise(FrozenError) end - ruby_version_is "3.2" do - it "raises an Encoding::CompatibilityError if the last non-space codepoint is invalid" do - s = "abc\xDF".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.rstrip! }.should raise_error(Encoding::CompatibilityError) + it "raises an Encoding::CompatibilityError if the last non-space codepoint is invalid" do + s = "abc\xDF".force_encoding(Encoding::UTF_8) + s.valid_encoding?.should == false + -> { s.rstrip! }.should.raise(Encoding::CompatibilityError) - s = "abc\xDF ".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.rstrip! }.should raise_error(Encoding::CompatibilityError) - end - end - - ruby_version_is ""..."3.2" do - it "raises an ArgumentError if the last non-space codepoint is invalid" do - s = "abc\xDF".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.rstrip! }.should raise_error(ArgumentError) - - s = "abc\xDF ".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.rstrip! }.should raise_error(ArgumentError) - end + s = "abc\xDF ".force_encoding(Encoding::UTF_8) + s.valid_encoding?.should == false + -> { s.rstrip! }.should.raise(Encoding::CompatibilityError) end end diff --git a/spec/ruby/core/string/scan_spec.rb b/spec/ruby/core/string/scan_spec.rb index 70c3b7fb7b..47fa7214c2 100644 --- a/spec/ruby/core/string/scan_spec.rb +++ b/spec/ruby/core/string/scan_spec.rb @@ -58,11 +58,11 @@ describe "String#scan" do end it "raises a TypeError if pattern isn't a Regexp and can't be converted to a String" do - -> { "cruel world".scan(5) }.should raise_error(TypeError) + -> { "cruel world".scan(5) }.should.raise(TypeError) not_supported_on :opal do - -> { "cruel world".scan(:test) }.should raise_error(TypeError) + -> { "cruel world".scan(:test) }.should.raise(TypeError) end - -> { "cruel world".scan(mock('x')) }.should raise_error(TypeError) + -> { "cruel world".scan(mock('x')) }.should.raise(TypeError) end # jruby/jruby#5513 @@ -80,8 +80,8 @@ end describe "String#scan with pattern and block" do it "returns self" do s = "foo" - s.scan(/./) {}.should equal(s) - s.scan(/roar/) {}.should equal(s) + s.scan(/./) {}.should.equal?(s) + s.scan(/roar/) {}.should.equal?(s) end it "passes each match to the block as one argument: an array" do @@ -103,11 +103,11 @@ describe "String#scan with pattern and block" do offsets = [] str.scan(/([aeiou])/) do - md = $~ - md.string.should == str - matches << md.to_a - offsets << md.offset(0) - str + md = $~ + md.string.should == str + matches << md.to_a + offsets << md.offset(0) + str end matches.should == [["e", "e"], ["o", "o"]] @@ -117,11 +117,11 @@ describe "String#scan with pattern and block" do offsets = [] str.scan("l") do - md = $~ - md.string.should == str - matches << md.to_a - offsets << md.offset(0) - str + md = $~ + md.string.should == str + matches << md.to_a + offsets << md.offset(0) + str end matches.should == [["l"], ["l"]] diff --git a/spec/ruby/core/string/scrub_spec.rb b/spec/ruby/core/string/scrub_spec.rb index bcee4db463..9dc55dbef7 100644 --- a/spec/ruby/core/string/scrub_spec.rb +++ b/spec/ruby/core/string/scrub_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -37,9 +38,9 @@ describe "String#scrub with a default replacement" do end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("foo").scrub.should be_an_instance_of(String) + StringSpecs::MyString.new("foo").scrub.should.instance_of?(String) input = [0x81].pack('C').force_encoding('utf-8') - StringSpecs::MyString.new(input).scrub.should be_an_instance_of(String) + StringSpecs::MyString.new(input).scrub.should.instance_of?(String) end end @@ -74,7 +75,7 @@ describe "String#scrub with a custom replacement" do xE4 = [0xE4].pack('C').force_encoding('utf-8') block = -> { "foo#{x81}".scrub(xE4) } - block.should raise_error(ArgumentError) + block.should.raise(ArgumentError) end it "returns a String in the same encoding as self" do @@ -86,13 +87,13 @@ describe "String#scrub with a custom replacement" do x81 = [0x81].pack('C').force_encoding('utf-8') block = -> { "foo#{x81}".scrub(1) } - block.should raise_error(TypeError) + block.should.raise(TypeError) end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("foo").scrub("*").should be_an_instance_of(String) + StringSpecs::MyString.new("foo").scrub("*").should.instance_of?(String) input = [0x81].pack('C').force_encoding('utf-8') - StringSpecs::MyString.new(input).scrub("*").should be_an_instance_of(String) + StringSpecs::MyString.new(input).scrub("*").should.instance_of?(String) end end @@ -120,9 +121,9 @@ describe "String#scrub with a block" do end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("foo").scrub { |b| "*" }.should be_an_instance_of(String) + StringSpecs::MyString.new("foo").scrub { |b| "*" }.should.instance_of?(String) input = [0x81].pack('C').force_encoding('utf-8') - StringSpecs::MyString.new(input).scrub { |b| "<#{b.unpack("H*")[0]}>" }.should be_an_instance_of(String) + StringSpecs::MyString.new(input).scrub { |b| "<#{b.unpack("H*")[0]}>" }.should.instance_of?(String) end end @@ -145,7 +146,7 @@ describe "String#scrub!" do input = "a" input.freeze input.scrub! - input.frozen?.should be_true + input.frozen?.should == true end it "preserves the instance variables of already valid strings" do diff --git a/spec/ruby/core/string/setbyte_spec.rb b/spec/ruby/core/string/setbyte_spec.rb index 77bff64038..d9fcc279c0 100644 --- a/spec/ruby/core/string/setbyte_spec.rb +++ b/spec/ruby/core/string/setbyte_spec.rb @@ -1,9 +1,10 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' describe "String#setbyte" do it "returns an Integer" do - "a".setbyte(0,1).should be_kind_of(Integer) + "a".setbyte(0,1).should.is_a?(Integer) end it "modifies the receiver" do @@ -33,9 +34,9 @@ describe "String#setbyte" do it "can invalidate a String's encoding" do str = "glark" - str.valid_encoding?.should be_true + str.valid_encoding?.should == true str.setbyte(2,253) - str.valid_encoding?.should be_false + str.valid_encoding?.should == false str = "ABC" str.setbyte(0, 0x20) # ' ' @@ -57,11 +58,11 @@ describe "String#setbyte" do end it "raises an IndexError if the index is greater than the String bytesize" do - -> { "?".setbyte(1, 97) }.should raise_error(IndexError) + -> { "?".setbyte(1, 97) }.should.raise(IndexError) end it "raises an IndexError if the negative index is greater magnitude than the String bytesize" do - -> { "???".setbyte(-5, 97) }.should raise_error(IndexError) + -> { "???".setbyte(-5, 97) }.should.raise(IndexError) end it "sets a byte at an index greater than String size" do @@ -83,12 +84,12 @@ describe "String#setbyte" do it "raises a FrozenError if self is frozen" do str = "cold".freeze - str.frozen?.should be_true - -> { str.setbyte(3,96) }.should raise_error(FrozenError) + str.frozen?.should == true + -> { str.setbyte(3,96) }.should.raise(FrozenError) end it "raises a TypeError unless the second argument is an Integer" do - -> { "a".setbyte(0,'a') }.should raise_error(TypeError) + -> { "a".setbyte(0,'a') }.should.raise(TypeError) end it "calls #to_int to convert the index" do diff --git a/spec/ruby/core/string/shared/byte_index_common.rb b/spec/ruby/core/string/shared/byte_index_common.rb index 3de1453f4f..bae6cff49f 100644 --- a/spec/ruby/core/string/shared/byte_index_common.rb +++ b/spec/ruby/core/string/shared/byte_index_common.rb @@ -4,43 +4,43 @@ require_relative '../../../spec_helper' describe :byte_index_common, shared: true do describe "raises on type errors" do it "raises a TypeError if passed nil" do - -> { "abc".send(@method, nil) }.should raise_error(TypeError, "no implicit conversion of nil into String") + -> { "abc".send(@method, nil) }.should.raise(TypeError, "no implicit conversion of nil into String") end it "raises a TypeError if passed a boolean" do - -> { "abc".send(@method, true) }.should raise_error(TypeError, "no implicit conversion of true into String") + -> { "abc".send(@method, true) }.should.raise(TypeError, "no implicit conversion of true into String") end it "raises a TypeError if passed a Symbol" do not_supported_on :opal do - -> { "abc".send(@method, :a) }.should raise_error(TypeError, "no implicit conversion of Symbol into String") + -> { "abc".send(@method, :a) }.should.raise(TypeError, "no implicit conversion of Symbol into String") end end it "raises a TypeError if passed a Symbol" do obj = mock('x') obj.should_not_receive(:to_int) - -> { "hello".send(@method, obj) }.should raise_error(TypeError, "no implicit conversion of MockObject into String") + -> { "hello".send(@method, obj) }.should.raise(TypeError, "no implicit conversion of MockObject into String") end it "raises a TypeError if passed an Integer" do - -> { "abc".send(@method, 97) }.should raise_error(TypeError, "no implicit conversion of Integer into String") + -> { "abc".send(@method, 97) }.should.raise(TypeError, "no implicit conversion of Integer into String") end end describe "with multibyte codepoints" do it "raises an IndexError when byte offset lands in the middle of a multibyte character" do - -> { "わ".send(@method, "", 1) }.should raise_error(IndexError, "offset 1 does not land on character boundary") - -> { "わ".send(@method, "", 2) }.should raise_error(IndexError, "offset 2 does not land on character boundary") - -> { "わ".send(@method, "", -1) }.should raise_error(IndexError, "offset 2 does not land on character boundary") - -> { "わ".send(@method, "", -2) }.should raise_error(IndexError, "offset 1 does not land on character boundary") + -> { "わ".send(@method, "", 1) }.should.raise(IndexError, "offset 1 does not land on character boundary") + -> { "わ".send(@method, "", 2) }.should.raise(IndexError, "offset 2 does not land on character boundary") + -> { "わ".send(@method, "", -1) }.should.raise(IndexError, "offset 2 does not land on character boundary") + -> { "わ".send(@method, "", -2) }.should.raise(IndexError, "offset 1 does not land on character boundary") end it "raises an Encoding::CompatibilityError if the encodings are incompatible" do re = Regexp.new "れ".encode(Encoding::EUC_JP) -> do "あれ".send(@method, re) - end.should raise_error(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)") + end.should.raise(Encoding::CompatibilityError, "incompatible encoding regexp match (EUC-JP regexp with UTF-8 string)") end end diff --git a/spec/ruby/core/string/shared/chars.rb b/spec/ruby/core/string/shared/chars.rb index e9fdf89fd6..826d403589 100644 --- a/spec/ruby/core/string/shared/chars.rb +++ b/spec/ruby/core/string/shared/chars.rb @@ -11,22 +11,21 @@ describe :string_chars, shared: true do it "returns self" do s = StringSpecs::MyString.new "hello" - s.send(@method){}.should equal(s) + s.send(@method){}.should.equal?(s) end - it "is unicode aware" do "\303\207\342\210\202\303\251\306\222g".send(@method).to_a.should == ["\303\207", "\342\210\202", "\303\251", "\306\222", "g"] end it "returns characters in the same encoding as self" do - "&%".force_encoding('Shift_JIS').send(@method).to_a.all? {|c| c.encoding.name.should == 'Shift_JIS'} + "&%".dup.force_encoding('Shift_JIS').send(@method).to_a.all? {|c| c.encoding.name.should == 'Shift_JIS'} "&%".encode('BINARY').send(@method).to_a.all? {|c| c.encoding.should == Encoding::BINARY } end it "works with multibyte characters" do - s = "\u{8987}".force_encoding("UTF-8") + s = "\u{8987}".dup.force_encoding("UTF-8") s.bytesize.should == 3 s.send(@method).to_a.should == [s] end @@ -34,19 +33,19 @@ describe :string_chars, shared: true do it "works if the String's contents is invalid for its encoding" do xA4 = [0xA4].pack('C') xA4.force_encoding('UTF-8') - xA4.valid_encoding?.should be_false + xA4.valid_encoding?.should == false xA4.send(@method).to_a.should == [xA4.force_encoding("UTF-8")] end it "returns a different character if the String is transcoded" do - s = "\u{20AC}".force_encoding('UTF-8') - s.encode('UTF-8').send(@method).to_a.should == ["\u{20AC}".force_encoding('UTF-8')] + s = "\u{20AC}".dup.force_encoding('UTF-8') + s.encode('UTF-8').send(@method).to_a.should == ["\u{20AC}".dup.force_encoding('UTF-8')] s.encode('iso-8859-15').send(@method).to_a.should == [[0xA4].pack('C').force_encoding('iso-8859-15')] - s.encode('iso-8859-15').encode('UTF-8').send(@method).to_a.should == ["\u{20AC}".force_encoding('UTF-8')] + s.encode('iso-8859-15').encode('UTF-8').send(@method).to_a.should == ["\u{20AC}".dup.force_encoding('UTF-8')] end it "uses the String's encoding to determine what characters it contains" do - s = "\u{24B62}" + s = +"\u{24B62}" s.force_encoding('UTF-8').send(@method).to_a.should == [ s.force_encoding('UTF-8') @@ -63,4 +62,25 @@ describe :string_chars, shared: true do [0xA2].pack('C').force_encoding('SJIS') ] end + + it "returns individual chars for dummy encodings" do + "ab".dup.force_encoding(Encoding::UTF_7).send(@method).to_a.should == [ + "\x61".dup.force_encoding(Encoding::UTF_7), + "\x62".dup.force_encoding(Encoding::UTF_7) + ] + + "abcd".dup.force_encoding(Encoding::UTF_16).send(@method).to_a.should == [ + "\x61".dup.force_encoding(Encoding::UTF_16), + "\x62".dup.force_encoding(Encoding::UTF_16), + "\x63".dup.force_encoding(Encoding::UTF_16), + "\x64".dup.force_encoding(Encoding::UTF_16) + ] + + "abcd".dup.force_encoding(Encoding::UTF_32).send(@method).to_a.should == [ + "\x61".dup.force_encoding(Encoding::UTF_32), + "\x62".dup.force_encoding(Encoding::UTF_32), + "\x63".dup.force_encoding(Encoding::UTF_32), + "\x64".dup.force_encoding(Encoding::UTF_32) + ] + end end diff --git a/spec/ruby/core/string/shared/codepoints.rb b/spec/ruby/core/string/shared/codepoints.rb index 0b2e078e0a..b6abf6a3ff 100644 --- a/spec/ruby/core/string/shared/codepoints.rb +++ b/spec/ruby/core/string/shared/codepoints.rb @@ -1,15 +1,15 @@ -# -*- encoding: binary -*- +# encoding: binary describe :string_codepoints, shared: true do it "returns self" do s = "foo" result = s.send(@method) {} - result.should equal s + result.should.equal? s end it "raises an ArgumentError when self has an invalid encoding and a method is called on the returned Enumerator" do - s = "\xDF".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.send(@method).to_a }.should raise_error(ArgumentError) + s = "\xDF".dup.force_encoding(Encoding::UTF_8) + s.valid_encoding?.should == false + -> { s.send(@method).to_a }.should.raise(ArgumentError) end it "yields each codepoint to the block if one is given" do @@ -21,14 +21,14 @@ describe :string_codepoints, shared: true do end it "raises an ArgumentError if self's encoding is invalid and a block is given" do - s = "\xDF".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - -> { s.send(@method) { } }.should raise_error(ArgumentError) + s = "\xDF".dup.force_encoding(Encoding::UTF_8) + s.valid_encoding?.should == false + -> { s.send(@method) { } }.should.raise(ArgumentError) end it "yields codepoints as Integers" do "glark\u{20}".send(@method).to_a.each do |codepoint| - codepoint.should be_an_instance_of(Integer) + codepoint.should.instance_of?(Integer) end end @@ -49,14 +49,19 @@ describe :string_codepoints, shared: true do it "round-trips to the original String using Integer#chr" do s = "\u{13}\u{7711}\u{1010}" - s2 = "" + s2 = +"" s.send(@method) {|n| s2 << n.chr(Encoding::UTF_8)} s.should == s2 end it "is synonymous with #bytes for Strings which are single-byte optimizable" do s = "(){}".encode('ascii') - s.ascii_only?.should be_true + s.ascii_only?.should == true s.send(@method).to_a.should == s.bytes.to_a end + + it "returns individual bytes for dummy encodings UTF-16 and UTF-32" do + "abcd".dup.force_encoding(Encoding::UTF_16).send(@method).to_a.should == [97, 98, 99, 100] + "abcd".dup.force_encoding(Encoding::UTF_32).send(@method).to_a.should == [97, 98, 99, 100] + end end diff --git a/spec/ruby/core/string/shared/concat.rb b/spec/ruby/core/string/shared/concat.rb index ee5ef2a98f..60cd0e12d4 100644 --- a/spec/ruby/core/string/shared/concat.rb +++ b/spec/ruby/core/string/shared/concat.rb @@ -1,7 +1,8 @@ +# frozen_string_literal: false describe :string_concat, shared: true do it "concatenates the given argument to self and returns self" do str = 'hello ' - str.send(@method, 'world').should equal(str) + str.send(@method, 'world').should.equal?(str) str.should == "hello world" end @@ -9,22 +10,22 @@ describe :string_concat, shared: true do a = "hello" a.freeze - -> { a.send(@method, "") }.should raise_error(FrozenError) - -> { a.send(@method, "test") }.should raise_error(FrozenError) + -> { a.send(@method, "") }.should.raise(FrozenError) + -> { a.send(@method, "test") }.should.raise(FrozenError) end it "returns a String when given a subclass instance" do a = "hello" a.send(@method, StringSpecs::MyString.new(" world")) a.should == "hello world" - a.should be_an_instance_of(String) + a.should.instance_of?(String) end it "returns an instance of same class when called on a subclass" do str = StringSpecs::MyString.new("hello") str.send(@method, " world") str.should == "hello world" - str.should be_an_instance_of(StringSpecs::MyString) + str.should.instance_of?(StringSpecs::MyString) end describe "with Integer" do @@ -49,28 +50,28 @@ describe :string_concat, shared: true do end it "raises RangeError if the argument is an invalid codepoint for self's encoding" do - -> { "".encode(Encoding::US_ASCII).send(@method, 256) }.should raise_error(RangeError) - -> { "".encode(Encoding::EUC_JP).send(@method, 0x81) }.should raise_error(RangeError) + -> { "".encode(Encoding::US_ASCII).send(@method, 256) }.should.raise(RangeError) + -> { "".encode(Encoding::EUC_JP).send(@method, 0x81) }.should.raise(RangeError) end it "raises RangeError if the argument is negative" do - -> { "".send(@method, -200) }.should raise_error(RangeError) - -> { "".send(@method, -bignum_value) }.should raise_error(RangeError) + -> { "".send(@method, -200) }.should.raise(RangeError) + -> { "".send(@method, -bignum_value) }.should.raise(RangeError) end it "doesn't call to_int on its argument" do x = mock('x') x.should_not_receive(:to_int) - -> { "".send(@method, x) }.should raise_error(TypeError) + -> { "".send(@method, x) }.should.raise(TypeError) end it "raises a FrozenError when self is frozen" do a = "hello" a.freeze - -> { a.send(@method, 0) }.should raise_error(FrozenError) - -> { a.send(@method, 33) }.should raise_error(FrozenError) + -> { a.send(@method, 0) }.should.raise(FrozenError) + -> { a.send(@method, 33) }.should.raise(FrozenError) end end end @@ -90,7 +91,7 @@ describe :string_concat_encoding, shared: true do end it "raises Encoding::CompatibilityError if neither are empty" do - -> { "x".encode("UTF-16LE").send(@method, "y".encode("UTF-8")) }.should raise_error(Encoding::CompatibilityError) + -> { "x".encode("UTF-16LE").send(@method, "y".encode("UTF-8")) }.should.raise(Encoding::CompatibilityError) end end @@ -108,7 +109,7 @@ describe :string_concat_encoding, shared: true do end it "raises Encoding::CompatibilityError if neither are empty" do - -> { "x".encode("UTF-8").send(@method, "y".encode("UTF-16LE")) }.should raise_error(Encoding::CompatibilityError) + -> { "x".encode("UTF-8").send(@method, "y".encode("UTF-16LE")) }.should.raise(Encoding::CompatibilityError) end end @@ -126,7 +127,7 @@ describe :string_concat_encoding, shared: true do end it "raises Encoding::CompatibilityError if neither are ASCII-only" do - -> { "\u00E9".encode("UTF-8").send(@method, "\u00E9".encode("ISO-8859-1")) }.should raise_error(Encoding::CompatibilityError) + -> { "\u00E9".encode("UTF-8").send(@method, "\u00E9".encode("ISO-8859-1")) }.should.raise(Encoding::CompatibilityError) end end @@ -146,13 +147,13 @@ describe :string_concat_type_coercion, shared: true do end it "raises a TypeError if the given argument can't be converted to a String" do - -> { 'hello '.send(@method, []) }.should raise_error(TypeError) - -> { 'hello '.send(@method, mock('x')) }.should raise_error(TypeError) + -> { 'hello '.send(@method, []) }.should.raise(TypeError) + -> { 'hello '.send(@method, mock('x')) }.should.raise(TypeError) end it "raises a NoMethodError if the given argument raises a NoMethodError during type coercion to a String" do obj = mock('world!') obj.should_receive(:to_str).and_raise(NoMethodError) - -> { 'hello '.send(@method, obj) }.should raise_error(NoMethodError) + -> { 'hello '.send(@method, obj) }.should.raise(NoMethodError) end end diff --git a/spec/ruby/core/string/shared/dedup.rb b/spec/ruby/core/string/shared/dedup.rb index 893fd1e360..59506c2901 100644 --- a/spec/ruby/core/string/shared/dedup.rb +++ b/spec/ruby/core/string/shared/dedup.rb @@ -1,9 +1,10 @@ +# frozen_string_literal: false describe :string_dedup, shared: true do it 'returns self if the String is frozen' do input = 'foo'.freeze output = input.send(@method) - output.should equal(input) + output.should.equal?(input) output.should.frozen? end @@ -12,7 +13,7 @@ describe :string_dedup, shared: true do output = input.send(@method) output.should.frozen? - output.should_not equal(input) + output.should_not.equal?(input) output.should == 'foo' end @@ -20,22 +21,22 @@ describe :string_dedup, shared: true do origin = "this is a string" dynamic = %w(this is a string).join(' ') - origin.should_not equal(dynamic) - origin.send(@method).should equal(dynamic.send(@method)) + origin.should_not.equal?(dynamic) + origin.send(@method).should.equal?(dynamic.send(@method)) end it "returns the same object when it's called on the same String literal" do - "unfrozen string".send(@method).should equal("unfrozen string".send(@method)) - "unfrozen string".send(@method).should_not equal("another unfrozen string".send(@method)) + "unfrozen string".send(@method).should.equal?("unfrozen string".send(@method)) + "unfrozen string".send(@method).should_not.equal?("another unfrozen string".send(@method)) end it "deduplicates frozen strings" do dynamic = %w(this string is frozen).join(' ').freeze - dynamic.should_not equal("this string is frozen".freeze) + dynamic.should_not.equal?("this string is frozen".freeze) - dynamic.send(@method).should equal("this string is frozen".freeze) - dynamic.send(@method).should equal("this string is frozen".send(@method).freeze) + dynamic.send(@method).should.equal?("this string is frozen".freeze) + dynamic.send(@method).should.equal?("this string is frozen".send(@method).freeze) end it "does not deduplicate a frozen string when it has instance variables" do @@ -43,13 +44,8 @@ describe :string_dedup, shared: true do dynamic.instance_variable_set(:@a, 1) dynamic.freeze - dynamic.send(@method).should_not equal("this string is frozen".freeze) - dynamic.send(@method).should_not equal("this string is frozen".send(@method).freeze) - dynamic.send(@method).should equal(dynamic) - end - - it "interns the provided string if it is frozen" do - dynamic = "this string is unique and frozen #{rand}".freeze - dynamic.send(@method).should equal(dynamic) + dynamic.send(@method).should_not.equal?("this string is frozen".freeze) + dynamic.send(@method).should_not.equal?("this string is frozen".send(@method).freeze) + dynamic.send(@method).should.equal?(dynamic) end end diff --git a/spec/ruby/core/string/shared/each_char_without_block.rb b/spec/ruby/core/string/shared/each_char_without_block.rb index 397100ce0e..3c32bae42b 100644 --- a/spec/ruby/core/string/shared/each_char_without_block.rb +++ b/spec/ruby/core/string/shared/each_char_without_block.rb @@ -6,7 +6,7 @@ describe :string_each_char_without_block, shared: true do describe "when no block is given" do it "returns an enumerator" do enum = "hello".send(@method) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == ['h', 'e', 'l', 'l', 'o'] end diff --git a/spec/ruby/core/string/shared/each_codepoint_without_block.rb b/spec/ruby/core/string/shared/each_codepoint_without_block.rb index 92b7f76032..60d603954c 100644 --- a/spec/ruby/core/string/shared/each_codepoint_without_block.rb +++ b/spec/ruby/core/string/shared/each_codepoint_without_block.rb @@ -1,14 +1,14 @@ -# -*- encoding: binary -*- +# encoding: binary describe :string_each_codepoint_without_block, shared: true do describe "when no block is given" do it "returns an Enumerator" do - "".send(@method).should be_an_instance_of(Enumerator) + "".send(@method).should.instance_of?(Enumerator) end it "returns an Enumerator even when self has an invalid encoding" do - s = "\xDF".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false - s.send(@method).should be_an_instance_of(Enumerator) + s = "\xDF".dup.force_encoding(Encoding::UTF_8) + s.valid_encoding?.should == false + s.send(@method).should.instance_of?(Enumerator) end describe "returned Enumerator" do @@ -23,8 +23,8 @@ describe :string_each_codepoint_without_block, shared: true do end it "should return the size of the string even when the string has an invalid encoding" do - s = "\xDF".force_encoding(Encoding::UTF_8) - s.valid_encoding?.should be_false + s = "\xDF".dup.force_encoding(Encoding::UTF_8) + s.valid_encoding?.should == false s.send(@method).size.should == 1 end end diff --git a/spec/ruby/core/string/shared/each_line.rb b/spec/ruby/core/string/shared/each_line.rb index a14b4d7779..d79c2b74c4 100644 --- a/spec/ruby/core/string/shared/each_line.rb +++ b/spec/ruby/core/string/shared/each_line.rb @@ -93,7 +93,7 @@ describe :string_each_line, shared: true do it "returns self" do s = "hello\nworld" - (s.send(@method) {}).should equal(s) + (s.send(@method) {}).should.equal?(s) end it "tries to convert the separator to a string using to_str" do @@ -106,7 +106,7 @@ describe :string_each_line, shared: true do end it "does not care if the string is modified while substituting" do - str = "hello\nworld." + str = +"hello\nworld." out = [] str.send(@method){|x| out << x; str[-1] = '!' }.should == "hello\nworld!" out.should == ["hello\n", "world."] @@ -119,8 +119,8 @@ describe :string_each_line, shared: true do end it "raises a TypeError when the separator can't be converted to a string" do - -> { "hello world".send(@method, false) {} }.should raise_error(TypeError) - -> { "hello world".send(@method, mock('x')) {} }.should raise_error(TypeError) + -> { "hello world".send(@method, false) {} }.should.raise(TypeError) + -> { "hello world".send(@method, mock('x')) {} }.should.raise(TypeError) end it "accepts a string separator" do @@ -128,7 +128,7 @@ describe :string_each_line, shared: true do end it "raises a TypeError when the separator is a symbol" do - -> { "hello world".send(@method, :o).to_a }.should raise_error(TypeError) + -> { "hello world".send(@method, :o).to_a }.should.raise(TypeError) end context "when `chomp` keyword argument is passed" do @@ -159,4 +159,18 @@ describe :string_each_line, shared: true do a.should == ["hello\r\n", "world\r\n"] end end + + it "does not split lines for dummy UTF-16" do + "a\nb".encode(Encoding::UTF_16).lines.should == [ + "\xFE\xFF\x00\x61\x00\n\x00\x62".dup.force_encoding(Encoding::UTF_16) + ] + + str = "\x00\n\n\x00".dup.force_encoding(Encoding::UTF_16) + str.lines.should == [str] + end + + it "raises Encoding::ConverterNotFoundError for dummy UTF-7" do + str = "a\nb".dup.force_encoding(Encoding::UTF_7) + -> { str.lines }.should.raise(Encoding::ConverterNotFoundError) + end end diff --git a/spec/ruby/core/string/shared/each_line_without_block.rb b/spec/ruby/core/string/shared/each_line_without_block.rb index 8e08b0390c..af0ab69c00 100644 --- a/spec/ruby/core/string/shared/each_line_without_block.rb +++ b/spec/ruby/core/string/shared/each_line_without_block.rb @@ -2,7 +2,7 @@ describe :string_each_line_without_block, shared: true do describe "when no block is given" do it "returns an enumerator" do enum = "hello world".send(@method, ' ') - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.to_a.should == ["hello ", "world"] end diff --git a/spec/ruby/core/string/shared/encode.rb b/spec/ruby/core/string/shared/encode.rb index a73de5b943..7f644c26d9 100644 --- a/spec/ruby/core/string/shared/encode.rb +++ b/spec/ruby/core/string/shared/encode.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false describe :string_encode, shared: true do describe "when passed no options" do it "transcodes to Encoding.default_internal when set" do @@ -10,7 +11,7 @@ describe :string_encode, shared: true do it "transcodes a 7-bit String despite no generic converting being available" do -> do Encoding::Converter.new Encoding::Emacs_Mule, Encoding::BINARY - end.should raise_error(Encoding::ConverterNotFoundError) + end.should.raise(Encoding::ConverterNotFoundError) Encoding.default_internal = Encoding::Emacs_Mule str = "\x79".force_encoding Encoding::BINARY @@ -21,7 +22,7 @@ describe :string_encode, shared: true do it "raises an Encoding::ConverterNotFoundError when no conversion is possible" do Encoding.default_internal = Encoding::Emacs_Mule str = [0x80].pack('C').force_encoding Encoding::BINARY - -> { str.send(@method) }.should raise_error(Encoding::ConverterNotFoundError) + -> { str.send(@method) }.should.raise(Encoding::ConverterNotFoundError) end end @@ -53,7 +54,7 @@ describe :string_encode, shared: true do it "transcodes a 7-bit String despite no generic converting being available" do -> do Encoding::Converter.new Encoding::Emacs_Mule, Encoding::BINARY - end.should raise_error(Encoding::ConverterNotFoundError) + end.should.raise(Encoding::ConverterNotFoundError) str = "\x79".force_encoding Encoding::BINARY str.send(@method, Encoding::Emacs_Mule).should == "y".force_encoding(Encoding::BINARY) @@ -63,13 +64,21 @@ describe :string_encode, shared: true do str = [0x80].pack('C').force_encoding Encoding::BINARY -> do str.send(@method, Encoding::Emacs_Mule) - end.should raise_error(Encoding::ConverterNotFoundError) + end.should.raise(Encoding::ConverterNotFoundError) end it "raises an Encoding::ConverterNotFoundError for an invalid encoding" do -> do "abc".send(@method, "xyz") - end.should raise_error(Encoding::ConverterNotFoundError) + end.should.raise(Encoding::ConverterNotFoundError) + end + + it "raises an Encoding::UndefinedConversionError when a character cannot be represented in the destination encoding" do + # U+0100 (Ā) is valid UTF-8 but not representable in windows-1252 + str = "test\u0100".force_encoding('utf-8') + -> { + str.send(@method, Encoding::Windows_1252) + }.should.raise(Encoding::UndefinedConversionError) end end @@ -98,7 +107,7 @@ describe :string_encode, shared: true do str = [0x80].pack('C').force_encoding Encoding::BINARY -> do str.send(@method, invalid: :replace, undef: :replace) - end.should raise_error(Encoding::ConverterNotFoundError) + end.should.raise(Encoding::ConverterNotFoundError) end it "replaces invalid characters when replacing Emacs-Mule encoded strings" do @@ -141,6 +150,14 @@ describe :string_encode, shared: true do "ab#{xFF}c".send(@method, Encoding::ISO_8859_1, invalid: :replace).should == "ab?c" end + it "raises UndefinedConversionError for characters not representable in destination encoding with only invalid: :replace" do + # U+0100 (Ā) is valid UTF-8 but not representable in windows-1252 + str = "test\u0100".force_encoding('utf-8') + -> { + str.send(@method, Encoding::Windows_1252, invalid: :replace, replace: "") + }.should.raise(Encoding::UndefinedConversionError) + end + it "calls #to_hash to convert the options object" do options = mock("string encode options") options.should_receive(:to_hash).and_return({ undef: :replace }) @@ -193,6 +210,190 @@ describe :string_encode, shared: true do end end + describe "given the fallback option" do + context "given a hash" do + it "looks up the replacement value from the hash" do + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: { "\ufffd" => "bar" }) + encoded.should == "Bbar" + end + + it "calls to_str on the returned value" do + obj = Object.new + obj.should_receive(:to_str).and_return("bar") + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: { "\ufffd" => obj }) + encoded.should == "Bbar" + end + + it "does not call to_s on the returned value" do + obj = Object.new + obj.should_not_receive(:to_s) + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: { "\ufffd" => obj }) + }.should.raise(TypeError, "no implicit conversion of Object into String") + end + + it "raises an error if the key is not present in the hash" do + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: { "foo" => "bar" }) + }.should.raise(Encoding::UndefinedConversionError, "U+FFFD from UTF-8 to US-ASCII") + end + + it "raises an error if the value is itself invalid" do + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: { "\ufffd" => "\uffee" }) + }.should.raise(ArgumentError, "too big fallback string") + end + + it "uses the hash's default value if set" do + hash = {} + hash.default = "bar" + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: hash) + encoded.should == "Bbar" + end + + it "uses the result of calling default_proc if set" do + hash = {} + hash.default_proc = -> _, _ { "bar" } + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: hash) + encoded.should == "Bbar" + end + end + + context "given an object inheriting from Hash" do + before do + klass = Class.new(Hash) + @hash_like = klass.new + @hash_like["\ufffd"] = "bar" + end + + it "looks up the replacement value from the object" do + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: @hash_like) + encoded.should == "Bbar" + end + end + + context "given an object responding to []" do + before do + klass = Class.new do + def [](c) = c.bytes.inspect + end + @hash_like = klass.new + end + + it "calls [] on the object, passing the invalid character" do + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: @hash_like) + encoded.should == "B[239, 191, 189]" + end + end + + context "given an object not responding to []" do + before do + @non_hash_like = Object.new + end + + it "raises an error" do + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: @non_hash_like) + }.should.raise(Encoding::UndefinedConversionError, "U+FFFD from UTF-8 to US-ASCII") + end + end + + context "given a proc" do + it "calls the proc to get the replacement value, passing in the invalid character" do + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: proc { |c| c.bytes.inspect }) + encoded.should == "B[239, 191, 189]" + end + + it "calls to_str on the returned value" do + obj = Object.new + obj.should_receive(:to_str).and_return("bar") + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: proc { |c| obj }) + encoded.should == "Bbar" + end + + it "does not call to_s on the returned value" do + obj = Object.new + obj.should_not_receive(:to_s) + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: proc { |c| obj }) + }.should.raise(TypeError, "no implicit conversion of Object into String") + end + + it "raises an error if the returned value is itself invalid" do + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { "\uffee" }) + }.should.raise(ArgumentError, "too big fallback string") + end + end + + context "given a lambda" do + it "calls the lambda to get the replacement value, passing in the invalid character" do + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { c.bytes.inspect }) + encoded.should == "B[239, 191, 189]" + end + + it "calls to_str on the returned value" do + obj = Object.new + obj.should_receive(:to_str).and_return("bar") + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { obj }) + encoded.should == "Bbar" + end + + it "does not call to_s on the returned value" do + obj = Object.new + obj.should_not_receive(:to_s) + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { obj }) + }.should.raise(TypeError, "no implicit conversion of Object into String") + end + + it "raises an error if the returned value is itself invalid" do + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: -> c { "\uffee" }) + }.should.raise(ArgumentError, "too big fallback string") + end + end + + context "given a method" do + def replace(c) = c.bytes.inspect + def replace_bad(c) = "\uffee" + + def replace_to_str(c) + obj = Object.new + obj.should_receive(:to_str).and_return("bar") + obj + end + + def replace_to_s(c) + obj = Object.new + obj.should_not_receive(:to_s) + obj + end + + it "calls the method to get the replacement value, passing in the invalid character" do + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: method(:replace)) + encoded.should == "B[239, 191, 189]" + end + + it "calls to_str on the returned value" do + encoded = "B\ufffd".encode(Encoding::US_ASCII, fallback: method(:replace_to_str)) + encoded.should == "Bbar" + end + + it "does not call to_s on the returned value" do + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: method(:replace_to_s)) + }.should.raise(TypeError, "no implicit conversion of Object into String") + end + + it "raises an error if the returned value is itself invalid" do + -> { + "B\ufffd".encode(Encoding::US_ASCII, fallback: method(:replace_bad)) + }.should.raise(ArgumentError, "too big fallback string") + end + end + end + describe "given the xml: :text option" do it "replaces all instances of '&' with '&'" do '& and &'.send(@method, "UTF-8", xml: :text).should == '& and &' @@ -242,6 +443,6 @@ describe :string_encode, shared: true do end it "raises ArgumentError if the value of the :xml option is not :text or :attr" do - -> { ''.send(@method, "UTF-8", xml: :other) }.should raise_error(ArgumentError) + -> { ''.send(@method, "UTF-8", xml: :other) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/shared/eql.rb b/spec/ruby/core/string/shared/eql.rb index 6f268c929c..0e356c69e8 100644 --- a/spec/ruby/core/string/shared/eql.rb +++ b/spec/ruby/core/string/shared/eql.rb @@ -1,38 +1,38 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' describe :string_eql_value, shared: true do it "returns true if self <=> string returns 0" do - 'hello'.send(@method, 'hello').should be_true + 'hello'.send(@method, 'hello').should == true end it "returns false if self <=> string does not return 0" do - "more".send(@method, "MORE").should be_false - "less".send(@method, "greater").should be_false + "more".send(@method, "MORE").should == false + "less".send(@method, "greater").should == false end it "ignores encoding difference of compatible string" do - "hello".force_encoding("utf-8").send(@method, "hello".force_encoding("iso-8859-1")).should be_true + "hello".dup.force_encoding("utf-8").send(@method, "hello".dup.force_encoding("iso-8859-1")).should == true end it "considers encoding difference of incompatible string" do - "\xff".force_encoding("utf-8").send(@method, "\xff".force_encoding("iso-8859-1")).should be_false + "\xff".dup.force_encoding("utf-8").send(@method, "\xff".dup.force_encoding("iso-8859-1")).should == false end it "considers encoding compatibility" do - "abcd".force_encoding("utf-8").send(@method, "abcd".force_encoding("utf-32le")).should be_false + "abcd".dup.force_encoding("utf-8").send(@method, "abcd".dup.force_encoding("utf-32le")).should == false end it "ignores subclass differences" do a = "hello" b = StringSpecs::MyString.new("hello") - a.send(@method, b).should be_true - b.send(@method, a).should be_true + a.send(@method, b).should == true + b.send(@method, a).should == true end it "returns true when comparing 2 empty strings but one is not ASCII-compatible" do - "".send(@method, "".force_encoding('iso-2022-jp')).should == true + "".send(@method, "".dup.force_encoding('iso-2022-jp')).should == true end end diff --git a/spec/ruby/core/string/shared/equal_value.rb b/spec/ruby/core/string/shared/equal_value.rb index fccafb5821..dfc5c7cd29 100644 --- a/spec/ruby/core/string/shared/equal_value.rb +++ b/spec/ruby/core/string/shared/equal_value.rb @@ -3,11 +3,11 @@ require_relative '../fixtures/classes' describe :string_equal_value, shared: true do it "returns false if obj does not respond to to_str" do - 'hello'.send(@method, 5).should be_false + 'hello'.send(@method, 5).should == false not_supported_on :opal do - 'hello'.send(@method, :hello).should be_false + 'hello'.send(@method, :hello).should == false end - 'hello'.send(@method, mock('x')).should be_false + 'hello'.send(@method, mock('x')).should == false end it "returns obj == self if obj responds to to_str" do @@ -20,10 +20,10 @@ describe :string_equal_value, shared: true do # Don't use @method for :== in `obj.should_receive(:==)` obj.should_receive(:==).and_return(true) - 'hello'.send(@method, obj).should be_true + 'hello'.send(@method, obj).should == true end it "is not fooled by NUL characters" do - "abc\0def".send(@method, "abc\0xyz").should be_false + "abc\0def".send(@method, "abc\0xyz").should == false end end diff --git a/spec/ruby/core/string/shared/grapheme_clusters.rb b/spec/ruby/core/string/shared/grapheme_clusters.rb index 8b666868b1..dd8c7ed5fe 100644 --- a/spec/ruby/core/string/shared/grapheme_clusters.rb +++ b/spec/ruby/core/string/shared/grapheme_clusters.rb @@ -9,8 +9,17 @@ describe :string_grapheme_clusters, shared: true do a.should == ['a', 'b', "\u{1f3f3}\u{fe0f}\u{200d}\u{1f308}", "\u{1F43E}"] end + it "returns grapheme clusters for various UTF encodings" do + [Encoding::UTF_16LE, Encoding::UTF_16BE, Encoding::UTF_32LE, Encoding::UTF_32BE].each do |enc| + a = [] + # test string: abc[rainbow flag emoji][paw prints] + "ab\u{1f3f3}\u{fe0f}\u{200d}\u{1f308}\u{1F43E}".encode(enc).send(@method) { |c| a << c } + a.should == ['a', 'b', "\u{1f3f3}\u{fe0f}\u{200d}\u{1f308}", "\u{1F43E}"].map { |s| s.encode(enc) } + end + end + it "returns self" do s = StringSpecs::MyString.new "ab\u{1f3f3}\u{fe0f}\u{200d}\u{1f308}\u{1F43E}" - s.send(@method) {}.should equal(s) + s.send(@method) {}.should.equal?(s) end end diff --git a/spec/ruby/core/string/shared/length.rb b/spec/ruby/core/string/shared/length.rb index 94e5ec135b..ae572ba755 100644 --- a/spec/ruby/core/string/shared/length.rb +++ b/spec/ruby/core/string/shared/length.rb @@ -18,7 +18,7 @@ describe :string_length, shared: true do end it "returns the length of the new self after encoding is changed" do - str = 'こにちわ' + str = +'こにちわ' str.send(@method) str.force_encoding('BINARY').send(@method).should == 12 @@ -44,12 +44,12 @@ describe :string_length, shared: true do end it "adds 1 (and not 2) for a incomplete surrogate in UTF-16" do - "\x00\xd8".force_encoding("UTF-16LE").send(@method).should == 1 - "\xd8\x00".force_encoding("UTF-16BE").send(@method).should == 1 + "\x00\xd8".dup.force_encoding("UTF-16LE").send(@method).should == 1 + "\xd8\x00".dup.force_encoding("UTF-16BE").send(@method).should == 1 end it "adds 1 for a broken sequence in UTF-32" do - "\x04\x03\x02\x01".force_encoding("UTF-32LE").send(@method).should == 1 - "\x01\x02\x03\x04".force_encoding("UTF-32BE").send(@method).should == 1 + "\x04\x03\x02\x01".dup.force_encoding("UTF-32LE").send(@method).should == 1 + "\x01\x02\x03\x04".dup.force_encoding("UTF-32BE").send(@method).should == 1 end end diff --git a/spec/ruby/core/string/shared/partition.rb b/spec/ruby/core/string/shared/partition.rb index 4cac149ce5..3f7e606eb3 100644 --- a/spec/ruby/core/string/shared/partition.rb +++ b/spec/ruby/core/string/shared/partition.rb @@ -4,15 +4,15 @@ require_relative '../fixtures/classes' describe :string_partition, shared: true do it "returns String instances when called on a subclass" do StringSpecs::MyString.new("hello").send(@method, "l").each do |item| - item.should be_an_instance_of(String) + item.should.instance_of?(String) end StringSpecs::MyString.new("hello").send(@method, "x").each do |item| - item.should be_an_instance_of(String) + item.should.instance_of?(String) end StringSpecs::MyString.new("hello").send(@method, /l./).each do |item| - item.should be_an_instance_of(String) + item.should.instance_of?(String) end end diff --git a/spec/ruby/core/string/shared/replace.rb b/spec/ruby/core/string/shared/replace.rb index a5108d9e7c..73b26351f1 100644 --- a/spec/ruby/core/string/shared/replace.rb +++ b/spec/ruby/core/string/shared/replace.rb @@ -1,7 +1,8 @@ +# frozen_string_literal: false describe :string_replace, shared: true do it "returns self" do a = "a" - a.send(@method, "b").should equal(a) + a.send(@method, "b").should.equal?(a) end it "replaces the content of self with other" do @@ -19,7 +20,7 @@ describe :string_replace, shared: true do it "carries over the encoding invalidity" do a = "\u{8765}".force_encoding('ascii') - "".send(@method, a).valid_encoding?.should be_false + "".send(@method, a).valid_encoding?.should == false end it "tries to convert other to string using to_str" do @@ -29,19 +30,19 @@ describe :string_replace, shared: true do end it "raises a TypeError if other can't be converted to string" do - -> { "hello".send(@method, 123) }.should raise_error(TypeError) - -> { "hello".send(@method, []) }.should raise_error(TypeError) - -> { "hello".send(@method, mock('x')) }.should raise_error(TypeError) + -> { "hello".send(@method, 123) }.should.raise(TypeError) + -> { "hello".send(@method, []) }.should.raise(TypeError) + -> { "hello".send(@method, mock('x')) }.should.raise(TypeError) end it "raises a FrozenError on a frozen instance that is modified" do a = "hello".freeze - -> { a.send(@method, "world") }.should raise_error(FrozenError) + -> { a.send(@method, "world") }.should.raise(FrozenError) end # see [ruby-core:23666] it "raises a FrozenError on a frozen instance when self-replacing" do a = "hello".freeze - -> { a.send(@method, a) }.should raise_error(FrozenError) + -> { a.send(@method, a) }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/shared/slice.rb b/spec/ruby/core/string/shared/slice.rb index 3ef4bc50d7..d296ab6680 100644 --- a/spec/ruby/core/string/shared/slice.rb +++ b/spec/ruby/core/string/shared/slice.rb @@ -21,17 +21,17 @@ describe :string_slice, shared: true do end it "raises a TypeError if the given index is nil" do - -> { "hello".send(@method, nil) }.should raise_error(TypeError) + -> { "hello".send(@method, nil) }.should.raise(TypeError) end it "raises a TypeError if the given index can't be converted to an Integer" do - -> { "hello".send(@method, mock('x')) }.should raise_error(TypeError) - -> { "hello".send(@method, {}) }.should raise_error(TypeError) - -> { "hello".send(@method, []) }.should raise_error(TypeError) + -> { "hello".send(@method, mock('x')) }.should.raise(TypeError) + -> { "hello".send(@method, {}) }.should.raise(TypeError) + -> { "hello".send(@method, []) }.should.raise(TypeError) end it "raises a RangeError if the index is too big" do - -> { "hello".send(@method, bignum_value) }.should raise_error(RangeError) + -> { "hello".send(@method, bignum_value) }.should.raise(RangeError) end end @@ -84,8 +84,8 @@ describe :string_slice_index_length, shared: true do s = "hello there" s.send(@method, 1, 9).encoding.should == s.encoding - a = "hello".force_encoding("binary") - b = " there".force_encoding("ISO-8859-1") + a = "hello".dup.force_encoding("binary") + b = " there".dup.force_encoding("ISO-8859-1") c = (a + b).force_encoding(Encoding::US_ASCII) c.send(@method, 0, 5).encoding.should == Encoding::US_ASCII @@ -119,6 +119,18 @@ describe :string_slice_index_length, shared: true do "hello there".send(@method, -4,-3).should == nil end + platform_is pointer_size: 64 do + it "returns nil if the length is negative big value" do + "hello there".send(@method, 4, -(1 << 31)).should == nil + + # by some reason length < -(1 << 31) on CI on Windows leads to + # 'RangeError: bignum too big to convert into `long'' error + platform_is_not :windows do + "hello there".send(@method, 4, -(1 << 63)).should == nil + end + end + end + it "calls to_int on the given index and the given length" do "hello".send(@method, 0.5, 1).should == "h" "hello".send(@method, 0.5, 2.5).should == "he" @@ -133,30 +145,35 @@ describe :string_slice_index_length, shared: true do end it "raises a TypeError when idx or length can't be converted to an integer" do - -> { "hello".send(@method, mock('x'), 0) }.should raise_error(TypeError) - -> { "hello".send(@method, 0, mock('x')) }.should raise_error(TypeError) + -> { "hello".send(@method, mock('x'), 0) }.should.raise(TypeError) + -> { "hello".send(@method, 0, mock('x')) }.should.raise(TypeError) # I'm deliberately including this here. # It means that str.send(@method, other, idx) isn't supported. - -> { "hello".send(@method, "", 0) }.should raise_error(TypeError) + -> { "hello".send(@method, "", 0) }.should.raise(TypeError) end it "raises a TypeError when the given index or the given length is nil" do - -> { "hello".send(@method, 1, nil) }.should raise_error(TypeError) - -> { "hello".send(@method, nil, 1) }.should raise_error(TypeError) - -> { "hello".send(@method, nil, nil) }.should raise_error(TypeError) + -> { "hello".send(@method, 1, nil) }.should.raise(TypeError) + -> { "hello".send(@method, nil, 1) }.should.raise(TypeError) + -> { "hello".send(@method, nil, nil) }.should.raise(TypeError) end it "raises a RangeError if the index or length is too big" do - -> { "hello".send(@method, bignum_value, 1) }.should raise_error(RangeError) - -> { "hello".send(@method, 0, bignum_value) }.should raise_error(RangeError) + -> { "hello".send(@method, bignum_value, 1) }.should.raise(RangeError) + -> { "hello".send(@method, 0, bignum_value) }.should.raise(RangeError) + end + + it "raises a RangeError if the index or length is too small" do + -> { "hello".send(@method, -bignum_value, 1) }.should.raise(RangeError) + -> { "hello".send(@method, 0, -bignum_value) }.should.raise(RangeError) end it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.send(@method, 0,0).should be_an_instance_of(String) - s.send(@method, 0,4).should be_an_instance_of(String) - s.send(@method, 1,4).should be_an_instance_of(String) + s.send(@method, 0,0).should.instance_of?(String) + s.send(@method, 0,4).should.instance_of?(String) + s.send(@method, 1,4).should.instance_of?(String) end it "handles repeated application" do @@ -233,9 +250,9 @@ describe :string_slice_range, shared: true do it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.send(@method, 0...0).should be_an_instance_of(String) - s.send(@method, 0..4).should be_an_instance_of(String) - s.send(@method, 1..4).should be_an_instance_of(String) + s.send(@method, 0...0).should.instance_of?(String) + s.send(@method, 0..4).should.instance_of?(String) + s.send(@method, 1..4).should.instance_of?(String) end it "calls to_int on range arguments" do @@ -276,12 +293,12 @@ describe :string_slice_range, shared: true do end it "raises a type error if a range is passed with a length" do - ->{ "hello".send(@method, 1..2, 1) }.should raise_error(TypeError) + ->{ "hello".send(@method, 1..2, 1) }.should.raise(TypeError) end it "raises a RangeError if one of the bound is too big" do - -> { "hello".send(@method, bignum_value..(bignum_value + 1)) }.should raise_error(RangeError) - -> { "hello".send(@method, 0..bignum_value) }.should raise_error(RangeError) + -> { "hello".send(@method, bignum_value..(bignum_value + 1)) }.should.raise(RangeError) + -> { "hello".send(@method, 0..bignum_value) }.should.raise(RangeError) end it "works with endless ranges" do @@ -316,8 +333,8 @@ describe :string_slice_regexp, shared: true do it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.send(@method, //).should be_an_instance_of(String) - s.send(@method, /../).should be_an_instance_of(String) + s.send(@method, //).should.instance_of?(String) + s.send(@method, /../).should.instance_of?(String) end it "sets $~ to MatchData when there is a match and nil when there's none" do @@ -377,19 +394,19 @@ describe :string_slice_regexp_index, shared: true do end it "raises a TypeError when the given index can't be converted to Integer" do - -> { "hello".send(@method, /(.)(.)(.)/, mock('x')) }.should raise_error(TypeError) - -> { "hello".send(@method, /(.)(.)(.)/, {}) }.should raise_error(TypeError) - -> { "hello".send(@method, /(.)(.)(.)/, []) }.should raise_error(TypeError) + -> { "hello".send(@method, /(.)(.)(.)/, mock('x')) }.should.raise(TypeError) + -> { "hello".send(@method, /(.)(.)(.)/, {}) }.should.raise(TypeError) + -> { "hello".send(@method, /(.)(.)(.)/, []) }.should.raise(TypeError) end it "raises a TypeError when the given index is nil" do - -> { "hello".send(@method, /(.)(.)(.)/, nil) }.should raise_error(TypeError) + -> { "hello".send(@method, /(.)(.)(.)/, nil) }.should.raise(TypeError) end it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.send(@method, /(.)(.)/, 0).should be_an_instance_of(String) - s.send(@method, /(.)(.)/, 1).should be_an_instance_of(String) + s.send(@method, /(.)(.)/, 0).should.instance_of?(String) + s.send(@method, /(.)(.)/, 1).should.instance_of?(String) end it "sets $~ to MatchData when there is a match and nil when there's none" do @@ -425,14 +442,14 @@ describe :string_slice_string, shared: true do o = mock('x') o.should_not_receive(:to_str) - -> { "hello".send(@method, o) }.should raise_error(TypeError) + -> { "hello".send(@method, o) }.should.raise(TypeError) end it "returns a String instance when given a subclass instance" do s = StringSpecs::MyString.new("el") r = "hello".send(@method, s) r.should == "el" - r.should be_an_instance_of(String) + r.should.instance_of?(String) end end @@ -459,28 +476,28 @@ describe :string_slice_regexp_group, shared: true do end it "returns nil if there is no match" do - "hello there".send(@method, /(?<whut>what?)/, 'whut').should be_nil + "hello there".send(@method, /(?<whut>what?)/, 'whut').should == nil end it "raises an IndexError if there is no capture for the given name" do -> do "hello there".send(@method, /[aeiou](.)\1/, 'non') - end.should raise_error(IndexError) + end.should.raise(IndexError) end it "raises a TypeError when the given name is not a String" do - -> { "hello".send(@method, /(?<q>.)/, mock('x')) }.should raise_error(TypeError) - -> { "hello".send(@method, /(?<q>.)/, {}) }.should raise_error(TypeError) - -> { "hello".send(@method, /(?<q>.)/, []) }.should raise_error(TypeError) + -> { "hello".send(@method, /(?<q>.)/, mock('x')) }.should.raise(TypeError) + -> { "hello".send(@method, /(?<q>.)/, {}) }.should.raise(TypeError) + -> { "hello".send(@method, /(?<q>.)/, []) }.should.raise(TypeError) end it "raises an IndexError when given the empty String as a group name" do - -> { "hello".send(@method, /(?<q>)/, '') }.should raise_error(IndexError) + -> { "hello".send(@method, /(?<q>)/, '') }.should.raise(IndexError) end it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.send(@method, /(?<q>.)/, 'q').should be_an_instance_of(String) + s.send(@method, /(?<q>.)/, 'q').should.instance_of?(String) end it "sets $~ to MatchData when there is a match and nil when there's none" do @@ -488,13 +505,13 @@ describe :string_slice_regexp_group, shared: true do $~[0].should == 'he' 'hello'.send(@method, /(?<non>not)/, 'non') - $~.should be_nil + $~.should == nil end end end describe :string_slice_symbol, shared: true do it "raises TypeError" do - -> { 'hello'.send(@method, :hello) }.should raise_error(TypeError) + -> { 'hello'.send(@method, :hello) }.should.raise(TypeError) end end diff --git a/spec/ruby/core/string/shared/strip.rb b/spec/ruby/core/string/shared/strip.rb index 3af77b50fe..39c7232ff9 100644 --- a/spec/ruby/core/string/shared/strip.rb +++ b/spec/ruby/core/string/shared/strip.rb @@ -7,8 +7,8 @@ describe :string_strip, shared: true do end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new(" hello ").send(@method).should be_an_instance_of(String) - StringSpecs::MyString.new(" ").send(@method).should be_an_instance_of(String) - StringSpecs::MyString.new("").send(@method).should be_an_instance_of(String) + StringSpecs::MyString.new(" hello ").send(@method).should.instance_of?(String) + StringSpecs::MyString.new(" ").send(@method).should.instance_of?(String) + StringSpecs::MyString.new("").send(@method).should.instance_of?(String) end end diff --git a/spec/ruby/core/string/shared/succ.rb b/spec/ruby/core/string/shared/succ.rb index 24a729ce26..8f1d327741 100644 --- a/spec/ruby/core/string/shared/succ.rb +++ b/spec/ruby/core/string/shared/succ.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :string_succ, shared: true do it "returns an empty string for empty strings" do "".send(@method).should == "" @@ -60,9 +60,9 @@ describe :string_succ, shared: true do end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("").send(@method).should be_an_instance_of(String) - StringSpecs::MyString.new("a").send(@method).should be_an_instance_of(String) - StringSpecs::MyString.new("z").send(@method).should be_an_instance_of(String) + StringSpecs::MyString.new("").send(@method).should.instance_of?(String) + StringSpecs::MyString.new("a").send(@method).should.instance_of?(String) + StringSpecs::MyString.new("z").send(@method).should.instance_of?(String) end it "returns a String in the same encoding as self" do @@ -73,14 +73,15 @@ end describe :string_succ_bang, shared: true do it "is equivalent to succ, but modifies self in place (still returns self)" do ["", "abcd", "THX1138"].each do |s| + s = +s r = s.dup.send(@method) - s.send(@method).should equal(s) + s.send(@method).should.equal?(s) s.should == r end end it "raises a FrozenError if self is frozen" do - -> { "".freeze.send(@method) }.should raise_error(FrozenError) - -> { "abcd".freeze.send(@method) }.should raise_error(FrozenError) + -> { "".freeze.send(@method) }.should.raise(FrozenError) + -> { "abcd".freeze.send(@method) }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/shared/to_s.rb b/spec/ruby/core/string/shared/to_s.rb index 4b87a6cbe1..96c59470d6 100644 --- a/spec/ruby/core/string/shared/to_s.rb +++ b/spec/ruby/core/string/shared/to_s.rb @@ -1,13 +1,13 @@ describe :string_to_s, shared: true do it "returns self when self.class == String" do a = "a string" - a.should equal(a.send(@method)) + a.should.equal?(a.send(@method)) end it "returns a new instance of String when called on a subclass" do a = StringSpecs::MyString.new("a string") s = a.send(@method) s.should == "a string" - s.should be_an_instance_of(String) + s.should.instance_of?(String) end end diff --git a/spec/ruby/core/string/shared/to_sym.rb b/spec/ruby/core/string/shared/to_sym.rb index 52d8314211..2a8a2e3182 100644 --- a/spec/ruby/core/string/shared/to_sym.rb +++ b/spec/ruby/core/string/shared/to_sym.rb @@ -1,42 +1,42 @@ describe :string_to_sym, shared: true do it "returns the symbol corresponding to self" do - "Koala".send(@method).should equal :Koala - 'cat'.send(@method).should equal :cat - '@cat'.send(@method).should equal :@cat - 'cat and dog'.send(@method).should equal :"cat and dog" - "abc=".send(@method).should equal :abc= + "Koala".send(@method).should.equal? :Koala + 'cat'.send(@method).should.equal? :cat + '@cat'.send(@method).should.equal? :@cat + 'cat and dog'.send(@method).should.equal? :"cat and dog" + "abc=".send(@method).should.equal? :abc= end it "does not special case +(binary) and -(binary)" do - "+(binary)".send(@method).should equal :"+(binary)" - "-(binary)".send(@method).should equal :"-(binary)" + "+(binary)".send(@method).should.equal? :"+(binary)" + "-(binary)".send(@method).should.equal? :"-(binary)" end it "does not special case certain operators" do - "!@".send(@method).should equal :"!@" - "~@".send(@method).should equal :"~@" - "!(unary)".send(@method).should equal :"!(unary)" - "~(unary)".send(@method).should equal :"~(unary)" - "+(unary)".send(@method).should equal :"+(unary)" - "-(unary)".send(@method).should equal :"-(unary)" + "!@".send(@method).should.equal? :"!@" + "~@".send(@method).should.equal? :"~@" + "!(unary)".send(@method).should.equal? :"!(unary)" + "~(unary)".send(@method).should.equal? :"~(unary)" + "+(unary)".send(@method).should.equal? :"+(unary)" + "-(unary)".send(@method).should.equal? :"-(unary)" end it "returns a US-ASCII Symbol for a UTF-8 String containing only US-ASCII characters" do sym = "foobar".send(@method) sym.encoding.should == Encoding::US_ASCII - sym.should equal :"foobar" + sym.should.equal? :"foobar" end it "returns a US-ASCII Symbol for a binary String containing only US-ASCII characters" do sym = "foobar".b.send(@method) sym.encoding.should == Encoding::US_ASCII - sym.should equal :"foobar" + sym.should.equal? :"foobar" end it "returns a UTF-8 Symbol for a UTF-8 String containing non US-ASCII characters" do sym = "il était une fois".send(@method) sym.encoding.should == Encoding::UTF_8 - sym.should equal :"il était une #{'fois'}" + sym.should.equal? :"il était une #{'fois'}" end it "returns a UTF-16LE Symbol for a UTF-16LE String containing non US-ASCII characters" do @@ -56,9 +56,9 @@ describe :string_to_sym, shared: true do it "ignores existing symbols with different encoding" do source = "fée" - iso_symbol = source.force_encoding(Encoding::ISO_8859_1).send(@method) + iso_symbol = source.dup.force_encoding(Encoding::ISO_8859_1).send(@method) iso_symbol.encoding.should == Encoding::ISO_8859_1 - binary_symbol = source.force_encoding(Encoding::BINARY).send(@method) + binary_symbol = source.dup.force_encoding(Encoding::BINARY).send(@method) binary_symbol.encoding.should == Encoding::BINARY end @@ -67,6 +67,6 @@ describe :string_to_sym, shared: true do invalid_utf8.should_not.valid_encoding? -> { invalid_utf8.send(@method) - }.should raise_error(EncodingError, 'invalid symbol in encoding UTF-8 :"\xC3"') + }.should.raise(EncodingError, 'invalid symbol in encoding UTF-8 :"\xC3"') end end diff --git a/spec/ruby/core/string/slice_spec.rb b/spec/ruby/core/string/slice_spec.rb index 87c5a7ac37..14e2251b3f 100644 --- a/spec/ruby/core/string/slice_spec.rb +++ b/spec/ruby/core/string/slice_spec.rb @@ -1,5 +1,5 @@ # -*- encoding: utf-8 -*- - +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/slice' @@ -54,9 +54,9 @@ describe "String#slice! with index" do end it "raises a FrozenError if self is frozen" do - -> { "hello".freeze.slice!(1) }.should raise_error(FrozenError) - -> { "hello".freeze.slice!(10) }.should raise_error(FrozenError) - -> { "".freeze.slice!(0) }.should raise_error(FrozenError) + -> { "hello".freeze.slice!(1) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(10) }.should.raise(FrozenError) + -> { "".freeze.slice!(0) }.should.raise(FrozenError) end it "calls to_int on index" do @@ -110,13 +110,13 @@ describe "String#slice! with index, length" do end it "raises a FrozenError if self is frozen" do - -> { "hello".freeze.slice!(1, 2) }.should raise_error(FrozenError) - -> { "hello".freeze.slice!(10, 3) }.should raise_error(FrozenError) - -> { "hello".freeze.slice!(-10, 3)}.should raise_error(FrozenError) - -> { "hello".freeze.slice!(4, -3) }.should raise_error(FrozenError) - -> { "hello".freeze.slice!(10, 3) }.should raise_error(FrozenError) - -> { "hello".freeze.slice!(-10, 3)}.should raise_error(FrozenError) - -> { "hello".freeze.slice!(4, -3) }.should raise_error(FrozenError) + -> { "hello".freeze.slice!(1, 2) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(10, 3) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(-10, 3)}.should.raise(FrozenError) + -> { "hello".freeze.slice!(4, -3) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(10, 3) }.should.raise(FrozenError) + -> { "hello".freeze.slice!(-10, 3)}.should.raise(FrozenError) + -> { "hello".freeze.slice!(4, -3) }.should.raise(FrozenError) end it "calls to_int on idx and length" do @@ -134,8 +134,8 @@ describe "String#slice! with index, length" do it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.slice!(0, 0).should be_an_instance_of(String) - s.slice!(0, 4).should be_an_instance_of(String) + s.slice!(0, 0).should.instance_of?(String) + s.slice!(0, 4).should.instance_of?(String) end it "returns the substring given by the character offsets" do @@ -177,8 +177,8 @@ describe "String#slice! Range" do it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.slice!(0...0).should be_an_instance_of(String) - s.slice!(0..4).should be_an_instance_of(String) + s.slice!(0...0).should.instance_of?(String) + s.slice!(0..4).should.instance_of?(String) end it "calls to_int on range arguments" do @@ -228,12 +228,12 @@ describe "String#slice! Range" do it "raises a FrozenError on a frozen instance that is modified" do - -> { "hello".freeze.slice!(1..3) }.should raise_error(FrozenError) + -> { "hello".freeze.slice!(1..3) }.should.raise(FrozenError) end # see redmine #1551 it "raises a FrozenError on a frozen instance that would not be modified" do - -> { "hello".freeze.slice!(10..20)}.should raise_error(FrozenError) + -> { "hello".freeze.slice!(10..20)}.should.raise(FrozenError) end end @@ -256,8 +256,8 @@ describe "String#slice! with Regexp" do it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.slice!(//).should be_an_instance_of(String) - s.slice!(/../).should be_an_instance_of(String) + s.slice!(//).should.instance_of?(String) + s.slice!(/../).should.instance_of?(String) end it "returns the matching portion of self with a multi byte character" do @@ -274,11 +274,11 @@ describe "String#slice! with Regexp" do end it "raises a FrozenError on a frozen instance that is modified" do - -> { "this is a string".freeze.slice!(/s.*t/) }.should raise_error(FrozenError) + -> { "this is a string".freeze.slice!(/s.*t/) }.should.raise(FrozenError) end it "raises a FrozenError on a frozen instance that would not be modified" do - -> { "this is a string".freeze.slice!(/zzz/) }.should raise_error(FrozenError) + -> { "this is a string".freeze.slice!(/zzz/) }.should.raise(FrozenError) end end @@ -316,8 +316,8 @@ describe "String#slice! with Regexp, index" do it "returns String instances" do s = StringSpecs::MyString.new("hello") - s.slice!(/(.)(.)/, 0).should be_an_instance_of(String) - s.slice!(/(.)(.)/, 1).should be_an_instance_of(String) + s.slice!(/(.)(.)/, 0).should.instance_of?(String) + s.slice!(/(.)(.)/, 1).should.instance_of?(String) end it "returns the encoding aware capture for the given index" do @@ -342,9 +342,9 @@ describe "String#slice! with Regexp, index" do end it "raises a FrozenError if self is frozen" do - -> { "this is a string".freeze.slice!(/s.*t/) }.should raise_error(FrozenError) - -> { "this is a string".freeze.slice!(/zzz/, 0)}.should raise_error(FrozenError) - -> { "this is a string".freeze.slice!(/(.)/, 2)}.should raise_error(FrozenError) + -> { "this is a string".freeze.slice!(/s.*t/) }.should.raise(FrozenError) + -> { "this is a string".freeze.slice!(/zzz/, 0)}.should.raise(FrozenError) + -> { "this is a string".freeze.slice!(/(.)/, 2)}.should.raise(FrozenError) end end @@ -372,19 +372,19 @@ describe "String#slice! with String" do o = mock('x') o.should_not_receive(:to_str) - -> { "hello".slice!(o) }.should raise_error(TypeError) + -> { "hello".slice!(o) }.should.raise(TypeError) end it "returns a subclass instance when given a subclass instance" do s = StringSpecs::MyString.new("el") r = "hello".slice!(s) r.should == "el" - r.should be_an_instance_of(String) + r.should.instance_of?(String) end it "raises a FrozenError if self is frozen" do - -> { "hello hello".freeze.slice!('llo') }.should raise_error(FrozenError) - -> { "this is a string".freeze.slice!('zzz')}.should raise_error(FrozenError) - -> { "this is a string".freeze.slice!('zzz')}.should raise_error(FrozenError) + -> { "hello hello".freeze.slice!('llo') }.should.raise(FrozenError) + -> { "this is a string".freeze.slice!('zzz')}.should.raise(FrozenError) + -> { "this is a string".freeze.slice!('zzz')}.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/split_spec.rb b/spec/ruby/core/string/split_spec.rb index c5cca651c2..6e8c1c6219 100644 --- a/spec/ruby/core/string/split_spec.rb +++ b/spec/ruby/core/string/split_spec.rb @@ -4,17 +4,17 @@ require_relative 'fixtures/classes' describe "String#split with String" do it "throws an ArgumentError if the string is not a valid" do - s = "\xDF".force_encoding(Encoding::UTF_8) + s = "\xDF".dup.force_encoding(Encoding::UTF_8) - -> { s.split }.should raise_error(ArgumentError) - -> { s.split(':') }.should raise_error(ArgumentError) + -> { s.split }.should.raise(ArgumentError) + -> { s.split(':') }.should.raise(ArgumentError) end it "throws an ArgumentError if the pattern is not a valid string" do str = 'проверка' - broken_str = "\xDF".force_encoding(Encoding::UTF_8) + broken_str = "\xDF".dup.force_encoding(Encoding::UTF_8) - -> { str.split(broken_str) }.should raise_error(ArgumentError) + -> { str.split(broken_str) }.should.raise(ArgumentError) end it "splits on multibyte characters" do @@ -94,7 +94,7 @@ describe "String#split with String" do end it "raises a RangeError when the limit is larger than int" do - -> { "a,b".split(" ", 2147483649) }.should raise_error(RangeError) + -> { "a,b".split(" ", 2147483649) }.should.raise(RangeError) end it "defaults to $; when string isn't given or nil" do @@ -119,21 +119,21 @@ describe "String#split with String" do $; = old_fs end end + end - context "when $; is not nil" do - before do - suppress_warning do - @old_value, $; = $;, 'foobar' - end + context "when $; is not nil" do + before do + suppress_warning do + @old_value, $; = $;, 'foobar' end + end - after do - $; = @old_value - end + after do + $; = @old_value + end - it "warns" do - -> { "".split }.should complain(/warning: \$; is set to non-nil value/) - end + it "warns" do + -> { "".split }.should complain(/warning: \$; is set to non-nil value/) end end @@ -197,11 +197,11 @@ describe "String#split with String" do ["", ".", " "].each do |pat| [-1, 0, 1, 2].each do |limit| StringSpecs::MyString.new(str).split(pat, limit).each do |x| - x.should be_an_instance_of(String) + x.should.instance_of?(String) end str.split(StringSpecs::MyString.new(pat), limit).each do |x| - x.should be_an_instance_of(String) + x.should.instance_of?(String) end end end @@ -229,9 +229,9 @@ end describe "String#split with Regexp" do it "throws an ArgumentError if the string is not a valid" do - s = "\xDF".force_encoding(Encoding::UTF_8) + s = "\xDF".dup.force_encoding(Encoding::UTF_8) - -> { s.split(/./) }.should raise_error(ArgumentError) + -> { s.split(/./) }.should.raise(ArgumentError) end it "divides self on regexp matches" do @@ -367,8 +367,8 @@ describe "String#split with Regexp" do end it "returns a type error if limit can't be converted to an integer" do - -> {"1.2.3.4".split(".", "three")}.should raise_error(TypeError) - -> {"1.2.3.4".split(".", nil) }.should raise_error(TypeError) + -> {"1.2.3.4".split(".", "three")}.should.raise(TypeError) + -> {"1.2.3.4".split(".", nil) }.should.raise(TypeError) end it "doesn't set $~" do @@ -391,7 +391,7 @@ describe "String#split with Regexp" do [//, /:/, /\s+/].each do |pat| [-1, 0, 1, 2].each do |limit| StringSpecs::MyString.new(str).split(pat, limit).each do |x| - x.should be_an_instance_of(String) + x.should.instance_of?(String) end end end @@ -409,11 +409,11 @@ describe "String#split with Regexp" do end it "returns an ArgumentError if an invalid UTF-8 string is supplied" do - broken_str = 'проверка' # in russian, means "test" + broken_str = +'проверка' # in russian, means "test" broken_str.force_encoding('binary') broken_str.chop! broken_str.force_encoding('utf-8') - ->{ broken_str.split(/\r\n|\r|\n/) }.should raise_error(ArgumentError) + ->{ broken_str.split(/\r\n|\r|\n/) }.should.raise(ArgumentError) end # See https://bugs.ruby-lang.org/issues/12689 and https://github.com/jruby/jruby/issues/4868 @@ -522,19 +522,19 @@ describe "String#split with Regexp" do StringSpecs::MyString.new("a|b").split("|") { |str| a << str } first, last = a - first.should be_an_instance_of(String) + first.should.instance_of?(String) first.should == "a" - last.should be_an_instance_of(String) + last.should.instance_of?(String) last.should == "b" end end it "raises a TypeError when not called with nil, String, or Regexp" do - -> { "hello".split(42) }.should raise_error(TypeError) - -> { "hello".split(:ll) }.should raise_error(TypeError) - -> { "hello".split(false) }.should raise_error(TypeError) - -> { "hello".split(Object.new) }.should raise_error(TypeError) + -> { "hello".split(42) }.should.raise(TypeError) + -> { "hello".split(:ll) }.should.raise(TypeError) + -> { "hello".split(false) }.should.raise(TypeError) + -> { "hello".split(Object.new) }.should.raise(TypeError) end it "returns Strings in the same encoding as self" do diff --git a/spec/ruby/core/string/squeeze_spec.rb b/spec/ruby/core/string/squeeze_spec.rb index 4796a170f2..52b6e1eed4 100644 --- a/spec/ruby/core/string/squeeze_spec.rb +++ b/spec/ruby/core/string/squeeze_spec.rb @@ -1,4 +1,5 @@ -# -*- encoding: binary -*- +# encoding: binary +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -50,8 +51,8 @@ describe "String#squeeze" do it "raises an ArgumentError when the parameter is out of sequence" do s = "--subbookkeeper--" - -> { s.squeeze("e-b") }.should raise_error(ArgumentError) - -> { s.squeeze("^e-b") }.should raise_error(ArgumentError) + -> { s.squeeze("e-b") }.should.raise(ArgumentError) + -> { s.squeeze("^e-b") }.should.raise(ArgumentError) end it "tries to convert each set arg to a string using to_str" do @@ -70,20 +71,20 @@ describe "String#squeeze" do end it "raises a TypeError when one set arg can't be converted to a string" do - -> { "hello world".squeeze([]) }.should raise_error(TypeError) - -> { "hello world".squeeze(Object.new)}.should raise_error(TypeError) - -> { "hello world".squeeze(mock('x')) }.should raise_error(TypeError) + -> { "hello world".squeeze([]) }.should.raise(TypeError) + -> { "hello world".squeeze(Object.new)}.should.raise(TypeError) + -> { "hello world".squeeze(mock('x')) }.should.raise(TypeError) end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("oh no!!!").squeeze("!").should be_an_instance_of(String) + StringSpecs::MyString.new("oh no!!!").squeeze("!").should.instance_of?(String) end end describe "String#squeeze!" do it "modifies self in place and returns self" do a = "yellow moon" - a.squeeze!.should equal(a) + a.squeeze!.should.equal?(a) a.should == "yelow mon" end @@ -96,15 +97,15 @@ describe "String#squeeze!" do it "raises an ArgumentError when the parameter is out of sequence" do s = "--subbookkeeper--" - -> { s.squeeze!("e-b") }.should raise_error(ArgumentError) - -> { s.squeeze!("^e-b") }.should raise_error(ArgumentError) + -> { s.squeeze!("e-b") }.should.raise(ArgumentError) + -> { s.squeeze!("^e-b") }.should.raise(ArgumentError) end it "raises a FrozenError when self is frozen" do a = "yellow moon" a.freeze - -> { a.squeeze!("") }.should raise_error(FrozenError) - -> { a.squeeze! }.should raise_error(FrozenError) + -> { a.squeeze!("") }.should.raise(FrozenError) + -> { a.squeeze! }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/start_with_spec.rb b/spec/ruby/core/string/start_with_spec.rb index 35e33b46a6..8b0ba6b5a7 100644 --- a/spec/ruby/core/string/start_with_spec.rb +++ b/spec/ruby/core/string/start_with_spec.rb @@ -11,17 +11,8 @@ describe "String#start_with?" do "\xA9".should.start_with?("\xA9") # A9 is not a character head for UTF-8 end - ruby_version_is ""..."3.3" do - it "does not check we are matching only part of a character" do - "\xe3\x81\x82".size.should == 1 - "\xe3\x81\x82".should.start_with?("\xe3") - end - end - - ruby_version_is "3.3" do # #19784 - it "checks we are matching only part of a character" do - "\xe3\x81\x82".size.should == 1 - "\xe3\x81\x82".should_not.start_with?("\xe3") - end + it "checks we are matching only part of a character" do + "\xe3\x81\x82".size.should == 1 + "\xe3\x81\x82".should_not.start_with?("\xe3") end end diff --git a/spec/ruby/core/string/strip_spec.rb b/spec/ruby/core/string/strip_spec.rb index 5e90fe35d0..81994a7f2e 100644 --- a/spec/ruby/core/string/strip_spec.rb +++ b/spec/ruby/core/string/strip_spec.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' require_relative 'shared/strip' @@ -19,7 +20,7 @@ end describe "String#strip!" do it "modifies self in place and returns self" do a = " hello " - a.strip!.should equal(a) + a.strip!.should.equal?(a) a.should == "hello" a = "\tgoodbye\r\v\n" @@ -46,12 +47,12 @@ describe "String#strip!" do end it "raises a FrozenError on a frozen instance that is modified" do - -> { " hello ".freeze.strip! }.should raise_error(FrozenError) + -> { " hello ".freeze.strip! }.should.raise(FrozenError) end # see #1552 it "raises a FrozenError on a frozen instance that would not be modified" do - -> {"hello".freeze.strip! }.should raise_error(FrozenError) - -> {"".freeze.strip! }.should raise_error(FrozenError) + -> {"hello".freeze.strip! }.should.raise(FrozenError) + -> {"".freeze.strip! }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/sub_spec.rb b/spec/ruby/core/string/sub_spec.rb index 51920486f5..f0082fba59 100644 --- a/spec/ruby/core/string/sub_spec.rb +++ b/spec/ruby/core/string/sub_spec.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -6,7 +7,7 @@ describe "String#sub with pattern, replacement" do a = "hello" b = a.sub(/w.*$/, "*") - b.should_not equal(a) + b.should_not.equal?(a) b.should == "hello" end @@ -146,16 +147,16 @@ describe "String#sub with pattern, replacement" do not_supported_on :opal do it "raises a TypeError when pattern is a Symbol" do - -> { "hello".sub(:woot, "x") }.should raise_error(TypeError) + -> { "hello".sub(:woot, "x") }.should.raise(TypeError) end end it "raises a TypeError when pattern is an Array" do - -> { "hello".sub([], "x") }.should raise_error(TypeError) + -> { "hello".sub([], "x") }.should.raise(TypeError) end it "raises a TypeError when pattern can't be converted to a string" do - -> { "hello".sub(Object.new, nil) }.should raise_error(TypeError) + -> { "hello".sub(Object.new, nil) }.should.raise(TypeError) end it "tries to convert replacement to a string using to_str" do @@ -166,15 +167,15 @@ describe "String#sub with pattern, replacement" do end it "raises a TypeError when replacement can't be converted to a string" do - -> { "hello".sub(/[aeiou]/, []) }.should raise_error(TypeError) - -> { "hello".sub(/[aeiou]/, 99) }.should raise_error(TypeError) + -> { "hello".sub(/[aeiou]/, []) }.should.raise(TypeError) + -> { "hello".sub(/[aeiou]/, 99) }.should.raise(TypeError) end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("").sub(//, "").should be_an_instance_of(String) - StringSpecs::MyString.new("").sub(/foo/, "").should be_an_instance_of(String) - StringSpecs::MyString.new("foo").sub(/foo/, "").should be_an_instance_of(String) - StringSpecs::MyString.new("foo").sub("foo", "").should be_an_instance_of(String) + StringSpecs::MyString.new("").sub(//, "").should.instance_of?(String) + StringSpecs::MyString.new("").sub(/foo/, "").should.instance_of?(String) + StringSpecs::MyString.new("foo").sub(/foo/, "").should.instance_of?(String) + StringSpecs::MyString.new("foo").sub("foo", "").should.instance_of?(String) end it "sets $~ to MatchData of match and nil when there's none" do @@ -231,10 +232,10 @@ describe "String#sub with pattern and block" do offsets = [] str.sub(/([aeiou])/) do - md = $~ - md.string.should == str - offsets << md.offset(0) - str + md = $~ + md.string.should == str + offsets << md.offset(0) + str end.should == "hhellollo" offsets.should == [[1, 2]] @@ -280,7 +281,7 @@ end describe "String#sub! with pattern, replacement" do it "modifies self in place and returns self" do a = "hello" - a.sub!(/[aeiou]/, '*').should equal(a) + a.sub!(/[aeiou]/, '*').should.equal?(a) a.should == "h*llo" end @@ -295,9 +296,9 @@ describe "String#sub! with pattern, replacement" do s = "hello" s.freeze - -> { s.sub!(/ROAR/, "x") }.should raise_error(FrozenError) - -> { s.sub!(/e/, "e") }.should raise_error(FrozenError) - -> { s.sub!(/[aeiou]/, '*') }.should raise_error(FrozenError) + -> { s.sub!(/ROAR/, "x") }.should.raise(FrozenError) + -> { s.sub!(/e/, "e") }.should.raise(FrozenError) + -> { s.sub!(/[aeiou]/, '*') }.should.raise(FrozenError) end it "handles a pattern in a superset encoding" do @@ -325,7 +326,7 @@ end describe "String#sub! with pattern and block" do it "modifies self in place and returns self" do a = "hello" - a.sub!(/[aeiou]/) { '*' }.should equal(a) + a.sub!(/[aeiou]/) { '*' }.should.equal?(a) a.should == "h*llo" end @@ -338,10 +339,10 @@ describe "String#sub! with pattern and block" do offsets = [] str.dup.sub!(/([aeiou])/) do - md = $~ - md.string.should == str - offsets << md.offset(0) - str + md = $~ + md.string.should == str + offsets << md.offset(0) + str end.should == "hhellollo" offsets.should == [[1, 2]] @@ -356,16 +357,16 @@ describe "String#sub! with pattern and block" do it "raises a RuntimeError if the string is modified while substituting" do str = "hello" - -> { str.sub!(//) { str << 'x' } }.should raise_error(RuntimeError) + -> { str.sub!(//) { str << 'x' } }.should.raise(RuntimeError) end it "raises a FrozenError when self is frozen" do s = "hello" s.freeze - -> { s.sub!(/ROAR/) { "x" } }.should raise_error(FrozenError) - -> { s.sub!(/e/) { "e" } }.should raise_error(FrozenError) - -> { s.sub!(/[aeiou]/) { '*' } }.should raise_error(FrozenError) + -> { s.sub!(/ROAR/) { "x" } }.should.raise(FrozenError) + -> { s.sub!(/e/) { "e" } }.should.raise(FrozenError) + -> { s.sub!(/[aeiou]/) { '*' } }.should.raise(FrozenError) end end @@ -500,12 +501,12 @@ end describe "String#sub with pattern and without replacement and block" do it "raises a ArgumentError" do - -> { "abca".sub(/a/) }.should raise_error(ArgumentError) + -> { "abca".sub(/a/) }.should.raise(ArgumentError) end end describe "String#sub! with pattern and without replacement and block" do it "raises a ArgumentError" do - -> { "abca".sub!(/a/) }.should raise_error(ArgumentError) + -> { "abca".sub!(/a/) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/swapcase_spec.rb b/spec/ruby/core/string/swapcase_spec.rb index d740fb86c6..f0e6e0182c 100644 --- a/spec/ruby/core/string/swapcase_spec.rb +++ b/spec/ruby/core/string/swapcase_spec.rb @@ -1,12 +1,13 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' describe "String#swapcase" do it "returns a new string with all uppercase chars from self converted to lowercase and vice versa" do - "Hello".swapcase.should == "hELLO" - "cYbEr_PuNk11".swapcase.should == "CyBeR_pUnK11" - "+++---111222???".swapcase.should == "+++---111222???" + "Hello".swapcase.should == "hELLO" + "cYbEr_PuNk11".swapcase.should == "CyBeR_pUnK11" + "+++---111222???".swapcase.should == "+++---111222???" end it "returns a String in the same encoding as self" do @@ -24,7 +25,7 @@ describe "String#swapcase" do swapcased.should == "aSSET" swapcased.size.should == 5 swapcased.bytesize.should == 5 - swapcased.ascii_only?.should be_true + swapcased.ascii_only?.should == true end end @@ -48,7 +49,7 @@ describe "String#swapcase" do end it "does not allow any other additional option" do - -> { "aiS".swapcase(:turkic, :ascii) }.should raise_error(ArgumentError) + -> { "aiS".swapcase(:turkic, :ascii) }.should.raise(ArgumentError) end end @@ -62,28 +63,28 @@ describe "String#swapcase" do end it "does not allow any other additional option" do - -> { "aiS".swapcase(:lithuanian, :ascii) }.should raise_error(ArgumentError) + -> { "aiS".swapcase(:lithuanian, :ascii) }.should.raise(ArgumentError) end end it "does not allow the :fold option for upcasing" do - -> { "abc".swapcase(:fold) }.should raise_error(ArgumentError) + -> { "abc".swapcase(:fold) }.should.raise(ArgumentError) end it "does not allow invalid options" do - -> { "abc".swapcase(:invalid_option) }.should raise_error(ArgumentError) + -> { "abc".swapcase(:invalid_option) }.should.raise(ArgumentError) end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("").swapcase.should be_an_instance_of(String) - StringSpecs::MyString.new("hello").swapcase.should be_an_instance_of(String) + StringSpecs::MyString.new("").swapcase.should.instance_of?(String) + StringSpecs::MyString.new("hello").swapcase.should.instance_of?(String) end end describe "String#swapcase!" do it "modifies self in place" do a = "cYbEr_PuNk11" - a.swapcase!.should equal(a) + a.swapcase!.should.equal?(a) a.should == "CyBeR_pUnK11" end @@ -113,7 +114,7 @@ describe "String#swapcase!" do swapcased.should == "aSSET" swapcased.size.should == 5 swapcased.bytesize.should == 5 - swapcased.ascii_only?.should be_true + swapcased.ascii_only?.should == true end end @@ -145,7 +146,7 @@ describe "String#swapcase!" do end it "does not allow any other additional option" do - -> { a = "aiS"; a.swapcase!(:turkic, :ascii) }.should raise_error(ArgumentError) + -> { a = "aiS"; a.swapcase!(:turkic, :ascii) }.should.raise(ArgumentError) end end @@ -163,16 +164,16 @@ describe "String#swapcase!" do end it "does not allow any other additional option" do - -> { a = "aiS"; a.swapcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError) + -> { a = "aiS"; a.swapcase!(:lithuanian, :ascii) }.should.raise(ArgumentError) end end it "does not allow the :fold option for upcasing" do - -> { a = "abc"; a.swapcase!(:fold) }.should raise_error(ArgumentError) + -> { a = "abc"; a.swapcase!(:fold) }.should.raise(ArgumentError) end it "does not allow invalid options" do - -> { a = "abc"; a.swapcase!(:invalid_option) }.should raise_error(ArgumentError) + -> { a = "abc"; a.swapcase!(:invalid_option) }.should.raise(ArgumentError) end it "returns nil if no modifications were made" do @@ -186,7 +187,7 @@ describe "String#swapcase!" do it "raises a FrozenError when self is frozen" do ["", "hello"].each do |a| a.freeze - -> { a.swapcase! }.should raise_error(FrozenError) + -> { a.swapcase! }.should.raise(FrozenError) end end end diff --git a/spec/ruby/core/string/to_c_spec.rb b/spec/ruby/core/string/to_c_spec.rb index 9d24f1f56c..9cd0ed4401 100644 --- a/spec/ruby/core/string/to_c_spec.rb +++ b/spec/ruby/core/string/to_c_spec.rb @@ -13,18 +13,20 @@ describe "String#to_c" do it "ignores trailing garbage" do '79+4iruby'.to_c.should == Complex(79, 4) - ruby_bug "[Bug #19087]", ""..."3.2" do - '7__9+4__0i'.to_c.should == Complex(7, 0) - end + '7__9+4__0i'.to_c.should == Complex(7, 0) end - it "understands Float::INFINITY" do - 'Infinity'.to_c.should == Complex(0, 1) - '-Infinity'.to_c.should == Complex(0, -1) - end + context "it treats special float value strings as characters" do + it "parses any string that starts with 'I' as 1i" do + 'Infinity'.to_c.should == Complex(0, 1) + '-Infinity'.to_c.should == Complex(0, -1) + 'Insecure'.to_c.should == Complex(0, 1) + '-Insecure'.to_c.should == Complex(0, -1) + end - it "understands Float::NAN" do - 'NaN'.to_c.should == Complex(0, 0) + it "does not parse any numeric information in 'NaN'" do + 'NaN'.to_c.should == Complex(0, 0) + end end it "allows null-byte" do @@ -36,18 +38,16 @@ describe "String#to_c" do it "raises Encoding::CompatibilityError if String is in not ASCII-compatible encoding" do -> { '79+4i'.encode("UTF-16").to_c - }.should raise_error(Encoding::CompatibilityError, "ASCII incompatible encoding: UTF-16") + }.should.raise(Encoding::CompatibilityError, "ASCII incompatible encoding: UTF-16") end - ruby_version_is "3.2" do - it "treats a sequence of underscores as an end of Complex string" do - "5+3_1i".to_c.should == Complex(5, 31) - "5+3__1i".to_c.should == Complex(5) - "5+3___1i".to_c.should == Complex(5) + it "treats a sequence of underscores as an end of Complex string" do + "5+3_1i".to_c.should == Complex(5, 31) + "5+3__1i".to_c.should == Complex(5) + "5+3___1i".to_c.should == Complex(5) - "12_3".to_c.should == Complex(123) - "12__3".to_c.should == Complex(12) - "12___3".to_c.should == Complex(12) - end + "12_3".to_c.should == Complex(123) + "12__3".to_c.should == Complex(12) + "12___3".to_c.should == Complex(12) end end diff --git a/spec/ruby/core/string/to_f_spec.rb b/spec/ruby/core/string/to_f_spec.rb index cf64ecfc5d..bb09e4f2f3 100644 --- a/spec/ruby/core/string/to_f_spec.rb +++ b/spec/ruby/core/string/to_f_spec.rb @@ -5,16 +5,15 @@ require_relative 'fixtures/classes' describe "String#to_f" do it "treats leading characters of self as a floating point number" do - "123.45e1".to_f.should == 1234.5 - "45.67 degrees".to_f.should == 45.67 - "0".to_f.should == 0.0 - "123.45e1".to_f.should == 1234.5 + "123.45e1".to_f.should == 1234.5 + "45.67 degrees".to_f.should == 45.67 + "0".to_f.should == 0.0 - ".5".to_f.should == 0.5 - ".5e1".to_f.should == 5.0 - "5.".to_f.should == 5.0 - "5e".to_f.should == 5.0 - "5E".to_f.should == 5.0 + ".5".to_f.should == 0.5 + ".5e1".to_f.should == 5.0 + "5.".to_f.should == 5.0 + "5e".to_f.should == 5.0 + "5E".to_f.should == 5.0 end it "treats special float value strings as characters" do @@ -43,18 +42,39 @@ describe "String#to_f" do "1_234_567.890_1".to_f.should == 1_234_567.890_1 end - it "returns 0 for strings with any non-digit in them" do - "blah".to_f.should == 0 - "0b5".to_f.should == 0 - "0d5".to_f.should == 0 - "0o5".to_f.should == 0 - "0xx5".to_f.should == 0 - end - it "returns 0 for strings with leading underscores" do "_9".to_f.should == 0 end + it "stops if the underscore is not followed or preceded by a number" do + "1__2".to_f.should == 1.0 + "1_.2".to_f.should == 1.0 + "1._2".to_f.should == 1.0 + "1.2_e2".to_f.should == 1.2 + "1.2e_2".to_f.should == 1.2 + "1_x2".to_f.should == 1.0 + "1x_2".to_f.should == 1.0 + "+_1".to_f.should == 0.0 + "-_1".to_f.should == 0.0 + end + + it "does not allow prefixes to autodetect the base" do + "0b10".to_f.should == 0 + "010".to_f.should == 10 + "0o10".to_f.should == 0 + "0d10".to_f.should == 0 + "0x10".to_f.should == 0 + end + + it "treats any non-numeric character other than '.', 'e' and '_' as terminals" do + "blah".to_f.should == 0 + "1b5".to_f.should == 1 + "1d5".to_f.should == 1 + "1o5".to_f.should == 1 + "1xx5".to_f.should == 1 + "x5".to_f.should == 0 + end + it "takes an optional sign" do "-45.67 degrees".to_f.should == -45.67 "+45.67 degrees".to_f.should == 45.67 @@ -63,8 +83,58 @@ describe "String#to_f" do (1.0 / "-0".to_f).to_s.should == "-Infinity" end + it "treats a second 'e' as terminal" do + "1.234e1e2".to_f.should == 1.234e1 + end + + it "treats a second '.' as terminal" do + "1.2.3".to_f.should == 1.2 + end + + it "treats a '.' after an 'e' as terminal" do + "1.234e1.9".to_f.should == 1.234e1 + end + it "returns 0.0 if the conversion fails" do "bad".to_f.should == 0.0 "thx1138".to_f.should == 0.0 end + + it "ignores leading and trailing whitespace" do + " 1.2".to_f.should == 1.2 + "1.2 ".to_f.should == 1.2 + " 1.2 ".to_f.should == 1.2 + "\t1.2".to_f.should == 1.2 + "\n1.2".to_f.should == 1.2 + "\v1.2".to_f.should == 1.2 + "\f1.2".to_f.should == 1.2 + "\r1.2".to_f.should == 1.2 + end + + it "treats non-printable ASCII characters as terminals" do + "\0001.2".to_f.should == 0 + "\0011.2".to_f.should == 0 + "\0371.2".to_f.should == 0 + "\1771.2".to_f.should == 0 + "\2001.2".b.to_f.should == 0 + "\3771.2".b.to_f.should == 0 + end + + it "raises Encoding::CompatibilityError if String is in not ASCII-compatible encoding" do + -> { + '1.2'.encode("UTF-16").to_f + }.should.raise(Encoding::CompatibilityError, "ASCII incompatible encoding: UTF-16") + end + + it "allows String representation without a fractional part" do + "1.".to_f.should == 1.0 + "+1.".to_f.should == 1.0 + "-1.".to_f.should == -1.0 + "1.e+0".to_f.should == 1.0 + "1.e+0".to_f.should == 1.0 + + ruby_bug "#20705", ""..."3.4" do + "1.e-2".to_f.should be_close(0.01, TOLERANCE) + end + end end diff --git a/spec/ruby/core/string/to_i_spec.rb b/spec/ruby/core/string/to_i_spec.rb index e4fa89aab3..629750bd73 100644 --- a/spec/ruby/core/string/to_i_spec.rb +++ b/spec/ruby/core/string/to_i_spec.rb @@ -10,6 +10,18 @@ describe "String#to_i" do "1_2_3asdf".to_i.should == 123 end + it "ignores multiple non-consecutive underscores when the first digit is 0" do + (2..16).each do |base| + "0_0_010".to_i(base).should == base; + end + end + + it "bails out at the first double underscore if the first digit is 0" do + (2..16).each do |base| + "010__1".to_i(base).should == base; + end + end + it "ignores leading whitespaces" do [ " 123", " 123", "\r\n\r\n123", "\t\t123", "\r\n\t\n123", " \t\n\r\t 123"].each do |str| @@ -126,31 +138,31 @@ describe "String#to_i" do end it "raises an ArgumentError for illegal bases (1, < 0 or > 36)" do - -> { "".to_i(1) }.should raise_error(ArgumentError) - -> { "".to_i(-1) }.should raise_error(ArgumentError) - -> { "".to_i(37) }.should raise_error(ArgumentError) + -> { "".to_i(1) }.should.raise(ArgumentError) + -> { "".to_i(-1) }.should.raise(ArgumentError) + -> { "".to_i(37) }.should.raise(ArgumentError) end it "returns an Integer for long strings with trailing spaces" do "0 ".to_i.should == 0 - "0 ".to_i.should be_an_instance_of(Integer) + "0 ".to_i.should.instance_of?(Integer) "10 ".to_i.should == 10 - "10 ".to_i.should be_an_instance_of(Integer) + "10 ".to_i.should.instance_of?(Integer) "-10 ".to_i.should == -10 - "-10 ".to_i.should be_an_instance_of(Integer) + "-10 ".to_i.should.instance_of?(Integer) end it "returns an Integer for long strings with leading spaces" do " 0".to_i.should == 0 - " 0".to_i.should be_an_instance_of(Integer) + " 0".to_i.should.instance_of?(Integer) " 10".to_i.should == 10 - " 10".to_i.should be_an_instance_of(Integer) + " 10".to_i.should.instance_of?(Integer) " -10".to_i.should == -10 - " -10".to_i.should be_an_instance_of(Integer) + " -10".to_i.should.instance_of?(Integer) end it "returns the correct Integer for long strings" do diff --git a/spec/ruby/core/string/to_r_spec.rb b/spec/ruby/core/string/to_r_spec.rb index 7e1d635d3b..fb7c9d108e 100644 --- a/spec/ruby/core/string/to_r_spec.rb +++ b/spec/ruby/core/string/to_r_spec.rb @@ -2,7 +2,7 @@ require_relative '../../spec_helper' describe "String#to_r" do it "returns a Rational object" do - String.new.to_r.should be_an_instance_of(Rational) + String.new.to_r.should.instance_of?(Rational) end it "returns (0/1) for the empty String" do @@ -33,6 +33,10 @@ describe "String#to_r" do "-20".to_r.should == Rational(-20, 1) end + it "accepts leading plus signs" do + "+20".to_r.should == Rational(20, 1) + end + it "does not treat a leading period without a numeric prefix as a decimal point" do ".9".to_r.should_not == Rational(8106479329266893, 9007199254740992) end diff --git a/spec/ruby/core/string/tr_s_spec.rb b/spec/ruby/core/string/tr_s_spec.rb index 3c31473044..22a193ec4b 100644 --- a/spec/ruby/core/string/tr_s_spec.rb +++ b/spec/ruby/core/string/tr_s_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -17,13 +18,11 @@ describe "String#tr_s" do "hello ^--^".tr_s("---", "_").should == "hello ^_^" end - ruby_bug "#19769", ""..."3.3" do - it "accepts c1-c1 notation to denote range of one character" do - "hello".tr_s('e-e', 'x').should == "hxllo" - "123456789".tr_s("2-23","xy").should == "1xy456789" - "hello ^-^".tr_s("e-", "a-a_").should == "hallo ^_^" - "hello ^-^".tr_s("---o", "_a").should == "hella ^_^" - end + it "accepts c1-c1 notation to denote range of one character" do + "hello".tr_s('e-e', 'x').should == "hxllo" + "123456789".tr_s("2-23","xy").should == "1xy456789" + "hello ^-^".tr_s("e-", "a-a_").should == "hallo ^_^" + "hello ^-^".tr_s("---o", "_a").should == "hella ^_^" end it "pads to_str with its last char if it is shorter than from_string" do @@ -55,7 +54,7 @@ describe "String#tr_s" do end it "returns String instances when called on a subclass" do - StringSpecs::MyString.new("hello").tr_s("e", "a").should be_an_instance_of(String) + StringSpecs::MyString.new("hello").tr_s("e", "a").should.instance_of?(String) end # http://redmine.ruby-lang.org/issues/show/1839 @@ -125,8 +124,8 @@ describe "String#tr_s!" do it "raises a FrozenError if self is frozen" do s = "hello".freeze - -> { s.tr_s!("el", "ar") }.should raise_error(FrozenError) - -> { s.tr_s!("l", "r") }.should raise_error(FrozenError) - -> { s.tr_s!("", "") }.should raise_error(FrozenError) + -> { s.tr_s!("el", "ar") }.should.raise(FrozenError) + -> { s.tr_s!("l", "r") }.should.raise(FrozenError) + -> { s.tr_s!("", "") }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/tr_spec.rb b/spec/ruby/core/string/tr_spec.rb index d60480dc7e..cb57c3851e 100644 --- a/spec/ruby/core/string/tr_spec.rb +++ b/spec/ruby/core/string/tr_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -16,13 +17,11 @@ describe "String#tr" do "hello ^-^".tr("---", "_").should == "hello ^_^" end - ruby_bug "#19769", ""..."3.3" do - it "accepts c1-c1 notation to denote range of one character" do - "hello".tr('e-e', 'x').should == "hxllo" - "123456789".tr("2-23","xy").should == "1xy456789" - "hello ^-^".tr("e-", "a-a_").should == "hallo ^_^" - "hello ^-^".tr("---o", "_a").should == "hella ^_^" - end + it "accepts c1-c1 notation to denote range of one character" do + "hello".tr('e-e', 'x').should == "hxllo" + "123456789".tr("2-23","xy").should == "1xy456789" + "hello ^-^".tr("e-", "a-a_").should == "hallo ^_^" + "hello ^-^".tr("---o", "_a").should == "hella ^_^" end it "pads to_str with its last char if it is shorter than from_string" do @@ -31,11 +30,11 @@ describe "String#tr" do end it "raises an ArgumentError a descending range in the replacement as containing just the start character" do - -> { "hello".tr("a-y", "z-b") }.should raise_error(ArgumentError) + -> { "hello".tr("a-y", "z-b") }.should.raise(ArgumentError) end it "raises an ArgumentError a descending range in the source as empty" do - -> { "hello".tr("l-a", "z") }.should raise_error(ArgumentError) + -> { "hello".tr("l-a", "z") }.should.raise(ArgumentError) end it "translates chars not in from_string when it starts with a ^" do @@ -67,7 +66,7 @@ describe "String#tr" do end it "returns Stringinstances when called on a subclass" do - StringSpecs::MyString.new("hello").tr("e", "a").should be_an_instance_of(String) + StringSpecs::MyString.new("hello").tr("e", "a").should.instance_of?(String) end # http://redmine.ruby-lang.org/issues/show/1839 @@ -120,8 +119,8 @@ describe "String#tr!" do it "raises a FrozenError if self is frozen" do s = "abcdefghijklmnopqR".freeze - -> { s.tr!("cdefg", "12") }.should raise_error(FrozenError) - -> { s.tr!("R", "S") }.should raise_error(FrozenError) - -> { s.tr!("", "") }.should raise_error(FrozenError) + -> { s.tr!("cdefg", "12") }.should.raise(FrozenError) + -> { s.tr!("R", "S") }.should.raise(FrozenError) + -> { s.tr!("", "") }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/try_convert_spec.rb b/spec/ruby/core/string/try_convert_spec.rb index 72ce5dd8b2..0c0219cd2e 100644 --- a/spec/ruby/core/string/try_convert_spec.rb +++ b/spec/ruby/core/string/try_convert_spec.rb @@ -4,47 +4,47 @@ require_relative 'fixtures/classes' describe "String.try_convert" do it "returns the argument if it's a String" do x = String.new - String.try_convert(x).should equal(x) + String.try_convert(x).should.equal?(x) end it "returns the argument if it's a kind of String" do x = StringSpecs::MyString.new - String.try_convert(x).should equal(x) + String.try_convert(x).should.equal?(x) end it "returns nil when the argument does not respond to #to_str" do - String.try_convert(Object.new).should be_nil + String.try_convert(Object.new).should == nil end it "sends #to_str to the argument and returns the result if it's nil" do obj = mock("to_str") obj.should_receive(:to_str).and_return(nil) - String.try_convert(obj).should be_nil + String.try_convert(obj).should == nil end it "sends #to_str to the argument and returns the result if it's a String" do x = String.new obj = mock("to_str") obj.should_receive(:to_str).and_return(x) - String.try_convert(obj).should equal(x) + String.try_convert(obj).should.equal?(x) end it "sends #to_str to the argument and returns the result if it's a kind of String" do x = StringSpecs::MyString.new obj = mock("to_str") obj.should_receive(:to_str).and_return(x) - String.try_convert(obj).should equal(x) + String.try_convert(obj).should.equal?(x) end it "sends #to_str to the argument and raises TypeError if it's not a kind of String" do obj = mock("to_str") obj.should_receive(:to_str).and_return(Object.new) - -> { String.try_convert obj }.should raise_error(TypeError, "can't convert MockObject to String (MockObject#to_str gives Object)") + -> { String.try_convert obj }.should raise_consistent_error(TypeError, "can't convert MockObject into String (MockObject#to_str gives Object)") end it "does not rescue exceptions raised by #to_str" do obj = mock("to_str") obj.should_receive(:to_str).and_raise(RuntimeError) - -> { String.try_convert obj }.should raise_error(RuntimeError) + -> { String.try_convert obj }.should.raise(RuntimeError) end end diff --git a/spec/ruby/core/string/undump_spec.rb b/spec/ruby/core/string/undump_spec.rb index 6ff220161c..8516e24b3b 100644 --- a/spec/ruby/core/string/undump_spec.rb +++ b/spec/ruby/core/string/undump_spec.rb @@ -8,7 +8,7 @@ describe "String#undump" do end it "always returns String instance" do - StringSpecs::MyString.new('"foo"').undump.should be_an_instance_of(String) + StringSpecs::MyString.new('"foo"').undump.should.instance_of?(String) end it "strips outer \"" do @@ -396,46 +396,46 @@ describe "String#undump" do describe "Limitations" do it "cannot undump non ASCII-compatible string" do - -> { '"foo"'.encode('utf-16le').undump }.should raise_error(Encoding::CompatibilityError) + -> { '"foo"'.encode('utf-16le').undump }.should.raise(Encoding::CompatibilityError) end end describe "invalid dump" do it "raises RuntimeError exception if wrapping \" are missing" do - -> { 'foo'.undump }.should raise_error(RuntimeError, /invalid dumped string/) - -> { '"foo'.undump }.should raise_error(RuntimeError, /unterminated dumped string/) - -> { 'foo"'.undump }.should raise_error(RuntimeError, /invalid dumped string/) - -> { "'foo'".undump }.should raise_error(RuntimeError, /invalid dumped string/) + -> { 'foo'.undump }.should.raise(RuntimeError, /invalid dumped string/) + -> { '"foo'.undump }.should.raise(RuntimeError, /unterminated dumped string/) + -> { 'foo"'.undump }.should.raise(RuntimeError, /invalid dumped string/) + -> { "'foo'".undump }.should.raise(RuntimeError, /invalid dumped string/) end it "raises RuntimeError if there is incorrect \\x sequence" do - -> { '"\x"'.undump }.should raise_error(RuntimeError, /invalid hex escape/) - -> { '"\\x3y"'.undump }.should raise_error(RuntimeError, /invalid hex escape/) + -> { '"\x"'.undump }.should.raise(RuntimeError, /invalid hex escape/) + -> { '"\\x3y"'.undump }.should.raise(RuntimeError, /invalid hex escape/) end it "raises RuntimeError in there is incorrect \\u sequence" do - -> { '"\\u"'.undump }.should raise_error(RuntimeError, /invalid Unicode escape/) - -> { '"\\u{"'.undump }.should raise_error(RuntimeError, /invalid Unicode escape/) - -> { '"\\u{3042"'.undump }.should raise_error(RuntimeError, /invalid Unicode escape/) - -> { '"\\u"'.undump }.should raise_error(RuntimeError, /invalid Unicode escape/) + -> { '"\\u"'.undump }.should.raise(RuntimeError, /invalid Unicode escape/) + -> { '"\\u{"'.undump }.should.raise(RuntimeError, /invalid Unicode escape/) + -> { '"\\u{3042"'.undump }.should.raise(RuntimeError, /invalid Unicode escape/) + -> { '"\\u"'.undump }.should.raise(RuntimeError, /invalid Unicode escape/) end it "raises RuntimeError if there is malformed dump of non ASCII-compatible string" do - -> { '"".force_encoding("BINARY"'.undump }.should raise_error(RuntimeError, /invalid dumped string/) - -> { '"".force_encoding("Unknown")'.undump }.should raise_error(RuntimeError, /dumped string has unknown encoding name/) - -> { '"".force_encoding()'.undump }.should raise_error(RuntimeError, /invalid dumped string/) + -> { '"".force_encoding("BINARY"'.undump }.should.raise(RuntimeError, /invalid dumped string/) + -> { '"".force_encoding("Unknown")'.undump }.should.raise(RuntimeError, /dumped string has unknown encoding name/) + -> { '"".force_encoding()'.undump }.should.raise(RuntimeError, /invalid dumped string/) end it "raises RuntimeError if string contains \0 character" do - -> { "\"foo\0\"".undump }.should raise_error(RuntimeError, /string contains null byte/) + -> { "\"foo\0\"".undump }.should.raise(RuntimeError, /string contains null byte/) end it "raises RuntimeError if string contains non ASCII character" do - -> { "\"\u3042\"".undump }.should raise_error(RuntimeError, /non-ASCII character detected/) + -> { "\"\u3042\"".undump }.should.raise(RuntimeError, /non-ASCII character detected/) end it "raises RuntimeError if there are some excessive \"" do - -> { '" "" "'.undump }.should raise_error(RuntimeError, /invalid dumped string/) + -> { '" "" "'.undump }.should.raise(RuntimeError, /invalid dumped string/) end end end diff --git a/spec/ruby/core/string/unicode_normalize_spec.rb b/spec/ruby/core/string/unicode_normalize_spec.rb index 6de7533fc7..92b3a46b43 100644 --- a/spec/ruby/core/string/unicode_normalize_spec.rb +++ b/spec/ruby/core/string/unicode_normalize_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' # Examples taken from http://www.unicode.org/reports/tr15/#Norm_Forms @@ -50,13 +51,13 @@ describe "String#unicode_normalize" do it "raises an Encoding::CompatibilityError if string is not in an unicode encoding" do -> do [0xE0].pack('C').force_encoding("ISO-8859-1").unicode_normalize(:nfd) - end.should raise_error(Encoding::CompatibilityError) + end.should.raise(Encoding::CompatibilityError) end it "raises an ArgumentError if the specified form is invalid" do -> { @angstrom.unicode_normalize(:invalid_form) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end @@ -103,13 +104,13 @@ describe "String#unicode_normalize!" do it "raises an Encoding::CompatibilityError if the string is not in an unicode encoding" do -> { [0xE0].pack('C').force_encoding("ISO-8859-1").unicode_normalize! - }.should raise_error(Encoding::CompatibilityError) + }.should.raise(Encoding::CompatibilityError) end it "raises an ArgumentError if the specified form is invalid" do ohm = "\u2126" -> { ohm.unicode_normalize!(:invalid_form) - }.should raise_error(ArgumentError) + }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unicode_normalized_spec.rb b/spec/ruby/core/string/unicode_normalized_spec.rb index 87f3740459..3ca27d35dd 100644 --- a/spec/ruby/core/string/unicode_normalized_spec.rb +++ b/spec/ruby/core/string/unicode_normalized_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' describe "String#unicode_normalized?" do @@ -37,38 +38,38 @@ describe "String#unicode_normalized?" do end it "raises an Encoding::CompatibilityError if the string is not in an unicode encoding" do - -> { @nfc_normalized_str.force_encoding("ISO-8859-1").unicode_normalized? }.should raise_error(Encoding::CompatibilityError) + -> { @nfc_normalized_str.force_encoding("ISO-8859-1").unicode_normalized? }.should.raise(Encoding::CompatibilityError) end it "raises an ArgumentError if the specified form is invalid" do - -> { @nfc_normalized_str.unicode_normalized?(:invalid_form) }.should raise_error(ArgumentError) + -> { @nfc_normalized_str.unicode_normalized?(:invalid_form) }.should.raise(ArgumentError) end it "returns true if str is in Unicode normalization form (nfc)" do str = "a\u0300" - str.unicode_normalized?(:nfc).should be_false + str.unicode_normalized?(:nfc).should == false str.unicode_normalize!(:nfc) - str.unicode_normalized?(:nfc).should be_true + str.unicode_normalized?(:nfc).should == true end it "returns true if str is in Unicode normalization form (nfd)" do str = "a\u00E0" - str.unicode_normalized?(:nfd).should be_false + str.unicode_normalized?(:nfd).should == false str.unicode_normalize!(:nfd) - str.unicode_normalized?(:nfd).should be_true + str.unicode_normalized?(:nfd).should == true end it "returns true if str is in Unicode normalization form (nfkc)" do str = "a\u0300" - str.unicode_normalized?(:nfkc).should be_false + str.unicode_normalized?(:nfkc).should == false str.unicode_normalize!(:nfkc) - str.unicode_normalized?(:nfkc).should be_true + str.unicode_normalized?(:nfkc).should == true end it "returns true if str is in Unicode normalization form (nfkd)" do str = "a\u00E0" - str.unicode_normalized?(:nfkd).should be_false + str.unicode_normalized?(:nfkd).should == false str.unicode_normalize!(:nfkd) - str.unicode_normalized?(:nfkd).should be_true + str.unicode_normalized?(:nfkd).should == true end end diff --git a/spec/ruby/core/string/unpack/a_spec.rb b/spec/ruby/core/string/unpack/a_spec.rb index 2d83b4c824..a68e842e15 100644 --- a/spec/ruby/core/string/unpack/a_spec.rb +++ b/spec/ruby/core/string/unpack/a_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -31,7 +31,7 @@ describe "String#unpack with format 'A'" do end it "decodes into raw (ascii) string values" do - str = "str".force_encoding('UTF-8').unpack("A*")[0] + str = "str".dup.force_encoding('UTF-8').unpack("A*")[0] str.encoding.should == Encoding::BINARY end diff --git a/spec/ruby/core/string/unpack/at_spec.rb b/spec/ruby/core/string/unpack/at_spec.rb index 70b2389d69..f4999f5922 100644 --- a/spec/ruby/core/string/unpack/at_spec.rb +++ b/spec/ruby/core/string/unpack/at_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -24,6 +24,6 @@ describe "String#unpack with format '@'" do end it "raises an ArgumentError if the count exceeds the size of the String" do - -> { "\x01\x02\x03\x04".unpack("C2@5C") }.should raise_error(ArgumentError) + -> { "\x01\x02\x03\x04".unpack("C2@5C") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unpack/b_spec.rb b/spec/ruby/core/string/unpack/b_spec.rb index 5c53eff721..fac6ef5151 100644 --- a/spec/ruby/core/string/unpack/b_spec.rb +++ b/spec/ruby/core/string/unpack/b_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -86,20 +86,10 @@ describe "String#unpack with format 'B'" do ].should be_computed_by(:unpack, "BBB") end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "\x80\x00".unpack("B\x00B").should == ["1", "0"] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "\x80\x00".unpack("B\x00B") - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x80\x00".unpack("B\x00B") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -107,7 +97,7 @@ describe "String#unpack with format 'B'" do end it "decodes into US-ASCII string values" do - str = "s".force_encoding('UTF-8').unpack("B*")[0] + str = "s".dup.force_encoding('UTF-8').unpack("B*")[0] str.encoding.name.should == 'US-ASCII' end end @@ -194,20 +184,10 @@ describe "String#unpack with format 'b'" do ].should be_computed_by(:unpack, "bbb") end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "\x01\x00".unpack("b\x00b").should == ["1", "0"] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "\x01\x00".unpack("b\x00b") - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x00".unpack("b\x00b") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -215,7 +195,7 @@ describe "String#unpack with format 'b'" do end it "decodes into US-ASCII string values" do - str = "s".force_encoding('UTF-8').unpack("b*")[0] + str = "s".dup.force_encoding('UTF-8').unpack("b*")[0] str.encoding.name.should == 'US-ASCII' end end diff --git a/spec/ruby/core/string/unpack/c_spec.rb b/spec/ruby/core/string/unpack/c_spec.rb index c2bf813954..d881015b5e 100644 --- a/spec/ruby/core/string/unpack/c_spec.rb +++ b/spec/ruby/core/string/unpack/c_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -35,20 +35,10 @@ describe :string_unpack_8bit, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "abc".unpack(unpack_format("\000", 2)).should == [97, 98] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "abc".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "abc".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/carret_spec.rb b/spec/ruby/core/string/unpack/carret_spec.rb new file mode 100644 index 0000000000..815df0c718 --- /dev/null +++ b/spec/ruby/core/string/unpack/carret_spec.rb @@ -0,0 +1,43 @@ +# encoding: binary +ruby_version_is "4.1" do + require_relative '../../../spec_helper' + require_relative '../fixtures/classes' + require_relative 'shared/basic' + + describe "String#unpack with format '^'" do + it_behaves_like :string_unpack_basic, '^' + it_behaves_like :string_unpack_no_platform, '^' + + it "returns the current offset that start from 0" do + "".unpack("^").should == [0] + end + + it "returns the current offset after the last decode ended" do + "a".unpack("CC^").should == [97, nil, 1] + end + + it "returns the current offset that start from the given offset" do + "abc".unpack("^", offset: 1).should == [1] + end + + it "returns the offset moved by 'X'" do + "\x01\x02\x03\x04".unpack("C3X2^").should == [1, 2, 3, 1] + end + + it "returns the offset moved by 'x'" do + "\x01\x02\x03\x04".unpack("Cx2^").should == [1, 3] + end + + it "returns the offset to the position the previous decode ended" do + "foo".unpack("A4^").should == ["foo", 3] + "foo".unpack("a4^").should == ["foo", 3] + "foo".unpack("Z5^").should == ["foo", 3] + end + + it "returns the offset including truncated part" do + "foo ".unpack("A*^").should == ["foo", 6] + "foo\0".unpack("Z*^").should == ["foo", 4] + "foo\0\0\0".unpack("Z5^").should == ["foo", 5] + end + end +end diff --git a/spec/ruby/core/string/unpack/comment_spec.rb b/spec/ruby/core/string/unpack/comment_spec.rb index e18a53df3c..050d2b7fc0 100644 --- a/spec/ruby/core/string/unpack/comment_spec.rb +++ b/spec/ruby/core/string/unpack/comment_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' diff --git a/spec/ruby/core/string/unpack/h_spec.rb b/spec/ruby/core/string/unpack/h_spec.rb index 19c4d63664..0cf8d943a7 100644 --- a/spec/ruby/core/string/unpack/h_spec.rb +++ b/spec/ruby/core/string/unpack/h_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -56,20 +56,10 @@ describe "String#unpack with format 'H'" do ].should be_computed_by(:unpack, "HHH") end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "\x01\x10".unpack("H\x00H").should == ["0", "1"] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "\x01\x10".unpack("H\x00H") - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x10".unpack("H\x00H") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -133,20 +123,10 @@ describe "String#unpack with format 'h'" do ].should be_computed_by(:unpack, "hhh") end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "\x01\x10".unpack("h\x00h").should == ["1", "0"] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "\x01\x10".unpack("h\x00h") - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x10".unpack("h\x00h") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/l_spec.rb b/spec/ruby/core/string/unpack/l_spec.rb index 18bb68b8d0..0adb567eca 100644 --- a/spec/ruby/core/string/unpack/l_spec.rb +++ b/spec/ruby/core/string/unpack/l_spec.rb @@ -14,7 +14,7 @@ describe "String#unpack with format 'L'" do it_behaves_like :string_unpack_32bit_be_unsigned, 'L>' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "with modifier '<' and '_'" do it_behaves_like :string_unpack_32bit_le, 'L<_' it_behaves_like :string_unpack_32bit_le, 'L_<' @@ -44,7 +44,7 @@ describe "String#unpack with format 'L'" do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "with modifier '<' and '_'" do it_behaves_like :string_unpack_64bit_le, 'L<_' it_behaves_like :string_unpack_64bit_le, 'L_<' @@ -86,7 +86,7 @@ describe "String#unpack with format 'l'" do it_behaves_like :string_unpack_32bit_be_signed, 'l>' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "with modifier '<' and '_'" do it_behaves_like :string_unpack_32bit_le, 'l<_' it_behaves_like :string_unpack_32bit_le, 'l_<' @@ -116,7 +116,7 @@ describe "String#unpack with format 'l'" do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "with modifier '<' and '_'" do it_behaves_like :string_unpack_64bit_le, 'l<_' it_behaves_like :string_unpack_64bit_le, 'l_<' @@ -160,7 +160,7 @@ little_endian do it_behaves_like :string_unpack_32bit_le_signed, 'l' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "String#unpack with format 'L' with modifier '_'" do it_behaves_like :string_unpack_32bit_le, 'L_' it_behaves_like :string_unpack_32bit_le_unsigned, 'L_' @@ -182,7 +182,7 @@ little_endian do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "String#unpack with format 'L' with modifier '_'" do it_behaves_like :string_unpack_64bit_le, 'L_' it_behaves_like :string_unpack_64bit_le_unsigned, 'L_' @@ -218,7 +218,7 @@ big_endian do it_behaves_like :string_unpack_32bit_be_signed, 'l' end - platform_is wordsize: 32 do + platform_is c_long_size: 32 do describe "String#unpack with format 'L' with modifier '_'" do it_behaves_like :string_unpack_32bit_be, 'L_' it_behaves_like :string_unpack_32bit_be_unsigned, 'L_' @@ -240,7 +240,7 @@ big_endian do end end - platform_is wordsize: 64 do + platform_is c_long_size: 64 do describe "String#unpack with format 'L' with modifier '_'" do it_behaves_like :string_unpack_64bit_be, 'L_' it_behaves_like :string_unpack_64bit_be_unsigned, 'L_' diff --git a/spec/ruby/core/string/unpack/m_spec.rb b/spec/ruby/core/string/unpack/m_spec.rb index c551c755d1..c1c1eea629 100644 --- a/spec/ruby/core/string/unpack/m_spec.rb +++ b/spec/ruby/core/string/unpack/m_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -186,7 +186,7 @@ describe "String#unpack with format 'm'" do end it "raises an ArgumentError for an invalid base64 character" do - -> { "dGV%zdA==".unpack("m0") }.should raise_error(ArgumentError) + -> { "dGV%zdA==".unpack("m0") }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/string/unpack/p_spec.rb b/spec/ruby/core/string/unpack/p_spec.rb index cd48c0523d..4103730269 100644 --- a/spec/ruby/core/string/unpack/p_spec.rb +++ b/spec/ruby/core/string/unpack/p_spec.rb @@ -15,7 +15,7 @@ describe "String#unpack with format 'P'" do packed = ["hello"].pack("P") packed.unpack("P5").should == ["hello"] packed.dup.unpack("P5").should == ["hello"] - -> { packed.to_sym.to_s.unpack("P5") }.should raise_error(ArgumentError, /no associated pointer/) + -> { packed.to_sym.to_s.unpack("P5") }.should.raise(ArgumentError, /no associated pointer/) end it "reads as many characters as specified" do @@ -39,6 +39,6 @@ describe "String#unpack with format 'p'" do packed = ["hello"].pack("p") packed.unpack("p").should == ["hello"] packed.dup.unpack("p").should == ["hello"] - -> { packed.to_sym.to_s.unpack("p") }.should raise_error(ArgumentError, /no associated pointer/) + -> { packed.to_sym.to_s.unpack("p") }.should.raise(ArgumentError, /no associated pointer/) end end diff --git a/spec/ruby/core/string/unpack/percent_spec.rb b/spec/ruby/core/string/unpack/percent_spec.rb index 0e27663195..7142bbf241 100644 --- a/spec/ruby/core/string/unpack/percent_spec.rb +++ b/spec/ruby/core/string/unpack/percent_spec.rb @@ -2,6 +2,6 @@ require_relative '../../../spec_helper' describe "String#unpack with format '%'" do it "raises an Argument Error" do - -> { "abc".unpack("%") }.should raise_error(ArgumentError) + -> { "abc".unpack("%") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unpack/r_spec.rb b/spec/ruby/core/string/unpack/r_spec.rb new file mode 100644 index 0000000000..a385951aa8 --- /dev/null +++ b/spec/ruby/core/string/unpack/r_spec.rb @@ -0,0 +1,85 @@ +# encoding: binary +require_relative '../../../spec_helper' +require_relative '../fixtures/classes' +require_relative 'shared/basic' + +ruby_version_is "4.1" do + describe "String#unpack with format 'R'" do + it_behaves_like :string_unpack_basic, 'R' + it_behaves_like :string_unpack_no_platform, 'R' + + it "decodes a ULEB128 integer" do + [ ["\x00", [0]], + ["\x01", [1]], + ["\x7f", [127]], + ["\x80\x01", [128]], + ["\xff\x7f", [0x3fff]], + ["\x80\x80\x01", [0x4000]], + ["\xff\xff\xff\xff\x0f", [0xffffffff]], + ["\x80\x80\x80\x80\x10", [0x100000000]], + ["\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", [0xffff_ffff_ffff_ffff]], + ].should be_computed_by(:unpack, "R") + end + + it "decodes multiple values with '*' modifier" do + "\x01\x02".unpack("R*").should == [1, 2] + "\x7f\x80\x01".unpack("R*").should == [127, 128] + end + + it "returns nil for incomplete data" do + "\xFF".unpack("R").should == [nil] + "\xFF".unpack1("R").should == nil + end + + it "returns nil for remaining incomplete values after a valid one" do + bytes = [256].pack("R") + (bytes + "\xFF").unpack("RRRR").should == [256, nil, nil, nil] + end + + it "skips incomplete values with '*' modifier" do + "\xFF".unpack("R*").should == [] + end + end + + describe "String#unpack with format 'r'" do + it_behaves_like :string_unpack_basic, 'r' + it_behaves_like :string_unpack_no_platform, 'r' + + it "decodes a SLEB128 integer" do + [ ["\x00", [0]], + ["\x01", [1]], + ["\x7f", [-1]], + ["\x7e", [-2]], + ["\xff\x00", [127]], + ["\x80\x01", [128]], + ["\x81\x7f", [-127]], + ["\x80\x7f", [-128]], + ].should be_computed_by(:unpack, "r") + end + + it "decodes larger numbers" do + "\xff\xff\x00".unpack("r").should == [0x3fff] + "\x80\x80\x01".unpack("r").should == [0x4000] + "\x81\x80\x7f".unpack("r").should == [-0x3fff] + "\x80\x80\x7f".unpack("r").should == [-0x4000] + end + + it "decodes multiple values with '*' modifier" do + "\x00\x01\x7f".unpack("r*").should == [0, 1, -1] + end + + it "returns nil for incomplete data" do + "\xFF".unpack("r").should == [nil] + "\xFF".unpack1("r").should == nil + end + + it "returns nil for remaining incomplete values after a valid one" do + bytes = [256].pack("r") + (bytes + "\xFF").unpack("rrrr").should == [256, nil, nil, nil] + end + + it "skips incomplete values with '*' modifier" do + "\xFF".unpack("r*").should == [] + end + end +end diff --git a/spec/ruby/core/string/unpack/shared/basic.rb b/spec/ruby/core/string/unpack/shared/basic.rb index bb5302edc5..2ee2d6899a 100644 --- a/spec/ruby/core/string/unpack/shared/basic.rb +++ b/spec/ruby/core/string/unpack/shared/basic.rb @@ -1,21 +1,27 @@ describe :string_unpack_basic, shared: true do it "ignores whitespace in the format string" do - "abc".unpack("a \t\n\v\f\r"+unpack_format).should be_an_instance_of(Array) + "abc".unpack("a \t\n\v\f\r"+unpack_format).should.instance_of?(Array) end it "calls #to_str to coerce the directives string" do d = mock("unpack directive") d.should_receive(:to_str).and_return("a"+unpack_format) - "abc".unpack(d).should be_an_instance_of(Array) + "abc".unpack(d).should.instance_of?(Array) + end + + it "raises ArgumentError when a directive is unknown" do + -> { "abcdefgh".unpack("a K" + unpack_format) }.should.raise(ArgumentError, "unknown unpack directive 'K' in 'a K#{unpack_format}'") + -> { "abcdefgh".unpack("a 0" + unpack_format) }.should.raise(ArgumentError, "unknown unpack directive '0' in 'a 0#{unpack_format}'") + -> { "abcdefgh".unpack("a :" + unpack_format) }.should.raise(ArgumentError, "unknown unpack directive ':' in 'a :#{unpack_format}'") end end describe :string_unpack_no_platform, shared: true do it "raises an ArgumentError when the format modifier is '_'" do - -> { "abcdefgh".unpack(unpack_format("_")) }.should raise_error(ArgumentError) + -> { "abcdefgh".unpack(unpack_format("_")) }.should.raise(ArgumentError) end it "raises an ArgumentError when the format modifier is '!'" do - -> { "abcdefgh".unpack(unpack_format("!")) }.should raise_error(ArgumentError) + -> { "abcdefgh".unpack(unpack_format("!")) }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unpack/shared/float.rb b/spec/ruby/core/string/unpack/shared/float.rb index 93282bf4c9..5525c3fe73 100644 --- a/spec/ruby/core/string/unpack/shared/float.rb +++ b/spec/ruby/core/string/unpack/shared/float.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :string_unpack_float_le, shared: true do it "decodes one float for a single format character" do @@ -53,24 +53,13 @@ describe :string_unpack_float_le, shared: true do it "decodes NaN" do # mumble mumble NaN mumble https://bugs.ruby-lang.org/issues/5884 - [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should be_true + [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should == true end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - array = "\x9a\x999@33\xb3?".unpack(unpack_format("\000", 2)) - array.should == [2.9000000953674316, 1.399999976158142] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "\x9a\x999@33\xb3?".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x9a\x999@33\xb3?".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -132,24 +121,13 @@ describe :string_unpack_float_be, shared: true do it "decodes NaN" do # mumble mumble NaN mumble https://bugs.ruby-lang.org/issues/5884 - [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should be_true - end - - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - array = "@9\x99\x9a?\xb333".unpack(unpack_format("\000", 2)) - array.should == [2.9000000953674316, 1.399999976158142] - end - end + [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should == true end - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "@9\x99\x9a?\xb333".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "@9\x99\x9a?\xb333".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -214,23 +192,13 @@ describe :string_unpack_double_le, shared: true do it "decodes NaN" do # mumble mumble NaN mumble https://bugs.ruby-lang.org/issues/5884 - [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should be_true + [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should == true end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "333333\x07@ffffff\xf6?".unpack(unpack_format("\000", 2)).should == [2.9, 1.4] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "333333\x07@ffffff\xf6?".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "333333\x07@ffffff\xf6?".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -294,23 +262,13 @@ describe :string_unpack_double_be, shared: true do it "decodes NaN" do # mumble mumble NaN mumble https://bugs.ruby-lang.org/issues/5884 - [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should be_true - end - - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "@\x07333333?\xf6ffffff".unpack(unpack_format("\000", 2)).should == [2.9, 1.4] - end - end + [nan_value].pack(unpack_format).unpack(unpack_format).first.nan?.should == true end - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "@\x07333333?\xf6ffffff".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "@\x07333333?\xf6ffffff".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/shared/integer.rb b/spec/ruby/core/string/unpack/shared/integer.rb index d71a2cf00d..c66156536b 100644 --- a/spec/ruby/core/string/unpack/shared/integer.rb +++ b/spec/ruby/core/string/unpack/shared/integer.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary describe :string_unpack_16bit_le, shared: true do it "decodes one short for a single format character" do @@ -32,20 +32,10 @@ describe :string_unpack_16bit_le, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "abcd".unpack(unpack_format("\000", 2)).should == [25185, 25699] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "abcd".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "abcd".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -97,20 +87,10 @@ describe :string_unpack_16bit_be, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "badc".unpack(unpack_format("\000", 2)).should == [25185, 25699] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "badc".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "badc".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -163,20 +143,10 @@ describe :string_unpack_32bit_le, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "abcdefgh".unpack(unpack_format("\000", 2)).should == [1684234849, 1751606885] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "abcdefgh".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "abcdefgh".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -229,20 +199,10 @@ describe :string_unpack_32bit_be, shared: true do ].should be_computed_by(:unpack, unpack_format(3)) end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "dcbahgfe".unpack(unpack_format("\000", 2)).should == [1684234849, 1751606885] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "dcbahgfe".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "dcbahgfe".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -291,21 +251,10 @@ describe :string_unpack_64bit_le, shared: true do "abc".unpack(unpack_format('*')).should == [] end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - array = "abcdefghabghefcd".unpack(unpack_format("\000", 2)) - array.should == [7523094288207667809, 7233738012216484449] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "badc".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "badc".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do @@ -365,21 +314,10 @@ describe :string_unpack_64bit_be, shared: true do "abc".unpack(unpack_format('*')).should == [] end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - array = "hgfedcbadcfehgba".unpack(unpack_format("\000", 2)) - array.should == [7523094288207667809, 7233738012216484449] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "hgfedcbadcfehgba".unpack(unpack_format("\000", 2)) - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "hgfedcbadcfehgba".unpack(unpack_format("\000", 2)) + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/shared/unicode.rb b/spec/ruby/core/string/unpack/shared/unicode.rb index 9fe07f53ae..9b4e0c09de 100644 --- a/spec/ruby/core/string/unpack/shared/unicode.rb +++ b/spec/ruby/core/string/unpack/shared/unicode.rb @@ -50,20 +50,10 @@ describe :string_unpack_unicode, shared: true do "\xc2\x80".unpack("UUUU").should == [0x80] end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "\x01\x02".unpack("U\x00U").should == [1, 2] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "\x01\x02".unpack("U\x00U") - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x02".unpack("U\x00U") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/u_spec.rb b/spec/ruby/core/string/unpack/u_spec.rb index 7845e6d5f2..720c1b8583 100644 --- a/spec/ruby/core/string/unpack/u_spec.rb +++ b/spec/ruby/core/string/unpack/u_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -12,11 +12,11 @@ describe "String#unpack with format 'U'" do it_behaves_like :string_unpack_taint, 'U' it "raises ArgumentError on a malformed byte sequence" do - -> { "\xE3".unpack('U') }.should raise_error(ArgumentError) + -> { "\xE3".unpack('U') }.should.raise(ArgumentError) end it "raises ArgumentError on a malformed byte sequence and doesn't continue when used with the * modifier" do - -> { "\xE3".unpack('U*') }.should raise_error(ArgumentError) + -> { "\xE3".unpack('U*') }.should.raise(ArgumentError) end end @@ -33,7 +33,7 @@ describe "String#unpack with format 'u'" do str = "".unpack("u")[0] str.encoding.should == Encoding::BINARY - str = "1".force_encoding('UTF-8').unpack("u")[0] + str = "1".dup.force_encoding('UTF-8').unpack("u")[0] str.encoding.should == Encoding::BINARY end diff --git a/spec/ruby/core/string/unpack/w_spec.rb b/spec/ruby/core/string/unpack/w_spec.rb index 6a1cff1965..cc9aecac9c 100644 --- a/spec/ruby/core/string/unpack/w_spec.rb +++ b/spec/ruby/core/string/unpack/w_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -15,20 +15,10 @@ describe "String#unpack with directive 'w'" do ].should be_computed_by(:unpack, "w") end - ruby_version_is ""..."3.3" do - it "ignores NULL bytes between directives" do - suppress_warning do - "\x01\x02\x03".unpack("w\x00w").should == [1, 2] - end - end - end - - ruby_version_is "3.3" do - it "raise ArgumentError for NULL bytes between directives" do - -> { - "\x01\x02\x03".unpack("w\x00w") - }.should raise_error(ArgumentError, /unknown unpack directive/) - end + it "raise ArgumentError for NULL bytes between directives" do + -> { + "\x01\x02\x03".unpack("w\x00w") + }.should.raise(ArgumentError, /unknown unpack directive/) end it "ignores spaces between directives" do diff --git a/spec/ruby/core/string/unpack/x_spec.rb b/spec/ruby/core/string/unpack/x_spec.rb index 5e248de77e..fb2e79fc1f 100644 --- a/spec/ruby/core/string/unpack/x_spec.rb +++ b/spec/ruby/core/string/unpack/x_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' @@ -24,11 +24,11 @@ describe "String#unpack with format 'X'" do end it "raises an ArgumentError when passed the '*' modifier if the remaining bytes exceed the bytes from the index to the start of the String" do - -> { "abcd".unpack("CX*C") }.should raise_error(ArgumentError) + -> { "abcd".unpack("CX*C") }.should.raise(ArgumentError) end it "raises an ArgumentError if the count exceeds the bytes from current index to the start of the String" do - -> { "\x01\x02\x03\x04".unpack("C3X4C") }.should raise_error(ArgumentError) + -> { "\x01\x02\x03\x04".unpack("C3X4C") }.should.raise(ArgumentError) end end @@ -57,6 +57,6 @@ describe "String#unpack with format 'x'" do end it "raises an ArgumentError if the count exceeds the size of the String" do - -> { "\x01\x02\x03\x04".unpack("C2x3C") }.should raise_error(ArgumentError) + -> { "\x01\x02\x03\x04".unpack("C2x3C") }.should.raise(ArgumentError) end end diff --git a/spec/ruby/core/string/unpack/z_spec.rb b/spec/ruby/core/string/unpack/z_spec.rb index ce8da4b29e..1030390550 100644 --- a/spec/ruby/core/string/unpack/z_spec.rb +++ b/spec/ruby/core/string/unpack/z_spec.rb @@ -1,4 +1,4 @@ -# -*- encoding: binary -*- +# encoding: binary require_relative '../../../spec_helper' require_relative '../fixtures/classes' require_relative 'shared/basic' diff --git a/spec/ruby/core/string/unpack1_spec.rb b/spec/ruby/core/string/unpack1_spec.rb index df830916a3..ee10042eb8 100644 --- a/spec/ruby/core/string/unpack1_spec.rb +++ b/spec/ruby/core/string/unpack1_spec.rb @@ -8,29 +8,54 @@ describe "String#unpack1" do "A".unpack1("B*").should == "01000001" end - ruby_version_is "3.1" do - it "starts unpacking from the given offset" do - "ZZABCD".unpack1('x3C', offset: 2).should == "ABCD".unpack('x3C')[0] - "ZZZZaG9nZWZ1Z2E=".unpack1("m", offset: 4).should == "hogefuga" - "ZA".unpack1("B*", offset: 1).should == "01000001" - end + it "starts unpacking from the given offset" do + "ZZABCD".unpack1('x3C', offset: 2).should == "ABCD".unpack('x3C')[0] + "ZZZZaG9nZWZ1Z2E=".unpack1("m", offset: 4).should == "hogefuga" + "ZA".unpack1("B*", offset: 1).should == "01000001" + end + + it "traits offset as a bytes offset" do + "؈".unpack("CC").should == [216, 136] + "؈".unpack1("C").should == 216 + "؈".unpack1("C", offset: 1).should == 136 + end + + describe "when the offset is negative" do + ruby_version_is "4.1" do + it "starts unpacking from the end" do + "abc".unpack1("C", offset: -2).should == 98 + end - it "traits offset as a bytes offset" do - "؈".unpack("CC").should == [216, 136] - "؈".unpack1("C").should == 216 - "؈".unpack1("C", offset: 1).should == 136 + it "raises an ArgumentError if it is less than -length" do + -> { "a".unpack1("C", offset: -2) }.should.raise(ArgumentError, "offset outside of string") + end end - it "raises an ArgumentError when the offset is negative" do - -> { "a".unpack1("C", offset: -1) }.should raise_error(ArgumentError, "offset can't be negative") + ruby_version_is ""..."4.1" do + it "raises an ArgumentError" do + -> { "a".unpack1("C", offset: -1) }.should.raise(ArgumentError, "offset can't be negative") + end end + end + + it "returns nil if the offset is at the end of the string" do + "a".unpack1("C", offset: 1).should == nil + end + + it "raises an ArgumentError when the offset is larger than the string bytesize" do + -> { "a".unpack1("C", offset: 2) }.should.raise(ArgumentError, "offset outside of string") + end + + context "with format 'm0'" do + # unpack1("m0") takes a special code path that calls Pack.unpackBase46Strict instead of Pack.unpack_m, + # which is why we repeat the tests for unpack("m0") here. - it "returns nil if the offset is at the end of the string" do - "a".unpack1("C", offset: 1).should == nil + it "decodes base64" do + "dGVzdA==".unpack1("m0").should == "test" end - it "raises an ArgumentError when the offset is larger than the string bytesize" do - -> { "a".unpack1("C", offset: 2) }.should raise_error(ArgumentError, "offset outside of string") + it "raises an ArgumentError for an invalid base64 character" do + -> { "dGV%zdA==".unpack1("m0") }.should.raise(ArgumentError) end end end diff --git a/spec/ruby/core/string/unpack_spec.rb b/spec/ruby/core/string/unpack_spec.rb index 52b4af3a95..eb4710ce14 100644 --- a/spec/ruby/core/string/unpack_spec.rb +++ b/spec/ruby/core/string/unpack_spec.rb @@ -2,33 +2,45 @@ require_relative '../../spec_helper' describe "String#unpack" do it "raises a TypeError when passed nil" do - -> { "abc".unpack(nil) }.should raise_error(TypeError) + -> { "abc".unpack(nil) }.should.raise(TypeError) end it "raises a TypeError when passed an Integer" do - -> { "abc".unpack(1) }.should raise_error(TypeError) + -> { "abc".unpack(1) }.should.raise(TypeError) end - ruby_version_is "3.1" do - it "starts unpacking from the given offset" do - "abc".unpack("CC", offset: 1).should == [98, 99] - end + it "starts unpacking from the given offset" do + "abc".unpack("CC", offset: 1).should == [98, 99] + end - it "traits offset as a bytes offset" do - "؈".unpack("CC").should == [216, 136] - "؈".unpack("CC", offset: 1).should == [136, nil] - end + it "traits offset as a bytes offset" do + "؈".unpack("CC").should == [216, 136] + "؈".unpack("CC", offset: 1).should == [136, nil] + end - it "raises an ArgumentError when the offset is negative" do - -> { "a".unpack("C", offset: -1) }.should raise_error(ArgumentError, "offset can't be negative") - end + describe "when the offset is negative" do + ruby_version_is "4.1" do + it "starts unpacking from the end" do + "abc".unpack("CC", offset: -2).should == [98, 99] + end - it "returns nil if the offset is at the end of the string" do - "a".unpack("C", offset: 1).should == [nil] + it "raises an ArgumentError if it is less than -length" do + -> { "a".unpack("C", offset: -2) }.should.raise(ArgumentError, "offset outside of string") + end end - it "raises an ArgumentError when the offset is larget than the string" do - -> { "a".unpack("C", offset: 2) }.should raise_error(ArgumentError, "offset outside of string") + ruby_version_is ""..."4.1" do + it "raises an ArgumentError" do + -> { "a".unpack("C", offset: -1) }.should.raise(ArgumentError, "offset can't be negative") + end end end + + it "returns nil if the offset is at the end of the string" do + "a".unpack("C", offset: 1).should == [nil] + end + + it "raises an ArgumentError when the offset is larger than the string" do + -> { "a".unpack("C", offset: 2) }.should.raise(ArgumentError, "offset outside of string") + end end diff --git a/spec/ruby/core/string/upcase_spec.rb b/spec/ruby/core/string/upcase_spec.rb index a2e34f5f40..a6e1869267 100644 --- a/spec/ruby/core/string/upcase_spec.rb +++ b/spec/ruby/core/string/upcase_spec.rb @@ -1,4 +1,5 @@ # -*- encoding: utf-8 -*- +# frozen_string_literal: false require_relative '../../spec_helper' require_relative 'fixtures/classes' @@ -23,7 +24,7 @@ describe "String#upcase" do upcased.should == "ASSET" upcased.size.should == 5 upcased.bytesize.should == 5 - upcased.ascii_only?.should be_true + upcased.ascii_only?.should == true end end @@ -47,7 +48,7 @@ describe "String#upcase" do end it "does not allow any other additional option" do - -> { "i".upcase(:turkic, :ascii) }.should raise_error(ArgumentError) + -> { "i".upcase(:turkic, :ascii) }.should.raise(ArgumentError) end end @@ -61,27 +62,27 @@ describe "String#upcase" do end it "does not allow any other additional option" do - -> { "iß".upcase(:lithuanian, :ascii) }.should raise_error(ArgumentError) + -> { "iß".upcase(:lithuanian, :ascii) }.should.raise(ArgumentError) end end it "does not allow the :fold option for upcasing" do - -> { "abc".upcase(:fold) }.should raise_error(ArgumentError) + -> { "abc".upcase(:fold) }.should.raise(ArgumentError) end it "does not allow invalid options" do - -> { "abc".upcase(:invalid_option) }.should raise_error(ArgumentError) + -> { "abc".upcase(:invalid_option) }.should.raise(ArgumentError) end it "returns a String instance for subclasses" do - StringSpecs::MyString.new("fooBAR").upcase.should be_an_instance_of(String) + StringSpecs::MyString.new("fooBAR").upcase.should.instance_of?(String) end end describe "String#upcase!" do it "modifies self in place" do a = "HeLlO" - a.upcase!.should equal(a) + a.upcase!.should.equal?(a) a.should == "HELLO" end @@ -111,7 +112,7 @@ describe "String#upcase!" do upcased.should == "ASSET" upcased.size.should == 5 upcased.bytesize.should == 5 - upcased.ascii_only?.should be_true + upcased.ascii_only?.should == true end end @@ -143,7 +144,7 @@ describe "String#upcase!" do end it "does not allow any other additional option" do - -> { a = "i"; a.upcase!(:turkic, :ascii) }.should raise_error(ArgumentError) + -> { a = "i"; a.upcase!(:turkic, :ascii) }.should.raise(ArgumentError) end end @@ -161,16 +162,16 @@ describe "String#upcase!" do end it "does not allow any other additional option" do - -> { a = "iß"; a.upcase!(:lithuanian, :ascii) }.should raise_error(ArgumentError) + -> { a = "iß"; a.upcase!(:lithuanian, :ascii) }.should.raise(ArgumentError) end end it "does not allow the :fold option for upcasing" do - -> { a = "abc"; a.upcase!(:fold) }.should raise_error(ArgumentError) + -> { a = "abc"; a.upcase!(:fold) }.should.raise(ArgumentError) end it "does not allow invalid options" do - -> { a = "abc"; a.upcase!(:invalid_option) }.should raise_error(ArgumentError) + -> { a = "abc"; a.upcase!(:invalid_option) }.should.raise(ArgumentError) end it "returns nil if no modifications were made" do @@ -180,7 +181,7 @@ describe "String#upcase!" do end it "raises a FrozenError when self is frozen" do - -> { "HeLlo".freeze.upcase! }.should raise_error(FrozenError) - -> { "HELLO".freeze.upcase! }.should raise_error(FrozenError) + -> { "HeLlo".freeze.upcase! }.should.raise(FrozenError) + -> { "HELLO".freeze.upcase! }.should.raise(FrozenError) end end diff --git a/spec/ruby/core/string/uplus_spec.rb b/spec/ruby/core/string/uplus_spec.rb index 65b66260dd..20767bcc01 100644 --- a/spec/ruby/core/string/uplus_spec.rb +++ b/spec/ruby/core/string/uplus_spec.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: false require_relative '../../spec_helper' describe 'String#+@' do @@ -12,14 +13,48 @@ describe 'String#+@' do output.should == 'foobar' end - it 'returns self if the String is not frozen' do - input = 'foo' + it 'returns a mutable String itself' do + input = String.new("foo") output = +input - output.equal?(input).should == true + output.should.equal?(input) + + input << "bar" + output.should == "foobar" + end + + context 'if file has "frozen_string_literal: true" magic comment' do + it 'returns mutable copy of a literal' do + ruby_exe(fixture(__FILE__, "freeze_magic_comment.rb")).should == 'mutable' + end end - it 'returns mutable copy despite freeze-magic-comment in file' do - ruby_exe(fixture(__FILE__, "freeze_magic_comment.rb")).should == 'mutable' + context 'if file has "frozen_string_literal: false" magic comment' do + it 'returns literal string itself' do + input = 'foo' + output = +input + + output.equal?(input).should == true + end + end + + context 'if file has no frozen_string_literal magic comment' do + ruby_version_is ''...'3.4' do + it 'returns literal string itself' do + eval(<<~RUBY).should == true + s = "foo" + s.equal?(+s) + RUBY + end + end + + ruby_version_is '3.4' do + it 'returns mutable copy of a literal' do + eval(<<~RUBY).should == false + s = "foo" + s.equal?(+s) + RUBY + end + end end end diff --git a/spec/ruby/core/string/upto_spec.rb b/spec/ruby/core/string/upto_spec.rb index 3799e338e0..2eea06fd01 100644 --- a/spec/ruby/core/string/upto_spec.rb +++ b/spec/ruby/core/string/upto_spec.rb @@ -53,13 +53,13 @@ describe "String#upto" do end it "raises a TypeError if other can't be converted to a string" do - -> { "abc".upto(123) { } }.should raise_error(TypeError) - -> { "abc".upto(mock('x')){ } }.should raise_error(TypeError) + -> { "abc".upto(123) { } }.should.raise(TypeError) + -> { "abc".upto(mock('x')){ } }.should.raise(TypeError) end it "does not work with symbols" do - -> { "a".upto(:c).to_a }.should raise_error(TypeError) + -> { "a".upto(:c).to_a }.should.raise(TypeError) end it "returns non-alphabetic characters in the ASCII range for single letters" do @@ -81,9 +81,9 @@ describe "String#upto" do end it "raises Encoding::CompatibilityError when incompatible characters are given" do - char1 = 'a'.force_encoding("EUC-JP") - char2 = 'b'.force_encoding("ISO-2022-JP") - -> { char1.upto(char2) {} }.should raise_error(Encoding::CompatibilityError, "incompatible character encodings: EUC-JP and ISO-2022-JP") + char1 = 'a'.dup.force_encoding("EUC-JP") + char2 = 'b'.dup.force_encoding("ISO-2022-JP") + -> { char1.upto(char2) {} }.should.raise(Encoding::CompatibilityError, "incompatible character encodings: EUC-JP and ISO-2022-JP") end describe "on sequence of numbers" do @@ -95,7 +95,7 @@ describe "String#upto" do describe "when no block is given" do it "returns an enumerator" do enum = "aaa".upto("baa", true) - enum.should be_an_instance_of(Enumerator) + enum.should.instance_of?(Enumerator) enum.count.should == 26**2 end diff --git a/spec/ruby/core/string/valid_encoding_spec.rb b/spec/ruby/core/string/valid_encoding_spec.rb index bb26062c0f..f29220fc99 100644 --- a/spec/ruby/core/string/valid_encoding_spec.rb +++ b/spec/ruby/core/string/valid_encoding_spec.rb @@ -2,132 +2,132 @@ require_relative '../../spec_helper' describe "String#valid_encoding?" do it "returns true if the String's encoding is valid" do - "a".valid_encoding?.should be_true - "\u{8365}\u{221}".valid_encoding?.should be_true + "a".valid_encoding?.should == true + "\u{8365}\u{221}".valid_encoding?.should == true end it "returns true if self is valid in the current encoding and other encodings" do - str = "\x77" - str.force_encoding('utf-8').valid_encoding?.should be_true - str.force_encoding('binary').valid_encoding?.should be_true + str = +"\x77" + str.force_encoding('utf-8').valid_encoding?.should == true + str.force_encoding('binary').valid_encoding?.should == true end it "returns true for all encodings self is valid in" do - str = "\xE6\x9D\x94" - str.force_encoding('BINARY').valid_encoding?.should be_true - str.force_encoding('UTF-8').valid_encoding?.should be_true - str.force_encoding('US-ASCII').valid_encoding?.should be_false - str.force_encoding('Big5').valid_encoding?.should be_false - str.force_encoding('CP949').valid_encoding?.should be_false - str.force_encoding('Emacs-Mule').valid_encoding?.should be_false - str.force_encoding('EUC-JP').valid_encoding?.should be_false - str.force_encoding('EUC-KR').valid_encoding?.should be_false - str.force_encoding('EUC-TW').valid_encoding?.should be_false - str.force_encoding('GB18030').valid_encoding?.should be_false - str.force_encoding('GBK').valid_encoding?.should be_false - str.force_encoding('ISO-8859-1').valid_encoding?.should be_true - str.force_encoding('ISO-8859-2').valid_encoding?.should be_true - str.force_encoding('ISO-8859-3').valid_encoding?.should be_true - str.force_encoding('ISO-8859-4').valid_encoding?.should be_true - str.force_encoding('ISO-8859-5').valid_encoding?.should be_true - str.force_encoding('ISO-8859-6').valid_encoding?.should be_true - str.force_encoding('ISO-8859-7').valid_encoding?.should be_true - str.force_encoding('ISO-8859-8').valid_encoding?.should be_true - str.force_encoding('ISO-8859-9').valid_encoding?.should be_true - str.force_encoding('ISO-8859-10').valid_encoding?.should be_true - str.force_encoding('ISO-8859-11').valid_encoding?.should be_true - str.force_encoding('ISO-8859-13').valid_encoding?.should be_true - str.force_encoding('ISO-8859-14').valid_encoding?.should be_true - str.force_encoding('ISO-8859-15').valid_encoding?.should be_true - str.force_encoding('ISO-8859-16').valid_encoding?.should be_true - str.force_encoding('KOI8-R').valid_encoding?.should be_true - str.force_encoding('KOI8-U').valid_encoding?.should be_true - str.force_encoding('Shift_JIS').valid_encoding?.should be_false - "\xD8\x00".force_encoding('UTF-16BE').valid_encoding?.should be_false - "\x00\xD8".force_encoding('UTF-16LE').valid_encoding?.should be_false - "\x04\x03\x02\x01".force_encoding('UTF-32BE').valid_encoding?.should be_false - "\x01\x02\x03\x04".force_encoding('UTF-32LE').valid_encoding?.should be_false - str.force_encoding('Windows-1251').valid_encoding?.should be_true - str.force_encoding('IBM437').valid_encoding?.should be_true - str.force_encoding('IBM737').valid_encoding?.should be_true - str.force_encoding('IBM775').valid_encoding?.should be_true - str.force_encoding('CP850').valid_encoding?.should be_true - str.force_encoding('IBM852').valid_encoding?.should be_true - str.force_encoding('CP852').valid_encoding?.should be_true - str.force_encoding('IBM855').valid_encoding?.should be_true - str.force_encoding('CP855').valid_encoding?.should be_true - str.force_encoding('IBM857').valid_encoding?.should be_true - str.force_encoding('IBM860').valid_encoding?.should be_true - str.force_encoding('IBM861').valid_encoding?.should be_true - str.force_encoding('IBM862').valid_encoding?.should be_true - str.force_encoding('IBM863').valid_encoding?.should be_true - str.force_encoding('IBM864').valid_encoding?.should be_true - str.force_encoding('IBM865').valid_encoding?.should be_true - str.force_encoding('IBM866').valid_encoding?.should be_true - str.force_encoding('IBM869').valid_encoding?.should be_true - str.force_encoding('Windows-1258').valid_encoding?.should be_true - str.force_encoding('GB1988').valid_encoding?.should be_true - str.force_encoding('macCentEuro').valid_encoding?.should be_true - str.force_encoding('macCroatian').valid_encoding?.should be_true - str.force_encoding('macCyrillic').valid_encoding?.should be_true - str.force_encoding('macGreek').valid_encoding?.should be_true - str.force_encoding('macIceland').valid_encoding?.should be_true - str.force_encoding('macRoman').valid_encoding?.should be_true - str.force_encoding('macRomania').valid_encoding?.should be_true - str.force_encoding('macThai').valid_encoding?.should be_true - str.force_encoding('macTurkish').valid_encoding?.should be_true - str.force_encoding('macUkraine').valid_encoding?.should be_true - str.force_encoding('stateless-ISO-2022-JP').valid_encoding?.should be_false - str.force_encoding('eucJP-ms').valid_encoding?.should be_false - str.force_encoding('CP51932').valid_encoding?.should be_false - str.force_encoding('GB2312').valid_encoding?.should be_false - str.force_encoding('GB12345').valid_encoding?.should be_false - str.force_encoding('ISO-2022-JP').valid_encoding?.should be_true - str.force_encoding('ISO-2022-JP-2').valid_encoding?.should be_true - str.force_encoding('CP50221').valid_encoding?.should be_true - str.force_encoding('Windows-1252').valid_encoding?.should be_true - str.force_encoding('Windows-1250').valid_encoding?.should be_true - str.force_encoding('Windows-1256').valid_encoding?.should be_true - str.force_encoding('Windows-1253').valid_encoding?.should be_true - str.force_encoding('Windows-1255').valid_encoding?.should be_true - str.force_encoding('Windows-1254').valid_encoding?.should be_true - str.force_encoding('TIS-620').valid_encoding?.should be_true - str.force_encoding('Windows-874').valid_encoding?.should be_true - str.force_encoding('Windows-1257').valid_encoding?.should be_true - str.force_encoding('Windows-31J').valid_encoding?.should be_false - str.force_encoding('MacJapanese').valid_encoding?.should be_false - str.force_encoding('UTF-7').valid_encoding?.should be_true - str.force_encoding('UTF8-MAC').valid_encoding?.should be_true + str = +"\xE6\x9D\x94" + str.force_encoding('BINARY').valid_encoding?.should == true + str.force_encoding('UTF-8').valid_encoding?.should == true + str.force_encoding('US-ASCII').valid_encoding?.should == false + str.force_encoding('Big5').valid_encoding?.should == false + str.force_encoding('CP949').valid_encoding?.should == false + str.force_encoding('Emacs-Mule').valid_encoding?.should == false + str.force_encoding('EUC-JP').valid_encoding?.should == false + str.force_encoding('EUC-KR').valid_encoding?.should == false + str.force_encoding('EUC-TW').valid_encoding?.should == false + str.force_encoding('GB18030').valid_encoding?.should == false + str.force_encoding('GBK').valid_encoding?.should == false + str.force_encoding('ISO-8859-1').valid_encoding?.should == true + str.force_encoding('ISO-8859-2').valid_encoding?.should == true + str.force_encoding('ISO-8859-3').valid_encoding?.should == true + str.force_encoding('ISO-8859-4').valid_encoding?.should == true + str.force_encoding('ISO-8859-5').valid_encoding?.should == true + str.force_encoding('ISO-8859-6').valid_encoding?.should == true + str.force_encoding('ISO-8859-7').valid_encoding?.should == true + str.force_encoding('ISO-8859-8').valid_encoding?.should == true + str.force_encoding('ISO-8859-9').valid_encoding?.should == true + str.force_encoding('ISO-8859-10').valid_encoding?.should == true + str.force_encoding('ISO-8859-11').valid_encoding?.should == true + str.force_encoding('ISO-8859-13').valid_encoding?.should == true + str.force_encoding('ISO-8859-14').valid_encoding?.should == true + str.force_encoding('ISO-8859-15').valid_encoding?.should == true + str.force_encoding('ISO-8859-16').valid_encoding?.should == true + str.force_encoding('KOI8-R').valid_encoding?.should == true + str.force_encoding('KOI8-U').valid_encoding?.should == true + str.force_encoding('Shift_JIS').valid_encoding?.should == false + "\xD8\x00".dup.force_encoding('UTF-16BE').valid_encoding?.should == false + "\x00\xD8".dup.force_encoding('UTF-16LE').valid_encoding?.should == false + "\x04\x03\x02\x01".dup.force_encoding('UTF-32BE').valid_encoding?.should == false + "\x01\x02\x03\x04".dup.force_encoding('UTF-32LE').valid_encoding?.should == false + str.force_encoding('Windows-1251').valid_encoding?.should == true + str.force_encoding('IBM437').valid_encoding?.should == true + str.force_encoding('IBM737').valid_encoding?.should == true + str.force_encoding('IBM775').valid_encoding?.should == true + str.force_encoding('CP850').valid_encoding?.should == true + str.force_encoding('IBM852').valid_encoding?.should == true + str.force_encoding('CP852').valid_encoding?.should == true + str.force_encoding('IBM855').valid_encoding?.should == true + str.force_encoding('CP855').valid_encoding?.should == true + str.force_encoding('IBM857').valid_encoding?.should == true + str.force_encoding('IBM860').valid_encoding?.should == true + str.force_encoding('IBM861').valid_encoding?.should == true + str.force_encoding('IBM862').valid_encoding?.should == true + str.force_encoding('IBM863').valid_encoding?.should == true + str.force_encoding('IBM864').valid_encoding?.should == true + str.force_encoding('IBM865').valid_encoding?.should == true + str.force_encoding('IBM866').valid_encoding?.should == true + str.force_encoding('IBM869').valid_encoding?.should == true + str.force_encoding('Windows-1258').valid_encoding?.should == true + str.force_encoding('GB1988').valid_encoding?.should == true + str.force_encoding('macCentEuro').valid_encoding?.should == true + str.force_encoding('macCroatian').valid_encoding?.should == true + str.force_encoding('macCyrillic').valid_encoding?.should == true + str.force_encoding('macGreek').valid_encoding?.should == true + str.force_encoding('macIceland').valid_encoding?.should == true + str.force_encoding('macRoman').valid_encoding?.should == true + str.force_encoding('macRomania').valid_encoding?.should == true + str.force_encoding('macThai').valid_encoding?.should == true + str.force_encoding('macTurkish').valid_encoding?.should == true + str.force_encoding('macUkraine').valid_encoding?.should == true + str.force_encoding('stateless-ISO-2022-JP').valid_encoding?.should == false + str.force_encoding('eucJP-ms').valid_encoding?.should == false + str.force_encoding('CP51932').valid_encoding?.should == false + str.force_encoding('GB2312').valid_encoding?.should == false + str.force_encoding('GB12345').valid_encoding?.should == false + str.force_encoding('ISO-2022-JP').valid_encoding?.should == true + str.force_encoding('ISO-2022-JP-2').valid_encoding?.should == true + str.force_encoding('CP50221').valid_encoding?.should == true + str.force_encoding('Windows-1252').valid_encoding?.should == true + str.force_encoding('Windows-1250').valid_encoding?.should == true + str.force_encoding('Windows-1256').valid_encoding?.should == true + str.force_encoding('Windows-1253').valid_encoding?.should == true + str.force_encoding('Windows-1255').valid_encoding?.should == true + str.force_encoding('Windows-1254').valid_encoding?.should == true + str.force_encoding('TIS-620').valid_encoding?.should == true + str.force_encoding('Windows-874').valid_encoding?.should == true + str.force_encoding('Windows-1257').valid_encoding?.should == true + str.force_encoding('Windows-31J').valid_encoding?.should == false + str.force_encoding('MacJapanese').valid_encoding?.should == false + str.force_encoding('UTF-7').valid_encoding?.should == true + str.force_encoding('UTF8-MAC').valid_encoding?.should == true end it "returns true for IBM720 encoding self is valid in" do - str = "\xE6\x9D\x94" - str.force_encoding('IBM720').valid_encoding?.should be_true - str.force_encoding('CP720').valid_encoding?.should be_true + str = +"\xE6\x9D\x94" + str.force_encoding('IBM720').valid_encoding?.should == true + str.force_encoding('CP720').valid_encoding?.should == true end it "returns false if self is valid in one encoding, but invalid in the one it's tagged with" do - str = "\u{8765}" - str.valid_encoding?.should be_true - str = str.force_encoding('ascii') - str.valid_encoding?.should be_false + str = +"\u{8765}" + str.valid_encoding?.should == true + str.force_encoding('ascii') + str.valid_encoding?.should == false end it "returns false if self contains a character invalid in the associated encoding" do - "abc#{[0x80].pack('C')}".force_encoding('ascii').valid_encoding?.should be_false + "abc#{[0x80].pack('C')}".dup.force_encoding('ascii').valid_encoding?.should == false end it "returns false if a valid String had an invalid character appended to it" do - str = "a" - str.valid_encoding?.should be_true + str = +"a" + str.valid_encoding?.should == true str << [0xDD].pack('C').force_encoding('utf-8') - str.valid_encoding?.should be_false + str.valid_encoding?.should == false end it "returns true if an invalid string is appended another invalid one but both make a valid string" do str = [0xD0].pack('C').force_encoding('utf-8') - str.valid_encoding?.should be_false + str.valid_encoding?.should == false str << [0xBF].pack('C').force_encoding('utf-8') - str.valid_encoding?.should be_true + str.valid_encoding?.should == true end end |
