PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/core/kernel/gsub_spec.rb

https://bitbucket.org/nicksieger/rubyspec
Ruby | 91 lines | 80 code | 11 blank | 0 comment | 11 complexity | 93d7fc06bd7463f8d1a2dba1d2875c34 MD5 | raw file
  1. require File.dirname(__FILE__) + '/../../spec_helper'
  2. require File.dirname(__FILE__) + '/fixtures/classes'
  3. describe "Kernel#gsub" do
  4. it "is a private method" do
  5. Kernel.should have_private_instance_method(:gsub)
  6. end
  7. it "raises a TypeError if $_ is not a String" do
  8. lambda {
  9. $_ = 123
  10. gsub /./, "!"
  11. }.should raise_error(TypeError)
  12. end
  13. it "when matches sets $_ to a new string, leaving the former value unaltered" do
  14. orig_value = $_ = "hello"
  15. gsub "ello", "ola"
  16. $_.should_not equal(orig_value)
  17. $_.should == "hola"
  18. orig_value.should == "hello"
  19. end
  20. it "returns a string with the same contents as $_ after the operation" do
  21. $_ = "bye"
  22. gsub("non-match", "?").should == "bye"
  23. orig_value = $_ = "bye"
  24. gsub(/$/, "!").should == "bye!"
  25. end
  26. it "accepts Regexps as patterns" do
  27. $_ = "food"
  28. gsub /.$/, "l"
  29. $_.should == "fool"
  30. end
  31. it "accepts Strings as patterns, treated literally" do
  32. $_ = "hello, world."
  33. gsub ".", "!"
  34. $_.should == "hello, world!"
  35. end
  36. it "accepts objects which respond to #to_str as patterns and treats them as strings" do
  37. $_ = "hello, world."
  38. stringlike = mock(".")
  39. stringlike.should_receive(:to_str).and_return(".")
  40. gsub stringlike, "!"
  41. $_.should == "hello, world!"
  42. end
  43. end
  44. describe "Kernel#gsub with a pattern and replacement" do
  45. it "accepts strings for replacement" do
  46. $_ = "hello"
  47. gsub /./, "."
  48. $_.should == "....."
  49. end
  50. it "accepts objects which respond to #to_str for replacement" do
  51. o = mock("o")
  52. o.should_receive(:to_str).and_return("o")
  53. $_ = "ping"
  54. gsub "i", o
  55. $_.should == "pong"
  56. end
  57. it "replaces \\1 sequences with the regexp's corresponding capture" do
  58. $_ = "hello!"
  59. gsub /(.)(.)/, '\2\1'
  60. $_.should == "ehll!o"
  61. end
  62. end
  63. describe "Kernel#gsub with pattern and block" do
  64. it "acts similarly to using $_.gsub" do
  65. $_ = "olleh dlrow"
  66. gsub(/(\w+)/){ $1.reverse }
  67. $_.should == "hello world"
  68. end
  69. end
  70. describe "Kernel#gsub!" do
  71. it "is a private method" do
  72. Kernel.should have_private_instance_method(:gsub!)
  73. end
  74. end
  75. describe "Kernel.gsub!" do
  76. it "needs to be reviewed for spec completeness"
  77. end