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/codepoints.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/codepoints.rb')
-rw-r--r-- | spec/rubyspec/core/string/shared/codepoints.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/spec/rubyspec/core/string/shared/codepoints.rb b/spec/rubyspec/core/string/shared/codepoints.rb new file mode 100644 index 0000000000..1ee13c82f4 --- /dev/null +++ b/spec/rubyspec/core/string/shared/codepoints.rb @@ -0,0 +1,56 @@ +# -*- encoding: binary -*- +describe :string_codepoints, shared: true do + 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 + lambda { s.send(@method).to_a }.should raise_error(ArgumentError) + end + + it "yields each codepoint to the block if one is given" do + codepoints = [] + "abcd".send(@method) do |codepoint| + codepoints << codepoint + end + codepoints.should == [97, 98, 99, 100] + 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 + lambda { s.send(@method) { } }.should raise_error(ArgumentError) + end + + it "returns codepoints as Fixnums" do + "glark\u{20}".send(@method).to_a.each do |codepoint| + codepoint.should be_an_instance_of(Fixnum) + end + end + + it "returns one codepoint for each character" do + s = "\u{9876}\u{28}\u{1987}" + s.send(@method).to_a.size.should == s.chars.to_a.size + end + + it "works for multibyte characters" do + s = "\u{9819}" + s.bytesize.should == 3 + s.send(@method).to_a.should == [38937] + end + + it "returns the codepoint corresponding to the character's position in the String's encoding" do + "\u{787}".send(@method).to_a.should == [1927] + end + + it "round-trips to the original String using Integer#chr" do + s = "\u{13}\u{7711}\u{1010}" + s2 = "" + s.send(@method) {|n| s2 << n.chr(Encoding::UTF_8)} + s.should == s2 + end + + it "is synonomous 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 + end +end |