diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-05-07 12:04:49 +0000 |
commit | a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5 (patch) | |
tree | 9eef7f720314ebaff56845a74e203770e62284e4 /spec/rubyspec/core/string/shared/chars.rb | |
parent | 52df1d0d3370919711c0577aaa42d1a864709885 (diff) | |
download | ruby-a3736e97a6ca517c2cd7d3d93a8f2ef86e39e5b5.tar.gz |
Add in-tree mspec and ruby/spec
* For easier modifications of ruby/spec by MRI developers.
* .gitignore: track changes under spec.
* spec/mspec, spec/rubyspec: add in-tree mspec and ruby/spec.
These files can therefore be updated like any other file in MRI.
Instructions are provided in spec/README.
[Feature #13156] [ruby-core:79246]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58595 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'spec/rubyspec/core/string/shared/chars.rb')
-rw-r--r-- | spec/rubyspec/core/string/shared/chars.rb | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/spec/rubyspec/core/string/shared/chars.rb b/spec/rubyspec/core/string/shared/chars.rb new file mode 100644 index 0000000000..2f7280a95f --- /dev/null +++ b/spec/rubyspec/core/string/shared/chars.rb @@ -0,0 +1,82 @@ +# -*- encoding: utf-8 -*- +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/classes', __FILE__) + +describe :string_chars, shared: true do + it "passes each char in self to the given block" do + a = [] + "hello".send(@method) { |c| a << c } + a.should == ['h', 'e', 'l', 'l', 'o'] + end + + it "returns self" do + s = StringSpecs::MyString.new "hello" + 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 + + with_feature :encoding do + 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'} + "&%".encode('ASCII-8BIT').send(@method).to_a.all? {|c| c.encoding.name.should == 'ASCII-8BIT'} + end + + it "works with multibyte characters" do + s = "\u{8987}".force_encoding("UTF-8") + s.bytesize.should == 3 + s.send(@method).to_a.should == [s] + end + + 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.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.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 + s = "\u{24B62}" + + s.force_encoding('UTF-8').send(@method).to_a.should == [ + s.force_encoding('UTF-8') + ] + s.force_encoding('BINARY').send(@method).to_a.should == [ + [0xF0].pack('C').force_encoding('BINARY'), + [0xA4].pack('C').force_encoding('BINARY'), + [0xAD].pack('C').force_encoding('BINARY'), + [0xA2].pack('C').force_encoding('BINARY') + ] + s.force_encoding('SJIS').send(@method).to_a.should == [ + [0xF0,0xA4].pack('CC').force_encoding('SJIS'), + [0xAD].pack('C').force_encoding('SJIS'), + [0xA2].pack('C').force_encoding('SJIS') + ] + end + + it "taints resulting strings when self is tainted" do + str = "hello" + + str.send(@method) do |x| + x.tainted?.should == false + end + + str.dup.taint.send(@method) do |x| + x.tainted?.should == true + end + end + end +end |