PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/test/ruby/test_literal.rb

http://github.com/ruby/ruby
Ruby | 603 lines | 560 code | 37 blank | 6 comment | 19 complexity | 02db6b4eebf431a7f1e41a2446dca3b7 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # -*- coding: us-ascii -*-
  2. # frozen_string_literal: false
  3. require 'test/unit'
  4. class TestRubyLiteral < Test::Unit::TestCase
  5. def test_special_const
  6. assert_equal 'true', true.inspect
  7. assert_instance_of TrueClass, true
  8. assert_equal 'false', false.inspect
  9. assert_instance_of FalseClass, false
  10. assert_equal 'nil', nil.inspect
  11. assert_instance_of NilClass, nil
  12. assert_equal ':sym', :sym.inspect
  13. assert_instance_of Symbol, :sym
  14. assert_equal '1234', 1234.inspect
  15. assert_instance_of Integer, 1234
  16. assert_equal '1234', 1_2_3_4.inspect
  17. assert_instance_of Integer, 1_2_3_4
  18. assert_equal '18', 0x12.inspect
  19. assert_instance_of Integer, 0x12
  20. assert_raise(SyntaxError) { eval("0x") }
  21. assert_equal '15', 0o17.inspect
  22. assert_instance_of Integer, 0o17
  23. assert_raise(SyntaxError) { eval("0o") }
  24. assert_equal '5', 0b101.inspect
  25. assert_instance_of Integer, 0b101
  26. assert_raise(SyntaxError) { eval("0b") }
  27. assert_equal '123456789012345678901234567890', 123456789012345678901234567890.inspect
  28. assert_instance_of Integer, 123456789012345678901234567890
  29. assert_instance_of Float, 1.3
  30. assert_equal '2', eval("0x00+2").inspect
  31. end
  32. def test_self
  33. assert_equal self, self
  34. assert_instance_of TestRubyLiteral, self
  35. assert_respond_to self, :test_self
  36. end
  37. def test_string
  38. assert_instance_of String, ?a
  39. assert_equal "a", ?a
  40. assert_instance_of String, ?A
  41. assert_equal "A", ?A
  42. assert_instance_of String, ?\n
  43. assert_equal "\n", ?\n
  44. assert_equal " ", ?\s
  45. assert_equal " ", ?\ # space
  46. assert_equal '', ''
  47. assert_equal 'string', 'string'
  48. assert_equal 'string string', 'string string'
  49. assert_equal ' ', ' '
  50. assert_equal ' ', " "
  51. assert_equal "\0", "\0"
  52. assert_equal "\1", "\1"
  53. assert_equal "3", "\x33"
  54. assert_equal "\n", "\n"
  55. bug2500 = '[ruby-core:27228]'
  56. bug5262 = '[ruby-core:39222]'
  57. %w[c C- M-].each do |pre|
  58. ["u", %w[u{ }]].each do |open, close|
  59. ["?", ['"', '"']].each do |qopen, qclose|
  60. str = "#{qopen}\\#{pre}\\#{open}5555#{close}#{qclose}"
  61. assert_raise(SyntaxError, "#{bug2500} eval(#{str})") {eval(str)}
  62. str = "#{qopen}\\#{pre}\\#{open}\u201c#{close}#{qclose}"
  63. assert_raise(SyntaxError, "#{bug5262} eval(#{str})") {eval(str)}
  64. str = "#{qopen}\\#{pre}\\#{open}\u201c#{close}#{qclose}".encode("euc-jp")
  65. assert_raise(SyntaxError, "#{bug5262} eval(#{str})") {eval(str)}
  66. str = "#{qopen}\\#{pre}\\#{open}\u201c#{close}#{qclose}".encode("iso-8859-13")
  67. assert_raise(SyntaxError, "#{bug5262} eval(#{str})") {eval(str)}
  68. str = "#{qopen}\\#{pre}\\#{open}\xe2\x7f#{close}#{qclose}".force_encoding("utf-8")
  69. assert_raise(SyntaxError, "#{bug5262} eval(#{str})") {eval(str)}
  70. end
  71. end
  72. end
  73. bug6069 = '[ruby-dev:45278]'
  74. assert_equal "\x13", "\c\x33"
  75. assert_equal "\x13", "\C-\x33"
  76. assert_equal "\xB3", "\M-\x33"
  77. assert_equal "\u201c", eval(%["\\\u{201c}"]), bug5262
  78. assert_equal "\u201c".encode("euc-jp"), eval(%["\\\u{201c}"].encode("euc-jp")), bug5262
  79. assert_equal "\u201c".encode("iso-8859-13"), eval(%["\\\u{201c}"].encode("iso-8859-13")), bug5262
  80. assert_equal "\\\u201c", eval(%['\\\u{201c}']), bug6069
  81. assert_equal "\\\u201c".encode("euc-jp"), eval(%['\\\u{201c}'].encode("euc-jp")), bug6069
  82. assert_equal "\\\u201c".encode("iso-8859-13"), eval(%['\\\u{201c}'].encode("iso-8859-13")), bug6069
  83. assert_equal "\u201c", eval(%[?\\\u{201c}]), bug6069
  84. assert_equal "\u201c".encode("euc-jp"), eval(%[?\\\u{201c}].encode("euc-jp")), bug6069
  85. assert_equal "\u201c".encode("iso-8859-13"), eval(%[?\\\u{201c}].encode("iso-8859-13")), bug6069
  86. assert_equal "ab", eval("?a 'b'")
  87. assert_equal "a\nb", eval("<<A 'b'\na\nA")
  88. end
  89. def test_dstring
  90. assert_equal '2', "#{1+1}"
  91. assert_equal '16', "#{2 ** 4}"
  92. s = "string"
  93. assert_equal s, "#{s}"
  94. a = 'Foo'
  95. b = "#{a}" << 'Bar'
  96. assert_equal('Foo', a, 'r3842')
  97. assert_equal('FooBar', b, 'r3842')
  98. end
  99. def test_dstring_encoding
  100. bug11519 = '[ruby-core:70703] [Bug #11519]'
  101. ['"foo#{}"', '"#{}foo"', '"#{}"'].each do |code|
  102. a = eval("#-*- coding: utf-8 -*-\n#{code}")
  103. assert_equal(Encoding::UTF_8, a.encoding,
  104. proc{"#{bug11519}: #{code}.encoding"})
  105. end
  106. end
  107. def test_dsymbol
  108. assert_equal :a3c, :"a#{1+2}c"
  109. end
  110. def test_dsymbol_redefined_intern
  111. assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}")
  112. begin;
  113. class String
  114. alias _intern intern
  115. def intern
  116. "<#{upcase}>"
  117. end
  118. end
  119. mesg = "literal symbol should not be affected by method redefinition"
  120. str = "foo"
  121. assert_equal(:foo, :"#{str}", mesg)
  122. end;
  123. end
  124. def test_xstring
  125. assert_equal "foo\n", `echo foo`
  126. s = 'foo'
  127. assert_equal "foo\n", `echo #{s}`
  128. end
  129. def test_frozen_string
  130. all_assertions do |a|
  131. a.for("false with indicator") do
  132. str = eval("# -*- frozen-string-literal: false -*-\n""'foo'")
  133. assert_not_predicate(str, :frozen?)
  134. end
  135. a.for("true with indicator") do
  136. str = eval("# -*- frozen-string-literal: true -*-\n""'foo'")
  137. assert_predicate(str, :frozen?)
  138. end
  139. a.for("false without indicator") do
  140. str = eval("# frozen-string-literal: false\n""'foo'")
  141. assert_not_predicate(str, :frozen?)
  142. end
  143. a.for("true without indicator") do
  144. str = eval("# frozen-string-literal: true\n""'foo'")
  145. assert_predicate(str, :frozen?)
  146. end
  147. a.for("false with preceding garbage") do
  148. str = eval("# x frozen-string-literal: false\n""'foo'")
  149. assert_not_predicate(str, :frozen?)
  150. end
  151. a.for("true with preceding garbage") do
  152. str = eval("# x frozen-string-literal: true\n""'foo'")
  153. assert_not_predicate(str, :frozen?)
  154. end
  155. a.for("false with succeeding garbage") do
  156. str = eval("# frozen-string-literal: false x\n""'foo'")
  157. assert_not_predicate(str, :frozen?)
  158. end
  159. a.for("true with succeeding garbage") do
  160. str = eval("# frozen-string-literal: true x\n""'foo'")
  161. assert_not_predicate(str, :frozen?)
  162. end
  163. end
  164. end
  165. def test_frozen_string_in_array_literal
  166. list = eval("# frozen-string-literal: true\n""['foo', 'bar']")
  167. assert_equal 2, list.length
  168. list.each { |str| assert_predicate str, :frozen? }
  169. end
  170. if defined?(RubyVM::InstructionSequence.compile_option) and
  171. RubyVM::InstructionSequence.compile_option.key?(:debug_frozen_string_literal)
  172. def test_debug_frozen_string
  173. src = 'n = 1; _="foo#{n ? "-#{n}" : ""}"'; f = "test.rb"; n = 1
  174. opt = {frozen_string_literal: true, debug_frozen_string_literal: true}
  175. str = RubyVM::InstructionSequence.compile(src, f, f, n, **opt).eval
  176. assert_equal("foo-1", str)
  177. assert_predicate(str, :frozen?)
  178. assert_raise_with_message(FrozenError, /created at #{Regexp.quote(f)}:#{n}/) {
  179. str << "x"
  180. }
  181. end
  182. def test_debug_frozen_string_in_array_literal
  183. src = '["foo"]'; f = "test.rb"; n = 1
  184. opt = {frozen_string_literal: true, debug_frozen_string_literal: true}
  185. ary = RubyVM::InstructionSequence.compile(src, f, f, n, **opt).eval
  186. assert_equal("foo", ary.first)
  187. assert_predicate(ary.first, :frozen?)
  188. assert_raise_with_message(FrozenError, /created at #{Regexp.quote(f)}:#{n}/) {
  189. ary.first << "x"
  190. } unless ENV['RUBY_ISEQ_DUMP_DEBUG']
  191. end
  192. end
  193. def test_regexp
  194. assert_instance_of Regexp, //
  195. assert_match(//, 'a')
  196. assert_match(//, '')
  197. assert_instance_of Regexp, /a/
  198. assert_match(/a/, 'a')
  199. assert_no_match(/test/, 'tes')
  200. re = /test/
  201. assert_match re, 'test'
  202. str = 'test'
  203. assert_match re, str
  204. assert_match(/test/, str)
  205. assert_equal 0, (/test/ =~ 'test')
  206. assert_equal 0, (re =~ 'test')
  207. assert_equal 0, (/test/ =~ str)
  208. assert_equal 0, (re =~ str)
  209. assert_equal 0, ('test' =~ /test/)
  210. assert_equal 0, ('test' =~ re)
  211. assert_equal 0, (str =~ /test/)
  212. assert_equal 0, (str =~ re)
  213. end
  214. def test_dregexp
  215. assert_instance_of Regexp, /re#{'ge'}xp/
  216. assert_equal(/regexp/, /re#{'ge'}xp/)
  217. bug3903 = '[ruby-core:32682]'
  218. assert_raise(SyntaxError, bug3903) {eval('/[#{"\x80"}]/')}
  219. end
  220. def test_array
  221. assert_instance_of Array, []
  222. assert_equal [], []
  223. assert_equal 0, [].size
  224. assert_instance_of Array, [0]
  225. assert_equal [3], [3]
  226. assert_equal 1, [3].size
  227. a = [3]
  228. assert_equal 3, a[0]
  229. assert_instance_of Array, [1,2]
  230. assert_equal [1,2], [1,2]
  231. assert_instance_of Array, [1,2,3,4,5]
  232. assert_equal [1,2,3,4,5], [1,2,3,4,5]
  233. assert_equal 5, [1,2,3,4,5].size
  234. a = [1,2]
  235. assert_equal 1, a[0]
  236. assert_equal 2, a[1]
  237. a = [1 + 2, 3 + 4, 5 + 6]
  238. assert_instance_of Array, a
  239. assert_equal [3, 7, 11], a
  240. assert_equal 7, a[1]
  241. assert_equal 1, ([0][0] += 1)
  242. assert_equal 1, ([2][0] -= 1)
  243. a = [obj = Object.new]
  244. assert_instance_of Array, a
  245. assert_equal 1, a.size
  246. assert_equal obj, a[0]
  247. a = [1,2,3]
  248. a[1] = 5
  249. assert_equal 5, a[1]
  250. end
  251. def test_hash
  252. assert_instance_of Hash, {}
  253. assert_equal({}, {})
  254. assert_instance_of Hash, {1 => 2}
  255. assert_equal({1 => 2}, {1 => 2})
  256. h = {1 => 2}
  257. assert_equal 2, h[1]
  258. h = {"string" => "literal", "goto" => "hell"}
  259. assert_equal h, h
  260. assert_equal 2, h.size
  261. assert_equal h, h
  262. assert_equal "literal", h["string"]
  263. end
  264. def test_hash_literal_frozen
  265. assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
  266. begin;
  267. def frozen_hash_literal_arg
  268. {0=>1,1=>4,2=>17}
  269. end
  270. ObjectSpace.each_object(Hash) do |a|
  271. if a.class == Hash and !a.default_proc and a.size == 3 &&
  272. a[0] == 1 && a[1] == 4 && a[2] == 17
  273. # should not be found.
  274. raise
  275. end
  276. end
  277. assert_not_include frozen_hash_literal_arg, 3
  278. end;
  279. end
  280. def test_big_array_and_hash_literal
  281. assert_normal_exit %q{GC.disable=true; x = nil; raise if eval("[#{(1..1_000_000).map{'x'}.join(", ")}]").size != 1_000_000}, "", timeout: 300, child_env: %[--disable-gems]
  282. assert_normal_exit %q{GC.disable=true; x = nil; raise if eval("[#{(1..1_000_000).to_a.join(", ")}]").size != 1_000_000}, "", timeout: 300, child_env: %[--disable-gems]
  283. assert_normal_exit %q{GC.disable=true; x = nil; raise if eval("{#{(1..1_000_000).map{|n| "#{n} => x"}.join(', ')}}").size != 1_000_000}, "", timeout: 300, child_env: %[--disable-gems]
  284. assert_normal_exit %q{GC.disable=true; x = nil; raise if eval("{#{(1..1_000_000).map{|n| "#{n} => #{n}"}.join(', ')}}").size != 1_000_000}, "", timeout: 300, child_env: %[--disable-gems]
  285. end
  286. def test_big_hash_literal
  287. bug7466 = '[ruby-dev:46658]'
  288. h = {
  289. 0xFE042 => 0xE5CD,
  290. 0xFE043 => 0xE5CD,
  291. 0xFE045 => 0xEA94,
  292. 0xFE046 => 0xE4E3,
  293. 0xFE047 => 0xE4E2,
  294. 0xFE048 => 0xEA96,
  295. 0xFE049 => 0x3013,
  296. 0xFE04A => 0xEB36,
  297. 0xFE04B => 0xEB37,
  298. 0xFE04C => 0xEB38,
  299. 0xFE04D => 0xEB49,
  300. 0xFE04E => 0xEB82,
  301. 0xFE04F => 0xE4D2,
  302. 0xFE050 => 0xEB35,
  303. 0xFE051 => 0xEAB9,
  304. 0xFE052 => 0xEABA,
  305. 0xFE053 => 0xE4D4,
  306. 0xFE054 => 0xE4CD,
  307. 0xFE055 => 0xEABB,
  308. 0xFE056 => 0xEABC,
  309. 0xFE057 => 0xEB32,
  310. 0xFE058 => 0xEB33,
  311. 0xFE059 => 0xEB34,
  312. 0xFE05A => 0xEB39,
  313. 0xFE05B => 0xEB5A,
  314. 0xFE190 => 0xE5A4,
  315. 0xFE191 => 0xE5A5,
  316. 0xFE192 => 0xEAD0,
  317. 0xFE193 => 0xEAD1,
  318. 0xFE194 => 0xEB47,
  319. 0xFE195 => 0xE509,
  320. 0xFE196 => 0xEAA0,
  321. 0xFE197 => 0xE50B,
  322. 0xFE198 => 0xEAA1,
  323. 0xFE199 => 0xEAA2,
  324. 0xFE19A => 0x3013,
  325. 0xFE19B => 0xE4FC,
  326. 0xFE19C => 0xE4FA,
  327. 0xFE19D => 0xE4FC,
  328. 0xFE19E => 0xE4FA,
  329. 0xFE19F => 0xE501,
  330. 0xFE1A0 => 0x3013,
  331. 0xFE1A1 => 0xE5DD,
  332. 0xFE1A2 => 0xEADB,
  333. 0xFE1A3 => 0xEAE9,
  334. 0xFE1A4 => 0xEB13,
  335. 0xFE1A5 => 0xEB14,
  336. 0xFE1A6 => 0xEB15,
  337. 0xFE1A7 => 0xEB16,
  338. 0xFE1A8 => 0xEB17,
  339. 0xFE1A9 => 0xEB18,
  340. 0xFE1AA => 0xEB19,
  341. 0xFE1AB => 0xEB1A,
  342. 0xFE1AC => 0xEB44,
  343. 0xFE1AD => 0xEB45,
  344. 0xFE1AE => 0xE4CB,
  345. 0xFE1AF => 0xE5BF,
  346. 0xFE1B0 => 0xE50E,
  347. 0xFE1B1 => 0xE4EC,
  348. 0xFE1B2 => 0xE4EF,
  349. 0xFE1B3 => 0xE4F8,
  350. 0xFE1B4 => 0x3013,
  351. 0xFE1B5 => 0x3013,
  352. 0xFE1B6 => 0xEB1C,
  353. 0xFE1B9 => 0xEB7E,
  354. 0xFE1D3 => 0xEB22,
  355. 0xFE7DC => 0xE4D8,
  356. 0xFE1D4 => 0xEB23,
  357. 0xFE1D5 => 0xEB24,
  358. 0xFE1D6 => 0xEB25,
  359. 0xFE1CC => 0xEB1F,
  360. 0xFE1CD => 0xEB20,
  361. 0xFE1CE => 0xE4D9,
  362. 0xFE1CF => 0xE48F,
  363. 0xFE1C5 => 0xE5C7,
  364. 0xFE1C6 => 0xEAEC,
  365. 0xFE1CB => 0xEB1E,
  366. 0xFE1DA => 0xE4DD,
  367. 0xFE1E1 => 0xEB57,
  368. 0xFE1E2 => 0xEB58,
  369. 0xFE1E3 => 0xE492,
  370. 0xFE1C9 => 0xEB1D,
  371. 0xFE1D9 => 0xE4D3,
  372. 0xFE1DC => 0xE5D4,
  373. 0xFE1BA => 0xE4E0,
  374. 0xFE1BB => 0xEB76,
  375. 0xFE1C8 => 0xE4E0,
  376. 0xFE1DD => 0xE5DB,
  377. 0xFE1BC => 0xE4DC,
  378. 0xFE1D8 => 0xE4DF,
  379. 0xFE1BD => 0xE49A,
  380. 0xFE1C7 => 0xEB1B,
  381. 0xFE1C2 => 0xE5C2,
  382. 0xFE1C0 => 0xE5C0,
  383. 0xFE1B8 => 0xE4DB,
  384. 0xFE1C3 => 0xE470,
  385. 0xFE1BE => 0xE4D8,
  386. 0xFE1C4 => 0xE4D9,
  387. 0xFE1B7 => 0xE4E1,
  388. 0xFE1BF => 0xE4DE,
  389. 0xFE1C1 => 0xE5C1,
  390. 0xFE1CA => 0x3013,
  391. 0xFE1D0 => 0xE4E1,
  392. 0xFE1D1 => 0xEB21,
  393. 0xFE1D2 => 0xE4D7,
  394. 0xFE1D7 => 0xE4DA,
  395. 0xFE1DB => 0xE4EE,
  396. 0xFE1DE => 0xEB3F,
  397. 0xFE1DF => 0xEB46,
  398. 0xFE1E0 => 0xEB48,
  399. 0xFE336 => 0xE4FB,
  400. 0xFE320 => 0xE472,
  401. 0xFE321 => 0xEB67,
  402. 0xFE322 => 0xEACA,
  403. 0xFE323 => 0xEAC0,
  404. 0xFE324 => 0xE5AE,
  405. 0xFE325 => 0xEACB,
  406. 0xFE326 => 0xEAC9,
  407. 0xFE327 => 0xE5C4,
  408. 0xFE328 => 0xEAC1,
  409. 0xFE329 => 0xE4E7,
  410. 0xFE32A => 0xE4E7,
  411. 0xFE32B => 0xEACD,
  412. 0xFE32C => 0xEACF,
  413. 0xFE32D => 0xEACE,
  414. 0xFE32E => 0xEAC7,
  415. 0xFE32F => 0xEAC8,
  416. 0xFE330 => 0xE471,
  417. 0xFE331 => "[Bug #7466]",
  418. }
  419. k = h.keys
  420. assert_equal([129, 0xFE331], [k.size, k.last], bug7466)
  421. code = [
  422. "h = {",
  423. (1..128).map {|i| "#{i} => 0,"},
  424. (129..140).map {|i| "#{i} => [],"},
  425. "}",
  426. ].join
  427. assert_separately([], <<-"end;")
  428. GC.stress = true
  429. #{code}
  430. GC.stress = false
  431. assert_equal(140, h.size)
  432. end;
  433. end
  434. def test_hash_duplicated_key
  435. h = EnvUtil.suppress_warning do
  436. eval <<~end
  437. # This is a syntax that renders warning at very early stage.
  438. # eval used to delay warning, to be suppressible by EnvUtil.
  439. {"a" => 100, "b" => 200, "a" => 300, "a" => 400}
  440. end
  441. end
  442. assert_equal(2, h.size)
  443. assert_equal(400, h['a'])
  444. assert_equal(200, h['b'])
  445. assert_nil(h['c'])
  446. assert_equal(nil, h.key('300'))
  447. end
  448. def test_hash_frozen_key_id
  449. key = "a".freeze
  450. h = {key => 100}
  451. assert_equal(100, h['a'])
  452. assert_same(key, *h.keys)
  453. end
  454. def test_hash_key_tampering
  455. key = "a"
  456. h = {key => 100}
  457. key.upcase!
  458. assert_equal(100, h['a'])
  459. end
  460. def test_range
  461. assert_instance_of Range, (1..2)
  462. assert_equal(1..2, 1..2)
  463. r = 1..2
  464. assert_equal 1, r.begin
  465. assert_equal 2, r.end
  466. assert_equal false, r.exclude_end?
  467. assert_instance_of Range, (1...3)
  468. assert_equal(1...3, 1...3)
  469. r = 1...3
  470. assert_equal 1, r.begin
  471. assert_equal 3, r.end
  472. assert_equal true, r.exclude_end?
  473. r = 1+2 .. 3+4
  474. assert_instance_of Range, r
  475. assert_equal 3, r.begin
  476. assert_equal 7, r.end
  477. assert_equal false, r.exclude_end?
  478. r = 1+2 ... 3+4
  479. assert_instance_of Range, r
  480. assert_equal 3, r.begin
  481. assert_equal 7, r.end
  482. assert_equal true, r.exclude_end?
  483. assert_instance_of Range, 'a'..'c'
  484. r = 'a'..'c'
  485. assert_equal 'a', r.begin
  486. assert_equal 'c', r.end
  487. end
  488. def test__FILE__
  489. assert_instance_of String, __FILE__
  490. assert_equal __FILE__, __FILE__
  491. assert_equal 'test_literal.rb', File.basename(__FILE__)
  492. end
  493. def test__LINE__
  494. assert_instance_of Integer, __LINE__
  495. assert_equal __LINE__, __LINE__
  496. end
  497. def test_integer
  498. head = ['', '0x', '0o', '0b', '0d', '-', '+']
  499. chars = ['0', '1', '_', '9', 'f']
  500. head.each {|h|
  501. 4.times {|len|
  502. a = [h]
  503. len.times { a = a.product(chars).map {|x| x.join('') } }
  504. a.each {|s|
  505. next if s.empty?
  506. begin
  507. r1 = Integer(s)
  508. rescue ArgumentError
  509. r1 = :err
  510. end
  511. begin
  512. r2 = eval(s)
  513. rescue NameError, SyntaxError
  514. r2 = :err
  515. end
  516. assert_equal(r1, r2, "Integer(#{s.inspect}) != eval(#{s.inspect})")
  517. }
  518. }
  519. }
  520. bug2407 = '[ruby-dev:39798]'
  521. head.grep_v(/^0/) do |s|
  522. head.grep(/^0/) do |h|
  523. h = "#{s}#{h}_"
  524. assert_syntax_error(h, /numeric literal without digits\Z/, "#{bug2407}: #{h.inspect}")
  525. end
  526. end
  527. end
  528. def test_float
  529. head = ['', '-', '+']
  530. chars = ['0', '1', '_', '9', 'f', '.']
  531. head.each {|h|
  532. 6.times {|len|
  533. a = [h]
  534. len.times { a = a.product(chars).map {|x| x.join('') } }
  535. a.each {|s|
  536. next if s.empty?
  537. next if /\.\z/ =~ s
  538. next if /\A[-+]?\./ =~ s
  539. next if /\A[-+]?0/ =~ s
  540. begin
  541. r1 = Float(s)
  542. rescue ArgumentError
  543. r1 = :err
  544. end
  545. begin
  546. r2 = eval(s)
  547. rescue NameError, SyntaxError
  548. r2 = :err
  549. end
  550. r2 = :err if Range === r2
  551. assert_equal(r1, r2, "Float(#{s.inspect}) != eval(#{s.inspect})")
  552. }
  553. }
  554. }
  555. assert_equal(100.0, 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100e100)
  556. end
  557. def test_symbol_list
  558. assert_equal([:foo, :bar], %i[foo bar])
  559. assert_equal([:"\"foo"], %i["foo])
  560. x = 10
  561. assert_equal([:foo, :b10], %I[foo b#{x}])
  562. assert_equal([:"\"foo10"], %I["foo#{x}])
  563. assert_ruby_status(["--disable-gems", "--dump=parsetree"], "%I[foo bar]")
  564. end
  565. end