1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60525 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
eregon 2017-10-28 15:15:48 +00:00
parent 6530b14cee
commit 8c5b60eb22
218 changed files with 4069 additions and 328 deletions

View file

@ -44,6 +44,14 @@ describe "String#capitalize!" do
a.should == "Hello"
end
ruby_version_is '2.4' do
it "capitalizes self in place for all of Unicode" do
a = "äöü"
a.capitalize!.should equal(a)
a.should == "Äöü"
end
end
it "returns nil when no changes are made" do
a = "Hello"
a.capitalize!.should == nil

View file

@ -1,4 +1,4 @@
# -*- encoding: ascii-8bit -*-
# -*- encoding: utf-8 -*-
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes.rb', __FILE__)
@ -40,10 +40,10 @@ describe "String#casecmp independent of case" do
describe "in UTF-8 mode" do
describe "for non-ASCII characters" do
before :each do
@upper_a_tilde = "\xc3\x83"
@lower_a_tilde = "\xc3\xa3"
@upper_a_umlaut = "\xc3\x84"
@lower_a_umlaut = "\xc3\xa4"
@upper_a_tilde = "Ã"
@lower_a_tilde = "ã"
@upper_a_umlaut = "Ä"
@lower_a_umlaut = "ä"
end
it "returns -1 when numerically less than other" do
@ -118,3 +118,67 @@ describe "String#casecmp independent of case" do
end
end
end
ruby_version_is "2.4" do
describe 'String#casecmp? independent of case' do
it 'returns true when equal to other' do
'abc'.casecmp?('abc').should == true
'abc'.casecmp?('ABC').should == true
end
it 'returns false when not equal to other' do
'abc'.casecmp?('DEF').should == false
'abc'.casecmp?('def').should == false
end
it "tries to convert other to string using to_str" do
other = mock('x')
other.should_receive(:to_str).and_return("abc")
"abc".casecmp?(other).should == true
end
describe 'for UNICODE characters' do
it 'returns true when downcase(:fold) on unicode' do
'äöü'.casecmp?('ÄÖÜ').should == true
end
end
describe "when comparing a subclass instance" do
it 'returns true when equal to other' do
a = StringSpecs::MyString.new "a"
'a'.casecmp?(a).should == true
'A'.casecmp?(a).should == true
end
it 'returns false when not equal to other' do
b = StringSpecs::MyString.new "a"
'b'.casecmp?(b).should == false
'B'.casecmp?(b).should == false
end
end
describe "in UTF-8 mode" do
describe "for non-ASCII characters" do
before :each do
@upper_a_tilde = "Ã"
@lower_a_tilde = "ã"
@upper_a_umlaut = "Ä"
@lower_a_umlaut = "ä"
end
it "returns true when they are the same with normalized case" do
@upper_a_tilde.casecmp?(@lower_a_tilde).should == true
end
it "returns false when they are unrelated" do
@upper_a_tilde.casecmp?(@upper_a_umlaut).should == false
end
it "returns true when they have the same bytes" do
@upper_a_tilde.casecmp?(@upper_a_tilde).should == true
end
end
end
end
end

View file

@ -46,6 +46,14 @@ describe "String#downcase!" do
a.should == "hello"
end
ruby_version_is '2.4' do
it "modifies self in place for all of Unicode" do
a = "ÄÖÜ"
a.downcase!.should equal(a)
a.should == "äöü"
end
end
it "returns nil if no modifications were made" do
a = "hello"
a.downcase!.should == nil

View file

@ -10,4 +10,13 @@ describe "String#lines" do
ary = "hello world".send(@method, ' ')
ary.should == ["hello ", "world"]
end
ruby_version_is '2.4' do
context "when `chomp` keyword argument is passed" do
it "removes new line characters" do
"hello \nworld\n".lines(chomp: true).should == ["hello ", "world"]
"hello \r\nworld\r\n".lines(chomp: true).should == ["hello ", "world"]
end
end
end
end

View file

@ -15,6 +15,13 @@ describe "String.new" do
end
end
ruby_version_is "2.4" do
it "accepts a capacity argument" do
String.new("", capacity: 100_000).should == ""
String.new("abc", capacity: 100_000).should == "abc"
end
end
it "returns a fully-formed String" do
str = String.new
str.size.should == 0

