PageRenderTime 30ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/training-web/vendor/bundle/gems/erubis-2.7.0/test/test-main.rb

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