Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add String#[]= with Regexp specs

  • Loading branch information...
commit b2c7ca4c779c91c78203c245c3bfa5e459a39109 1 parent bc6cf71
Evan Phoenix authored
Showing with 35 additions and 6 deletions.
  1. +35 −6 spec/ruby/core/string/element_set_spec.rb
41 spec/ruby/core/string/element_set_spec.rb
View
@@ -138,12 +138,6 @@
str.should == "hello"
end
- it "raises IndexError if the regexp index doesn't match a position in the string" do
- str = "hello"
- lambda { str[/y/] = "bam" }.should raise_error(IndexError)
- str.should == "hello"
- end
-
ruby_version_is ""..."1.9" do
it "raises a TypeError when self is frozen" do
a = "hello"
@@ -189,6 +183,41 @@
end
end
+describe "String#[]= matching with a Regexp" do
+ it "replaces the matched text with the rhs" do
+ str = "hello"
+ str[/lo/] = "x"
+ str.should == "helx"
+ end
+
+ it "raises IndexError if the regexp index doesn't match a position in the string" do
+ str = "hello"
+ lambda { str[/y/] = "bam" }.should raise_error(IndexError)
+ str.should == "hello"
+ end
+
+ describe "with 3 arguments" do
+ it "uses the 2nd of 3 arguments as which capture should be replaced" do
+ str = "aaa bbb ccc"
+ str[/a (bbb) c/, 1] = "ddd"
+ str.should == "aaa ddd ccc"
+ end
+
+ it "allows the specified capture to be negative and count from the end" do
+ str = "abcd"
+ str[/(a)(b)(c)(d)/, -2] = "e"
+ str.should == "abed"
+ end
+
+ it "raises IndexError if the specified capture isn't available" do
+ str = "aaa bbb ccc"
+ lambda { str[/a (bbb) c/, 2] = "ddd" }.should raise_error(IndexError)
+ lambda { str[/a (bbb) c/, -2] = "ddd" }.should raise_error(IndexError)
+ end
+ end
+
+end
+
describe "String#[]= with index, count" do
it "starts at idx and overwrites count characters before inserting the rest of other_str" do
a = "hello"
Please sign in to comment.
Something went wrong with that request. Please try again.