PageRenderTime 29ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ripper/test_parser_events.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1142 lines | 992 code | 146 blank | 4 comment | 7 complexity | f7dac584cfdd2b6244998ba280eb1d81 MD5 | raw file
  1. begin
  2. require_relative 'dummyparser'
  3. require_relative '../ruby/envutil'
  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. # should be enabled
  11. def test_event_coverage
  12. dispatched = Ripper::PARSER_EVENTS.map {|event,*| event }
  13. dispatched.each do |e|
  14. assert_equal true, respond_to?("test_#{e}", true),
  15. "event not tested: #{e.inspect}"
  16. end
  17. end
  18. def parse(str, nm = nil, &bl)
  19. dp = DummyParser.new(str)
  20. dp.hook(*nm, &bl) if nm
  21. dp.parse.to_s
  22. end
  23. def test_program
  24. thru_program = false
  25. assert_equal '[void()]', parse('', :on_program) {thru_program = true}
  26. assert_equal true, thru_program
  27. end
  28. def test_stmts_new
  29. assert_equal '[void()]', parse('')
  30. end
  31. def test_stmts_add
  32. assert_equal '[ref(nil)]', parse('nil')
  33. assert_equal '[ref(nil),ref(nil)]', parse('nil;nil')
  34. assert_equal '[ref(nil),ref(nil),ref(nil)]', parse('nil;nil;nil')
  35. end
  36. def test_void_stmt
  37. assert_equal '[void()]', parse('')
  38. assert_equal '[void()]', parse('; ;')
  39. end
  40. def test_var_ref
  41. assert_equal '[assign(var_field(a),ref(a))]', parse('a=a')
  42. assert_equal '[ref(nil)]', parse('nil')
  43. assert_equal '[ref(true)]', parse('true')
  44. end
  45. def test_vcall
  46. assert_equal '[vcall(a)]', parse('a')
  47. end
  48. def test_BEGIN
  49. assert_equal '[BEGIN([void()])]', parse('BEGIN{}')
  50. assert_equal '[BEGIN([ref(nil)])]', parse('BEGIN{nil}')
  51. end
  52. def test_END
  53. assert_equal '[END([void()])]', parse('END{}')
  54. assert_equal '[END([ref(nil)])]', parse('END{nil}')
  55. end
  56. def test_alias
  57. assert_equal '[alias(symbol_literal(a),symbol_literal(b))]', parse('alias a b')
  58. end
  59. def test_var_alias
  60. assert_equal '[valias($a,$g)]', parse('alias $a $g')
  61. end
  62. def test_alias_error
  63. assert_equal '[aliaserr(valias($a,$1))]', parse('alias $a $1')
  64. end
  65. def test_arglist
  66. assert_equal '[fcall(m,[])]', parse('m()')
  67. assert_equal '[fcall(m,[1])]', parse('m(1)')
  68. assert_equal '[fcall(m,[1,2])]', parse('m(1,2)')
  69. assert_equal '[fcall(m,[*vcall(r)])]', parse('m(*r)')
  70. assert_equal '[fcall(m,[1,*vcall(r)])]', parse('m(1,*r)')
  71. assert_equal '[fcall(m,[1,2,*vcall(r)])]', parse('m(1,2,*r)')
  72. assert_equal '[fcall(m,[&vcall(r)])]', parse('m(&r)')
  73. assert_equal '[fcall(m,[1,&vcall(r)])]', parse('m(1,&r)')
  74. assert_equal '[fcall(m,[1,2,&vcall(r)])]', parse('m(1,2,&r)')
  75. assert_equal '[fcall(m,[*vcall(a),&vcall(b)])]', parse('m(*a,&b)')
  76. assert_equal '[fcall(m,[1,*vcall(a),&vcall(b)])]', parse('m(1,*a,&b)')
  77. assert_equal '[fcall(m,[1,2,*vcall(a),&vcall(b)])]', parse('m(1,2,*a,&b)')
  78. end
  79. def test_args_add
  80. thru_args_add = false
  81. parse('m(a)', :on_args_add) {thru_args_add = true}
  82. assert_equal true, thru_args_add
  83. end
  84. def test_args_add_block
  85. thru_args_add_block = false
  86. parse('m(&b)', :on_args_add_block) {thru_args_add_block = true}
  87. assert_equal true, thru_args_add_block
  88. end
  89. def test_args_add_star
  90. thru_args_add_star = false
  91. parse('m(*a)', :on_args_add_star) {thru_args_add_star = true}
  92. assert_equal true, thru_args_add_star
  93. thru_args_add_star = false
  94. parse('m(*a, &b)', :on_args_add_star) {thru_args_add_star = true}
  95. assert_equal true, thru_args_add_star
  96. end
  97. def test_args_new
  98. thru_args_new = false
  99. parse('m()', :on_args_new) {thru_args_new = true}
  100. assert_equal true, thru_args_new
  101. end
  102. def test_arg_paren
  103. # FIXME
  104. end
  105. def test_aref
  106. assert_equal '[aref(vcall(v),[1])]', parse('v[1]')
  107. assert_equal '[aref(vcall(v),[1,2])]', parse('v[1,2]')
  108. end
  109. def test_assoclist_from_args
  110. thru_assoclist_from_args = false
  111. parse('{a=>b}', :on_assoclist_from_args) {thru_assoclist_from_args = true}
  112. assert_equal true, thru_assoclist_from_args
  113. end
  114. def test_assocs
  115. assert_equal '[fcall(m,[assocs(assoc(1,2))])]', parse('m(1=>2)')
  116. assert_equal '[fcall(m,[assocs(assoc(1,2),assoc(3,4))])]', parse('m(1=>2,3=>4)')
  117. assert_equal '[fcall(m,[3,assocs(assoc(1,2))])]', parse('m(3,1=>2)')
  118. end
  119. def test_assoc_new
  120. thru_assoc_new = false
  121. parse('{a=>b}', :on_assoc_new) {thru_assoc_new = true}
  122. assert_equal true, thru_assoc_new
  123. end
  124. def test_aref_field
  125. assert_equal '[assign(aref_field(vcall(a),[1]),2)]', parse('a[1]=2')
  126. end
  127. def test_arg_ambiguous
  128. thru_arg_ambiguous = false
  129. parse('m //', :on_arg_ambiguous) {thru_arg_ambiguous = true}
  130. assert_equal true, thru_arg_ambiguous
  131. end
  132. def test_operator_ambiguous
  133. thru_operator_ambiguous = false
  134. parse('a=1; a %[]', :on_operator_ambiguous) {thru_operator_ambiguous = true}
  135. assert_equal true, thru_operator_ambiguous
  136. end
  137. def test_array # array literal
  138. assert_equal '[array([1,2,3])]', parse('[1,2,3]')
  139. assert_equal '[array([abc,def])]', parse('%w[abc def]')
  140. assert_equal '[array([abc,def])]', parse('%W[abc def]')
  141. end
  142. def test_assign # generic assignment
  143. assert_equal '[assign(var_field(v),1)]', parse('v=1')
  144. end
  145. def test_assign_error
  146. thru_assign_error = false
  147. parse('$` = 1', :on_assign_error) {thru_assign_error = true}
  148. assert_equal true, thru_assign_error
  149. thru_assign_error = false
  150. parse('$`, _ = 1', :on_assign_error) {thru_assign_error = true}
  151. assert_equal true, thru_assign_error
  152. thru_assign_error = false
  153. parse('self::X = 1', :on_assign_error) {thru_assign_error = true}
  154. assert_equal false, thru_assign_error
  155. parse('def m\n self::X = 1\nend', :on_assign_error) {thru_assign_error = true}
  156. assert_equal true, thru_assign_error
  157. thru_assign_error = false
  158. parse('X = 1', :on_assign_error) {thru_assign_error = true}
  159. assert_equal false, thru_assign_error
  160. parse('def m\n X = 1\nend', :on_assign_error) {thru_assign_error = true}
  161. assert_equal true, thru_assign_error
  162. thru_assign_error = false
  163. parse('::X = 1', :on_assign_error) {thru_assign_error = true}
  164. assert_equal false, thru_assign_error
  165. parse('def m\n ::X = 1\nend', :on_assign_error) {thru_assign_error = true}
  166. assert_equal true, thru_assign_error
  167. end
  168. def test_bare_assoc_hash
  169. thru_bare_assoc_hash = false
  170. parse('x[a=>b]', :on_bare_assoc_hash) {thru_bare_assoc_hash = true}
  171. assert_equal true, thru_bare_assoc_hash
  172. thru_bare_assoc_hash = false
  173. parse('x[1, a=>b]', :on_bare_assoc_hash) {thru_bare_assoc_hash = true}
  174. assert_equal true, thru_bare_assoc_hash
  175. thru_bare_assoc_hash = false
  176. parse('x(a=>b)', :on_bare_assoc_hash) {thru_bare_assoc_hash = true}
  177. assert_equal true, thru_bare_assoc_hash
  178. thru_bare_assoc_hash = false
  179. parse('x(1, a=>b)', :on_bare_assoc_hash) {thru_bare_assoc_hash = true}
  180. assert_equal true, thru_bare_assoc_hash
  181. end
  182. def test_begin
  183. thru_begin = false
  184. parse('begin end', :on_begin) {thru_begin = true}
  185. assert_equal true, thru_begin
  186. end
  187. def test_binary
  188. thru_binary = nil
  189. %w"and or + - * / % ** | ^ & <=> > >= < <= == === != =~ !~ << >> && ||".each do |op|
  190. thru_binary = false
  191. parse("a #{op} b", :on_binary) {thru_binary = true}
  192. assert_equal true, thru_binary
  193. end
  194. end
  195. def test_blockarg
  196. thru_blockarg = false
  197. parse("def a(&b) end", :on_blockarg) {thru_blockarg = true}
  198. assert_equal true, thru_blockarg
  199. thru_blockarg = false
  200. parse("def a(x, &b) end", :on_blockarg) {thru_blockarg = true}
  201. assert_equal true, thru_blockarg
  202. thru_blockarg = false
  203. parse("proc{|&b|}", :on_blockarg) {thru_blockarg = true}
  204. assert_equal true, thru_blockarg
  205. thru_blockarg = false
  206. parse("proc{|x, &b|}", :on_blockarg) {thru_blockarg = true}
  207. assert_equal true, thru_blockarg
  208. thru_blockarg = false
  209. parse("proc{|&b;y|}", :on_blockarg) {thru_blockarg = true}
  210. assert_equal true, thru_blockarg
  211. thru_blockarg = false
  212. parse("proc{|&b,x;y|}", :on_blockarg) {thru_blockarg = true}
  213. assert_equal true, thru_blockarg
  214. thru_blockarg = false
  215. parse("proc do |&b| end", :on_blockarg) {thru_blockarg = true}
  216. assert_equal true, thru_blockarg
  217. thru_blockarg = false
  218. parse("proc do |&b, x| end", :on_blockarg) {thru_blockarg = true}
  219. assert_equal true, thru_blockarg
  220. thru_blockarg = false
  221. parse("proc do |&b;y| end", :on_blockarg) {thru_blockarg = true}
  222. assert_equal true, thru_blockarg
  223. thru_blockarg = false
  224. parse("proc do |&b, x;y| end", :on_blockarg) {thru_blockarg = true}
  225. assert_equal true, thru_blockarg
  226. end
  227. def test_block_var
  228. thru_block_var = false
  229. parse("proc{||}", :on_block_var) {thru_block_var = true}
  230. assert_equal true, thru_block_var
  231. thru_block_var = false
  232. parse("proc{| |}", :on_block_var) {thru_block_var = true}
  233. assert_equal true, thru_block_var
  234. thru_block_var = false
  235. parse("proc{|x|}", :on_block_var) {thru_block_var = true}
  236. assert_equal true, thru_block_var
  237. thru_block_var = false
  238. parse("proc{|;y|}", :on_block_var) {thru_block_var = true}
  239. assert_equal true, thru_block_var
  240. thru_block_var = false
  241. parse("proc{|x;y|}", :on_block_var) {thru_block_var = true}
  242. assert_equal true, thru_block_var
  243. thru_block_var = false
  244. parse("proc do || end", :on_block_var) {thru_block_var = true}
  245. assert_equal true, thru_block_var
  246. thru_block_var = false
  247. parse("proc do | | end", :on_block_var) {thru_block_var = true}
  248. assert_equal true, thru_block_var
  249. thru_block_var = false
  250. parse("proc do |x| end", :on_block_var) {thru_block_var = true}
  251. assert_equal true, thru_block_var
  252. thru_block_var = false
  253. parse("proc do |;y| end", :on_block_var) {thru_block_var = true}
  254. assert_equal true, thru_block_var
  255. thru_block_var = false
  256. parse("proc do |x;y| end", :on_block_var) {thru_block_var = true}
  257. assert_equal true, thru_block_var
  258. end
  259. def test_block_var_add_block
  260. # not used
  261. end
  262. def test_block_var_add_star
  263. # not used
  264. end
  265. def test_bodystmt
  266. thru_bodystmt = false
  267. parse("class X\nend", :on_bodystmt) {thru_bodystmt = true}
  268. assert_equal true, thru_bodystmt
  269. end
  270. def test_call
  271. bug2233 = '[ruby-core:26165]'
  272. tree = nil
  273. thru_call = false
  274. assert_nothing_raised {
  275. tree = parse("self.foo", :on_call) {thru_call = true}
  276. }
  277. assert_equal true, thru_call
  278. assert_equal "[call(ref(self),.,foo)]", tree
  279. thru_call = false
  280. assert_nothing_raised(bug2233) {
  281. tree = parse("foo.()", :on_call) {thru_call = true}
  282. }
  283. assert_equal true, thru_call
  284. assert_equal "[call(vcall(foo),.,call,[])]", tree
  285. end
  286. def test_excessed_comma
  287. thru_excessed_comma = false
  288. parse("proc{|x,|}", :on_excessed_comma) {thru_excessed_comma = true}
  289. assert_equal true, thru_excessed_comma
  290. thru_excessed_comma = false
  291. parse("proc{|x,y,|}", :on_excessed_comma) {thru_excessed_comma = true}
  292. assert_equal true, thru_excessed_comma
  293. thru_excessed_comma = false
  294. parse("proc do |x,| end", :on_excessed_comma) {thru_excessed_comma = true}
  295. assert_equal true, thru_excessed_comma
  296. thru_excessed_comma = false
  297. parse("proc do |x,y,| end", :on_excessed_comma) {thru_excessed_comma = true}
  298. assert_equal true, thru_excessed_comma
  299. end
  300. def test_heredoc
  301. bug1921 = '[ruby-core:24855]'
  302. thru_heredoc_beg = false
  303. tree = parse("<<EOS\nheredoc\nEOS\n", :on_heredoc_beg) {thru_heredoc_beg = true}
  304. assert_equal true, thru_heredoc_beg
  305. assert_match(/string_content\(\),heredoc\n/, tree, bug1921)
  306. heredoc = nil
  307. parse("<<EOS\nheredoc1\nheredoc2\nEOS\n", :on_string_add) {|e, n, s| heredoc = s}
  308. assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921)
  309. heredoc = nil
  310. parse("<<-EOS\nheredoc1\nheredoc2\n\tEOS\n", :on_string_add) {|e, n, s| heredoc = s}
  311. assert_equal("heredoc1\nheredoc2\n", heredoc, bug1921)
  312. end
  313. def test_massign
  314. thru_massign = false
  315. parse("a, b = 1, 2", :on_massign) {thru_massign = true}
  316. assert_equal true, thru_massign
  317. end
  318. def test_mlhs_add
  319. thru_mlhs_add = false
  320. parse("a, b = 1, 2", :on_mlhs_add) {thru_mlhs_add = true}
  321. assert_equal true, thru_mlhs_add
  322. end
  323. def test_mlhs_add_star
  324. bug2232 = '[ruby-core:26163]'
  325. bug4364 = '[ruby-core:35078]'
  326. thru_mlhs_add_star = false
  327. tree = parse("a, *b = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  328. assert_equal true, thru_mlhs_add_star
  329. assert_match(/mlhs_add_star\(mlhs_add\(mlhs_new\(\),a\),b\)/, tree)
  330. thru_mlhs_add_star = false
  331. tree = parse("a, *b, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  332. assert_equal true, thru_mlhs_add_star
  333. assert_match(/mlhs_add\(mlhs_add_star\(mlhs_add\(mlhs_new\(\),a\),b\),mlhs_add\(mlhs_new\(\),c\)\)/, tree, bug2232)
  334. thru_mlhs_add_star = false
  335. tree = parse("a, *, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  336. assert_equal true, thru_mlhs_add_star
  337. assert_match(/mlhs_add\(mlhs_add_star\(mlhs_add\(mlhs_new\(\),a\)\),mlhs_add\(mlhs_new\(\),c\)\)/, tree, bug4364)
  338. thru_mlhs_add_star = false
  339. tree = parse("*b, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  340. assert_equal true, thru_mlhs_add_star
  341. assert_match(/mlhs_add\(mlhs_add_star\(mlhs_new\(\),b\),mlhs_add\(mlhs_new\(\),c\)\)/, tree, bug4364)
  342. thru_mlhs_add_star = false
  343. tree = parse("*, c = 1, 2", :on_mlhs_add_star) {thru_mlhs_add_star = true}
  344. assert_equal true, thru_mlhs_add_star
  345. assert_match(/mlhs_add\(mlhs_add_star\(mlhs_new\(\)\),mlhs_add\(mlhs_new\(\),c\)\)/, tree, bug4364)
  346. end
  347. def test_mlhs_new
  348. thru_mlhs_new = false
  349. parse("a, b = 1, 2", :on_mlhs_new) {thru_mlhs_new = true}
  350. assert_equal true, thru_mlhs_new
  351. end
  352. def test_mlhs_paren
  353. thru_mlhs_paren = false
  354. parse("a, b = 1, 2", :on_mlhs_paren) {thru_mlhs_paren = true}
  355. assert_equal false, thru_mlhs_paren
  356. thru_mlhs_paren = false
  357. parse("(a, b) = 1, 2", :on_mlhs_paren) {thru_mlhs_paren = true}
  358. assert_equal true, thru_mlhs_paren
  359. end
  360. def test_brace_block
  361. thru_brace_block = false
  362. parse('proc {}', :on_brace_block) {thru_brace_block = true}
  363. assert_equal true, thru_brace_block
  364. end
  365. def test_break
  366. thru_break = false
  367. parse('proc {break}', :on_break) {thru_break = true}
  368. assert_equal true, thru_break
  369. end
  370. def test_case
  371. thru_case = false
  372. parse('case foo when true; end', :on_case) {thru_case = true}
  373. assert_equal true, thru_case
  374. end
  375. def test_class
  376. thru_class = false
  377. parse('class Foo; end', :on_class) {thru_class = true}
  378. assert_equal true, thru_class
  379. end
  380. def test_class_name_error
  381. thru_class_name_error = false
  382. parse('class foo; end', :on_class_name_error) {thru_class_name_error = true}
  383. assert_equal true, thru_class_name_error
  384. end
  385. def test_command
  386. thru_command = false
  387. parse('foo a b', :on_command) {thru_command = true}
  388. assert_equal true, thru_command
  389. end
  390. def test_command_call
  391. thru_command_call = false
  392. parse('foo.bar a, b', :on_command_call) {thru_command_call = true}
  393. assert_equal true, thru_command_call
  394. end
  395. def test_const_ref
  396. thru_const_ref = false
  397. parse('class A;end', :on_const_ref) {thru_const_ref = true}
  398. assert_equal true, thru_const_ref
  399. thru_const_ref = false
  400. parse('module A;end', :on_const_ref) {thru_const_ref = true}
  401. assert_equal true, thru_const_ref
  402. end
  403. def test_const_path_field
  404. thru_const_path_field = false
  405. parse('foo::X = 1', :on_const_path_field) {thru_const_path_field = true}
  406. assert_equal true, thru_const_path_field
  407. end
  408. def test_const_path_ref
  409. thru_const_path_ref = false
  410. parse('foo::X', :on_const_path_ref) {thru_const_path_ref = true}
  411. assert_equal true, thru_const_path_ref
  412. end
  413. def test_def
  414. thru_def = false
  415. parse('def foo; end', :on_def) {
  416. thru_def = true
  417. }
  418. assert_equal true, thru_def
  419. assert_equal '[def(foo,[],bodystmt([void()]))]', parse('def foo ;end')
  420. end
  421. def test_defined
  422. thru_defined = false
  423. parse('defined?(x)', :on_defined) {thru_defined = true}
  424. assert_equal true, thru_defined
  425. end
  426. def test_defs
  427. thru_defs = false
  428. parse('def foo.bar; end', :on_defs) {thru_defs = true}
  429. assert_equal true, thru_defs
  430. end
  431. def test_do_block
  432. thru_do_block = false
  433. parse('proc do end', :on_do_block) {thru_do_block = true}
  434. assert_equal true, thru_do_block
  435. end
  436. def test_dot2
  437. thru_dot2 = false
  438. parse('a..b', :on_dot2) {thru_dot2 = true}
  439. assert_equal true, thru_dot2
  440. end
  441. def test_dot3
  442. thru_dot3 = false
  443. parse('a...b', :on_dot3) {thru_dot3 = true}
  444. assert_equal true, thru_dot3
  445. end
  446. def test_dyna_symbol
  447. thru_dyna_symbol = false
  448. parse(':"#{foo}"', :on_dyna_symbol) {thru_dyna_symbol = true}
  449. assert_equal true, thru_dyna_symbol
  450. end
  451. def test_else
  452. thru_else = false
  453. parse('if foo; bar else zot end', :on_else) {thru_else = true}
  454. assert_equal true, thru_else
  455. end
  456. def test_elsif
  457. thru_elsif = false
  458. parse('if foo; bar elsif qux; zot end', :on_elsif) {thru_elsif = true}
  459. assert_equal true, thru_elsif
  460. end
  461. def test_ensure
  462. thru_ensure = false
  463. parse('begin foo ensure bar end', :on_ensure) {thru_ensure = true}
  464. assert_equal true, thru_ensure
  465. end
  466. def test_fcall
  467. thru_fcall = false
  468. parse('foo()', :on_fcall) {thru_fcall = true}
  469. assert_equal true, thru_fcall
  470. end
  471. def test_field
  472. thru_field = false
  473. parse('foo.x = 1', :on_field) {thru_field = true}
  474. assert_equal true, thru_field
  475. end
  476. def test_for
  477. thru_for = false
  478. parse('for i in foo; end', :on_for) {thru_for = true}
  479. assert_equal true, thru_for
  480. end
  481. def test_hash
  482. thru_hash = false
  483. parse('{1=>2}', :on_hash) {thru_hash = true}
  484. assert_equal true, thru_hash
  485. thru_hash = false
  486. parse('{a: 2}', :on_hash) {thru_hash = true}
  487. assert_equal true, thru_hash
  488. end
  489. def test_if
  490. thru_if = false
  491. parse('if false; end', :on_if) {thru_if = true}
  492. assert_equal true, thru_if
  493. end
  494. def test_if_mod
  495. thru_if_mod = false
  496. parse('nil if nil', :on_if_mod) {thru_if_mod = true}
  497. assert_equal true, thru_if_mod
  498. end
  499. def test_ifop
  500. thru_ifop = false
  501. parse('a ? b : c', :on_ifop) {thru_ifop = true}
  502. assert_equal true, thru_ifop
  503. end
  504. def test_lambda
  505. thru_lambda = false
  506. parse('->{}', :on_lambda) {thru_lambda = true}
  507. assert_equal true, thru_lambda
  508. end
  509. def test_magic_comment
  510. thru_magic_comment = false
  511. parse('# -*- foo:bar -*-', :on_magic_comment) {thru_magic_comment = true}
  512. assert_equal true, thru_magic_comment
  513. end
  514. def test_method_add_block
  515. thru_method_add_block = false
  516. parse('a {}', :on_method_add_block) {thru_method_add_block = true}
  517. assert_equal true, thru_method_add_block
  518. thru_method_add_block = false
  519. parse('a do end', :on_method_add_block) {thru_method_add_block = true}
  520. assert_equal true, thru_method_add_block
  521. end
  522. def test_method_add_arg
  523. thru_method_add_arg = false
  524. parse('a()', :on_method_add_arg) {thru_method_add_arg = true}
  525. assert_equal true, thru_method_add_arg
  526. thru_method_add_arg = false
  527. parse('a {}', :on_method_add_arg) {thru_method_add_arg = true}
  528. assert_equal true, thru_method_add_arg
  529. thru_method_add_arg = false
  530. parse('a.b(1)', :on_method_add_arg) {thru_method_add_arg = true}
  531. assert_equal true, thru_method_add_arg
  532. thru_method_add_arg = false
  533. parse('a::b(1)', :on_method_add_arg) {thru_method_add_arg = true}
  534. assert_equal true, thru_method_add_arg
  535. end
  536. def test_module
  537. thru_module = false
  538. parse('module A; end', :on_module) {thru_module = true}
  539. assert_equal true, thru_module
  540. end
  541. def test_mrhs_add
  542. thru_mrhs_add = false
  543. parse('a = a, b', :on_mrhs_add) {thru_mrhs_add = true}
  544. assert_equal true, thru_mrhs_add
  545. end
  546. def test_mrhs_add_star
  547. thru_mrhs_add_star = false
  548. parse('a = a, *b', :on_mrhs_add_star) {thru_mrhs_add_star = true}
  549. assert_equal true, thru_mrhs_add_star
  550. end
  551. def test_mrhs_new
  552. thru_mrhs_new = false
  553. parse('a = *a', :on_mrhs_new) {thru_mrhs_new = true}
  554. assert_equal true, thru_mrhs_new
  555. end
  556. def test_mrhs_new_from_args
  557. thru_mrhs_new_from_args = false
  558. parse('a = a, b', :on_mrhs_new_from_args) {thru_mrhs_new_from_args = true}
  559. assert_equal true, thru_mrhs_new_from_args
  560. end
  561. def test_next
  562. thru_next = false
  563. parse('a {next}', :on_next) {thru_next = true}
  564. assert_equal true, thru_next
  565. end
  566. def test_opassign
  567. thru_opassign = false
  568. parse('a += b', :on_opassign) {thru_opassign = true}
  569. assert_equal true, thru_opassign
  570. thru_opassign = false
  571. parse('a -= b', :on_opassign) {thru_opassign = true}
  572. assert_equal true, thru_opassign
  573. thru_opassign = false
  574. parse('a *= b', :on_opassign) {thru_opassign = true}
  575. assert_equal true, thru_opassign
  576. thru_opassign = false
  577. parse('a /= b', :on_opassign) {thru_opassign = true}
  578. assert_equal true, thru_opassign
  579. thru_opassign = false
  580. parse('a %= b', :on_opassign) {thru_opassign = true}
  581. assert_equal true, thru_opassign
  582. thru_opassign = false
  583. parse('a **= b', :on_opassign) {thru_opassign = true}
  584. assert_equal true, thru_opassign
  585. thru_opassign = false
  586. parse('a &= b', :on_opassign) {thru_opassign = true}
  587. assert_equal true, thru_opassign
  588. thru_opassign = false
  589. parse('a |= b', :on_opassign) {thru_opassign = true}
  590. assert_equal true, thru_opassign
  591. thru_opassign = false
  592. parse('a <<= b', :on_opassign) {thru_opassign = true}
  593. assert_equal true, thru_opassign
  594. thru_opassign = false
  595. parse('a >>= b', :on_opassign) {thru_opassign = true}
  596. assert_equal true, thru_opassign
  597. thru_opassign = false
  598. parse('a &&= b', :on_opassign) {thru_opassign = true}
  599. assert_equal true, thru_opassign
  600. thru_opassign = false
  601. parse('a ||= b', :on_opassign) {thru_opassign = true}
  602. assert_equal true, thru_opassign
  603. end
  604. def test_opassign_error
  605. thru_opassign = []
  606. events = [:on_opassign, :on_assign_error]
  607. parse('a::X ||= c 1', events) {|a,*b|
  608. thru_opassign << a
  609. }
  610. assert_equal events, thru_opassign
  611. end
  612. def test_param_error
  613. thru_param_error = false
  614. parse('def foo(A) end', :on_param_error) {thru_param_error = true}
  615. assert_equal true, thru_param_error
  616. thru_param_error = false
  617. parse('def foo($a) end', :on_param_error) {thru_param_error = true}
  618. assert_equal true, thru_param_error
  619. thru_param_error = false
  620. parse('def foo(@a) end', :on_param_error) {thru_param_error = true}
  621. assert_equal true, thru_param_error
  622. thru_param_error = false
  623. parse('def foo(@@a) end', :on_param_error) {thru_param_error = true}
  624. assert_equal true, thru_param_error
  625. end
  626. def test_params
  627. thru_params = false
  628. parse('a {||}', :on_params) {thru_params = true}
  629. assert_equal true, thru_params
  630. thru_params = false
  631. parse('a {|x|}', :on_params) {thru_params = true}
  632. assert_equal true, thru_params
  633. thru_params = false
  634. parse('a {|*x|}', :on_params) {thru_params = true}
  635. assert_equal true, thru_params
  636. end
  637. def test_paren
  638. thru_paren = false
  639. parse('()', :on_paren) {thru_paren = true}
  640. assert_equal true, thru_paren
  641. end
  642. def test_parse_error
  643. thru_parse_error = false
  644. parse('<>', :on_parse_error) {thru_parse_error = true}
  645. assert_equal true, thru_parse_error
  646. end
  647. def test_qwords_add
  648. thru_qwords_add = false
  649. parse('%w[a]', :on_qwords_add) {thru_qwords_add = true}
  650. assert_equal true, thru_qwords_add
  651. end
  652. def test_qwords_new
  653. thru_qwords_new = false
  654. parse('%w[]', :on_qwords_new) {thru_qwords_new = true}
  655. assert_equal true, thru_qwords_new
  656. end
  657. def test_redo
  658. thru_redo = false
  659. parse('redo', :on_redo) {thru_redo = true}
  660. assert_equal true, thru_redo
  661. end
  662. def test_regexp_add
  663. thru_regexp_add = false
  664. parse('/foo/', :on_regexp_add) {thru_regexp_add = true}
  665. assert_equal true, thru_regexp_add
  666. end
  667. def test_regexp_literal
  668. thru_regexp_literal = false
  669. parse('//', :on_regexp_literal) {thru_regexp_literal = true}
  670. assert_equal true, thru_regexp_literal
  671. end
  672. def test_regexp_new
  673. thru_regexp_new = false
  674. parse('//', :on_regexp_new) {thru_regexp_new = true}
  675. assert_equal true, thru_regexp_new
  676. end
  677. def test_rescue
  678. thru_rescue = false
  679. parsed = parse('begin; 1; rescue => e; 2; end', :on_rescue) {thru_rescue = true}
  680. assert_equal true, thru_rescue
  681. assert_match /1.*rescue/, parsed
  682. assert_match /rescue\(,var_field\(e\),\[2\]\)/, parsed
  683. end
  684. def test_rescue_mod
  685. thru_rescue_mod = false
  686. parsed = parse('1 rescue 2', :on_rescue_mod) {thru_rescue_mod = true}
  687. assert_equal true, thru_rescue_mod
  688. bug4716 = '[ruby-core:36248]'
  689. assert_equal "[rescue_mod(1,2)]", parsed, bug4716
  690. end
  691. def test_rest_param
  692. thru_rest_param = false
  693. parse('def a(*) end', :on_rest_param) {thru_rest_param = true}
  694. assert_equal true, thru_rest_param
  695. thru_rest_param = false
  696. parse('def a(*x) end', :on_rest_param) {thru_rest_param = true}
  697. assert_equal true, thru_rest_param
  698. end
  699. def test_retry
  700. thru_retry = false
  701. parse('retry', :on_retry) {thru_retry = true}
  702. assert_equal true, thru_retry
  703. end
  704. def test_return
  705. thru_return = false
  706. parse('return a', :on_return) {thru_return = true}
  707. assert_equal true, thru_return
  708. end
  709. def test_return0
  710. thru_return0 = false
  711. parse('return', :on_return0) {thru_return0 = true}
  712. assert_equal true, thru_return0
  713. end
  714. def test_sclass
  715. thru_sclass = false
  716. parse('class << a; end', :on_sclass) {thru_sclass = true}
  717. assert_equal true, thru_sclass
  718. end
  719. def test_string_add
  720. thru_string_add = false
  721. parse('"aa"', :on_string_add) {thru_string_add = true}
  722. assert_equal true, thru_string_add
  723. end
  724. def test_string_concat
  725. thru_string_concat = false
  726. parse('"a" "b"', :on_string_concat) {thru_string_concat = true}
  727. assert_equal true, thru_string_concat
  728. end
  729. def test_string_content
  730. thru_string_content = false
  731. parse('""', :on_string_content) {thru_string_content = true}
  732. assert_equal true, thru_string_content
  733. thru_string_content = false
  734. parse('"a"', :on_string_content) {thru_string_content = true}
  735. assert_equal true, thru_string_content
  736. thru_string_content = false
  737. parse('%[a]', :on_string_content) {thru_string_content = true}
  738. assert_equal true, thru_string_content
  739. thru_string_content = false
  740. parse('\'a\'', :on_string_content) {thru_string_content = true}
  741. assert_equal true, thru_string_content
  742. thru_string_content = false
  743. parse('%<a>', :on_string_content) {thru_string_content = true}
  744. assert_equal true, thru_string_content
  745. thru_string_content = false
  746. parse('%!a!', :on_string_content) {thru_string_content = true}
  747. assert_equal true, thru_string_content
  748. thru_string_content = false
  749. parse('%q!a!', :on_string_content) {thru_string_content = true}
  750. assert_equal true, thru_string_content
  751. thru_string_content = false
  752. parse('%Q!a!', :on_string_content) {thru_string_content = true}
  753. assert_equal true, thru_string_content
  754. end
  755. def test_string_dvar
  756. thru_string_dvar = false
  757. parse('"#$a"', :on_string_dvar) {thru_string_dvar = true}
  758. assert_equal true, thru_string_dvar
  759. thru_string_dvar = false
  760. parse('\'#$a\'', :on_string_dvar) {thru_string_dvar = true}
  761. assert_equal false, thru_string_dvar
  762. thru_string_dvar = false
  763. parse('"#@a"', :on_string_dvar) {thru_string_dvar = true}
  764. assert_equal true, thru_string_dvar
  765. thru_string_dvar = false
  766. parse('\'#@a\'', :on_string_dvar) {thru_string_dvar = true}
  767. assert_equal false, thru_string_dvar
  768. thru_string_dvar = false
  769. parse('"#@@a"', :on_string_dvar) {thru_string_dvar = true}
  770. assert_equal true, thru_string_dvar
  771. thru_string_dvar = false
  772. parse('\'#@@a\'', :on_string_dvar) {thru_string_dvar = true}
  773. assert_equal false, thru_string_dvar
  774. thru_string_dvar = false
  775. parse('"#$1"', :on_string_dvar) {thru_string_dvar = true}
  776. assert_equal true, thru_string_dvar
  777. thru_string_dvar = false
  778. parse('\'#$1\'', :on_string_dvar) {thru_string_dvar = true}
  779. assert_equal false, thru_string_dvar
  780. end
  781. def test_string_embexpr
  782. thru_string_embexpr = false
  783. parse('"#{}"', :on_string_embexpr) {thru_string_embexpr = true}
  784. assert_equal true, thru_string_embexpr
  785. thru_string_embexpr = false
  786. parse('\'#{}\'', :on_string_embexpr) {thru_string_embexpr = true}
  787. assert_equal false, thru_string_embexpr
  788. end
  789. def test_string_literal
  790. thru_string_literal = false
  791. parse('""', :on_string_literal) {thru_string_literal = true}
  792. assert_equal true, thru_string_literal
  793. end
  794. def test_super
  795. thru_super = false
  796. parse('super()', :on_super) {thru_super = true}
  797. assert_equal true, thru_super
  798. end
  799. def test_symbol
  800. thru_symbol = false
  801. parse(':a', :on_symbol) {thru_symbol = true}
  802. assert_equal true, thru_symbol
  803. thru_symbol = false
  804. parse(':$a', :on_symbol) {thru_symbol = true}
  805. assert_equal true, thru_symbol
  806. thru_symbol = false
  807. parse(':@a', :on_symbol) {thru_symbol = true}
  808. assert_equal true, thru_symbol
  809. thru_symbol = false
  810. parse(':@@a', :on_symbol) {thru_symbol = true}
  811. assert_equal true, thru_symbol
  812. thru_symbol = false
  813. parse(':==', :on_symbol) {thru_symbol = true}
  814. assert_equal true, thru_symbol
  815. end
  816. def test_symbol_literal
  817. thru_symbol_literal = false
  818. parse(':a', :on_symbol_literal) {thru_symbol_literal = true}
  819. assert_equal true, thru_symbol_literal
  820. end
  821. def test_top_const_field
  822. thru_top_const_field = false
  823. parse('::A=1', :on_top_const_field) {thru_top_const_field = true}
  824. assert_equal true, thru_top_const_field
  825. end
  826. def test_top_const_ref
  827. thru_top_const_ref = false
  828. parse('::A', :on_top_const_ref) {thru_top_const_ref = true}
  829. assert_equal true, thru_top_const_ref
  830. end
  831. def test_unary
  832. thru_unary = false
  833. parse('not a 1, 2', :on_unary) {thru_unary = true}
  834. assert_equal true, thru_unary
  835. thru_unary = false
  836. parse('not (a)', :on_unary) {thru_unary = true}
  837. assert_equal true, thru_unary
  838. thru_unary = false
  839. parse('!a', :on_unary) {thru_unary = true}
  840. assert_equal true, thru_unary
  841. thru_unary = false
  842. parse('-10', :on_unary) {thru_unary = true}
  843. assert_equal true, thru_unary
  844. thru_unary = false
  845. parse('-10*2', :on_unary) {thru_unary = true}
  846. assert_equal true, thru_unary
  847. thru_unary = false
  848. parse('-10.1', :on_unary) {thru_unary = true}
  849. assert_equal true, thru_unary
  850. thru_unary = false
  851. parse('-10.1*2', :on_unary) {thru_unary = true}
  852. assert_equal true, thru_unary
  853. thru_unary = false
  854. parse('-a', :on_unary) {thru_unary = true}
  855. assert_equal true, thru_unary
  856. thru_unary = false
  857. parse('+a', :on_unary) {thru_unary = true}
  858. assert_equal true, thru_unary
  859. thru_unary = false
  860. parse('~a', :on_unary) {thru_unary = true}
  861. assert_equal true, thru_unary
  862. thru_unary = false
  863. parse('not()', :on_unary) {thru_unary = true}
  864. assert_equal true, thru_unary
  865. end
  866. def test_undef
  867. thru_undef = false
  868. parse('undef a', :on_undef) {thru_undef = true}
  869. assert_equal true, thru_undef
  870. thru_undef = false
  871. parse('undef <=>', :on_undef) {thru_undef = true}
  872. assert_equal true, thru_undef
  873. thru_undef = false
  874. parse('undef a, b', :on_undef) {thru_undef = true}
  875. assert_equal true, thru_undef
  876. end
  877. def test_unless
  878. thru_unless = false
  879. parse('unless a; end', :on_unless) {thru_unless = true}
  880. assert_equal true, thru_unless
  881. end
  882. def test_unless_mod
  883. thru_unless_mod = false
  884. parse('nil unless a', :on_unless_mod) {thru_unless_mod = true}
  885. assert_equal true, thru_unless_mod
  886. end
  887. def test_until
  888. thru_until = false
  889. parse('until a; end', :on_until) {thru_until = true}
  890. assert_equal true, thru_until
  891. end
  892. def test_until_mod
  893. thru_until_mod = false
  894. parse('nil until a', :on_until_mod) {thru_until_mod = true}
  895. assert_equal true, thru_until_mod
  896. end
  897. def test_var_field
  898. thru_var_field = false
  899. parse('a = 1', :on_var_field) {thru_var_field = true}
  900. assert_equal true, thru_var_field
  901. thru_var_field = false
  902. parse('a += 1', :on_var_field) {thru_var_field = true}
  903. assert_equal true, thru_var_field
  904. end
  905. def test_when
  906. thru_when = false
  907. parse('case a when b; end', :on_when) {thru_when = true}
  908. assert_equal true, thru_when
  909. thru_when = false
  910. parse('case when a; end', :on_when) {thru_when = true}
  911. assert_equal true, thru_when
  912. end
  913. def test_while
  914. thru_while = false
  915. parse('while a; end', :on_while) {thru_while = true}
  916. assert_equal true, thru_while
  917. end
  918. def test_while_mod
  919. thru_while_mod = false
  920. parse('nil while a', :on_while_mod) {thru_while_mod = true}
  921. assert_equal true, thru_while_mod
  922. end
  923. def test_word_add
  924. thru_word_add = false
  925. parse('%W[a]', :on_word_add) {thru_word_add = true}
  926. assert_equal true, thru_word_add
  927. end
  928. def test_word_new
  929. thru_word_new = false
  930. parse('%W[a]', :on_word_new) {thru_word_new = true}
  931. assert_equal true, thru_word_new
  932. end
  933. def test_words_add
  934. thru_words_add = false
  935. parse('%W[a]', :on_words_add) {thru_words_add = true}
  936. assert_equal true, thru_words_add
  937. end
  938. def test_words_new
  939. thru_words_new = false
  940. parse('%W[]', :on_words_new) {thru_words_new = true}
  941. assert_equal true, thru_words_new
  942. end
  943. def test_xstring_add
  944. thru_xstring_add = false
  945. parse('`x`', :on_xstring_add) {thru_xstring_add = true}
  946. assert_equal true, thru_xstring_add
  947. end
  948. def test_xstring_literal
  949. thru_xstring_literal = false
  950. parse('``', :on_xstring_literal) {thru_xstring_literal = true}
  951. assert_equal true, thru_xstring_literal
  952. end
  953. def test_xstring_new
  954. thru_xstring_new = false
  955. parse('``', :on_xstring_new) {thru_xstring_new = true}
  956. assert_equal true, thru_xstring_new
  957. end
  958. def test_yield
  959. thru_yield = false
  960. parse('yield a', :on_yield) {thru_yield = true}
  961. assert_equal true, thru_yield
  962. end
  963. def test_yield0
  964. thru_yield0 = false
  965. parse('yield', :on_yield0) {thru_yield0 = true}
  966. assert_equal true, thru_yield0
  967. end
  968. def test_zsuper
  969. thru_zsuper = false
  970. parse('super', :on_zsuper) {thru_zsuper = true}
  971. assert_equal true, thru_zsuper
  972. end
  973. def test_local_variables
  974. cmd = 'command(w,[regexp_literal(regexp_add(regexp_new(),25 # ),/)])'
  975. div = 'binary(ref(w),/,25)'
  976. var = '[w]'
  977. bug1939 = '[ruby-core:24923]'
  978. assert_equal("[#{cmd}]", parse('w /25 # /'), bug1939)
  979. assert_equal("[assign(var_field(w),1),#{div}]", parse("w = 1; w /25 # /"), bug1939)
  980. assert_equal("[fcall(p,[],&block([w],[#{div}]))]", parse("p{|w|w /25 # /\n}"), bug1939)
  981. assert_equal("[def(p,[w],bodystmt([#{div}]))]", parse("def p(w)\nw /25 # /\nend"), bug1939)
  982. end
  983. def test_block_variables
  984. assert_equal("[fcall(proc,[],&block([],[void()]))]", parse("proc{|;y|}"))
  985. if defined?(Process::RLIMIT_AS)
  986. assert_in_out_err(["-I#{File.dirname(__FILE__)}", "-rdummyparser"],
  987. 'Process.setrlimit(Process::RLIMIT_AS,100*1024*1024); puts DummyParser.new("proc{|;y|!y}").parse',
  988. ["[fcall(proc,[],&block([],[unary(!,ref(y))]))]"], [], '[ruby-dev:39423]')
  989. end
  990. end
  991. def test_unterminated_regexp
  992. compile_error = false
  993. parse('/', :compile_error) {|e, msg| compile_error = msg}
  994. assert_equal("unterminated regexp meets end of file", compile_error)
  995. end
  996. end if ripper_test