View file

@ -42,10 +42,8 @@ describe :string_chars, shared: true do
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.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').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')]
end
it "uses the String's encoding to determine what characters it contains" do

View file

@ -48,7 +48,7 @@ describe :string_codepoints, shared: true do
s.should == s2
end
it "is synonomous with #bytes for Strings which are single-byte optimisable" do
it "is synonymous with #bytes for Strings which are single-byte optimisable" do
s = "(){}".encode('ascii')
s.ascii_only?.should be_true
s.send(@method).to_a.should == s.bytes.to_a

View file

@ -133,4 +133,18 @@ end
it "raises a TypeError when the separator is a symbol" do
lambda { "hello world".send(@method, :o).to_a }.should raise_error(TypeError)
end
ruby_version_is '2.4' do
context "when `chomp` keyword argument is passed" do
it "removes new line characters" do
a = []
"hello \nworld\n".send(@method, chomp: true) { |s| a << s }
a.should == ["hello ", "world"]
a = []
"hello \r\nworld\r\n".send(@method, chomp: true) { |s| a << s }
a.should == ["hello ", "world"]
end
end
end
end

View file

@ -1,24 +1,63 @@
describe :string_to_sym, shared: true do
it "returns the symbol corresponding to self" do
"Koala".send(@method).should == :Koala
'cat'.send(@method).should == :cat
'@cat'.send(@method).should == :@cat
'cat and dog'.send(@method).should == :"cat and dog"
"abc=".send(@method).should == :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 == :"+(binary)"
"-(binary)".send(@method).should == :"-(binary)"
"+(binary)".send(@method).should equal :"+(binary)"
"-(binary)".send(@method).should equal :"-(binary)"
end
it "does not special case certain operators" do
[ ["!@", :"!@"],
["~@", :"~@"],
["!(unary)", :"!(unary)"],
["~(unary)", :"~(unary)"],
["+(unary)", :"+(unary)"],
["-(unary)", :"-(unary)"]
].should be_computed_by(@method)
"!@".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"
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"
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'}"
end
it "returns a UTF-16LE Symbol for a UTF-16LE String containing non US-ASCII characters" do
utf16_str = "UtéF16".encode(Encoding::UTF_16LE)
sym = utf16_str.send(@method)
sym.encoding.should == Encoding::UTF_16LE
sym.to_s.should == utf16_str
end
it "returns a binary Symbol for a binary String containing non US-ASCII characters" do
binary_string = "binarí".b
sym = binary_string.send(@method)
sym.encoding.should == Encoding::BINARY
sym.to_s.should == binary_string
end
it "raises an EncodingError for UTF-8 String containing invalid bytes" do
invalid_utf8 = "\xC3"
invalid_utf8.valid_encoding?.should == false
-> {
invalid_utf8.send(@method)
}.should raise_error(EncodingError, /invalid/)
end
end

View file

@ -41,6 +41,14 @@ describe "String#swapcase!" do
a.should == "CyBeR_pUnK11"
end
ruby_version_is '2.4' do
it "modifies self in place for all of Unicode" do
a = "äÖü"
a.swapcase!.should equal(a)
a.should == "ÄöÜ"
end
end
it "returns nil if no modifications were made" do
a = "+++---111222???"
a.swapcase!.should == nil

View file

@ -0,0 +1,12 @@
require File.expand_path('../../../spec_helper', __FILE__)
ruby_version_is "2.4" do
describe "String#unpack1" do
it "returns the first value of #unpack" do
"ABCD".unpack1('x3C').should == "ABCD".unpack('x3C')[0]
"\u{3042 3044 3046}".unpack1("U*").should == 0x3042
"aG9nZWZ1Z2E=".unpack1("m").should == "hogefuga"
"A".unpack1("B*").should == "01000001"
end
end
end

View file

@ -46,6 +46,15 @@ describe "String#upcase!" do
a.should == "HELLO"
end
ruby_version_is '2.4' do
it "modifies self in place for all of Unicode" do
a = "äöü"
a.upcase!.should equal(a)
a.should == "ÄÖÜ"
end
end
it "returns nil if no modifications were made" do
a = "HELLO"
a.upcase!.should == nil