PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/test/ripper/test_parser_events.rb

http://github.com/ruby/ruby
Ruby | 1548 lines | 1377 code | 167 blank | 4 comment | 6 complexity | f758e507ee15dd939394e6d0eda18976 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. # frozen_string_literal: true
  2. begin
  3. require_relative 'dummyparser'
  4. require 'test/unit'
  5. ripper_test = true
  6. module TestRipper; end
  7. rescue LoadError
  8. end
  9. class TestRipper::ParserEvents < Test::Unit::TestCase
  10. def test_event_coverage
  11. dispatched = Ripper::PARSER_EVENTS
  12. tested = self.class.instance_methods(false).grep(/\Atest_(\w+)/) {$1.intern}
  13. assert_empty dispatched-tested
  14. end
  15. def parse(str, nm = nil, &bl)
  16. dp = DummyParser.new(str)
  17. dp.hook(*nm, &bl) if nm
  18. dp.parse.to_s
  19. end
  20. def compile_error(str)
  21. parse(str, :compile_error) {|e, msg| return msg}
  22. end
  23. def warning(str)
  24. parse(str, :warning) {|e, *args| return args}
  25. end
  26. def warn(str)
  27. parse(str, :warn) {|e, *args| return args}
  28. end
  29. def test_program
  30. thru_program = false
  31. assert_equal '[void()]', parse('', :on_program) {thru_program = true}
  32. assert_equal true, thru_program
  33. end
  34. def test_stmts_new
  35. assert_equal '[void()]', parse('')
  36. end
  37. def test_stmts_add
  38. assert_equal '[ref(nil)]', parse('nil')
  39. assert_equal '[ref(nil),ref(nil)]', parse('nil;nil')
  40. assert_equal '[ref(nil),ref(nil),ref(nil)]', parse('nil;nil;nil')
  41. end
  42. def test_void_stmt
  43. assert_equal '[void()]', parse('')
  44. assert_equal '[void()]', parse('; ;')
  45. end
  46. def test_var_ref
  47. assert_equal '[assign(var_field(a),ref(a))]', parse('a=a')
  48. assert_equal '[ref(nil)]', parse('nil')
  49. assert_equal '[ref(true)]', parse('true')
  50. assert_equal '[vcall(_0)]', parse('_0')
  51. assert_equal '[vcall(_1)]', parse('_1')
  52. assert_include parse('proc{_1}'), '[ref(_1)]'
  53. end
  54. def test_vcall
  55. assert_equal '[vcall(a)]', parse('a')
  56. end
  57. def test_BEGIN
  58. assert_equal '[BEGIN([void()])]', parse('BEGIN{}')
  59. assert_equal '[BEGIN([ref(nil)])]', parse('BEGIN{nil}')
  60. end
  61. def test_END
  62. assert_equal '[END([void()])]', parse('END{}')
  63. assert_equal '[END([ref(nil)])]', parse('END{nil}')
  64. end
  65. def test_alias
  66. assert_equal '[alias(symbol_literal(a),symbol_literal(b))]', parse('alias a b')
  67. end
  68. def test_var_alias
  69. assert_equal '[valias($a,$g)]', parse('alias $a $g')
  70. end
  71. def test_alias_error
  72. assert_equal '[aliaserr(valias($a,$1))]', parse('alias $a $1')
  73. end
  74. def test_arglist
  75. assert_equal '[fcall(m,[])]', parse('m()')
  76. assert_equal '[fcall(m,[1])]', parse('m(1)')
  77. assert_equal '[fcall(m,[1,2])]', parse('m(1,2)')
  78. assert_equal '[fcall(m,[*vcall(r)])]', parse('m(*r)')
  79. assert_equal '[fcall(m,[1,*vcall(r)])]', parse('m(1,*r)')
  80. assert_equal '[fcall(m,[1,2,*vcall(r)])]', parse('m(1,2,*r)')
  81. assert_equal '[fcall(m,[&vcall(r)])]', parse('m(&r)')
  82. assert_equal '[fcall(m,[1,&vcall(r)])]', parse('m(1,&r)')
  83. assert_equal '[fcall(m,[1,2,&vcall(r)])]', parse('m(1,2,&r)')
  84. assert_equal '[fcall(m,[*vcall(a),&vcall(b)])]', parse('m(*a,&b)')
  85. assert_equal '[fcall(m,[1,*vcall(a),&vcall(b)])]', parse('m(1,*a,&b)')
  86. assert_equal '[fcall(m,[1,2,*vcall(a),&vcall(b)])]', parse('m(1,2,*a,&b)')
  87. end
  88. def test_args_add
  89. thru_args_add = false
  90. parse('m(a)', :on_args_add) {thru_args_add = true}
  91. assert_equal true, thru_args_add
  92. end
  93. def test_args_add_block
  94. thru_args_add_block = false
  95. parse('m(&b)', :on_args_add_block) {thru_args_add_block = true}
  96. assert_equal true, thru_args_add_block
  97. end
  98. def test_args_add_star
  99. thru_args_add_star = false
  100. parse('m(*a)', :on_args_add_star) {thru_args_add_star = true}
  101. assert_equal true, thru_args_add_star
  102. thru_args_add_star = false
  103. parse('m(*a, &b)', :on_args_add_star) {thru_args_add_star = true}
  104. assert_equal true, thru_args_add_star
  105. end
  106. def test_args_new
  107. thru_args_new = false
  108. parse('m()', :on_args_new) {thru_args_new = true}
  109. assert_equal true, thru_args_new
  110. end
  111. def test_args_forward
  112. thru_args_forward = false
  113. parse('def m(...) n(...) end', :on_args_forward) {thru_args_forward = true}
  114. assert_equal true, thru_args_forward
  115. end
  116. def test_arg_paren
  117. # FIXME
  118. end
  119. def test_aref
  120. assert_equal '[aref(vcall(v),[1])]', parse('v[1]')
  121. assert_equal '[aref(vcall(v),[1,2])]', parse('v[1,2]')
  122. end
  123. def test_assoclist_from_args
  124. thru_assoclist_from_args = false
  125. parse('{a=>b}', :on_assoclist_from_args) {thru_assoclist_from_args = true}
  126. assert_equal true, thru_assoclist_from_args
  127. end
  128. def test_assocs
  129. assert_equal '[fcall(m,[assocs(assoc(1,2))])]', parse('m(1=>2)')
  130. assert_equal '[fcall(m,[assocs(assoc(1,2),assoc(3,4))])]', parse('m(1=>2,3=>4)')
  131. assert_equal '[fcall(m,[3,assocs(assoc(1,2))])]', parse('m(3,1=>2)')
  132. end
  133. def test_assoc_new
  134. thru_assoc_new = false
  135. parse('{a=>b}', :on_assoc_new) {thru_assoc_new = true}
  136. assert_equal true, thru_assoc_new
  137. end
  138. def test_assoc_splat
  139. thru_assoc_splat = false
  140. parse('m(**h)', :on_assoc_splat) {thru_assoc_splat = true}
  141. assert_equal true, thru_assoc_splat
  142. end
  143. def test_aref_field
  144. assert_equal '[assign(aref_field(vcall(a),[1]),2)]', parse('a[1]=2')
  145. end
  146. def test_arg_ambiguous
  147. thru_arg_ambiguous = false
  148. parse('m //', :on_arg_ambiguous) {thru_arg_ambiguous = true}
  149. assert_equal true, thru_arg_ambiguous
  150. end
  151. def test_operator_ambiguous
  152. thru_operator_ambiguous = false
  153. token = syntax = nil
  154. parse('a=1; a %[]', :on_operator_ambiguous) {|*a|
  155. thru_operator_ambiguous = true
  156. _, token, syntax = *a
  157. }
  158. assert_equal true, thru_operator_ambiguous
  159. assert_equal :%, token
  160. assert_equal "string literal", syntax
  161. end
  162. def test_array # array literal
  163. assert_equal '[array([1,2,3])]', parse('[1,2,3]')
  164. assert_equal '[array([abc,def])]', parse('%w[abc def]')
  165. assert_equal '[array([abc,def])]', parse('%W[abc def]')
  166. end
  167. def test_assign # generic assignment
  168. assert_equal '[assign(var_field(v),1)]', parse('v=1')
  169. end
  170. def test_assign_error
  171. thru_assign_error = false
  172. result = parse('self = 1', :on_assign_error) {thru_assign_error = true}
  173. assert_equal true, thru_assign_error
  174. assert_equal '[assign(assign_error(var_field(self)),1)]', result
  175. end
  176. def test_assign_error_backref
  177. thru_assign_error = false
  178. result =
  179. parse('$` = 1', :on_assign_error) {thru_assign_error = true}
  180. assert_equal true, thru_assign_error
  181. assert_equal '[assign(assign_error(var_field($`)),1)]', result
  182. thru_assign_error = false
  183. result =
  184. parse('$`, _ = 1', :on_assign_error) {thru_assign_error = true}
  185. assert_equal true, thru_assign_error
  186. assert_equal '[massign([assign_error(var_field($`)),var_field(_)],1)]', result
  187. end
  188. def test_assign_error_const_qualified
  189. thru_assign_error = false
  190. result =
  191. parse('self::X = 1', :on_assign_error) {thru_assign_error = true}
  192. assert_equal false, thru_assign_error
  193. assert_equal "[assign(const_path_field(ref(self),X),1)]", result
  194. thru_assign_error = false
  195. result =
  196. parse("def m\n self::X = 1\nend", :on_assign_error) {thru_assign_error = true}
  197. assert_equal true, thru_assign_error
  198. assert_include result, "assign_error(const_path_field(ref(self),X))"
  199. thru_assign_error = false
  200. result =
  201. parse("def m\n self::X, a = 1, 2\nend", :on_assign_error) {thru_assign_error = true}
  202. assert_equal true, thru_assign_error
  203. assert_include result, "assign_error(const_path_field(ref(self),X))"
  204. end
  205. def test_assign_error_const
  206. thru_assign_error = false
  207. result = parse('X = 1', :on_assign_error) {thru_assign_error = true}
  208. assert_equal false, thru_assign_error
  209. assert_equal "[assign(var_field(X),1)]", result
  210. thru_assign_error = false
  211. result = parse('X, a = 1, 2', :on_assign_error) {thru_assign_error = true}
  212. assert_equal false, thru_assign_error
  213. assert_include result, "massign([var_field(X),var_field(a)],"
  214. result = parse("def m\n X = 1\nend", :on_assign_error) {thru_assign_error = true}
  215. assert_equal true, thru_assign_error
  216. assert_include result, "assign_error(var_field(X))"
  217. thru_assign_error = false
  218. result = parse("def m\n X, a = 1, 2\nend", :on_assign_error) {thru_assign_error = true}
  219. assert_equal true, thru_assign_error
  220. assert_include result, "assign_error(var_field(X))"
  221. end
  222. def test_assign_error_const_toplevel
  223. thru_assign_error = false
  224. parse('::X = 1', :on_assign_error) {thru_assign_error = true}
  225. assert_equal false, thru_assign_error
  226. parse("def m\n ::X = 1\nend", :on_assign_error) {thru_assign_error = true}
  227. assert_equal true, thru_assign_error
  228. thru_assign_error = false
  229. parse("def m\n ::X, a = 1, 2\nend", :on_assign_error) {thru_assign_error = true}
  230. assert_equal true, thru_assign_error
  231. end
  232. def test_bare_assoc_hash
  233. thru_bare_assoc_hash = false
  234. parse('x[a=>b]', :on_bare_assoc_hash) {thru_bare_assoc_hash = true}
  235. assert_equal true, thru_bare_assoc_hash
  236. thru_bare_assoc_hash = false
  237. parse('x[1, a=>b]', :on_bare_assoc_hash) {thru_bare_assoc_hash = true}
  238. assert_equal true, thru_bare_assoc_hash
  239. thru_bare_assoc_hash = false
  240. parse('x(a=>b)', :on_bare_assoc_hash) {thru_bare_assoc_hash = true}
  241. assert_equal true, thru_bare_assoc_hash
  242. thru_bare_assoc_hash = false
  243. parse('x(1, a=>b)', :on_bare_assoc_hash) {thru_bare_assoc_hash = true}
  244. assert_equal true, thru_bare_assoc_hash
  245. end
  246. def test_begin
  247. thru_begin = false
  248. parse('begin end', :on_begin) {thru_begin = true}
  249. assert_equal true, thru_begin
  250. end
  251. %w"and or + - * / % ** | ^ & <=> > >= < <= == === != =~ !~ << >> && ||".each do |op|
  252. define_method("test_binary(#{op})") do
  253. thru_binary = false
  254. parse("a #{op} b", :on_binary) {thru_binary = true}
  255. assert_equal true, thru_binary
  256. end
  257. end
  258. def test_blockarg
  259. thru_blockarg = false
  260. parse("def a(&b) end", :on_blockarg) {thru_blockarg = true}
  261. assert_equal true, thru_blockarg
  262. thru_blockarg = false
  263. parse("def a(x, &b) end", :on_blockarg) {thru_blockarg = true}
  264. assert_equal true, thru_blockarg
  265. thru_blockarg = false
  266. parse("proc{|&b|}", :on_blockarg) {thru_blockarg = true}
  267. assert_equal true, thru_blockarg
  268. thru_blockarg = false
  269. parse("proc{|x, &b|}", :on_blockarg) {thru_blockarg = true}
  270. assert_equal true, thru_blockarg
  271. thru_blockarg = false
  272. parse("proc{|&b;y|}", :on_blockarg) {thru_blockarg = true}
  273. assert_equal true, thru_blockarg
  274. thru_blockarg = false
  275. parse("proc{|&b,x;y|}", :on_blockarg) {thru_blockarg = true}
  276. assert_equal true, thru_blockarg
  277. thru_blockarg = false
  278. parse("proc do |&b| end", :on_blockarg) {thru_blockarg = true}
  279. assert_equal true, thru_blockarg
  280. thru_blockarg = false
  281. parse("proc do |&b, x| end", :on_blockarg) {thru_blockarg = true}
  282. assert_equal true, thru_blockarg
  283. thru_blockarg = false
  284. parse("proc do |&b;y| end", :on_blockarg) {thru_blockarg = true}
  285. assert_equal true, thru_blockarg
  286. thru_blockarg = false
  287. parse("proc do |&b, x;y| end", :on_blockarg) {thru_blockarg = true}
  288. assert_equal true, thru_blockarg
  289. end
  290. def test_block_var
  291. thru_block_var = false
  292. parse("proc{||}", :on_block_var) {thru_block_var = true}
  293. assert_equal true, thru_block_var
  294. thru_block_var = false
  295. parse("proc{| |}", :on_block_var) {thru_block_var = true}
  296. assert_equal true, thru_block_var
  297. thru_block_var = false
  298. parse("proc{|x|}", :on_block_var) {thru_block_var = true}
  299. assert_equal true, thru_block_var
  300. thru_block_var = false
  301. parse("proc{|;y|}", :on_block_var) {thru_block_var = true}
  302. assert_equal true, thru_block_var
  303. thru_block_var = false
  304. parse("proc{|x;y|}", :on_block_var) {thru_block_var = true}
  305. assert_equal true, thru_block_var
  306. thru_block_var = false
  307. parse("proc do || end", :on_block_var) {thru_block_var = true}
  308. assert_equal true, thru_block_var
  309. thru_block_var = false
  310. parse("proc do | | end", :on_block_var) {thru_block_var = true}
  311. assert_equal true, thru_block_var
  312. thru_block_var = false
  313. parse("proc do |x| end", :on_block_var) {thru_block_var = true}
  314. assert_equal true, thru_block_var
  315. thru_block_var = false
  316. parse("proc do |;y| end", :on_block_var) {thru_block_var = true}
  317. assert_equal true, thru_block_var
  318. thru_block_var = false
  319. parse("proc do |x;y| end", :on_block_var) {thru_block_var = true}
  320. assert_equal true, thru_block_var
  321. end
  322. def test_block_var_add_block
  323. # not used
  324. end
  325. def test_block_var_add_star
  326. # not used
  327. end
  328. def test_bodystmt
  329. thru_bodystmt = false
  330. parse("class X\nend", :on_bodystmt) {thru_bodystmt = true}
  331. assert_equal true, thru_bodystmt
  332. end
  333. def test_call
  334. bug2233 = '[ruby-core:26165]'
  335. tree = nil
  336. thru_call = false
  337. assert_nothing_raised {
  338. tree = parse("self.foo", :on_call) {thru_call = true}
  339. }
  340. assert_equal true, thru_call
  341. assert_equal "[call(ref(self),.,foo)]", tree
  342. thru_call = false
  343. assert_nothing_raised {
  344. tree = parse("self.foo()", :on_call) {thru_call = true}
  345. }
  346. assert_equal true, thru_call
  347. assert_equal "[call(ref(self),.,foo,[])]", tree
  348. thru_call = false
  349. assert_nothing_raised(bug2233) {
  350. tree = parse("foo.()", :on_call) {thru_call = true}
  351. }
  352. assert_equal true, thru_call
  353. assert_equal "[call(vcall(foo),.,call,[])]", tree
  354. thru_call = false
  355. assert_nothing_raised {
  356. tree = parse("self::foo", :on_call) {thru_call = true}
  357. }
  358. assert_equal true, thru_call
  359. assert_equal "[call(ref(self),::,foo)]", tree
  360. thru_call = false
  361. assert_nothing_raised {
  362. tree = parse("self::foo()", :on_call) {thru_call = true}
  363. }
  364. assert_equal true, thru_call
  365. assert_equal "[call(ref(self),::,foo,[])]", tree
  366. thru_call = false
  367. assert_nothing_raised(bug2233) {
  368. tree = parse("foo::()", :on_call) {thru_call = true}
  369. }
  370. assert_equal true, thru_call
  371. assert_equal "[call(vcall(foo),::,call,[])]", tree
  372. thru_call = false
  373. tree = parse("self&.foo", :on_call) {thru_call = true}
  374. assert_equal true, thru_call
  375. assert_equal "[call(ref(self),&.,foo)]", tree
  376. thru_call = false
  377. tree = parse("self&.foo()", :on_call) {thru_call = true}
  378. assert_equal true, thru_call
  379. assert_equal "[call(ref(self),&.,foo,[])]", tree
  380. end
  381. def test_excessed_comma
  382. thru_excessed_comma = false
  383. parse("proc{|x,|}", :on_excessed_comma) {thru_excessed_comma = true}
  384. assert_equal true, thru_excessed_comma
  385. thru_excessed_comma = false
  386. parse("proc{|x,y,|}", :on_excessed_comma) {thru_excessed_comma = true}
  387. assert_equal true, thru_excessed_comma
  388. thru_excessed_comma = false
  389. parse("proc do |x,| end", :on_excessed_comma) {thru_excessed_comma = true}
  390. assert_equal true, thru_excessed_comma
  391. thru_excessed_comma = false
  392. parse("proc do |x,y,| end", :on_excessed_comma) {thru_excessed_comma = true}
  393. assert_equal true, thru_excessed_comma
  394. end
  395. def test_heredoc
  396. bug1921 = '[ruby-core:24855]'
  397. thru_heredoc_beg = false
  398. tree = parse("<""<EOS\nheredoc\nEOS\n", :on_heredoc_beg) {thru_heredoc_beg = true}
  399. assert_equal true, thru_heredoc_beg
  400. assert_match(/string_content\(\),heredoc\n/, tree, bug1921)
  401. heredoc = nil
  402. parse("<""<EOS\nheredoc1\nheredoc2\nEOS\n", :on_string_add) {|e, n, s| heredoc = s}
  403. assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921)
  404. heredoc = nil
  405. parse("<""<-EOS\nheredoc1\nheredoc2\n\tEOS\n", :on_string_add) {|e, n, s| heredoc = s}
  406. assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921)
  407. end
  408. def test_heredoc_dedent
  409. thru_heredoc_dedent = false
  410. str = width = nil
  411. tree = parse("<""<~EOS\n heredoc\nEOS\n", :on_heredoc_dedent) {|e, s, w|
  412. thru_heredoc_dedent = true
  413. str = s
  414. width = w
  415. }
  416. assert_equal true, thru_heredoc_dedent
  417. assert_match(/string_content\(\), heredoc\n/, tree)
  418. assert_equal(" heredoc\n", str.children[1])
  419. assert_equal(1, width)
  420. end
  421. def test_unterminated_heredoc
  422. assert_match("can't find string \"a\" anywhere before EOF", compile_error("<<a"))
  423. assert_match("can't find string \"a\" anywhere before EOF", compile_error('<<"a"'))
  424. assert_match("can't find string \"a\" anywhere before EOF", compile_error("<<'a'"))
  425. msg = nil
  426. parse('<<"', :on_parse_error) {|_, e| msg = e}
  427. assert_equal("unterminated here document identifier", msg)
  428. end
  429. def test_massign
  430. thru_massign = false
  431. parse("a, b = 1, 2", :on_massign) {thru_massign = true}
  432. assert_equal true, thru_massign
  433. end
  434. def test_mlhs_add
  435. thru_mlhs_add = false
  436. parse("a, b = 1, 2", :on_mlhs_add) {thru_mlhs_add = true}
  437. assert_equal true, thru_mlhs_add
  438. end
  439. def test_mlhs_add_star
  440. bug2232 = '[ruby-core:26163]'
  441. bug4364 = '[ruby-core:35078]'
  442. thru_mlhs_add_star = false
  443. tree = parse("a, *b = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  444. assert_equal true, thru_mlhs_add_star
  445. assert_include(tree, "massign([var_field(a),*var_field(b)]")
  446. thru_mlhs_add_star = false
  447. tree = parse("a, *b, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  448. assert_equal true, thru_mlhs_add_star
  449. assert_include(tree, "massign([var_field(a),*var_field(b),var_field(c)]", bug2232)
  450. thru_mlhs_add_star = false
  451. tree = parse("a, *, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  452. assert_equal true, thru_mlhs_add_star
  453. assert_include(tree, "massign([var_field(a),*,var_field(c)]", bug4364)
  454. thru_mlhs_add_star = false
  455. tree = parse("*b, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  456. assert_equal true, thru_mlhs_add_star
  457. assert_include(tree, "massign([*var_field(b),var_field(c)]", bug4364)
  458. thru_mlhs_add_star = false
  459. tree = parse("*, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  460. assert_equal true, thru_mlhs_add_star
  461. assert_include(tree, "massign([*,var_field(c)],", bug4364)
  462. end
  463. def test_mlhs_add_post
  464. thru_mlhs_add_post = false
  465. tree = parse("a, *b = 1, 2", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  466. assert_equal false, thru_mlhs_add_post
  467. assert_include(tree, "massign([var_field(a),*var_field(b)],")
  468. thru_mlhs_add_post = false
  469. tree = parse("a, *b, c = 1, 2", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  470. assert_equal true, thru_mlhs_add_post
  471. assert_include(tree, "massign([var_field(a),*var_field(b),var_field(c)],")
  472. thru_mlhs_add_post = false
  473. tree = parse("a, *, c = 1, 2", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  474. assert_equal true, thru_mlhs_add_post
  475. assert_include(tree, "massign([var_field(a),*,var_field(c)],")
  476. thru_mlhs_add_post = false
  477. tree = parse("*b, c = 1, 2", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  478. assert_equal true, thru_mlhs_add_post
  479. assert_include(tree, "massign([*var_field(b),var_field(c)],")
  480. thru_mlhs_add_post = false
  481. tree = parse("*, c = 1, 2", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  482. assert_equal true, thru_mlhs_add_post
  483. assert_include(tree, "massign([*,var_field(c)],")
  484. end
  485. def test_mlhs_new
  486. thru_mlhs_new = false
  487. parse("a, b = 1, 2", :on_mlhs_new) {thru_mlhs_new = true}
  488. assert_equal true, thru_mlhs_new
  489. end
  490. def test_mlhs_paren
  491. thru_mlhs_paren = false
  492. parse("a, b = 1, 2", :on_mlhs_paren) {thru_mlhs_paren = true}
  493. assert_equal false, thru_mlhs_paren
  494. thru_mlhs_paren = false
  495. parse("(a, b) = 1, 2", :on_mlhs_paren) {thru_mlhs_paren = true}
  496. assert_equal true, thru_mlhs_paren
  497. end
  498. def test_brace_block
  499. thru_brace_block = false
  500. parse('proc {}', :on_brace_block) {thru_brace_block = true}
  501. assert_equal true, thru_brace_block
  502. end
  503. def test_break
  504. thru_break = false
  505. parse('proc {break}', :on_break) {thru_break = true}
  506. assert_equal true, thru_break
  507. end
  508. def test_case
  509. thru_case = false
  510. parse('case foo when true; end', :on_case) {thru_case = true}
  511. assert_equal true, thru_case
  512. end
  513. def test_class
  514. thru_class = false
  515. parse('class Foo; end', :on_class) {thru_class = true}
  516. assert_equal true, thru_class
  517. end
  518. def test_class_name_error
  519. thru_class_name_error = false
  520. parse('class foo; end', :on_class_name_error) {thru_class_name_error = true}
  521. assert_equal true, thru_class_name_error
  522. end
  523. def test_command
  524. thru_command = false
  525. parse('foo a b', :on_command) {thru_command = true}
  526. assert_equal true, thru_command
  527. end
  528. def test_command_call
  529. thru_command_call = false
  530. parse('foo.bar a, b', :on_command_call) {thru_command_call = true}
  531. assert_equal true, thru_command_call
  532. end
  533. def test_const_ref
  534. thru_const_ref = false
  535. parse('class A;end', :on_const_ref) {thru_const_ref = true}
  536. assert_equal true, thru_const_ref
  537. thru_const_ref = false
  538. parse('module A;end', :on_const_ref) {thru_const_ref = true}
  539. assert_equal true, thru_const_ref
  540. end
  541. def test_const_path_field
  542. thru_const_path_field = false
  543. parse('foo::X = 1', :on_const_path_field) {thru_const_path_field = true}
  544. assert_equal true, thru_const_path_field
  545. end
  546. def test_const_path_ref
  547. thru_const_path_ref = false
  548. parse('foo::X', :on_const_path_ref) {thru_const_path_ref = true}
  549. assert_equal true, thru_const_path_ref
  550. end
  551. def test_def
  552. thru_def = false
  553. parse('def foo; end', :on_def) {
  554. thru_def = true
  555. }
  556. assert_equal true, thru_def
  557. assert_equal '[def(foo,[],bodystmt([void()]))]', parse('def foo ;end')
  558. end
  559. def test_defined
  560. thru_defined = false
  561. parse('defined?(x)', :on_defined) {thru_defined = true}
  562. assert_equal true, thru_defined
  563. end
  564. def test_defs
  565. thru_defs = false
  566. tree = parse('def foo.bar; end', :on_defs) {thru_defs = true}
  567. assert_equal true, thru_defs
  568. assert_equal("[defs(vcall(foo),.,bar,[],bodystmt([void()]))]", tree)
  569. thru_parse_error = false
  570. tree = parse('def foo&.bar; end', :on_parse_error) {thru_parse_error = true}
  571. assert_equal(true, thru_parse_error)
  572. end
  573. def test_do_block
  574. thru_do_block = false
  575. parse('proc do end', :on_do_block) {thru_do_block = true}
  576. assert_equal true, thru_do_block
  577. end
  578. def test_dot2
  579. thru_dot2 = false
  580. parse('a..b', :on_dot2) {thru_dot2 = true}
  581. assert_equal true, thru_dot2
  582. end
  583. def test_dot3
  584. thru_dot3 = false
  585. parse('a...b', :on_dot3) {thru_dot3 = true}
  586. assert_equal true, thru_dot3
  587. end
  588. def test_dyna_symbol
  589. thru_dyna_symbol = false
  590. parse(':"#{foo}"', :on_dyna_symbol) {thru_dyna_symbol = true}
  591. assert_equal true, thru_dyna_symbol
  592. thru_dyna_symbol = false
  593. parse('{"#{foo}": 1}', :on_dyna_symbol) {thru_dyna_symbol = true}
  594. assert_equal true, thru_dyna_symbol
  595. end
  596. def test_else
  597. thru_else = false
  598. parse('if foo; bar else zot end', :on_else) {thru_else = true}
  599. assert_equal true, thru_else
  600. end
  601. def test_elsif
  602. thru_elsif = false
  603. parse('if foo; bar elsif qux; zot end', :on_elsif) {thru_elsif = true}
  604. assert_equal true, thru_elsif
  605. end
  606. def test_ensure
  607. thru_ensure = false
  608. parse('begin foo ensure bar end', :on_ensure) {thru_ensure = true}
  609. assert_equal true, thru_ensure
  610. end
  611. def test_fcall
  612. thru_fcall = false
  613. parse('foo()', :on_fcall) {thru_fcall = true}
  614. assert_equal true, thru_fcall
  615. end
  616. def test_field
  617. thru_field = false
  618. parse('foo.x = 1', :on_field) {thru_field = true}
  619. assert_equal true, thru_field
  620. end
  621. def test_for
  622. thru_for = false
  623. parse('for i in foo; end', :on_for) {thru_for = true}
  624. assert_equal true, thru_for
  625. end
  626. def test_hash
  627. thru_hash = false
  628. parse('{1=>2}', :on_hash) {thru_hash = true}
  629. assert_equal true, thru_hash
  630. thru_hash = false
  631. parse('{a: 2}', :on_hash) {thru_hash = true}
  632. assert_equal true, thru_hash
  633. end
  634. def test_if
  635. thru_if = false
  636. parse('if false; end', :on_if) {thru_if = true}
  637. assert_equal true, thru_if
  638. end
  639. def test_if_mod
  640. thru_if_mod = false
  641. parse('nil if nil', :on_if_mod) {thru_if_mod = true}
  642. assert_equal true, thru_if_mod
  643. end
  644. def test_ifop
  645. thru_ifop = false
  646. parse('a ? b : c', :on_ifop) {thru_ifop = true}
  647. assert_equal true, thru_ifop
  648. end
  649. def test_ignored_nl
  650. ignored_nl = []
  651. parse("foo # comment\n...\n", :on_ignored_nl) {|_, a| ignored_nl << a}
  652. assert_equal ["\n"], ignored_nl
  653. end
  654. def test_lambda
  655. thru_lambda = false
  656. parse('->{}', :on_lambda) {thru_lambda = true}
  657. assert_equal true, thru_lambda
  658. end
  659. def test_magic_comment
  660. thru_magic_comment = false
  661. parse('# -*- bug-5753: ruby-dev:44984 -*-', :on_magic_comment) {|*x|thru_magic_comment = x}
  662. assert_equal [:on_magic_comment, "bug_5753", "ruby-dev:44984"], thru_magic_comment
  663. end
  664. def test_method_add_block
  665. thru_method_add_block = false
  666. parse('a {}', :on_method_add_block) {thru_method_add_block = true}
  667. assert_equal true, thru_method_add_block
  668. thru_method_add_block = false
  669. parse('a do end', :on_method_add_block) {thru_method_add_block = true}
  670. assert_equal true, thru_method_add_block
  671. end
  672. def test_method_add_arg
  673. thru_method_add_arg = false
  674. parse('a()', :on_method_add_arg) {thru_method_add_arg = true}
  675. assert_equal true, thru_method_add_arg
  676. thru_method_add_arg = false
  677. parse('a {}', :on_method_add_arg) {thru_method_add_arg = true}
  678. assert_equal true, thru_method_add_arg
  679. thru_method_add_arg = false
  680. parse('a.b(1)', :on_method_add_arg) {thru_method_add_arg = true}
  681. assert_equal true, thru_method_add_arg
  682. thru_method_add_arg = false
  683. parse('a::b(1)', :on_method_add_arg) {thru_method_add_arg = true}
  684. assert_equal true, thru_method_add_arg
  685. end
  686. def test_module
  687. thru_module = false
  688. parse('module A; end', :on_module) {thru_module = true}
  689. assert_equal true, thru_module
  690. end
  691. def test_mrhs_add
  692. thru_mrhs_add = false
  693. parse('a = a, b', :on_mrhs_add) {thru_mrhs_add = true}
  694. assert_equal true, thru_mrhs_add
  695. end
  696. def test_mrhs_add_star
  697. thru_mrhs_add_star = false
  698. parse('a = a, *b', :on_mrhs_add_star) {thru_mrhs_add_star = true}
  699. assert_equal true, thru_mrhs_add_star
  700. end
  701. def test_mrhs_new
  702. thru_mrhs_new = false
  703. parse('a = *a', :on_mrhs_new) {thru_mrhs_new = true}
  704. assert_equal true, thru_mrhs_new
  705. end
  706. def test_mrhs_new_from_args
  707. thru_mrhs_new_from_args = false
  708. parse('a = a, b', :on_mrhs_new_from_args) {thru_mrhs_new_from_args = true}
  709. assert_equal true, thru_mrhs_new_from_args
  710. end
  711. def test_next
  712. thru_next = false
  713. parse('a {next}', :on_next) {thru_next = true}
  714. assert_equal true, thru_next
  715. end
  716. def test_opassign
  717. thru_opassign = false
  718. tree = parse('a += b', :on_opassign) {thru_opassign = true}
  719. assert_equal true, thru_opassign
  720. assert_equal "[opassign(var_field(a),+=,vcall(b))]", tree
  721. thru_opassign = false
  722. tree = parse('a -= b', :on_opassign) {thru_opassign = true}
  723. assert_equal true, thru_opassign
  724. assert_equal "[opassign(var_field(a),-=,vcall(b))]", tree
  725. thru_opassign = false
  726. tree = parse('a *= b', :on_opassign) {thru_opassign = true}
  727. assert_equal true, thru_opassign
  728. assert_equal "[opassign(var_field(a),*=,vcall(b))]", tree
  729. thru_opassign = false
  730. tree = parse('a /= b', :on_opassign) {thru_opassign = true}
  731. assert_equal true, thru_opassign
  732. assert_equal "[opassign(var_field(a),/=,vcall(b))]", tree
  733. thru_opassign = false
  734. tree = parse('a %= b', :on_opassign) {thru_opassign = true}
  735. assert_equal true, thru_opassign
  736. assert_equal "[opassign(var_field(a),%=,vcall(b))]", tree
  737. thru_opassign = false
  738. tree = parse('a **= b', :on_opassign) {thru_opassign = true}
  739. assert_equal true, thru_opassign
  740. assert_equal "[opassign(var_field(a),**=,vcall(b))]", tree
  741. thru_opassign = false
  742. tree = parse('a &= b', :on_opassign) {thru_opassign = true}
  743. assert_equal true, thru_opassign
  744. assert_equal "[opassign(var_field(a),&=,vcall(b))]", tree
  745. thru_opassign = false
  746. tree = parse('a |= b', :on_opassign) {thru_opassign = true}
  747. assert_equal "[opassign(var_field(a),|=,vcall(b))]", tree
  748. assert_equal true, thru_opassign
  749. thru_opassign = false
  750. tree = parse('a <<= b', :on_opassign) {thru_opassign = true}
  751. assert_equal true, thru_opassign
  752. assert_equal "[opassign(var_field(a),<<=,vcall(b))]", tree
  753. thru_opassign = false
  754. tree = parse('a >>= b', :on_opassign) {thru_opassign = true}
  755. assert_equal true, thru_opassign
  756. assert_equal "[opassign(var_field(a),>>=,vcall(b))]", tree
  757. thru_opassign = false
  758. tree = parse('a &&= b', :on_opassign) {thru_opassign = true}
  759. assert_equal true, thru_opassign
  760. assert_equal "[opassign(var_field(a),&&=,vcall(b))]", tree
  761. thru_opassign = false
  762. tree = parse('a ||= b', :on_opassign) {thru_opassign = true}
  763. assert_equal true, thru_opassign
  764. assert_equal "[opassign(var_field(a),||=,vcall(b))]", tree
  765. thru_opassign = false
  766. tree = parse('a::X ||= c 1', :on_opassign) {thru_opassign = true}
  767. assert_equal true, thru_opassign
  768. assert_equal "[opassign(const_path_field(vcall(a),X),||=,command(c,[1]))]", tree
  769. thru_opassign = false
  770. tree = parse("self.foo += 1", :on_opassign) {thru_opassign = true}
  771. assert_equal true, thru_opassign
  772. assert_equal "[opassign(field(ref(self),.,foo),+=,1)]", tree
  773. thru_opassign = false
  774. tree = parse("self&.foo += 1", :on_opassign) {thru_opassign = true}
  775. assert_equal true, thru_opassign
  776. assert_equal "[opassign(field(ref(self),&.,foo),+=,1)]", tree
  777. end
  778. def test_opassign_error
  779. thru_opassign = []
  780. events = [:on_opassign]
  781. parse('$~ ||= 1', events) {|a,*b|
  782. thru_opassign << a
  783. }
  784. assert_equal events, thru_opassign
  785. end
  786. def test_param_error
  787. thru_param_error = false
  788. parse('def foo(A) end', :on_param_error) {thru_param_error = true}
  789. assert_equal true, thru_param_error
  790. thru_param_error = false
  791. parse('def foo($a) end', :on_param_error) {thru_param_error = true}
  792. assert_equal true, thru_param_error
  793. thru_param_error = false
  794. parse('def foo(@a) end', :on_param_error) {thru_param_error = true}
  795. assert_equal true, thru_param_error
  796. thru_param_error = false
  797. parse('def foo(@@a) end', :on_param_error) {thru_param_error = true}
  798. assert_equal true, thru_param_error
  799. end
  800. def test_params
  801. arg = nil
  802. thru_params = false
  803. parse('a {||}', :on_params) {|_, *v| thru_params = true; arg = v}
  804. assert_equal true, thru_params
  805. assert_equal [nil, nil, nil, nil, nil, nil, nil], arg
  806. thru_params = false
  807. parse('a {|x|}', :on_params) {|_, *v| thru_params = true; arg = v}
  808. assert_equal true, thru_params
  809. assert_equal [["x"], nil, nil, nil, nil, nil, nil], arg
  810. thru_params = false
  811. parse('a {|*x|}', :on_params) {|_, *v| thru_params = true; arg = v}
  812. assert_equal true, thru_params
  813. assert_equal [nil, nil, "*x", nil, nil, nil, nil], arg
  814. thru_params = false
  815. parse('a {|x: 1|}', :on_params) {|_, *v| thru_params = true; arg = v}
  816. assert_equal true, thru_params
  817. assert_equal [nil, nil, nil, nil, [["x:", "1"]], nil, nil], arg
  818. thru_params = false
  819. parse('a {|x:|}', :on_params) {|_, *v| thru_params = true; arg = v}
  820. assert_equal true, thru_params
  821. assert_equal [nil, nil, nil, nil, [["x:", false]], nil, nil], arg
  822. thru_params = false
  823. parse('a {|**x|}', :on_params) {|_, *v| thru_params = true; arg = v}
  824. assert_equal true, thru_params
  825. assert_equal [nil, nil, nil, nil, nil, "**x", nil], arg
  826. thru_params = false
  827. parse('a {|**nil|}', :on_params) {|_, *v| thru_params = true; arg = v}
  828. assert_equal true, thru_params
  829. assert_equal [nil, nil, nil, nil, nil, :nil, nil], arg
  830. end
  831. def test_params_mlhs
  832. thru_mlhs = false
  833. tree = parse("proc {|(a, b)|}", :on_mlhs_paren) {thru_mlhs = true}
  834. assert_equal true, thru_mlhs
  835. assert_include(tree, "[mlhs([a,b])]")
  836. end
  837. def test_params_mlhs_add
  838. thru_mlhs_add = false
  839. tree = parse("proc {|(a, b)|}", :on_mlhs_add) {thru_mlhs_add = true}
  840. assert_equal true, thru_mlhs_add
  841. assert_include(tree, "[mlhs([a,b])]")
  842. end
  843. def test_params_mlhs_add_star
  844. thru_mlhs_add_star = false
  845. tree = parse("proc {|(a, *b)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  846. assert_equal true, thru_mlhs_add_star
  847. assert_include(tree, "[mlhs([a,*b])]")
  848. thru_mlhs_add_star = false
  849. tree = parse("proc {|(a, *b, c)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  850. assert_equal true, thru_mlhs_add_star
  851. assert_include(tree, "[mlhs([a,*b,c])]")
  852. thru_mlhs_add_star = false
  853. tree = parse("proc {|(a, *, c)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  854. assert_equal true, thru_mlhs_add_star
  855. assert_include(tree, "[mlhs([a,*,c])]")
  856. thru_mlhs_add_star = false
  857. tree = parse("proc {|(*b, c)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  858. assert_equal true, thru_mlhs_add_star
  859. assert_include(tree, "[mlhs([*b,c])]")
  860. thru_mlhs_add_star = false
  861. tree = parse("proc {|(*b)|}", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  862. assert_equal true, thru_mlhs_add_star
  863. assert_include(tree, "[mlhs([*b])]")
  864. end
  865. def test_params_mlhs_add_post
  866. thru_mlhs_add_post = false
  867. tree = parse("proc {|(a, *b)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  868. assert_equal false, thru_mlhs_add_post
  869. assert_include(tree, "mlhs([a,*b])")
  870. thru_mlhs_add_post = false
  871. tree = parse("proc {|(a, *b, c)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  872. assert_equal true, thru_mlhs_add_post
  873. assert_include(tree, "mlhs([a,*b,c])")
  874. thru_mlhs_add_post = false
  875. tree = parse("proc {|(a, *, c)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  876. assert_equal true, thru_mlhs_add_post
  877. assert_include(tree, "mlhs([a,*,c])")
  878. thru_mlhs_add_post = false
  879. tree = parse("proc {|(*b, c)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  880. assert_equal true, thru_mlhs_add_post
  881. assert_include(tree, "mlhs([*b,c])")
  882. thru_mlhs_add_post = false
  883. tree = parse("proc {|(*, c)|}", :on_mlhs_add_post) {thru_mlhs_add_post = true}
  884. assert_equal true, thru_mlhs_add_post
  885. assert_include(tree, "mlhs([*,c])")
  886. end
  887. def test_params_mlhs_new
  888. thru_mlhs_new = false
  889. tree = parse("proc {|(a, b)|}", :on_mlhs_new) {thru_mlhs_new = true}
  890. assert_equal true, thru_mlhs_new
  891. assert_include(tree, "[mlhs([a,b])]")
  892. end
  893. def test_params_mlhs_paren
  894. thru_mlhs_paren = 0
  895. tree = parse("proc {|(a, b)|}", :on_mlhs_paren) {thru_mlhs_paren += 1}
  896. assert_equal 1, thru_mlhs_paren
  897. assert_include(tree, "[mlhs([a,b])]")
  898. thru_mlhs_paren = 0
  899. tree = parse("proc {|((a, b))|}", :on_mlhs_paren) {thru_mlhs_paren += 1}
  900. assert_equal 2, thru_mlhs_paren
  901. assert_include(tree, "[mlhs([a,b])]")
  902. end
  903. def test_paren
  904. thru_paren = false
  905. parse('()', :on_paren) {thru_paren = true}
  906. assert_equal true, thru_paren
  907. end
  908. def test_parse_error
  909. thru_parse_error = false
  910. parse('<>', :on_parse_error) {thru_parse_error = true}
  911. assert_equal true, thru_parse_error
  912. end
  913. def test_qwords_add
  914. thru_qwords_add = false
  915. tree = parse('%w[a]', :on_qwords_add) {thru_qwords_add = true}
  916. assert_equal true, thru_qwords_add
  917. assert_equal '[array([a])]', tree
  918. thru_qwords_add = false
  919. tree = parse('%w[ a ]', :on_qwords_add) {thru_qwords_add = true}
  920. assert_equal true, thru_qwords_add
  921. assert_equal '[array([a])]', tree
  922. end
  923. def test_qsymbols_add
  924. thru_qsymbols_add = false
  925. tree = parse('%i[a]', :on_qsymbols_add) {thru_qsymbols_add = true}
  926. assert_equal true, thru_qsymbols_add
  927. assert_equal '[array([:a])]', tree
  928. thru_qsymbols_add = false
  929. tree = parse('%i[ a ]', :on_qsymbols_add) {thru_qsymbols_add = true}
  930. assert_equal true, thru_qsymbols_add
  931. assert_equal '[array([:a])]', tree
  932. end
  933. def test_symbols_add
  934. thru_symbols_add = false
  935. tree = parse('%I[a]', :on_symbols_add) {thru_symbols_add = true}
  936. assert_equal true, thru_symbols_add
  937. assert_equal '[array([:a])]', tree
  938. thru_symbols_add = false
  939. tree = parse('%I[ a ]', :on_symbols_add) {thru_symbols_add = true}
  940. assert_equal true, thru_symbols_add
  941. assert_equal '[array([:a])]', tree
  942. end
  943. def test_qwords_new
  944. thru_qwords_new = false
  945. parse('%w[]', :on_qwords_new) {thru_qwords_new = true}
  946. assert_equal true, thru_qwords_new
  947. end
  948. def test_qsymbols_new
  949. thru_qsymbols_new = false
  950. parse('%i[]', :on_qsymbols_new) {thru_qsymbols_new = true}
  951. assert_equal true, thru_qsymbols_new
  952. end
  953. def test_symbols_new
  954. thru_symbols_new = false
  955. parse('%I[]', :on_symbols_new) {thru_symbols_new = true}
  956. assert_equal true, thru_symbols_new
  957. end
  958. def test_redo
  959. thru_redo = false
  960. parse('redo', :on_redo) {thru_redo = true}
  961. assert_equal true, thru_redo
  962. end
  963. def test_regexp_add
  964. thru_regexp_add = false
  965. parse('/foo/', :on_regexp_add) {thru_regexp_add = true}
  966. assert_equal true, thru_regexp_add
  967. end
  968. def test_regexp_literal
  969. thru_regexp_literal = false
  970. parse('//', :on_regexp_literal) {thru_regexp_literal = true}
  971. assert_equal true, thru_regexp_literal
  972. end
  973. def test_regexp_new
  974. thru_regexp_new = false
  975. parse('//', :on_regexp_new) {thru_regexp_new = true}
  976. assert_equal true, thru_regexp_new
  977. end
  978. def test_rescue
  979. thru_rescue = false
  980. parsed = parse('begin; 1; rescue => e; 2; end', :on_rescue) {thru_rescue = true}
  981. assert_equal true, thru_rescue
  982. assert_match(/1.*rescue/, parsed)
  983. assert_match(/rescue\(,var_field\(e\),\[2\]\)/, parsed)
  984. end
  985. def test_rescue_class
  986. thru_rescue = false
  987. parsed = parse('begin; 1; rescue RuntimeError => e; 2; end', :on_rescue) {thru_rescue = true}
  988. assert_equal true, thru_rescue
  989. assert_match(/1.*rescue/, parsed)
  990. assert_match(/rescue\(\[ref\(RuntimeError\)\],var_field\(e\),\[2\]\)/, parsed)
  991. end
  992. def test_rescue_mod
  993. thru_rescue_mod = false
  994. parsed = parse('1 rescue 2', :on_rescue_mod) {thru_rescue_mod = true}
  995. assert_equal true, thru_rescue_mod
  996. bug4716 = '[ruby-core:36248]'
  997. assert_equal "[rescue_mod(1,2)]", parsed, bug4716
  998. end
  999. def test_rest_param
  1000. thru_rest_param = false
  1001. parse('def a(*) end', :on_rest_param) {thru_rest_param = true}
  1002. assert_equal true, thru_rest_param
  1003. thru_rest_param = false
  1004. parse('def a(*x) end', :on_rest_param) {thru_rest_param = true}
  1005. assert_equal true, thru_rest_param
  1006. end
  1007. def test_kwrest_param
  1008. thru_kwrest = false
  1009. parse('def a(**) end', :on_kwrest_param) {|n, val| thru_kwrest = val}
  1010. assert_equal nil, thru_kwrest
  1011. thru_kwrest = false
  1012. parse('def a(**x) end', :on_kwrest_param) {|n, val| thru_kwrest = val}
  1013. assert_equal "x", thru_kwrest
  1014. end
  1015. def test_nokw_param
  1016. thru_nokw = false
  1017. parse('def a(**nil) end', :on_nokw_param) {|n, val| thru_nokw = val}
  1018. assert_equal nil, thru_nokw
  1019. end
  1020. def test_retry
  1021. thru_retry = false
  1022. parse('retry', :on_retry) {thru_retry = true}
  1023. assert_equal true, thru_retry
  1024. end
  1025. def test_return
  1026. thru_return = false
  1027. parse('return a', :on_return) {thru_return = true}
  1028. assert_equal true, thru_return
  1029. end
  1030. def test_return0
  1031. thru_return0 = false
  1032. parse('return', :on_return0) {thru_return0 = true}
  1033. assert_equal true, thru_return0
  1034. end
  1035. def test_sclass
  1036. thru_sclass = false
  1037. parse('class << a; end', :on_sclass) {thru_sclass = true}
  1038. assert_equal true, thru_sclass
  1039. end
  1040. def test_string_add
  1041. thru_string_add = false
  1042. parse('"aa"', :on_string_add) {thru_string_add = true}
  1043. assert_equal true, thru_string_add
  1044. end
  1045. def test_string_concat
  1046. thru_string_concat = false
  1047. parse('"a" "b"', :on_string_concat) {thru_string_concat = true}
  1048. assert_equal true, thru_string_concat
  1049. end
  1050. def test_string_content
  1051. thru_string_content = false
  1052. parse('""', :on_string_content) {thru_string_content = true}
  1053. assert_equal true, thru_string_content
  1054. thru_string_content = false
  1055. parse('"a"', :on_string_content) {thru_string_content = true}
  1056. assert_equal true, thru_string_content
  1057. thru_string_content = false
  1058. parse('%[a]', :on_string_content) {thru_string_content = true}
  1059. assert_equal true, thru_string_content
  1060. thru_string_content = false
  1061. parse('\'a\'', :on_string_content) {thru_string_content = true}
  1062. assert_equal true, thru_string_content
  1063. thru_string_content = false
  1064. parse('%<a>', :on_string_content) {thru_string_content = true}
  1065. assert_equal true, thru_string_content
  1066. thru_string_content = false
  1067. parse('%!a!', :on_string_content) {thru_string_content = true}
  1068. assert_equal true, thru_string_content
  1069. thru_string_content = false
  1070. parse('%q!a!', :on_string_content) {thru_string_content = true}
  1071. assert_equal true, thru_string_content
  1072. thru_string_content = false
  1073. parse('%Q!a!', :on_string_content) {thru_string_content = true}
  1074. assert_equal true, thru_string_content
  1075. end
  1076. def test_string_dvar
  1077. thru_string_dvar = false
  1078. parse('"#$a"', :on_string_dvar) {thru_string_dvar = true}
  1079. assert_equal true, thru_string_dvar
  1080. thru_string_dvar = false
  1081. parse('\'#$a\'', :on_string_dvar) {thru_string_dvar = true}
  1082. assert_equal false, thru_string_dvar
  1083. thru_string_dvar = false
  1084. parse('"#@a"', :on_string_dvar) {thru_string_dvar = true}
  1085. assert_equal true, thru_string_dvar
  1086. thru_string_dvar = false
  1087. parse('\'#@a\'', :on_string_dvar) {thru_string_dvar = true}
  1088. assert_equal false, thru_string_dvar
  1089. thru_string_dvar = false
  1090. parse('"#@@a"', :on_string_dvar) {thru_string_dvar = true}
  1091. assert_equal true, thru_string_dvar
  1092. thru_string_dvar = false
  1093. parse('\'#@@a\'', :on_string_dvar) {thru_string_dvar = true}
  1094. assert_equal false, thru_string_dvar
  1095. thru_string_dvar = false
  1096. parse('"#$1"', :on_string_dvar) {thru_string_dvar = true}
  1097. assert_equal true, thru_string_dvar
  1098. thru_string_dvar = false
  1099. parse('\'#$1\'', :on_string_dvar) {thru_string_dvar = true}
  1100. assert_equal false, thru_string_dvar
  1101. end
  1102. def test_string_embexpr
  1103. thru_string_embexpr = false
  1104. parse('"#{}"', :on_string_embexpr) {thru_string_embexpr = true}
  1105. assert_equal true, thru_string_embexpr
  1106. thru_string_embexpr = false
  1107. parse('\'#{}\'', :on_string_embexpr) {thru_string_embexpr = true}
  1108. assert_equal false, thru_string_embexpr
  1109. end
  1110. def test_string_literal
  1111. thru_string_literal = false
  1112. parse('""', :on_string_literal) {thru_string_literal = true}
  1113. assert_equal true, thru_string_literal
  1114. end
  1115. def test_super
  1116. thru_super = false
  1117. parse('super()', :on_super) {thru_super = true}
  1118. assert_equal true, thru_super
  1119. end
  1120. def test_symbol
  1121. thru_symbol = false
  1122. parse(':a', :on_symbol) {thru_symbol = true}
  1123. assert_equal true, thru_symbol
  1124. thru_symbol = false
  1125. parse(':$a', :on_symbol) {thru_symbol = true}
  1126. assert_equal true, thru_symbol
  1127. thru_symbol = false
  1128. parse(':@a', :on_symbol) {thru_symbol = true}
  1129. assert_equal true, thru_symbol
  1130. thru_symbol = false
  1131. parse(':@@a', :on_symbol) {thru_symbol = true}
  1132. assert_equal true, thru_symbol
  1133. thru_symbol = false
  1134. parse(':==', :on_symbol) {thru_symbol = true}
  1135. assert_equal true, thru_symbol
  1136. end
  1137. def test_symbol_literal
  1138. thru_symbol_literal = false
  1139. parse(':a', :on_symbol_literal) {thru_symbol_literal = true}
  1140. assert_equal true, thru_symbol_literal
  1141. end
  1142. def test_top_const_field
  1143. thru_top_const_field = false
  1144. parse('::A=1', :on_top_const_field) {thru_top_const_field = true}
  1145. assert_equal true, thru_top_const_field
  1146. end
  1147. def test_top_const_ref
  1148. thru_top_const_ref = false
  1149. parse('::A', :on_top_const_ref) {thru_top_const_ref = true}
  1150. assert_equal true, thru_top_const_ref
  1151. end
  1152. def test_unary
  1153. thru_unary = false
  1154. parse('not a 1, 2', :on_unary) {thru_unary = true}
  1155. assert_equal true, thru_unary
  1156. thru_unary = false
  1157. parse('not (a)', :on_unary) {thru_unary = true}
  1158. assert_equal true, thru_unary
  1159. thru_unary = false
  1160. parse('!a', :on_unary) {thru_unary = true}
  1161. assert_equal true, thru_unary
  1162. thru_unary = false
  1163. parse('-10', :on_unary) {thru_unary = true}
  1164. assert_equal true, thru_unary
  1165. thru_unary = false
  1166. parse('-10*2', :on_unary) {thru_unary = true}
  1167. assert_equal true, thru_unary
  1168. thru_unary = false
  1169. parse('-10.1', :on_unary) {thru_unary = true}
  1170. assert_equal true, thru_unary
  1171. thru_unary = false
  1172. parse('-10.1*2', :on_unary) {thru_unary = true}
  1173. assert_equal true, thru_unary
  1174. thru_unary = false
  1175. parse('-a', :on_unary) {thru_unary = true}
  1176. assert_equal true, thru_unary
  1177. thru_unary = false
  1178. parse('+a', :on_unary) {thru_unary = true}
  1179. assert_equal true, thru_unary
  1180. thru_unary = false
  1181. parse('~a', :on_unary) {thru_unary = true}
  1182. assert_equal true, thru_unary
  1183. thru_unary = false
  1184. parse('not()', :on_unary) {thru_unary = true}
  1185. assert_equal true, thru_unary
  1186. end
  1187. def test_undef
  1188. thru_undef = false
  1189. parse('undef a', :on_undef) {thru_undef = true}
  1190. assert_equal true, thru_undef
  1191. thru_undef = false
  1192. parse('undef <=>', :on_undef) {thru_undef = true}
  1193. assert_equal true, thru_undef
  1194. thru_undef = false
  1195. parse('undef a, b', :on_undef) {thru_undef = true}
  1196. assert_equal true, thru_undef
  1197. end
  1198. def test_unless
  1199. thru_unless = false
  1200. parse('unless a; end', :on_unless) {thru_unless = true}
  1201. assert_equal true, thru_unless
  1202. end
  1203. def test_unless_mod
  1204. thru_unless_mod = false
  1205. parse('nil unless a', :on_unless_mod) {thru_unless_mod = true}
  1206. assert_equal true, thru_unless_mod
  1207. end
  1208. def test_until
  1209. thru_until = false
  1210. parse('until a; end', :on_until) {thru_until = true}
  1211. assert_equal true, thru_until
  1212. end
  1213. def test_until_mod
  1214. thru_until_mod = false
  1215. parse('nil until a', :on_until_mod) {thru_until_mod = true}
  1216. assert_equal true, thru_until_mod
  1217. end
  1218. def test_var_field
  1219. thru_var_field = false
  1220. parse('a = 1', :on_var_field) {thru_var_field = true}
  1221. assert_equal true, thru_var_field
  1222. thru_var_field = false
  1223. parse('a += 1', :on_var_field) {thru_var_field = true}
  1224. assert_equal true, thru_var_field
  1225. end
  1226. def test_when
  1227. thru_when = false
  1228. parse('case a when b; end', :on_when) {thru_when = true}
  1229. assert_equal true, thru_when
  1230. thru_when = false
  1231. parse('case when a; end', :on_when) {thru_when = true}
  1232. assert_equal true, thru_when
  1233. end
  1234. def test_while
  1235. thru_while = false
  1236. parse('while a; end', :on_while) {thru_while = true}
  1237. assert_equal true, thru_while
  1238. end
  1239. def test_while_mod
  1240. thru_while_mod = false
  1241. parse('nil while a', :on_while_mod) {thru_while_mod = true}
  1242. assert_equal true, thru_while_mod
  1243. end
  1244. def test_word_add
  1245. thru_word_add = false
  1246. parse('%W[a]', :on_word_add) {thru_word_add = true}
  1247. assert_equal true, thru_word_add
  1248. end
  1249. def test_word_new
  1250. thru_word_new = false
  1251. parse('%W[a]', :on_word_new) {thru_word_new = true}
  1252. assert_equal true, thru_word_new
  1253. end
  1254. def test_words_add
  1255. thru_words_add = false
  1256. tree = parse('%W[a]', :on_words_add) {thru_words_add = true}
  1257. assert_equal true, thru_words_add
  1258. assert_equal '[array([a])]', tree
  1259. thru_words_add = false
  1260. tree = parse('%W[ a ]', :on_words_add) {thru_words_add = true}
  1261. assert_equal true, thru_words_add
  1262. assert_equal '[array([a])]', tree
  1263. end
  1264. def test_words_new
  1265. thru_words_new = false
  1266. parse('%W[]', :on_words_new) {thru_words_new = true}
  1267. assert_equal true, thru_words_new
  1268. end
  1269. def test_xstring_add
  1270. thru_xstring_add = false
  1271. parse('`x`', :on_xstring_add) {thru_xstring_add = true}
  1272. assert_equal true, thru_xstring_add
  1273. end
  1274. def test_xstring_literal
  1275. thru_xstring_literal = false
  1276. parse('``', :on_xstring_literal) {thru_xstring_literal = true}
  1277. assert_equal true, thru_xstring_literal
  1278. end
  1279. def test_xstring_new
  1280. thru_xstring_new = false
  1281. parse('``', :on_xstring_new) {thru_xstring_new = true}
  1282. assert_equal true, thru_xstring_new
  1283. end
  1284. def test_yield
  1285. thru_yield = false
  1286. parse('yield a', :on_yield) {thru_yield = true}
  1287. assert_equal true, thru_yield
  1288. end
  1289. def test_yield0
  1290. thru_yield0 = false
  1291. parse('yield', :on_yield0) {thru_yield0 = true}
  1292. assert_equal true, thru_yield0
  1293. end
  1294. def test_zsuper
  1295. thru_zsuper = false
  1296. parse('super', :on_zsuper) {thru_zsuper = true}
  1297. assert_equal true, thru_zsuper
  1298. end
  1299. def test_local_variables
  1300. cmd = 'command(w,[regexp_literal(regexp_add(regexp_new(),25 # ),/)])'
  1301. div = 'binary(ref(w),/,25)'
  1302. bug1939 = '[ruby-core:24923]'
  1303. assert_equal("[#{cmd}]", parse('w /25 # /'), bug1939)
  1304. assert_equal("[assign(var_field(w),1),#{div}]", parse("w = 1; w /25 # /"), bug1939)
  1305. assert_equal("[fcall(p,[],&block([w],[#{div}]))]", parse("p{|w|w /25 # /\n}"), bug1939)
  1306. assert_equal("[def(p,[w],bodystmt([#{div}]))]", parse("def p(w)\nw /25 # /\nend"), bug1939)
  1307. end
  1308. def test_block_variables
  1309. bug4159 = '[ruby-dev:39423]'
  1310. assert_equal("[fcall(proc,[],&block([],[void()]))]", parse("proc{|;y|}"), bug4159)
  1311. assert_equal("[fcall(proc,[],&block([],[unary(!,ref(y))]))]", parse("proc{|;y|!y}"), bug4159)
  1312. end
  1313. def test_unterminated_regexp
  1314. assert_equal("unterminated regexp meets end of file", compile_error('/'))
  1315. end
  1316. def test_invalid_instance_variable_name
  1317. assert_equal("`@1' is not allowed as an instance variable name", compile_error('proc{@1}'))
  1318. assert_equal("`@' without identifiers is not allowed as an instance variable name", compile_error('@%'))
  1319. assert_equal("`@' without identifiers is not allowed as an instance variable name", compile_error('@'))
  1320. end
  1321. def test_invalid_class_variable_name
  1322. assert_equal("`@@1' is not allowed as a class variable name", compile_error('@@1'))
  1323. assert_equal("`@@' without identifiers is not allowed as a class variable name", compile_error('@@%'))
  1324. assert_equal("`@@' without identifiers is not allowed as a class variable name", compile_error('@@'))
  1325. en

Large files files are truncated, but you can click here to view the full file