diff options
Diffstat (limited to 'spec/ruby/core/string/shared')
-rw-r--r-- | spec/ruby/core/string/shared/chars.rb | 6 | ||||
-rw-r--r-- | spec/ruby/core/string/shared/codepoints.rb | 2 | ||||
-rw-r--r-- | spec/ruby/core/string/shared/each_line.rb | 14 | ||||
-rw-r--r-- | spec/ruby/core/string/shared/to_sym.rb | 67 |
4 files changed, 70 insertions, 19 deletions
diff --git a/spec/ruby/core/string/shared/chars.rb b/spec/ruby/core/string/shared/chars.rb index 2f7280a95f..c1cf324dc5 100644 --- a/spec/ruby/core/string/shared/chars.rb +++ b/spec/ruby/core/string/shared/chars.rb @@ -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 diff --git a/spec/ruby/core/string/shared/codepoints.rb b/spec/ruby/core/string/shared/codepoints.rb index 1ee13c82f4..68f82b4468 100644 --- a/spec/ruby/core/string/shared/codepoints.rb +++ b/spec/ruby/core/string/shared/codepoints.rb @@ -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 diff --git a/spec/ruby/core/string/shared/each_line.rb b/spec/ruby/core/string/shared/each_line.rb index fe8b76b47b..dee741e270 100644 --- a/spec/ruby/core/string/shared/each_line.rb +++ b/spec/ruby/core/string/shared/each_line.rb @@ -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 diff --git a/spec/ruby/core/string/shared/to_sym.rb b/spec/ruby/core/string/shared/to_sym.rb index 501247078d..1180d64712 100644 --- a/spec/ruby/core/string/shared/to_sym.rb +++ b/spec/ruby/core/string/shared/to_sym.rb @@ -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 |