summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/partition_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/string/partition_spec.rb')
-rw-r--r--spec/ruby/core/string/partition_spec.rb33
1 files changed, 29 insertions, 4 deletions
diff --git a/spec/ruby/core/string/partition_spec.rb b/spec/ruby/core/string/partition_spec.rb
index 04f49db1b1..29fe910b39 100644
--- a/spec/ruby/core/string/partition_spec.rb
+++ b/spec/ruby/core/string/partition_spec.rb
@@ -1,7 +1,10 @@
-require File.expand_path('../../../spec_helper', __FILE__)
-require File.expand_path('../fixtures/classes.rb', __FILE__)
+require_relative '../../spec_helper'
+require_relative 'fixtures/classes'
+require_relative 'shared/partition'
describe "String#partition with String" do
+ it_behaves_like :string_partition, :partition
+
it "returns an array of substrings based on splitting on the given string" do
"hello world".partition("o").should == ["hell", "o", " world"]
end
@@ -28,11 +31,33 @@ describe "String#partition with String" do
end
it "raises an error if not convertible to string" do
- lambda{ "hello".partition(5) }.should raise_error(TypeError)
- lambda{ "hello".partition(nil) }.should raise_error(TypeError)
+ ->{ "hello".partition(5) }.should.raise(TypeError)
+ ->{ "hello".partition(nil) }.should.raise(TypeError)
end
it "takes precedence over a given block" do
"hello world".partition("o") { true }.should == ["hell", "o", " world"]
end
+
+ it "handles a pattern in a superset encoding" do
+ string = "hello".dup.force_encoding(Encoding::US_ASCII)
+
+ result = string.partition("é")
+
+ result.should == ["hello", "", ""]
+ result[0].encoding.should == Encoding::US_ASCII
+ result[1].encoding.should == Encoding::US_ASCII
+ result[2].encoding.should == Encoding::US_ASCII
+ end
+
+ it "handles a pattern in a subset encoding" do
+ pattern = "o".dup.force_encoding(Encoding::US_ASCII)
+
+ result = "héllo world".partition(pattern)
+
+ result.should == ["héll", "o", " world"]
+ result[0].encoding.should == Encoding::UTF_8
+ result[1].encoding.should == Encoding::US_ASCII
+ result[2].encoding.should == Encoding::UTF_8
+ end
end