summaryrefslogtreecommitdiff
path: root/spec/ruby/core/string/slice_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/core/string/slice_spec.rb')
-rw-r--r--spec/ruby/core/string/slice_spec.rb170
1 files changed, 46 insertions, 124 deletions
diff --git a/spec/ruby/core/string/slice_spec.rb b/spec/ruby/core/string/slice_spec.rb
index c9e13ed1bc..16d7665bbf 100644
--- a/spec/ruby/core/string/slice_spec.rb
+++ b/spec/ruby/core/string/slice_spec.rb
@@ -1,39 +1,12 @@
-# -*- encoding: utf-8 -*-
-
+# frozen_string_literal: false
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative 'shared/slice'
describe "String#slice" do
- it_behaves_like :string_slice, :slice
-end
-
-describe "String#slice with index, length" do
- it_behaves_like :string_slice_index_length, :slice
-end
-
-describe "String#slice with Range" do
- it_behaves_like :string_slice_range, :slice
-end
-
-describe "String#slice with Regexp" do
- it_behaves_like :string_slice_regexp, :slice
-end
-
-describe "String#slice with Regexp, index" do
- it_behaves_like :string_slice_regexp_index, :slice
-end
-
-describe "String#slice with Regexp, group" do
- it_behaves_like :string_slice_regexp_group, :slice
-end
-
-describe "String#slice with String" do
- it_behaves_like :string_slice_string, :slice
-end
-
-describe "String#slice with Symbol" do
- it_behaves_like :string_slice_symbol, :slice
+ it "is an alias of String#[]" do
+ String.instance_method(:slice).should == String.instance_method(:[])
+ end
end
describe "String#slice! with index" do
@@ -54,9 +27,9 @@ describe "String#slice! with index" do
end
it "raises a FrozenError if self is frozen" do
- -> { "hello".freeze.slice!(1) }.should raise_error(FrozenError)
- -> { "hello".freeze.slice!(10) }.should raise_error(FrozenError)
- -> { "".freeze.slice!(0) }.should raise_error(FrozenError)
+ -> { "hello".freeze.slice!(1) }.should.raise(FrozenError)
+ -> { "hello".freeze.slice!(10) }.should.raise(FrozenError)
+ -> { "".freeze.slice!(0) }.should.raise(FrozenError)
end
it "calls to_int on index" do
@@ -110,13 +83,13 @@ describe "String#slice! with index, length" do
end
it "raises a FrozenError if self is frozen" do
- -> { "hello".freeze.slice!(1, 2) }.should raise_error(FrozenError)
- -> { "hello".freeze.slice!(10, 3) }.should raise_error(FrozenError)
- -> { "hello".freeze.slice!(-10, 3)}.should raise_error(FrozenError)
- -> { "hello".freeze.slice!(4, -3) }.should raise_error(FrozenError)
- -> { "hello".freeze.slice!(10, 3) }.should raise_error(FrozenError)
- -> { "hello".freeze.slice!(-10, 3)}.should raise_error(FrozenError)
- -> { "hello".freeze.slice!(4, -3) }.should raise_error(FrozenError)
+ -> { "hello".freeze.slice!(1, 2) }.should.raise(FrozenError)
+ -> { "hello".freeze.slice!(10, 3) }.should.raise(FrozenError)
+ -> { "hello".freeze.slice!(-10, 3)}.should.raise(FrozenError)
+ -> { "hello".freeze.slice!(4, -3) }.should.raise(FrozenError)
+ -> { "hello".freeze.slice!(10, 3) }.should.raise(FrozenError)
+ -> { "hello".freeze.slice!(-10, 3)}.should.raise(FrozenError)
+ -> { "hello".freeze.slice!(4, -3) }.should.raise(FrozenError)
end
it "calls to_int on idx and length" do
@@ -132,20 +105,10 @@ describe "String#slice! with index, length" do
"hello".slice!(obj, obj).should == "ll"
end
- ruby_version_is ''...'3.0' do
- it "returns subclass instances" do
- s = StringSpecs::MyString.new("hello")
- s.slice!(0, 0).should be_an_instance_of(StringSpecs::MyString)
- s.slice!(0, 4).should be_an_instance_of(StringSpecs::MyString)
- end
- end
-
- ruby_version_is '3.0' do
- it "returns String instances" do
- s = StringSpecs::MyString.new("hello")
- s.slice!(0, 0).should be_an_instance_of(String)
- s.slice!(0, 4).should be_an_instance_of(String)
- end
+ it "returns String instances" do
+ s = StringSpecs::MyString.new("hello")
+ s.slice!(0, 0).should.instance_of?(String)
+ s.slice!(0, 4).should.instance_of?(String)
end
it "returns the substring given by the character offsets" do
@@ -185,20 +148,10 @@ describe "String#slice! Range" do
b.should == "hello"
end
- ruby_version_is ''...'3.0' do
- it "returns subclass instances" do
- s = StringSpecs::MyString.new("hello")
- s.slice!(0...0).should be_an_instance_of(StringSpecs::MyString)
- s.slice!(0..4).should be_an_instance_of(StringSpecs::MyString)
- end
- end
-
- ruby_version_is '3.0' do
- it "returns String instances" do
- s = StringSpecs::MyString.new("hello")
- s.slice!(0...0).should be_an_instance_of(String)
- s.slice!(0..4).should be_an_instance_of(String)
- end
+ it "returns String instances" do
+ s = StringSpecs::MyString.new("hello")
+ s.slice!(0...0).should.instance_of?(String)
+ s.slice!(0..4).should.instance_of?(String)
end
it "calls to_int on range arguments" do
@@ -248,12 +201,12 @@ describe "String#slice! Range" do
it "raises a FrozenError on a frozen instance that is modified" do
- -> { "hello".freeze.slice!(1..3) }.should raise_error(FrozenError)
+ -> { "hello".freeze.slice!(1..3) }.should.raise(FrozenError)
end
# see redmine #1551
it "raises a FrozenError on a frozen instance that would not be modified" do
- -> { "hello".freeze.slice!(10..20)}.should raise_error(FrozenError)
+ -> { "hello".freeze.slice!(10..20)}.should.raise(FrozenError)
end
end
@@ -274,20 +227,10 @@ describe "String#slice! with Regexp" do
s.should == "this is a string"
end
- ruby_version_is ''...'3.0' do
- it "returns subclass instances" do
- s = StringSpecs::MyString.new("hello")
- s.slice!(//).should be_an_instance_of(StringSpecs::MyString)
- s.slice!(/../).should be_an_instance_of(StringSpecs::MyString)
- end
- end
-
- ruby_version_is '3.0' do
- it "returns String instances" do
- s = StringSpecs::MyString.new("hello")
- s.slice!(//).should be_an_instance_of(String)
- s.slice!(/../).should be_an_instance_of(String)
- end
+ it "returns String instances" do
+ s = StringSpecs::MyString.new("hello")
+ s.slice!(//).should.instance_of?(String)
+ s.slice!(/../).should.instance_of?(String)
end
it "returns the matching portion of self with a multi byte character" do
@@ -304,11 +247,11 @@ describe "String#slice! with Regexp" do
end
it "raises a FrozenError on a frozen instance that is modified" do
- -> { "this is a string".freeze.slice!(/s.*t/) }.should raise_error(FrozenError)
+ -> { "this is a string".freeze.slice!(/s.*t/) }.should.raise(FrozenError)
end
it "raises a FrozenError on a frozen instance that would not be modified" do
- -> { "this is a string".freeze.slice!(/zzz/) }.should raise_error(FrozenError)
+ -> { "this is a string".freeze.slice!(/zzz/) }.should.raise(FrozenError)
end
end
@@ -344,20 +287,10 @@ describe "String#slice! with Regexp, index" do
"har".slice!(/(.)(.)(.)/, obj).should == "a"
end
- ruby_version_is ''...'3.0' do
- it "returns subclass instances" do
- s = StringSpecs::MyString.new("hello")
- s.slice!(/(.)(.)/, 0).should be_an_instance_of(StringSpecs::MyString)
- s.slice!(/(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString)
- end
- end
-
- ruby_version_is '3.0' do
- it "returns String instances" do
- s = StringSpecs::MyString.new("hello")
- s.slice!(/(.)(.)/, 0).should be_an_instance_of(String)
- s.slice!(/(.)(.)/, 1).should be_an_instance_of(String)
- end
+ it "returns String instances" do
+ s = StringSpecs::MyString.new("hello")
+ s.slice!(/(.)(.)/, 0).should.instance_of?(String)
+ s.slice!(/(.)(.)/, 1).should.instance_of?(String)
end
it "returns the encoding aware capture for the given index" do
@@ -382,9 +315,9 @@ describe "String#slice! with Regexp, index" do
end
it "raises a FrozenError if self is frozen" do
- -> { "this is a string".freeze.slice!(/s.*t/) }.should raise_error(FrozenError)
- -> { "this is a string".freeze.slice!(/zzz/, 0)}.should raise_error(FrozenError)
- -> { "this is a string".freeze.slice!(/(.)/, 2)}.should raise_error(FrozenError)
+ -> { "this is a string".freeze.slice!(/s.*t/) }.should.raise(FrozenError)
+ -> { "this is a string".freeze.slice!(/zzz/, 0)}.should.raise(FrozenError)
+ -> { "this is a string".freeze.slice!(/(.)/, 2)}.should.raise(FrozenError)
end
end
@@ -412,30 +345,19 @@ describe "String#slice! with String" do
o = mock('x')
o.should_not_receive(:to_str)
- -> { "hello".slice!(o) }.should raise_error(TypeError)
- end
-
- ruby_version_is ''...'3.0' do
- it "returns a subclass instance when given a subclass instance" do
- s = StringSpecs::MyString.new("el")
- r = "hello".slice!(s)
- r.should == "el"
- r.should be_an_instance_of(StringSpecs::MyString)
- end
+ -> { "hello".slice!(o) }.should.raise(TypeError)
end
- ruby_version_is '3.0' do
- it "returns a subclass instance when given a subclass instance" do
- s = StringSpecs::MyString.new("el")
- r = "hello".slice!(s)
- r.should == "el"
- r.should be_an_instance_of(String)
- end
+ it "returns a subclass instance when given a subclass instance" do
+ s = StringSpecs::MyString.new("el")
+ r = "hello".slice!(s)
+ r.should == "el"
+ r.should.instance_of?(String)
end
it "raises a FrozenError if self is frozen" do
- -> { "hello hello".freeze.slice!('llo') }.should raise_error(FrozenError)
- -> { "this is a string".freeze.slice!('zzz')}.should raise_error(FrozenError)
- -> { "this is a string".freeze.slice!('zzz')}.should raise_error(FrozenError)
+ -> { "hello hello".freeze.slice!('llo') }.should.raise(FrozenError)
+ -> { "this is a string".freeze.slice!('zzz')}.should.raise(FrozenError)
+ -> { "this is a string".freeze.slice!('zzz')}.should.raise(FrozenError)
end
end