PageRenderTime 4ms CodeModel.GetById 101ms app.highlight 120ms RepoModel.GetById 41ms app.codeStats 0ms

/vendor/bundle/ruby/1.9.1/gems/erubis-2.7.0/test/test-main.rb

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