PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/IronLanguages/main
Ruby | 518 lines | 487 code | 31 blank | 0 comment | 37 complexity | c071e151ad2afb269c779f1ee1992f55 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.rb'
  3. describe "String#gsub with pattern and replacement" do
  4. it "doesn't freak out when replacing ^" do
  5. "Text\n".gsub(/^/, ' ').should == " Text\n"
  6. "Text\nFoo".gsub(/^/, ' ').should == " Text\n Foo"
  7. end
  8. it "returns a copy of self with all occurrences of pattern replaced with replacement" do
  9. "hello".gsub(/[aeiou]/, '*').should == "h*ll*"
  10. str = "hello homely world. hah!"
  11. str.gsub(/\Ah\S+\s*/, "huh? ").should == "huh? homely world. hah!"
  12. "hello".gsub(//, ".").should == ".h.e.l.l.o."
  13. end
  14. it "ignores a block if supplied" do
  15. "food".gsub(/f/, "g") { "w" }.should == "good"
  16. end
  17. it "supports \\G which matches at the beginning of the remaining (non-matched) string" do
  18. str = "hello homely world. hah!"
  19. str.gsub(/\Gh\S+\s*/, "huh? ").should == "huh? huh? world. hah!"
  20. end
  21. it "supports /i for ignoring case" do
  22. str = "Hello. How happy are you?"
  23. str.gsub(/h/i, "j").should == "jello. jow jappy are you?"
  24. str.gsub(/H/i, "j").should == "jello. jow jappy are you?"
  25. end
  26. it "doesn't interpret regexp metacharacters if pattern is a string" do
  27. "12345".gsub('\d', 'a').should == "12345"
  28. '\d'.gsub('\d', 'a').should == "a"
  29. end
  30. it "replaces \\1 sequences with the regexp's corresponding capture" do
  31. str = "hello"
  32. str.gsub(/([aeiou])/, '<\1>').should == "h<e>ll<o>"
  33. str.gsub(/(.)/, '\1\1').should == "hheelllloo"
  34. str.gsub(/.(.?)/, '<\0>(\1)').should == "<he>(e)<ll>(l)<o>()"
  35. str.gsub(/.(.)+/, '\1').should == "o"
  36. str = "ABCDEFGHIJKLabcdefghijkl"
  37. re = /#{"(.)" * 12}/
  38. str.gsub(re, '\1').should == "Aa"
  39. str.gsub(re, '\9').should == "Ii"
  40. # Only the first 9 captures can be accessed in MRI
  41. str.gsub(re, '\10').should == "A0a0"
  42. end
  43. it "treats \\1 sequences without corresponding captures as empty strings" do
  44. str = "hello!"
  45. str.gsub("", '<\1>').should == "<>h<>e<>l<>l<>o<>!<>"
  46. str.gsub("h", '<\1>').should == "<>ello!"
  47. str.gsub(//, '<\1>').should == "<>h<>e<>l<>l<>o<>!<>"
  48. str.gsub(/./, '\1\2\3').should == ""
  49. str.gsub(/.(.{20})?/, '\1').should == ""
  50. end
  51. it "replaces \\& and \\0 with the complete match" do
  52. str = "hello!"
  53. str.gsub("", '<\0>').should == "<>h<>e<>l<>l<>o<>!<>"
  54. str.gsub("", '<\&>').should == "<>h<>e<>l<>l<>o<>!<>"
  55. str.gsub("he", '<\0>').should == "<he>llo!"
  56. str.gsub("he", '<\&>').should == "<he>llo!"
  57. str.gsub("l", '<\0>').should == "he<l><l>o!"
  58. str.gsub("l", '<\&>').should == "he<l><l>o!"
  59. str.gsub(//, '<\0>').should == "<>h<>e<>l<>l<>o<>!<>"
  60. str.gsub(//, '<\&>').should == "<>h<>e<>l<>l<>o<>!<>"
  61. str.gsub(/../, '<\0>').should == "<he><ll><o!>"
  62. str.gsub(/../, '<\&>').should == "<he><ll><o!>"
  63. str.gsub(/(.)./, '<\0>').should == "<he><ll><o!>"
  64. end
  65. it "replaces \\` with everything before the current match" do
  66. str = "hello!"
  67. str.gsub("", '<\`>').should == "<>h<h>e<he>l<hel>l<hell>o<hello>!<hello!>"
  68. str.gsub("h", '<\`>').should == "<>ello!"
  69. str.gsub("l", '<\`>').should == "he<he><hel>o!"
  70. str.gsub("!", '<\`>').should == "hello<hello>"
  71. str.gsub(//, '<\`>').should == "<>h<h>e<he>l<hel>l<hell>o<hello>!<hello!>"
  72. str.gsub(/../, '<\`>').should == "<><he><hell>"
  73. end
  74. it "replaces \\' with everything after the current match" do
  75. str = "hello!"
  76. str.gsub("", '<\\\'>').should == "<hello!>h<ello!>e<llo!>l<lo!>l<o!>o<!>!<>"
  77. str.gsub("h", '<\\\'>').should == "<ello!>ello!"
  78. str.gsub("ll", '<\\\'>').should == "he<o!>o!"
  79. str.gsub("!", '<\\\'>').should == "hello<>"
  80. str.gsub(//, '<\\\'>').should == "<hello!>h<ello!>e<llo!>l<lo!>l<o!>o<!>!<>"
  81. str.gsub(/../, '<\\\'>').should == "<llo!><o!><>"
  82. end
  83. it "replaces \\+ with the last paren that actually matched" do
  84. str = "hello!"
  85. str.gsub(/(.)(.)/, '\+').should == "el!"
  86. str.gsub(/(.)(.)+/, '\+').should == "!"
  87. str.gsub(/(.)()/, '\+').should == ""
  88. str.gsub(/(.)(.{20})?/, '<\+>').should == "<h><e><l><l><o><!>"
  89. str = "ABCDEFGHIJKLabcdefghijkl"
  90. re = /#{"(.)" * 12}/
  91. str.gsub(re, '\+').should == "Ll"
  92. end
  93. it "treats \\+ as an empty string if there was no captures" do
  94. "hello!".gsub(/./, '\+').should == ""
  95. end
  96. it "maps \\\\ in replacement to \\" do
  97. "hello".gsub(/./, '\\\\').should == '\\' * 5
  98. end
  99. it "leaves unknown \\x escapes in replacement untouched" do
  100. "hello".gsub(/./, '\\x').should == '\\x' * 5
  101. "hello".gsub(/./, '\\y').should == '\\y' * 5
  102. end
  103. it "leaves \\ at the end of replacement untouched" do
  104. "hello".gsub(/./, 'hah\\').should == 'hah\\' * 5
  105. end
  106. it "taints the result if the original string or replacement is tainted" do
  107. hello = "hello"
  108. hello_t = "hello"
  109. a = "a"
  110. a_t = "a"
  111. empty = ""
  112. empty_t = ""
  113. hello_t.taint; a_t.taint; empty_t.taint
  114. hello_t.gsub(/./, a).tainted?.should == true
  115. hello_t.gsub(/./, empty).tainted?.should == true
  116. hello.gsub(/./, a_t).tainted?.should == true
  117. hello.gsub(/./, empty_t).tainted?.should == true
  118. hello.gsub(//, empty_t).tainted?.should == true
  119. hello.gsub(//.taint, "foo").tainted?.should == false
  120. end
  121. ruby_version_is "1.9" do
  122. it "untrusts the result if the original string or replacement is untrusted" do
  123. hello = "hello"
  124. hello_t = "hello"
  125. a = "a"
  126. a_t = "a"
  127. empty = ""
  128. empty_t = ""
  129. hello_t.untrust; a_t.untrust; empty_t.untrust
  130. hello_t.gsub(/./, a).untrusted?.should == true
  131. hello_t.gsub(/./, empty).untrusted?.should == true
  132. hello.gsub(/./, a_t).untrusted?.should == true
  133. hello.gsub(/./, empty_t).untrusted?.should == true
  134. hello.gsub(//, empty_t).untrusted?.should == true
  135. hello.gsub(//.untrust, "foo").untrusted?.should == false
  136. end
  137. end
  138. it "tries to convert pattern to a string using to_str" do
  139. pattern = mock('.')
  140. def pattern.to_str() "." end
  141. "hello.".gsub(pattern, "!").should == "hello!"
  142. end
  143. it "raises a TypeError when pattern can't be converted to a string" do
  144. lambda { "hello".gsub([], "x") }.should raise_error(TypeError)
  145. lambda { "hello".gsub(Object.new, "x") }.should raise_error(TypeError)
  146. lambda { "hello".gsub(nil, "x") }.should raise_error(TypeError)
  147. end
  148. it "tries to convert replacement to a string using to_str" do
  149. replacement = mock('hello_replacement')
  150. def replacement.to_str() "hello_replacement" end
  151. "hello".gsub(/hello/, replacement).should == "hello_replacement"
  152. end
  153. it "raises a TypeError when replacement can't be converted to a string" do
  154. lambda { "hello".gsub(/[aeiou]/, []) }.should raise_error(TypeError)
  155. lambda { "hello".gsub(/[aeiou]/, Object.new) }.should raise_error(TypeError)
  156. lambda { "hello".gsub(/[aeiou]/, nil) }.should raise_error(TypeError)
  157. end
  158. it "returns subclass instances when called on a subclass" do
  159. StringSpecs::MyString.new("").gsub(//, "").class.should == StringSpecs::MyString
  160. StringSpecs::MyString.new("").gsub(/foo/, "").class.should == StringSpecs::MyString
  161. StringSpecs::MyString.new("foo").gsub(/foo/, "").class.should == StringSpecs::MyString
  162. StringSpecs::MyString.new("foo").gsub("foo", "").class.should == StringSpecs::MyString
  163. end
  164. # Note: $~ cannot be tested because mspec messes with it
  165. it "sets $~ to MatchData of last match and nil when there's none" do
  166. 'hello.'.gsub('hello', 'x')
  167. $~[0].should == 'hello'
  168. 'hello.'.gsub('not', 'x')
  169. $~.should == nil
  170. 'hello.'.gsub(/.(.)/, 'x')
  171. $~[0].should == 'o.'
  172. 'hello.'.gsub(/not/, 'x')
  173. $~.should == nil
  174. end
  175. end
  176. ruby_version_is "1.9" do
  177. describe "String#gsub with pattern and Hash" do
  178. it "returns a copy of self with all occurrences of pattern replaced with the value of the corresponding hash key" do
  179. "hello".gsub(/./, 'l' => 'L').should == "LL"
  180. "hello!".gsub(/(.)(.)/, 'he' => 'she ', 'll' => 'said').should == 'she said'
  181. "hello".gsub('l', 'l' => 'el').should == 'heelelo'
  182. end
  183. it "ignores keys that don't correspond to matches" do
  184. "hello".gsub(/./, 'z' => 'L', 'h' => 'b', 'o' => 'ow').should == "bow"
  185. end
  186. it "returns an empty string if the pattern matches but the hash specifies no replacements" do
  187. "hello".gsub(/./, 'z' => 'L').should == ""
  188. end
  189. it "ignores non-String keys" do
  190. "hello".gsub(/(ll)/, 'll' => 'r', :ll => 'z').should == "hero"
  191. end
  192. it "uses a key's value as many times as needed" do
  193. "food".gsub(/o/, 'o' => '0').should == "f00d"
  194. end
  195. it "uses the hash's default value for missing keys" do
  196. hsh = new_hash
  197. hsh.default='?'
  198. hsh['o'] = '0'
  199. "food".gsub(/./, hsh).should == "?00?"
  200. end
  201. it "coerces the hash values with #to_s" do
  202. hsh = new_hash
  203. hsh.default=[]
  204. hsh['o'] = 0
  205. obj = mock('!')
  206. obj.should_receive(:to_s).and_return('!')
  207. hsh['!'] = obj
  208. "food!".gsub(/./, hsh).should == "[]00[]!"
  209. end
  210. it "raises a TypeError if the hash has a default proc" do
  211. hsh = new_hash
  212. hsh.default_proc = lambda { |k,v| 'lamb' }
  213. lambda do
  214. "food!".gsub(/./, hsh)
  215. end.should_not raise_error(TypeError)
  216. end
  217. it "sets $~ to MatchData of last match and nil when there's none for access from outside" do
  218. 'hello.'.gsub('l', 'l' => 'L')
  219. $~.begin(0).should == 3
  220. $~[0].should == 'l'
  221. 'hello.'.gsub('not', 'ot' => 'to')
  222. $~.should == nil
  223. 'hello.'.gsub(/.(.)/, 'o' => ' hole')
  224. $~[0].should == 'o.'
  225. 'hello.'.gsub(/not/, 'z' => 'glark')
  226. $~.should == nil
  227. end
  228. it "doesn't interpolate special sequences like \\1 for the block's return value" do
  229. repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
  230. "hello".gsub(/(.+)/, 'hello' => repl ).should == repl
  231. end
  232. it "untrusts the result if the original string is untrusted" do
  233. str = "Ghana".untrust
  234. str.gsub(/[Aa]na/, 'ana' => '').untrusted?.should be_true
  235. end
  236. it "untrusts the result if a hash value is untrusted" do
  237. str = "Ghana"
  238. str.gsub(/a$/, 'a' => 'di'.untrust).untrusted?.should be_true
  239. end
  240. it "taints the result if the original string is tainted" do
  241. str = "Ghana".taint
  242. str.gsub(/[Aa]na/, 'ana' => '').tainted?.should be_true
  243. end
  244. it "taints the result if a hash value is tainted" do
  245. str = "Ghana"
  246. str.gsub(/a$/, 'a' => 'di'.taint).tainted?.should be_true
  247. end
  248. end
  249. end
  250. describe "String#gsub with pattern and block" do
  251. it "returns a copy of self with all occurrences of pattern replaced with the block's return value" do
  252. "hello".gsub(/./) { |s| s.succ + ' ' }.should == "i f m m p "
  253. "hello!".gsub(/(.)(.)/) { |*a| a.inspect }.should == '["he"]["ll"]["o!"]'
  254. "hello".gsub('l') { 'x'}.should == 'hexxo'
  255. end
  256. it "sets $~ for access from the block" do
  257. str = "hello"
  258. str.gsub(/([aeiou])/) { "<#{$~[1]}>" }.should == "h<e>ll<o>"
  259. str.gsub(/([aeiou])/) { "<#{$1}>" }.should == "h<e>ll<o>"
  260. str.gsub("l") { "<#{$~[0]}>" }.should == "he<l><l>o"
  261. offsets = []
  262. str.gsub(/([aeiou])/) do
  263. md = $~
  264. md.string.should == str
  265. offsets << md.offset(0)
  266. str
  267. end.should == "hhellollhello"
  268. offsets.should == [[1, 2], [4, 5]]
  269. end
  270. it "restores $~ after leaving the block" do
  271. [/./, "l"].each do |pattern|
  272. old_md = nil
  273. "hello".gsub(pattern) do
  274. old_md = $~
  275. "ok".match(/./)
  276. "x"
  277. end
  278. $~[0].should == old_md[0]
  279. $~.string.should == "hello"
  280. end
  281. end
  282. it "sets $~ to MatchData of last match and nil when there's none for access from outside" do
  283. 'hello.'.gsub('l') { 'x' }
  284. $~.begin(0).should == 3
  285. $~[0].should == 'l'
  286. 'hello.'.gsub('not') { 'x' }
  287. $~.should == nil
  288. 'hello.'.gsub(/.(.)/) { 'x' }
  289. $~[0].should == 'o.'
  290. 'hello.'.gsub(/not/) { 'x' }
  291. $~.should == nil
  292. end
  293. ruby_version_is ""..."1.9" do
  294. it "raises a RuntimeError if the string is modified while substituting" do
  295. str = "hello"
  296. lambda { str.gsub(//) { str[0] = 'x' } }.should raise_error(RuntimeError)
  297. end
  298. end
  299. it "doesn't interpolate special sequences like \\1 for the block's return value" do
  300. repl = '\& \0 \1 \` \\\' \+ \\\\ foo'
  301. "hello".gsub(/(.+)/) { repl }.should == repl
  302. end
  303. it "converts the block's return value to a string using to_s" do
  304. replacement = mock('hello_replacement')
  305. def replacement.to_s() "hello_replacement" end
  306. "hello".gsub(/hello/) { replacement }.should == "hello_replacement"
  307. obj = mock('ok')
  308. def obj.to_s() "ok" end
  309. "hello".gsub(/.+/) { obj }.should == "ok"
  310. end
  311. ruby_version_is "1.9" do
  312. it "untrusts the result if the original string or replacement is untrusted" do
  313. hello = "hello"
  314. hello_t = "hello"
  315. a = "a"
  316. a_t = "a"
  317. empty = ""
  318. empty_t = ""
  319. hello_t.untrust; a_t.untrust; empty_t.untrust
  320. hello_t.gsub(/./) { a }.untrusted?.should == true
  321. hello_t.gsub(/./) { empty }.untrusted?.should == true
  322. hello.gsub(/./) { a_t }.untrusted?.should == true
  323. hello.gsub(/./) { empty_t }.untrusted?.should == true
  324. hello.gsub(//) { empty_t }.untrusted?.should == true
  325. hello.gsub(//.untrust) { "foo" }.untrusted?.should == false
  326. end
  327. end
  328. end
  329. describe "String#gsub! with pattern and replacement" do
  330. it "modifies self in place and returns self" do
  331. a = "hello"
  332. a.gsub!(/[aeiou]/, '*').should equal(a)
  333. a.should == "h*ll*"
  334. end
  335. it "taints self if replacement is tainted" do
  336. a = "hello"
  337. a.gsub!(/./.taint, "foo").tainted?.should == false
  338. a.gsub!(/./, "foo".taint).tainted?.should == true
  339. end
  340. ruby_version_is "1.9" do
  341. it "untrusts self if replacement is untrusted" do
  342. a = "hello"
  343. a.gsub!(/./.untrust, "foo").untrusted?.should == false
  344. a.gsub!(/./, "foo".untrust).untrusted?.should == true
  345. end
  346. end
  347. it "returns nil if no modifications were made" do
  348. a = "hello"
  349. a.gsub!(/z/, '*').should == nil
  350. a.gsub!(/z/, 'z').should == nil
  351. a.should == "hello"
  352. end
  353. ruby_version_is ""..."1.9" do
  354. it "raises a TypeError when self is frozen" do
  355. s = "hello"
  356. s.freeze
  357. s.gsub!(/ROAR/, "x") # ok
  358. lambda { s.gsub!(/e/, "e") }.should raise_error(TypeError)
  359. lambda { s.gsub!(/[aeiou]/, '*') }.should raise_error(TypeError)
  360. end
  361. end
  362. ruby_version_is "1.9" do
  363. ruby_bug "[ruby-core:23666]", "1.9.2" do
  364. it "raises a RuntimeError when self is frozen" do
  365. s = "hello"
  366. s.freeze
  367. lambda { s.gsub!(/ROAR/, "x") }.should raise_error(RuntimeError)
  368. lambda { s.gsub!(/e/, "e") }.should raise_error(RuntimeError)
  369. lambda { s.gsub!(/[aeiou]/, '*') }.should raise_error(RuntimeError)
  370. end
  371. end
  372. end
  373. end
  374. describe "String#gsub! with pattern and block" do
  375. it "modifies self in place and returns self" do
  376. a = "hello"
  377. a.gsub!(/[aeiou]/) { '*' }.should equal(a)
  378. a.should == "h*ll*"
  379. end
  380. it "taints self if block's result is tainted" do
  381. a = "hello"
  382. a.gsub!(/./.taint) { "foo" }.tainted?.should == false
  383. a.gsub!(/./) { "foo".taint }.tainted?.should == true
  384. end
  385. ruby_version_is "1.9" do
  386. it "untrusts self if block's result is untrusted" do
  387. a = "hello"
  388. a.gsub!(/./.untrust) { "foo" }.untrusted?.should == false
  389. a.gsub!(/./) { "foo".untrust }.untrusted?.should == true
  390. end
  391. end
  392. it "returns nil if no modifications were made" do
  393. a = "hello"
  394. a.gsub!(/z/) { '*' }.should == nil
  395. a.gsub!(/z/) { 'z' }.should == nil
  396. a.should == "hello"
  397. end
  398. ruby_bug "[ruby-core:23663]", "1.9" do
  399. it "raises a RuntimeError when self is frozen" do
  400. s = "hello"
  401. s.freeze
  402. lambda { s.gsub!(/ROAR/) { "x" } }.should raise_error(RuntimeError)
  403. lambda { s.gsub!(/e/) { "e" } }.should raise_error(RuntimeError)
  404. lambda { s.gsub!(/[aeiou]/) { '*' } }.should raise_error(RuntimeError)
  405. end
  406. end
  407. end