diff options
Diffstat (limited to 'spec/ruby/core/string/center_spec.rb')
| -rw-r--r-- | spec/ruby/core/string/center_spec.rb | 86 |
1 files changed, 35 insertions, 51 deletions
diff --git a/spec/ruby/core/string/center_spec.rb b/spec/ruby/core/string/center_spec.rb index 4a40ef88bf..ac5b8a2ff3 100644 --- a/spec/ruby/core/string/center_spec.rb +++ b/spec/ruby/core/string/center_spec.rb @@ -1,6 +1,6 @@ # -*- encoding: utf-8 -*- -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/classes.rb', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/classes' describe "String#center with length, padding" do it "returns a new string of specified length with self centered and padded with padstr" do @@ -47,14 +47,6 @@ describe "String#center with length, padding" do "radiology".center(8, '-').should == "radiology" end - it "taints result when self or padstr is tainted" do - "x".taint.center(4).tainted?.should == true - "x".taint.center(0).tainted?.should == true - "".taint.center(0).tainted?.should == true - "x".taint.center(4, "*").tainted?.should == true - "x".center(4, "*".taint).tainted?.should == true - end - it "calls #to_int to convert length to an integer" do "_".center(3.8, "^").should == "^_^" @@ -65,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 - lambda { "hello".center("x") }.should raise_error(TypeError) - lambda { "hello".center("x", "y") }.should raise_error(TypeError) - lambda { "hello".center([]) }.should raise_error(TypeError) - lambda { "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 @@ -79,55 +71,47 @@ describe "String#center with length, padding" do end it "raises a TypeError when padstr can't be converted to a string" do - lambda { "hello".center(20, 100) }.should raise_error(TypeError) - lambda { "hello".center(20, []) }.should raise_error(TypeError) - lambda { "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 - lambda { "hello".center(10, "") }.should raise_error(ArgumentError) - lambda { "hello".center(0, "") }.should raise_error(ArgumentError) + -> { "hello".center(10, "") }.should.raise(ArgumentError) + -> { "hello".center(0, "") }.should.raise(ArgumentError) end - it "returns subclass instances when called on subclasses" do - StringSpecs::MyString.new("").center(10).should be_an_instance_of(StringSpecs::MyString) - StringSpecs::MyString.new("foo").center(10).should be_an_instance_of(StringSpecs::MyString) - StringSpecs::MyString.new("foo").center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(StringSpecs::MyString) + it "returns String instances when called on subclasses" do + 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 - it "when padding is tainted and self is untainted returns a tainted string if and only if length is longer than self" do - "hello".center(4, 'X'.taint).tainted?.should be_false - "hello".center(5, 'X'.taint).tainted?.should be_false - "hello".center(6, 'X'.taint).tainted?.should be_true + describe "with width" do + it "returns a String in the same encoding as the original" do + str = "abc".dup.force_encoding Encoding::IBM437 + result = str.center 6 + result.should == " abc " + result.encoding.should.equal?(Encoding::IBM437) + end end - with_feature :encoding do - describe "with width" do - it "returns a String in the same encoding as the original" do - str = "abc".force_encoding Encoding::IBM437 - result = str.center 6 - result.should == " abc " - result.encoding.should equal(Encoding::IBM437) - end + describe "with width, pattern" do + it "returns a String in the compatible encoding" do + str = "abc".dup.force_encoding Encoding::IBM437 + result = str.center 6, "あ" + result.should == "あabcああ" + result.encoding.should.equal?(Encoding::UTF_8) end - describe "with width, pattern" do - it "returns a String in the compatible encoding" do - str = "abc".force_encoding Encoding::IBM437 - result = str.center 6, "あ" - result.should == "あabcああ" - result.encoding.should equal(Encoding::UTF_8) - end - - it "raises an Encoding::CompatibilityError if the encodings are incompatible" do - pat = "ア".encode Encoding::EUC_JP - lambda do - "あれ".center 5, pat - end.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the encodings are incompatible" do + pat = "ア".encode Encoding::EUC_JP + -> do + "あれ".center 5, pat + end.should.raise(Encoding::CompatibilityError) end end end |
