PageRenderTime 474ms CodeModel.GetById 80ms RepoModel.GetById 68ms app.codeStats 0ms

/Bundles/eBundles/Ruby.tmbundle/Support/vendor/rcodetools/test/test_completion.rb

https://bitbucket.org/missdeer/luapack
Ruby | 640 lines | 537 code | 92 blank | 11 comment | 5 complexity | 165d0b2df64aa813f07042254df5616c MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.0, CC-BY-SA-3.0, ISC, LGPL-2.1, GPL-2.0
  1. $: << ".." << "../lib"
  2. require 'rcodetools/completion'
  3. require 'test/unit'
  4. class TestXMPCompletionFilter < Test::Unit::TestCase
  5. def doit(code, lineno, column=nil, options={})
  6. xmp = XMPCompletionFilter.new options
  7. xmp.candidates(code, lineno, column).sort
  8. end
  9. def test_complete_method__simple
  10. assert_equal(["length"], doit('"a".lengt', 1))
  11. assert_equal(["length"], doit('`echo a`.lengt', 1))
  12. end
  13. def test_complete_method__in_arg
  14. assert_equal(["length"], doit('print("a".lengt)', 1, 15))
  15. assert_equal(["length"], doit("print('a'.lengt)", 1, 15))
  16. assert_equal(["length"], doit("((a, b = 1 + 'a'.lengt))", 1, 22))
  17. end
  18. def test_complete_method__in_method
  19. assert_equal(["length"], doit(<<EOC, 2))
  20. def hoge
  21. "a".lengt
  22. end
  23. hoge
  24. EOC
  25. end
  26. def test_complete_method__in_not_passing_method
  27. ## FIXME I do not know how to handle not-passing method!!
  28. assert_equal([], doit(<<EOC, 2))
  29. def hoge
  30. "a".lengt
  31. end
  32. EOC
  33. end
  34. def test_complete_singleton_method
  35. assert_equal(["aaaaa", "aaaab"], doit(<<EOC, 6))
  36. a = 'a'
  37. def a.aaaaa
  38. end
  39. def a.aaaab
  40. end
  41. a.aa
  42. EOC
  43. end
  44. def test_complete_global_variable
  45. assert_equal(["$hoge"], doit(<<EOC, 2))
  46. $hoge = 100
  47. $ho
  48. EOC
  49. end
  50. def test_complete_global_variable__with_class
  51. assert_equal(["open"], doit(<<EOC, 2))
  52. $hoge = File
  53. $hoge.op
  54. EOC
  55. end
  56. def test_complete_instance_variable
  57. assert_equal(["@hoge"], doit(<<EOC, 2))
  58. @hoge = 100
  59. @ho
  60. EOC
  61. end
  62. def test_complete_class_variable_module
  63. assert_equal(["@@hoge"], doit(<<EOC, 3))
  64. module X
  65. @@hoge = 100
  66. @@ho
  67. end
  68. EOC
  69. end
  70. def test_complete_class_variable__in_class
  71. assert_equal(["@@hoge"], doit(<<EOC, 3))
  72. class X
  73. @@hoge = 100
  74. @@ho
  75. end
  76. EOC
  77. end
  78. def test_complete_class_variable__toplevel
  79. assert_equal(["@@hoge"], doit(<<EOC, 2))
  80. @@hoge = 100
  81. @@ho
  82. EOC
  83. end
  84. def test_complete_class_variable__in_method
  85. assert_equal(["@@hoge"], doit(<<EOC, 4))
  86. class Foo
  87. def foo
  88. @@hoge = 100
  89. @@ho
  90. end
  91. end
  92. Foo.new.foo
  93. EOC
  94. end
  95. def test_complete_constant__nested
  96. assert_equal(["Stat"], doit('File::Sta',1))
  97. end
  98. def test_complete_class_method
  99. assert_equal(["popen"], doit('File::pop',1))
  100. assert_equal(["popen"], doit('::File::pop',1))
  101. assert_equal(["popen"], doit('File.pop',1))
  102. assert_equal(["popen"], doit('::File.pop',1))
  103. assert_equal(["new"], doit('::File::Stat.ne',1))
  104. assert_equal(["new"], doit('File::Stat.ne',1))
  105. end
  106. def test_complete_constant__in_class
  107. assert_equal(["Fixclass", "Fixnum"], doit(<<EOC, 3))
  108. class Fixclass
  109. class Bar
  110. Fix
  111. end
  112. end
  113. EOC
  114. end
  115. def test_complete_toplevel_constant
  116. assert_equal(["Fixnum"], doit(<<EOC,3))
  117. class Foo
  118. class Fixnum
  119. ::Fixn
  120. end
  121. end
  122. EOC
  123. assert_equal(["Fixnum"], doit(<<EOC,3))
  124. class Foo
  125. class Fixnum
  126. ::Foo::Fixn
  127. end
  128. end
  129. EOC
  130. assert_equal(["Bar"], doit(<<EOC,5))
  131. class Foo
  132. class Bar
  133. end
  134. end
  135. ::Foo::B
  136. EOC
  137. end
  138. def test_bare_word__local_variable
  139. assert_equal(["aaaaaxx"], doit(<<EOC,2))
  140. aaaaaxx = 1
  141. aaaa
  142. EOC
  143. end
  144. def test_bare_word__method
  145. assert_equal(["trace_var"], doit("trace",1))
  146. end
  147. def test_bare_word__constant
  148. assert_equal(["Fixnum"], doit("Fixn",1))
  149. end
  150. def test_bare_word__method_in_class
  151. assert_equal(["attr_accessor"], doit(<<EOC,2))
  152. class X
  153. attr_acc
  154. end
  155. EOC
  156. end
  157. def test_bare_word__public_method
  158. assert_equal(["hoge"], doit(<<EOC,4))
  159. class X
  160. def hoge() end
  161. def boke
  162. hog
  163. end
  164. new.boke
  165. end
  166. EOC
  167. end
  168. def test_bare_word__private_method
  169. assert_equal(["hoge"], doit(<<EOC,5))
  170. class X
  171. def hoge() end
  172. private :hoge
  173. def boke
  174. hog
  175. end
  176. new.boke
  177. end
  178. EOC
  179. end
  180. def test_complete_symbol
  181. assert_equal([":vccaex"], doit(<<EOC,2))
  182. a = :vccaex
  183. :vcca
  184. EOC
  185. end
  186. #### tricky testcases
  187. def test_two_pass
  188. assert_equal(["inspect"], doit(<<EOC,2))
  189. [1, "a"].each do |x|
  190. x.inspec
  191. end
  192. EOC
  193. end
  194. def test_string
  195. assert_equal(["inspect"], doit('"()".inspe',1))
  196. assert_equal(["inspect"], doit('`echo ()`.inspe',1))
  197. end
  198. def test_not_last_line
  199. assert_equal(["inspect"], doit(<<EOC,1))
  200. "".inspe
  201. 1
  202. EOC
  203. end
  204. def test_column
  205. assert_equal(["length"], doit('print("a".lengt + "b".size)', 1, 15))
  206. end
  207. def test_method_chain__String
  208. assert_equal(["length"], doit('"a".upcase.capitalize.leng', 1))
  209. end
  210. def test_method_chain__Fixnum
  211. assert_equal(["length"], doit('1.to_s.upcase.leng', 1))
  212. end
  213. def test_multi_line__do
  214. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  215. [].each_with_ind do |x|
  216. end
  217. EOC
  218. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  219. [].each_with_ind()do |x|
  220. end
  221. EOC
  222. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  223. [].each_with_ind do |x,y| end
  224. EOC
  225. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  226. [1].each_with_index do |x,y| [].each do end end
  227. EOC
  228. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  229. [].each_with_ind ; do-do
  230. EOC
  231. end
  232. def test_multi_line__braces
  233. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  234. [].each_with_ind { |x|
  235. }
  236. EOC
  237. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  238. [].each_with_ind(){ |x|
  239. }
  240. EOC
  241. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  242. [].each_with_ind {|x,y| }
  243. EOC
  244. assert_equal(["each_with_index"], doit(<<EOC, 1, 16))
  245. [1].each_with_in {|x,y| [].each { }}
  246. EOC
  247. assert_equal(["each_with_index"], doit(<<EOC, 1, 20))
  248. { [1].each_with_inde {|x,y| [].each { }},1}
  249. EOC
  250. end
  251. def test_multi_line__brackets
  252. assert_equal(["each_with_index"], doit(<<EOC, 1, 20))
  253. [ [1].each_with_inde {|x,y| [].each { }},
  254. 1]
  255. EOC
  256. end
  257. def test_multi_line__parentheses
  258. assert_equal(["each_with_index"], doit(<<EOC, 1, 23))
  259. foo( [1].each_with_inde {|x,y| [].each { }},
  260. 1)
  261. EOC
  262. =begin FIXME
  263. assert_equal(["each_with_index"], doit(<<EOC, 2, 23))
  264. foo( 1,
  265. [1].each_with_inde {|x,y| [].each { }})
  266. EOC
  267. =end
  268. end
  269. def test_multi_line__control_structures__if
  270. assert_equal(["length"], doit(<<EOC, 1))
  271. if "a".leng
  272. end
  273. EOC
  274. assert_equal(["length"], doit(<<EOC, 1, 8))
  275. "a".leng if true
  276. EOC
  277. assert_equal(["length"], doit(<<EOC, 1, 8))
  278. "a".leng ; if true
  279. 1
  280. end
  281. EOC
  282. assert_equal(["length"], doit(<<EOC, 1, 8))
  283. "a".leng ;if true
  284. 1
  285. end
  286. EOC
  287. end
  288. def test_multi_line__control_structures__if_in_string
  289. assert_equal(["length"], doit(<<EOC, 1))
  290. "if a".leng
  291. EOC
  292. assert_equal(["length"], doit(<<EOC, 1))
  293. 'if a'.leng
  294. EOC
  295. assert_equal(["length"], doit(<<EOC, 1))
  296. `if a`.leng
  297. EOC
  298. end
  299. def test_multi_line__control_structures__other_keywords
  300. assert_equal(["length"], doit(<<EOC, 1))
  301. unless "a".leng
  302. end
  303. EOC
  304. assert_equal(["length"], doit(<<EOC, 1))
  305. while "a".leng
  306. end
  307. EOC
  308. assert_equal(["length"], doit(<<EOC, 1))
  309. until "a".leng
  310. end
  311. EOC
  312. assert_equal(["split"], doit(<<EOC, 1))
  313. for a in "a".spli
  314. end
  315. EOC
  316. end
  317. def test_phrase
  318. assert_equal(["uniq", "uniq!"], doit('Array.new(3).uni',1))
  319. assert_equal(["uniq", "uniq!"], doit('Array.new(3).to_a.uni',1))
  320. assert_equal(["uniq", "uniq!"], doit('Array.new(3).map{|x| x.to_i}.uni',1))
  321. assert_equal(["uniq", "uniq!"], doit('[][0,(1+1)].uni',1))
  322. end
  323. def test_percent__String
  324. assert_equal(["length"], doit('%!foo!.leng',1))
  325. assert_equal(["length"], doit('%q!foo!.leng',1))
  326. assert_equal(["length"], doit('%Q!foo!.leng',1))
  327. assert_equal(["length"], doit('%x!foo!.leng',1))
  328. assert_equal(["length"], doit('%{foo}.leng',1))
  329. assert_equal(["length"], doit('%q{foo}.leng',1))
  330. assert_equal(["length"], doit('%q!(!.leng',1))
  331. assert_equal(["length"], doit('%Q!(!.leng',1))
  332. assert_equal(["length"], doit('%x!(!.leng',1))
  333. assert_equal(["length"], doit('%x{(}.leng',1))
  334. assert_equal(["length"], doit('%{f(o)o}.leng',1))
  335. assert_equal(["length"], doit('%{f{o}o}.leng',1))
  336. assert_equal(["length"], doit('(%{f{o}o}+%!}x!).leng',1))
  337. end
  338. def test_percent__Array
  339. assert_equal(["length"], doit('%w!foo!.leng',1))
  340. assert_equal(["length"], doit('%W!foo!.leng',1))
  341. assert_equal(["length"], doit('%w{foo}.leng',1))
  342. assert_equal(["length"], doit('%W{foo}.leng',1))
  343. assert_equal(["length"], doit('%w!(!.leng',1))
  344. assert_equal(["length"], doit('%W!(!.leng',1))
  345. assert_equal(["length"], doit('%w{(}.leng',1))
  346. assert_equal(["length"], doit('%w{f(o)o}.leng',1))
  347. assert_equal(["length"], doit('%w{f{o}o}.leng',1))
  348. assert_equal(["length"], doit('(%W{f{o}o}+%w!}x!).leng',1))
  349. end
  350. def test_percent__Regexp
  351. assert_equal(["kcode"], doit('%r!foo!.kcod',1))
  352. assert_equal(["kcode"], doit('%r{foo}.kcod',1))
  353. assert_equal(["kcode"], doit('%r!(!.kcod',1))
  354. assert_equal(["kcode"], doit('%r{(}.kcod',1))
  355. assert_equal(["kcode"], doit('%r{f(o)o}.kcod',1))
  356. end
  357. def test_percent__Symbol
  358. assert_equal(["id2name"], doit('%s!foo!.id2nam',1))
  359. assert_equal(["id2name"], doit('%s{foo}.id2nam',1))
  360. assert_equal(["id2name"], doit('%s!(!.id2nam',1))
  361. assert_equal(["id2name"], doit('%s{(}.id2nam',1))
  362. assert_equal(["id2name"], doit('%s{f(o)o}.id2nam',1))
  363. end
  364. def test_complete_method__with_NoMethodError
  365. assert_equal(["module_function"], doit(<<EOC, 3, nil, :ignore_NoMethodError=>true))
  366. module X
  367. xx # normally NoMethodError
  368. module_funct
  369. end
  370. EOC
  371. end
  372. # drawback of ignore_NoMethodError
  373. def test_with_or_without_ignore_NoMethodError
  374. code = <<EOC
  375. a=[1]
  376. x = a[1][0] rescue "aaa"
  377. x.lengt
  378. EOC
  379. assert_equal(["length"], doit(code, 3))
  380. assert_equal([], doit(code, 3, nil, :ignore_NoMethodError=>true))
  381. end
  382. def test__syntax_error
  383. assert_raise(ProcessParticularLine::NewCodeError) do
  384. doit(<<EOC, 5)
  385. end
  386. module X
  387. def x
  388. end
  389. module_function
  390. end
  391. EOC
  392. end
  393. end
  394. def test__runtime_error
  395. assert_raise(ProcessParticularLine::NewCodeError) do
  396. doit(<<EOC, 5)
  397. __undefined_method__
  398. module X
  399. def x
  400. end
  401. module_function
  402. end
  403. EOC
  404. end
  405. end
  406. # This is a caveat!! You should use dabbrev for this case.
  407. def XXtest_oneline
  408. assert_equal(["aaa"], doit('aaa=1; aa', 1))
  409. end
  410. ################################################################
  411. def get_class(code, lineno, column=nil, options={})
  412. xmp = XMPCompletionFilter.new options
  413. klass, = xmp.runtime_data_with_class(code, lineno, column).sort
  414. klass
  415. end
  416. def test_class__Class
  417. assert_equal("File", get_class("File::n", 1))
  418. assert_equal("File", get_class("File.n", 1))
  419. assert_equal("File::Stat", get_class("File::Stat::n", 1))
  420. assert_equal("File::Stat", get_class("File::Stat.n", 1))
  421. assert_equal("FileTest", get_class("FileTest.exis", 1))
  422. assert_equal("FileTest", get_class("FileTest::exis", 1))
  423. end
  424. def test_class__NotClass
  425. assert_equal("Fixnum", get_class("1.ch", 1))
  426. assert_equal("String", get_class("'a'.siz", 1))
  427. end
  428. end
  429. class TestXMPCompletionVerboseFilter < Test::Unit::TestCase
  430. def doit(code, lineno, column=nil, options={})
  431. xmp = XMPCompletionVerboseFilter.new options
  432. xmp.candidates(code, lineno, column).sort
  433. end
  434. def test_complete_global_variable
  435. assert_equal(["$hoge"], doit(<<EOC, 2))
  436. $hoge = 100
  437. $ho
  438. EOC
  439. end
  440. def test_complete_instance_variable
  441. assert_equal(["@hoge"], doit(<<EOC, 2))
  442. @hoge = 100
  443. @ho
  444. EOC
  445. end
  446. def test_complete_class_variable_module
  447. assert_equal(["@@hoge"], doit(<<EOC, 3))
  448. module X
  449. @@hoge = 100
  450. @@ho
  451. end
  452. EOC
  453. end
  454. def test_complete_constant__nested
  455. assert_equal(["Stat"], doit('File::Sta',1))
  456. end
  457. def test_complete_class_method
  458. assert_equal(["popen\0IO.popen"], doit('File::pop',1))
  459. assert_equal(["popen\0IO.popen"], doit('::File::pop',1))
  460. assert_equal(["popen\0IO.popen"], doit('File.pop',1))
  461. assert_equal(["popen\0IO.popen"], doit('::File.pop',1))
  462. assert_equal(["new\0File::Stat.new"], doit('::File::Stat.ne', 1))
  463. assert_equal(["new\0File::Stat.new"], doit('File::Stat.ne',1))
  464. end
  465. def test_complete_constant__in_class
  466. assert_equal(["Fixclass", "Fixnum"], doit(<<EOC, 3))
  467. class Fixclass
  468. class Bar
  469. Fix
  470. end
  471. end
  472. EOC
  473. end
  474. def test_complete_toplevel_constant
  475. assert_equal(["Fixnum"], doit(<<EOC,3))
  476. class Foo
  477. class Fixnum
  478. ::Fixn
  479. end
  480. end
  481. EOC
  482. assert_equal(["Fixnum"], doit(<<EOC,3))
  483. class Foo
  484. class Fixnum
  485. ::Foo::Fixn
  486. end
  487. end
  488. EOC
  489. assert_equal(["Bar"], doit(<<EOC,5))
  490. class Foo
  491. class Bar
  492. end
  493. end
  494. ::Foo::B
  495. EOC
  496. end
  497. def test_complete_symbol
  498. assert_equal([":vccaex"], doit(<<EOC,2))
  499. a = :vccaex
  500. :vcca
  501. EOC
  502. end
  503. def test_method_chain__String
  504. assert_equal(["length\0String#length"], doit('"a".upcase.capitalize.leng', 1))
  505. end
  506. def test_bare_word__local_variable
  507. assert_equal(["aaaaaxx"], doit(<<EOC,2))
  508. aaaaaxx = 1
  509. aaaa
  510. EOC
  511. end
  512. def test_bare_word__method
  513. assert_equal(["trace_var\0Kernel#trace_var"], doit("trace",1))
  514. end
  515. def test_bare_word__constant
  516. assert_equal(["Fixnum"], doit("Fixn",1))
  517. end
  518. def test_bare_word__method_in_class
  519. assert_equal(["attr_accessor\0Module#attr_accessor"], doit(<<EOC,2))
  520. class X
  521. attr_acc
  522. end
  523. EOC
  524. end
  525. def test_bare_word__public_method
  526. assert_equal(["hoge\0X#hoge"], doit(<<EOC,4))
  527. class X
  528. def hoge() end
  529. def boke
  530. hog
  531. end
  532. new.boke
  533. end
  534. EOC
  535. end
  536. def test_bare_word__private_method
  537. assert_equal(["hoge\0X#hoge"], doit(<<EOC,5))
  538. class X
  539. def hoge() end
  540. private :hoge
  541. def boke
  542. hog
  543. end
  544. new.boke
  545. end
  546. EOC
  547. end
  548. end