1require_relative '../../spec_helper' 2require_relative 'fixtures/classes' 3 4# FIXME: These methods exist only when the -n or -p option is passed to 5# ruby, but we currently don't have a way of specifying that. 6ruby_version_is ""..."1.9" do 7 describe "Kernel#gsub" do 8 it "is a private method" do 9 Kernel.should have_private_instance_method(:gsub) 10 end 11 12 it "raises a TypeError if $_ is not a String" do 13 lambda { 14 $_ = 123 15 gsub(/./, "!") 16 }.should raise_error(TypeError) 17 end 18 19 it "when matches sets $_ to a new string, leaving the former value unaltered" do 20 orig_value = $_ = "hello" 21 gsub("ello", "ola") 22 $_.should_not equal(orig_value) 23 $_.should == "hola" 24 orig_value.should == "hello" 25 end 26 27 it "returns a string with the same contents as $_ after the operation" do 28 $_ = "bye" 29 gsub("non-match", "?").should == "bye" 30 31 orig_value = $_ = "bye" 32 gsub(/$/, "!").should == "bye!" 33 orig_value.should == "bye" 34 end 35 36 it "accepts Regexps as patterns" do 37 $_ = "food" 38 gsub(/.$/, "l") 39 $_.should == "fool" 40 end 41 42 it "accepts Strings as patterns, treated literally" do 43 $_ = "hello, world." 44 gsub(".", "!") 45 $_.should == "hello, world!" 46 end 47 48 it "accepts objects which respond to #to_str as patterns and treats them as strings" do 49 $_ = "hello, world." 50 stringlike = mock(".") 51 stringlike.should_receive(:to_str).and_return(".") 52 gsub(stringlike, "!") 53 $_.should == "hello, world!" 54 end 55 end 56 57 describe "Kernel#gsub with a pattern and replacement" do 58 it "accepts strings for replacement" do 59 $_ = "hello" 60 gsub(/./, ".") 61 $_.should == "....." 62 end 63 64 it "accepts objects which respond to #to_str for replacement" do 65 o = mock("o") 66 o.should_receive(:to_str).and_return("o") 67 $_ = "ping" 68 gsub("i", o) 69 $_.should == "pong" 70 end 71 72 it "replaces \\1 sequences with the regexp's corresponding capture" do 73 $_ = "hello!" 74 gsub(/(.)(.)/, '\2\1') 75 $_.should == "ehll!o" 76 end 77 end 78 79 describe "Kernel#gsub with pattern and block" do 80 it "acts similarly to using $_.gsub" do 81 $_ = "olleh dlrow" 82 gsub(/(\w+)/){ $1.reverse } 83 $_.should == "hello world" 84 end 85 end 86 87 describe "Kernel#gsub!" do 88 it "is a private method" do 89 Kernel.should have_private_instance_method(:gsub!) 90 end 91 end 92 93 describe "Kernel.gsub!" do 94 it "needs to be reviewed for spec completeness" 95 end 96end 97