123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752 |
- ##
- ## $Rev$
- ## $Release: 2.7.0 $
- ## $Date$
- ##
- require "#{File.dirname(__FILE__)}/test.rb"
- require 'tempfile'
- require 'fileutils'
- require 'erubis/main'
- $script = File.dirname(TESTDIR) + '/bin/erubis'
- #if test(?f, 'bin/erubis')
- # $script = 'bin/erubis'
- #elsif test(?f, '../bin/erubis')
- # $script = '../bin/erubis'
- #end
- class StringWriter < String
- def write(arg)
- self << arg
- end
- def flush(*args)
- # pass
- end
- def puts(arg)
- case arg
- when Array
- arg.each do |item|
- self << item << "\n"
- end
- else
- self << arg.to_s
- self << "\n" unless arg =~ /\n$/
- end
- end
- end
- class Erubis::Main
- public :usage
- public :show_properties
- public :show_enhancers
- end
- class MainTest < Test::Unit::TestCase
- INPUT = <<'END'
- list:
- <% list = ['<aaa>', 'b&b', '"ccc"']
- for item in list %>
- - <%= item %>
- <% end %>
- user: <%= defined?(user) ? user : "(none)" %>
- END
- INPUT2 = INPUT.gsub(/\blist([^:])/, '@list\1').gsub(/\buser([^:])/, '@user\1')
- # SRC = <<'END'
- #_buf = ''; _buf << "list:\n"
- # list = ['<aaa>', 'b&b', '"ccc"']
- # for item in list
- #_buf << " - "; _buf << ( item ).to_s; _buf << "\n"
- # end
- #_buf << "user: "; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << "\n"
- #_buf
- #END
- SRC = <<'END'
- _buf = ''; _buf << 'list:
- '; list = ['<aaa>', 'b&b', '"ccc"']
- for item in list
- _buf << ' - '; _buf << ( item ).to_s; _buf << '
- '; end
- _buf << 'user: '; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << '
- ';
- _buf.to_s
- END
- # SRC2 = SRC.gsub(/\blist /, '@list ').gsub(/\buser /, '@user ')
- OUTPUT = <<'END'
- list:
- - <aaa>
- - b&b
- - "ccc"
- user: (none)
- END
- ESCAPED_OUTPUT = <<'END'
- list:
- - <aaa>
- - b&b
- - "ccc"
- user: (none)
- END
- PI_INPUT = <<'END'
- <ul>
- <?rb @list = ['<aaa>', 'b&b', '"ccc"']
- for item in @list ?>
- <li>@{item}@ / @!{item}@
- <%= item %> / <%== item %></li>
- <?rb end ?>
- <ul>
- END
- PI_SRC = <<'END'
- _buf = ''; _buf << '<ul>
- '; @list = ['<aaa>', 'b&b', '"ccc"']
- for item in @list
- _buf << ' <li>'; _buf << Erubis::XmlHelper.escape_xml(item); _buf << ' / '; _buf << (item).to_s; _buf << '
- '; _buf << ( item ).to_s; _buf << ' / '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</li>
- '; end
- _buf << '<ul>
- ';
- _buf.to_s
- END
- PI_ESCAPED_SRC = <<'END'
- _buf = ''; _buf << '<ul>
- '; @list = ['<aaa>', 'b&b', '"ccc"']
- for item in @list
- _buf << ' <li>'; _buf << (item).to_s; _buf << ' / '; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '
- '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << ' / '; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '<ul>
- ';
- _buf.to_s
- END
- PI_OUTPUT = <<'END'
- <ul>
- <li><aaa> / <aaa>
- <aaa> / <aaa></li>
- <li>b&b / b&b
- b&b / b&b</li>
- <li>"ccc" / "ccc"
- "ccc" / "ccc"</li>
- <ul>
- END
- PI_ESCAPED_OUTPUT = <<'END'
- <ul>
- <li><aaa> / <aaa>
- <aaa> / <aaa></li>
- <li>b&b / b&b
- b&b / b&b</li>
- <li>"ccc" / "ccc"
- "ccc" / "ccc"</li>
- <ul>
- END
- def _test()
- if @filename.nil?
- method = (caller[0] =~ /in `(.*)'/) && $1 #'
- method =~ /block in (.*)/ and method = $1 # for Ruby 1.9
- @filename = "tmp.#{method}"
- end
- File.open(@filename, 'w') {|f| f.write(@input) } if @filename
- begin
- argv = @options.is_a?(Array) ? @options.dup : @options.split
- argv << @filename if @filename
- $stdout = output = StringWriter.new
- Erubis::Main.new.execute(argv)
- ensure
- $stdout = STDOUT
- File.unlink(@filename) if @filename && test(?f, @filename)
- end
- assert_text_equal(@expected, output)
- end
- def _error_test(errclass, errmsg)
- ex = assert_raise(errclass) { _test() }
- assert_equal(errmsg, ex.message)
- end
- def test_help # -h
- @options = '-h'
- m = Erubis::Main.new
- @expected = m.usage() + "\n" + m.show_properties() + m.show_enhancers()
- @filename = false
- _test()
- end
- def test_version # -v
- @options = '-v'
- @expected = (("$Release: 2.7.0 $" =~ /[.\d]+/) && $&) + "\n"
- @filename = false
- _test()
- end
- def test_basic1
- @input = INPUT
- @expected = OUTPUT
- @options = ''
- _test()
- end
- def test_source1 # -x
- @input = INPUT
- @expected = SRC
- @options = '-x'
- _test()
- end
- def _with_dummy_file
- bindir = File.join(File.dirname(File.dirname(__FILE__)), 'bin')
- env_path = ENV['PATH']
- env__ = ENV['_']
- begin
- ENV['PATH'] = bindir + File::PATH_SEPARATOR + ENV['PATH']
- ENV['_'] = 'erubis'
- Tempfile.open(self.name.gsub(/[^\w]/,'_')) do |f|
- f.write(INPUT)
- f.flush
- yield(f.path)
- end
- ensure
- ENV['PATH'] = env_path
- ENV['_'] = env__ if env__
- end
- end
- def test_syntax1 # -z (syntax ok)
- @input = INPUT
- @expected = "Syntax OK\n"
- @options = '-z'
- _test()
- #
- _with_dummy_file do |filepath|
- actual = `erubis #{@options} #{filepath}`
- assert_equal @expected, actual
- end
- end
- def test_syntax2 # -z (syntax error)
- inputs = []
- inputs << <<'END'
- <ul>
- <% for item in list %>
- <li><%= item[:name]] %></li>
- <% end %>
- </ul>
- END
- inputs << <<'END'
- <ul>
- <% for item in list %>
- <li><%= item[:name] %></li>
- <% edn %>
- </ul>
- END
- basename = 'tmp.test_syntax2_%d.rhtml'
- filenames = [ basename % 0, basename % 1 ]
- errmsgs = []
- if ruby19?
- errmsgs << <<'END'
- 3: syntax error, unexpected ']', expecting ')'
- _buf << ' <li>'; _buf << ( item[:name]] ).to_s; _buf << '</li>
- ^
- -:4: syntax error, unexpected keyword_end, expecting ')'
- '; end
- ^
- -:7: syntax error, unexpected $end, expecting ')'
- END
- errmsgs << <<'END'
- 7: syntax error, unexpected $end, expecting keyword_end
- END
- elsif rubinius?
- errmsgs << <<'END'
- 3: expecting ')'
- _buf << ' <li>'; _buf << ( item[:name]] ).to_s; _buf << '</li>
- ^
- END
- errmsgs << <<'END'
- 7: missing 'end' for 'for' started on line 2
- _buf.to_s
- ^
- END
- else
- errmsgs << <<'END'
- 3: syntax error, unexpected ']', expecting ')'
- _buf << ' <li>'; _buf << ( item[:name]] ).to_s; _buf << '</li>
- ^
- -:4: syntax error, unexpected kEND, expecting ')'
- '; end
- ^
- -:7: syntax error, unexpected $end, expecting ')'
- END
- errmsgs << <<'END'
- 7: syntax error, unexpected $end, expecting kEND
- END
- end
- #
- max = inputs.length
- (0...max).each do |i|
- @input = inputs[i]
- @expected = "tmp.test_syntax2:#{errmsgs[i]}"
- @options = '-z'
- if rubinius?
- @expected.sub! /unexpected kEND/, 'unexpected keyword_end'
- @expected.sub! /expecting kEND/, 'expecting keyword_end'
- end
- _test()
- end
- #
- begin
- (0...max).each do |i|
- File.open(filenames[i], 'w') {|f| f.write(inputs[i]) }
- end
- @input = '<ok/>'
- @expected = ''
- @options = '-z'
- (0...max).each do |i|
- @expected << "#{filenames[i]}:#{errmsgs[i]}"
- @options << " #{filenames[i]}"
- end
- if rubinius?
- @expected.sub! /unexpected kEND/, 'unexpected keyword_end'
- @expected.sub! /expecting kEND/, 'expecting keyword_end'
- end
- _test()
- ensure
- (0...max).each do |i|
- File.unlink(filenames[i]) if test(?f, filenames[i])
- end
- end
- end
- def test_pattern1 # -p
- @input = INPUT.gsub(/<%/, '<!--%').gsub(/%>/, '%-->')
- @expected = OUTPUT
- #@options = "-p '<!--% %-->'"
- @options = ["-p", "<!--% %-->"]
- _test()
- end
- # def test_class1 # -C
- # @input = INPUT
- # @expected = OUTPUT.gsub(/<aaa>/, '<aaa>').gsub(/b&b/, 'b&b').gsub(/"ccc"/, '"ccc"')
- # @options = "-C XmlEruby"
- # _test()
- # end
- def test_notrim1 # --trim=false
- @input = INPUT
- @expected = <<'END'
- list:
- - <aaa>
- - b&b
- - "ccc"
- user: (none)
- END
- @options = "--trim=false" # -T
- _test()
- end
- def test_notrim2 # --trim=false
- @input = INPUT
- # @expected = <<'END'
- #_buf = ''; _buf << "list:\n"
- # list = ['<aaa>', 'b&b', '"ccc"']
- # for item in list ; _buf << "\n"
- #_buf << " - "; _buf << ( item ).to_s; _buf << "\n"
- # end ; _buf << "\n"
- #_buf << "user: "; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << "\n"
- #_buf
- #END
- @expected = <<'END'
- _buf = ''; _buf << 'list:
- '; list = ['<aaa>', 'b&b', '"ccc"']
- for item in list ; _buf << '
- '; _buf << ' - '; _buf << ( item ).to_s; _buf << '
- '; end ; _buf << '
- '; _buf << 'user: '; _buf << ( defined?(user) ? user : "(none)" ).to_s; _buf << '
- ';
- _buf.to_s
- END
- @options = "-x --trim=false" # -xT
- _test()
- end
- #--
- #def test_context1
- # @input = INPUT
- # @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
- # @options = '--user=Hello'
- # _test()
- #end
- #++
- def test_datafile1 # -f data.yaml
- datafile = "test.context1.yaml"
- @input = INPUT2
- @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
- @options = "-f #{datafile}"
- #
- str = <<-END
- user: Hello
- password: world
- END
- File.open(datafile, 'w') {|f| f.write(str) }
- begin
- _test()
- ensure
- File.unlink(datafile) if test(?f, datafile)
- end
- end
- def test_datafile2 # -f data.rb
- datafile = "test.context1.rb"
- @input = INPUT2
- @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
- @options = "-f #{datafile}"
- #
- str = <<-END
- @user = 'Hello'
- @password = 'world'
- END
- File.open(datafile, 'w') {|f| f.write(str) }
- begin
- _test()
- ensure
- File.unlink(datafile) if test(?f, datafile)
- end
- end
- def test_untabify1 # -t (obsolete)
- yamlfile = "test.context2.yaml"
- @input = INPUT2
- @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
- @options = "-tf #{yamlfile}"
- #
- yaml = <<-END
- user: Hello
- password: world
- END
- File.open(yamlfile, 'w') {|f| f.write(yaml) }
- begin
- _test()
- ensure
- File.unlink(yamlfile) if test(?f, yamlfile)
- end
- end
- def test_untabify2 # -T
- yamlfile = "test.context2.yaml"
- @input = INPUT2
- @expected = OUTPUT.gsub(/\(none\)/, 'Hello')
- @options = "-Tf #{yamlfile}"
- #
- yaml = <<-END
- user: Hello
- items:
- - aaa
- - bbb
- - ccc
- END
- File.open(yamlfile, 'w') {|f| f.write(yaml) }
- assert_raise(ArgumentError) do
- _test()
- end
- File.open(yamlfile, 'w') {|f| f.write(yaml.gsub(/\t/, ' '*8)) }
- _test()
- ensure
- File.unlink(yamlfile) if test(?f, yamlfile)
- end
- def test_symbolify1 # -S
- yamlfile = "test.context3.yaml"
- @input = <<END
- <% for h in @list %>
- <tr>
- <td><%= h[:name] %></td><td><%= h[:mail] %></td>
- </tr>
- <% end %>
- END
- @expected = <<END
- <tr>
- <td>foo</td><td>foo@mail.com</td>
- </tr>
- <tr>
- <td>bar</td><td>bar@mail.org</td>
- </tr>
- END
- @options = "-f #{yamlfile} -S"
- #
- yaml = <<-END
- list:
- - name: foo
- mail: foo@mail.com
- - name: bar
- mail: bar@mail.org
- END
- File.open(yamlfile, 'w') { |f| f.write(yaml) }
- begin
- _test()
- ensure
- File.unlink(yamlfile) if test(?f, yamlfile)
- end
- end
- def test_result1 # -B
- yamlfile = "test.context4.yaml"
- #
- @input = <<'END'
- user = <%= user %>
- <% for item in list %>
- - <%= item %>
- <% end %>
- END
- @expected = <<'END'
- user = World
- - aaa
- - bbb
- - ccc
- END
- @options = "-f #{yamlfile} -B "
- #
- yaml = <<-END
- user: World
- list:
- - aaa
- - bbb
- - ccc
- END
- File.open(yamlfile, 'w') {|f| f.write(yaml) }
- begin
- _test()
- ensure
- File.unlink(yamlfile) if test(?f, yamlfile)
- end
- end
- def test_context1 # -c
- @input = <<'END'
- user = <%= @user %>
- <% for item in @list %>
- - <%= item %>
- <% end %>
- END
- @expected = <<'END'
- user = World
- - aaa
- - bbb
- - ccc
- END
- #
- @options = ['-c', '{user: World, list: [aaa, bbb, ccc]}']
- _test()
- @options = ['-c', '@user="World"; @list=%w[aaa bbb ccc]']
- _test()
- end
- def test_include1 # -I
- dir = 'foo'
- lib = 'bar'
- Dir.mkdir dir unless test(?d, dir)
- filename = "#{dir}/#{lib}.rb"
- File.open(filename, 'w') do |f|
- f.write <<-'END'
- def escape(str)
- return "<#{str.upcase}>"
- end
- END
- end
- #
- @input = "<% require '#{lib}' %>\n" + INPUT.gsub(/<%= item %>/, '<%= escape(item) %>')
- @expected = OUTPUT.gsub(/<aaa>/, '<<AAA>>').gsub(/b\&b/, '<B&B>').gsub(/"ccc"/, '<"CCC">')
- @options = "-I #{dir}"
- #
- begin
- _test()
- ensure
- File.unlink filename if test(?f, filename)
- FileUtils.rm_r dir if test(?d, dir)
- end
- end
- def test_require1 # -r
- dir = 'foo'
- lib = 'bar'
- Dir.mkdir dir unless test(?d, dir)
- filename = "#{dir}/#{lib}.rb"
- File.open(filename, 'w') do |f|
- f.write <<-'END'
- def escape(str)
- return "<#{str.upcase}>"
- end
- END
- end
- #
- @input = INPUT.gsub(/<%= item %>/, '<%= escape(item) %>')
- @expected = OUTPUT.gsub(/<aaa>/, '<<AAA>>').gsub(/b\&b/, '<B&B>').gsub(/"ccc"/, '<"CCC">')
- @options = "-I #{dir} -r #{lib}"
- #
- begin
- _test()
- ensure
- File.unlink filename if test(?f, filename)
- FileUtils.rm_r dir if test(?d, dir)
- end
- end
- def test_enhancers1 # -E
- @input = <<END
- <% list = %w[<aaa> b&b "ccc"] %>
- % for item in list
- - <%= item %> : <%== item %>
- - [= item =] : [== item =]
- % end
- END
- @expected = <<END
- - <aaa> : <aaa>
- - <aaa> : <aaa>
- - b&b : b&b
- - b&b : b&b
- - "ccc" : "ccc"
- - "ccc" : "ccc"
- END
- @options = "-E Escape,PercentLine,HeaderFooter,BiPattern"
- _test()
- end
- def test_bodyonly1 # -b
- @input = INPUT
- @expected = SRC.sub(/\A_buf = '';/,'').sub(/\n_buf.to_s\n\z/,'')
- @options = '-b -x'
- _test()
- end
- def test_escape1 # -e
- @input = INPUT
- @expected = SRC.gsub(/<< \((.*?)\).to_s;/, '<< Erubis::XmlHelper.escape_xml(\1);')
- @options = '-ex'
- _test()
- end
- def test_invalid_option # -1 (invalid option)
- @input = INPUT
- @options = '-1'
- _error_test(Erubis::CommandOptionError, "-1: unknown option.")
- end
- def test_invalid_enhancer # -E hoge
- @options = '-E hoge'
- errmsg = "hoge: no such Enhancer (try '-h' to show all enhancers)."
- _error_test(Erubis::CommandOptionError, errmsg)
- end
- def test_invalid_lang # -l hoge
- @options = '-l hoge'
- errmsg = "-l hoge: invalid language name (class Erubis::Ehoge not found)."
- _error_test(Erubis::CommandOptionError, errmsg)
- end
- def test_missing_argument # -E
- @filename = false
- @options = '-E'
- _error_test(Erubis::CommandOptionError, "-E: enhancers required.")
- @options = '-l'
- _error_test(Erubis::CommandOptionError, "-l: lang required.")
- end
- def test_pi1 # --pi -x
- @input = PI_INPUT
- @expected = PI_SRC
- @options = '-x --pi'
- _test()
- end
- def test_pi2 # --pi -x --escape=false
- @input = PI_INPUT
- @expected = PI_ESCAPED_SRC
- @options = '-x --pi --escape=false'
- _test()
- end
- def test_pi3 # --pi
- @input = PI_INPUT
- @expected = PI_OUTPUT
- @options = '--pi'
- _test()
- end
- def test_pi4 # --pi --escape=false
- @input = PI_INPUT
- @expected = PI_ESCAPED_OUTPUT
- @options = '--pi --escape=false'
- _test()
- end
- def test_pi5 # --pi=ruby -x
- @input = PI_INPUT.gsub(/<\?rb/, '<?ruby')
- @expected = PI_SRC
- @options = '--pi=ruby -x'
- _test()
- end
- def test_pi6 # --pi -xl java
- @input = <<'END'
- <?java for (int i = 0; i < arr.length; i++) { ?>
- - @{arr[i]}@ / @!{arr[i]}@
- <?java } ?>
- END
- @expected = <<'END'
- StringBuffer _buf = new StringBuffer(); for (int i = 0; i < arr.length; i++) {
- _buf.append(" - "); _buf.append(escape(arr[i])); _buf.append(" / "); _buf.append(arr[i]); _buf.append("\n");
- }
- return _buf.toString();
- END
- @options = '--pi -xl java'
- _test()
- end
- self.post_definition()
- end
|