PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/Ruby/ruby19/lib/ruby/gems/1.9.1/gems/erubis-2.6.6/test/test-main.rb

http://github.com/IronLanguages/main
Ruby | 721 lines | 571 code | 90 blank | 60 comment | 33 complexity | 0d9ae4338349b267909f2066e28c83da MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. ##
  2. ## $Rev$
  3. ## $Release: 2.6.6 $
  4. ## $Date$
  5. ##
  6. require "#{File.dirname(__FILE__)}/test.rb"
  7. require 'tempfile'
  8. require 'erubis/main'
  9. $script = File.dirname(TESTDIR) + '/bin/erubis'
  10. #if test(?f, 'bin/erubis')
  11. # $script = 'bin/erubis'
  12. #elsif test(?f, '../bin/erubis')
  13. # $script = '../bin/erubis'
  14. #end
  15. class StringWriter < String
  16. def write(arg)
  17. self << arg
  18. end
  19. def flush(*args)
  20. # pass
  21. end
  22. end
  23. class Erubis::Main
  24. public :usage
  25. public :show_properties
  26. public :show_enhancers
  27. end
  28. class MainTest < Test::Unit::TestCase
  29. INPUT = <<'END'
  30. list:
  31. <% list = ['<aaa>', 'b&b', '"ccc"']
  32. for item in list %>
  33. - <%= item %>
  34. <% end %>
  35. user: <%= defined?(user) ? user : "(none)" %>
  36. END
  37. INPUT2 = INPUT.gsub(/\blist([^:])/, '@list\1').gsub(/\buser([^:])/, '@user\1')
  38. # SRC = <<'END'
  39. #_buf = ''; _buf << "list:\n"
  40. # list = ['<aaa>', 'b&b', '"ccc"']
  41. # for item in list
  42. #_buf << " - "; _buf << ( item ).to_s; _buf << "\n"
  43. # end
  44. #_buf << "user: "; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << "\n"
  45. #_buf
  46. #END
  47. SRC = <<'END'
  48. _buf = ''; _buf << 'list:
  49. '; list = ['<aaa>', 'b&b', '"ccc"']
  50. for item in list
  51. _buf << ' - '; _buf << ( item ).to_s; _buf << '
  52. '; end
  53. _buf << 'user: '; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << '
  54. ';
  55. _buf.to_s
  56. END
  57. # SRC2 = SRC.gsub(/\blist /, '@list ').gsub(/\buser /, '@user ')
  58. OUTPUT = <<'END'
  59. list:
  60. - <aaa>
  61. - b&b
  62. - "ccc"
  63. user: (none)
  64. END
  65. ESCAPED_OUTPUT = <<'END'
  66. list:
  67. - &lt;aaa&gt;
  68. - b&amp;b
  69. - &quot;ccc&quot;
  70. user: (none)
  71. END
  72. PI_INPUT = <<'END'
  73. <ul>
  74. <?rb @list = ['<aaa>', 'b&b', '"ccc"']
  75. for item in @list ?>
  76. <li>@{item}@ / @!{item}@
  77. <%= item %> / <%== item %></li>
  78. <?rb end ?>
  79. <ul>
  80. END
  81. PI_SRC = <<'END'
  82. _buf = ''; _buf << '<ul>
  83. '; @list = ['<aaa>', 'b&b', '"ccc"']
  84. for item in @list
  85. _buf << ' <li>'; _buf << Erubis::XmlHelper.escape_xml(item); _buf << ' / '; _buf << (item).to_s; _buf << '
  86. '; _buf << ( item ).to_s; _buf << ' / '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</li>
  87. '; end
  88. _buf << '<ul>
  89. ';
  90. _buf.to_s
  91. END
  92. PI_ESCAPED_SRC = <<'END'
  93. _buf = ''; _buf << '<ul>
  94. '; @list = ['<aaa>', 'b&b', '"ccc"']
  95. for item in @list
  96. _buf << ' <li>'; _buf << (item).to_s; _buf << ' / '; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '
  97. '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << ' / '; _buf << ( item ).to_s; _buf << '</li>
  98. '; end
  99. _buf << '<ul>
  100. ';
  101. _buf.to_s
  102. END
  103. PI_OUTPUT = <<'END'
  104. <ul>
  105. <li>&lt;aaa&gt; / <aaa>
  106. <aaa> / &lt;aaa&gt;</li>
  107. <li>b&amp;b / b&b
  108. b&b / b&amp;b</li>
  109. <li>&quot;ccc&quot; / "ccc"
  110. "ccc" / &quot;ccc&quot;</li>
  111. <ul>
  112. END
  113. PI_ESCAPED_OUTPUT = <<'END'
  114. <ul>
  115. <li><aaa> / &lt;aaa&gt;
  116. &lt;aaa&gt; / <aaa></li>
  117. <li>b&b / b&amp;b
  118. b&amp;b / b&b</li>
  119. <li>"ccc" / &quot;ccc&quot;
  120. &quot;ccc&quot; / "ccc"</li>
  121. <ul>
  122. END
  123. def _test()
  124. if @filename.nil?
  125. method = (caller[0] =~ /in `(.*)'/) && $1 #'
  126. method =~ /block in (.*)/ and method = $1 # for Ruby 1.9
  127. @filename = "tmp.#{method}"
  128. end
  129. File.open(@filename, 'w') {|f| f.write(@input) } if @filename
  130. begin
  131. argv = @options.is_a?(Array) ? @options.dup : @options.split
  132. argv << @filename if @filename
  133. $stdout = output = StringWriter.new
  134. Erubis::Main.new.execute(argv)
  135. ensure
  136. $stdout = STDOUT
  137. File.unlink(@filename) if @filename && test(?f, @filename)
  138. end
  139. assert_text_equal(@expected, output)
  140. end
  141. def _error_test(errclass, errmsg)
  142. ex = assert_raise(errclass) { _test() }
  143. assert_equal(errmsg, ex.message)
  144. end
  145. def test_help # -h
  146. @options = '-h'
  147. m = Erubis::Main.new
  148. @expected = m.usage() + "\n" + m.show_properties() + m.show_enhancers()
  149. @filename = false
  150. _test()
  151. end
  152. def test_version # -v
  153. @options = '-v'
  154. @expected = (("$Release: 2.6.6 $" =~ /[.\d]+/) && $&) + "\n"
  155. @filename = false
  156. _test()
  157. end
  158. def test_basic1
  159. @input = INPUT
  160. @expected = OUTPUT
  161. @options = ''
  162. _test()
  163. end
  164. def test_source1 # -x
  165. @input = INPUT
  166. @expected = SRC
  167. @options = '-x'
  168. _test()
  169. end
  170. def _with_dummy_file
  171. bindir = File.join(File.dirname(File.dirname(__FILE__)), 'bin')
  172. env_path = ENV['PATH']
  173. env__ = ENV['_']
  174. begin
  175. ENV['PATH'] = bindir + File::PATH_SEPARATOR + ENV['PATH']
  176. ENV['_'] = 'erubis'
  177. Tempfile.open(self.name.gsub(/[^\w]/,'_')) do |f|
  178. f.write(INPUT)
  179. f.flush
  180. yield(f.path)
  181. end
  182. ensure
  183. ENV['PATH'] = env_path
  184. ENV['_'] = env__ if env__
  185. end
  186. end
  187. def test_syntax1 # -z (syntax ok)
  188. @input = INPUT
  189. @expected = "Syntax OK\n"
  190. @options = '-z'
  191. _test()
  192. #
  193. _with_dummy_file do |filepath|
  194. actual = `erubis #{@options} #{filepath}`
  195. assert_equal @expected, actual
  196. end
  197. end
  198. def test_syntax2 # -z (syntax error)
  199. inputs = []
  200. inputs << <<'END'
  201. <ul>
  202. <% for item in list %>
  203. <li><%= item[:name]] %></li>
  204. <% end %>
  205. </ul>
  206. END
  207. inputs << <<'END'
  208. <ul>
  209. <% for item in list %>
  210. <li><%= item[:name] %></li>
  211. <% edn %>
  212. </ul>
  213. END
  214. basename = 'tmp.test_syntax2_%d.rhtml'
  215. filenames = [ basename % 0, basename % 1 ]
  216. errmsgs = []
  217. if ruby19?
  218. errmsgs << <<'END'
  219. 3: syntax error, unexpected ']', expecting ')'
  220. _buf << ' <li>'; _buf << ( item[:name]] ).to_s; _buf << '</li>
  221. ^
  222. -:4: syntax error, unexpected keyword_end, expecting ')'
  223. '; end
  224. ^
  225. -:7: syntax error, unexpected $end, expecting ')'
  226. END
  227. errmsgs << <<'END'
  228. 7: syntax error, unexpected $end, expecting keyword_end
  229. END
  230. else
  231. errmsgs << <<'END'
  232. 3: syntax error, unexpected ']', expecting ')'
  233. _buf << ' <li>'; _buf << ( item[:name]] ).to_s; _buf << '</li>
  234. ^
  235. -:4: syntax error, unexpected kEND, expecting ')'
  236. '; end
  237. ^
  238. -:7: syntax error, unexpected $end, expecting ')'
  239. END
  240. errmsgs << <<'END'
  241. 7: syntax error, unexpected $end, expecting kEND
  242. END
  243. end
  244. #
  245. max = inputs.length
  246. (0...max).each do |i|
  247. @input = inputs[i]
  248. @expected = "tmp.test_syntax2:#{errmsgs[i]}"
  249. @options = '-z'
  250. _test()
  251. end
  252. #
  253. begin
  254. (0...max).each do |i|
  255. File.open(filenames[i], 'w') {|f| f.write(inputs[i]) }
  256. end
  257. @input = '<ok/>'
  258. @expected = ''
  259. @options = '-z'
  260. (0...max).each do |i|
  261. @expected << "#{filenames[i]}:#{errmsgs[i]}"
  262. @options << " #{filenames[i]}"
  263. end
  264. _test()
  265. ensure
  266. (0...max).each do |i|
  267. File.unlink(filenames[i]) if test(?f, filenames[i])
  268. end
  269. end
  270. end
  271. def test_pattern1 # -p
  272. @input = INPUT.gsub(/<%/, '<!--%').gsub(/%>/, '%-->')
  273. @expected = OUTPUT
  274. #@options = "-p '<!--% %-->'"
  275. @options = ["-p", "<!--% %-->"]
  276. _test()
  277. end
  278. # def test_class1 # -C
  279. # @input = INPUT
  280. # @expected = OUTPUT.gsub(/<aaa>/, '&lt;aaa&gt;').gsub(/b&b/, 'b&amp;b').gsub(/"ccc"/, '&quot;ccc&quot;')
  281. # @options = "-C XmlEruby"
  282. # _test()
  283. # end
  284. def test_notrim1 # --trim=false
  285. @input = INPUT
  286. @expected = <<'END'
  287. list:
  288. - <aaa>
  289. - b&b
  290. - "ccc"
  291. user: (none)
  292. END
  293. @options = "--trim=false" # -T
  294. _test()
  295. end
  296. def test_notrim2 # --trim=false
  297. @input = INPUT
  298. # @expected = <<'END'
  299. #_buf = ''; _buf << "list:\n"
  300. # list = ['<aaa>', 'b&b', '"ccc"']
  301. # for item in list ; _buf << "\n"
  302. #_buf << " - "; _buf << ( item ).to_s; _buf << "\n"
  303. # end ; _buf << "\n"
  304. #_buf << "user: "; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << "\n"
  305. #_buf
  306. #END
  307. @expected = <<'END'
  308. _buf = ''; _buf << 'list:
  309. '; list = ['<aaa>', 'b&b', '"ccc"']
  310. for item in list ; _buf << '
  311. '; _buf << ' - '; _buf << ( item ).to_s; _buf << '
  312. '; end ; _buf << '
  313. '; _buf << 'user: '; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << '
  314. ';
  315. _buf.to_s
  316. END
  317. @options = "-x --trim=false" # -xT
  318. _test()
  319. end
  320. #--
  321. #def test_context1
  322. # @input = INPUT
  323. # @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
  324. # @options = '--user=Hello'
  325. # _test()
  326. #end
  327. #++
  328. def test_datafile1 # -f data.yaml
  329. datafile = "test.context1.yaml"
  330. @input = INPUT2
  331. @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
  332. @options = "-f #{datafile}"
  333. #
  334. str = <<-END
  335. user: Hello
  336. password: world
  337. END
  338. File.open(datafile, 'w') {|f| f.write(str) }
  339. begin
  340. _test()
  341. ensure
  342. File.unlink(datafile) if test(?f, datafile)
  343. end
  344. end
  345. def test_datafile2 # -f data.rb
  346. datafile = "test.context1.rb"
  347. @input = INPUT2
  348. @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
  349. @options = "-f #{datafile}"
  350. #
  351. str = <<-END
  352. @user = 'Hello'
  353. @password = 'world'
  354. END
  355. File.open(datafile, 'w') {|f| f.write(str) }
  356. begin
  357. _test()
  358. ensure
  359. File.unlink(datafile) if test(?f, datafile)
  360. end
  361. end
  362. def test_untabify1 # -t (obsolete)
  363. yamlfile = "test.context2.yaml"
  364. @input = INPUT2
  365. @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
  366. @options = "-tf #{yamlfile}"
  367. #
  368. yaml = <<-END
  369. user: Hello
  370. password: world
  371. END
  372. File.open(yamlfile, 'w') {|f| f.write(yaml) }
  373. begin
  374. _test()
  375. ensure
  376. File.unlink(yamlfile) if test(?f, yamlfile)
  377. end
  378. end
  379. def test_untabify2 # -T
  380. yamlfile = "test.context2.yaml"
  381. @input = INPUT2
  382. @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
  383. @options = "-Tf #{yamlfile}"
  384. #
  385. yaml = <<-END
  386. user: Hello
  387. items:
  388. - aaa
  389. - bbb
  390. - ccc
  391. END
  392. File.open(yamlfile, 'w') {|f| f.write(yaml) }
  393. assert_raise(ArgumentError) do
  394. _test()
  395. end
  396. File.open(yamlfile, 'w') {|f| f.write(yaml.gsub(/\t/, ' '*8)) }
  397. _test()
  398. ensure
  399. File.unlink(yamlfile) if test(?f, yamlfile)
  400. end
  401. def test_symbolify1 # -S
  402. yamlfile = "test.context3.yaml"
  403. @input = <<END
  404. <% for h in @list %>
  405. <tr>
  406. <td><%= h[:name] %></td><td><%= h[:mail] %></td>
  407. </tr>
  408. <% end %>
  409. END
  410. @expected = <<END
  411. <tr>
  412. <td>foo</td><td>foo@mail.com</td>
  413. </tr>
  414. <tr>
  415. <td>bar</td><td>bar@mail.org</td>
  416. </tr>
  417. END
  418. @options = "-f #{yamlfile} -S"
  419. #
  420. yaml = <<-END
  421. list:
  422. - name: foo
  423. mail: foo@mail.com
  424. - name: bar
  425. mail: bar@mail.org
  426. END
  427. File.open(yamlfile, 'w') { |f| f.write(yaml) }
  428. begin
  429. _test()
  430. ensure
  431. File.unlink(yamlfile) if test(?f, yamlfile)
  432. end
  433. end
  434. def test_result1 # -B
  435. yamlfile = "test.context4.yaml"
  436. #
  437. @input = <<'END'
  438. user = <%= user %>
  439. <% for item in list %>
  440. - <%= item %>
  441. <% end %>
  442. END
  443. @expected = <<'END'
  444. user = World
  445. - aaa
  446. - bbb
  447. - ccc
  448. END
  449. @options = "-f #{yamlfile} -B "
  450. #
  451. yaml = <<-END
  452. user: World
  453. list:
  454. - aaa
  455. - bbb
  456. - ccc
  457. END
  458. File.open(yamlfile, 'w') {|f| f.write(yaml) }
  459. begin
  460. _test()
  461. ensure
  462. File.unlink(yamlfile) if test(?f, yamlfile)
  463. end
  464. end
  465. def test_context1 # -c
  466. @input = <<'END'
  467. user = <%= @user %>
  468. <% for item in @list %>
  469. - <%= item %>
  470. <% end %>
  471. END
  472. @expected = <<'END'
  473. user = World
  474. - aaa
  475. - bbb
  476. - ccc
  477. END
  478. #
  479. @options = ['-c', '{user: World, list: [aaa, bbb, ccc]}']
  480. _test()
  481. @options = ['-c', '@user="World"; @list=%w[aaa bbb ccc]']
  482. _test()
  483. end
  484. def test_include1 # -I
  485. dir = 'foo'
  486. lib = 'bar'
  487. Dir.mkdir dir unless test(?d, dir)
  488. filename = "#{dir}/#{lib}.rb"
  489. File.open(filename, 'w') do |f|
  490. f.write <<-'END'
  491. def escape(str)
  492. return "<#{str.upcase}>"
  493. end
  494. END
  495. end
  496. #
  497. @input = "<% require '#{lib}' %>\n" + INPUT.gsub(/<%= item %>/, '<%= escape(item) %>')
  498. @expected = OUTPUT.gsub(/<aaa>/, '<<AAA>>').gsub(/b\&b/, '<B&B>').gsub(/"ccc"/, '<"CCC">')
  499. @options = "-I #{dir}"
  500. #
  501. begin
  502. _test()
  503. ensure
  504. File.unlink filename if test(?f, filename)
  505. Dir.rmdir dir if test(?d, dir)
  506. end
  507. end
  508. def test_require1 # -r
  509. dir = 'foo'
  510. lib = 'bar'
  511. Dir.mkdir dir unless test(?d, dir)
  512. filename = "#{dir}/#{lib}.rb"
  513. File.open(filename, 'w') do |f|
  514. f.write <<-'END'
  515. def escape(str)
  516. return "<#{str.upcase}>"
  517. end
  518. END
  519. end
  520. #
  521. @input = INPUT.gsub(/<%= item %>/, '<%= escape(item) %>')
  522. @expected = OUTPUT.gsub(/<aaa>/, '<<AAA>>').gsub(/b\&b/, '<B&B>').gsub(/"ccc"/, '<"CCC">')
  523. @options = "-I #{dir} -r #{lib}"
  524. #
  525. begin
  526. _test()
  527. ensure
  528. File.unlink filename if test(?f, filename)
  529. Dir.rmdir dir if test(?d, dir)
  530. end
  531. end
  532. def test_enhancers1 # -E
  533. @input = <<END
  534. <% list = %w[<aaa> b&b "ccc"] %>
  535. % for item in list
  536. - <%= item %> : <%== item %>
  537. - [= item =] : [== item =]
  538. % end
  539. END
  540. @expected = <<END
  541. - &lt;aaa&gt; : <aaa>
  542. - &lt;aaa&gt; : <aaa>
  543. - b&amp;b : b&b
  544. - b&amp;b : b&b
  545. - &quot;ccc&quot; : "ccc"
  546. - &quot;ccc&quot; : "ccc"
  547. END
  548. @options = "-E Escape,PercentLine,HeaderFooter,BiPattern"
  549. _test()
  550. end
  551. def test_bodyonly1 # -b
  552. @input = INPUT
  553. @expected = SRC.sub(/\A_buf = '';/,'').sub(/\n_buf.to_s\n\z/,'')
  554. @options = '-b -x'
  555. _test()
  556. end
  557. def test_escape1 # -e
  558. @input = INPUT
  559. @expected = SRC.gsub(/<< \((.*?)\).to_s;/, '<< Erubis::XmlHelper.escape_xml(\1);')
  560. @options = '-ex'
  561. _test()
  562. end
  563. def test_invalid_option # -1 (invalid option)
  564. @input = INPUT
  565. @options = '-1'
  566. _error_test(Erubis::CommandOptionError, "-1: unknown option.")
  567. end
  568. def test_invalid_enhancer # -E hoge
  569. @options = '-E hoge'
  570. errmsg = "hoge: no such Enhancer (try '-h' to show all enhancers)."
  571. _error_test(Erubis::CommandOptionError, errmsg)
  572. end
  573. def test_invalid_lang # -l hoge
  574. @options = '-l hoge'
  575. errmsg = "-l hoge: invalid language name (class Erubis::Ehoge not found)."
  576. _error_test(Erubis::CommandOptionError, errmsg)
  577. end
  578. def test_missing_argument # -E
  579. @filename = false
  580. @options = '-E'
  581. _error_test(Erubis::CommandOptionError, "-E: enhancers required.")
  582. @options = '-l'
  583. _error_test(Erubis::CommandOptionError, "-l: lang required.")
  584. end
  585. def test_pi1 # --pi -x
  586. @input = PI_INPUT
  587. @expected = PI_SRC
  588. @options = '-x --pi'
  589. _test()
  590. end
  591. def test_pi2 # --pi -x --escape=false
  592. @input = PI_INPUT
  593. @expected = PI_ESCAPED_SRC
  594. @options = '-x --pi --escape=false'
  595. _test()
  596. end
  597. def test_pi3 # --pi
  598. @input = PI_INPUT
  599. @expected = PI_OUTPUT
  600. @options = '--pi'
  601. _test()
  602. end
  603. def test_pi4 # --pi --escape=false
  604. @input = PI_INPUT
  605. @expected = PI_ESCAPED_OUTPUT
  606. @options = '--pi --escape=false'
  607. _test()
  608. end
  609. def test_pi5 # --pi=ruby -x
  610. @input = PI_INPUT.gsub(/<\?rb/, '<?ruby')
  611. @expected = PI_SRC
  612. @options = '--pi=ruby -x'
  613. _test()
  614. end
  615. def test_pi6 # --pi -xl java
  616. @input = <<'END'
  617. <?java for (int i = 0; i < arr.length; i++) { ?>
  618. - @{arr[i]}@ / @!{arr[i]}@
  619. <?java } ?>
  620. END
  621. @expected = <<'END'
  622. StringBuffer _buf = new StringBuffer(); for (int i = 0; i < arr.length; i++) {
  623. _buf.append(" - "); _buf.append(escape(arr[i])); _buf.append(" / "); _buf.append(arr[i]); _buf.append("\n");
  624. }
  625. return _buf.toString();
  626. END
  627. @options = '--pi -xl java'
  628. _test()
  629. end
  630. self.post_definition()
  631. end