PageRenderTime 34ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Tests/mspec/rubyspec/core/kernel/gsub_spec.rb

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