## ## $Release: 2.7.0 $ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved. ## require "#{File.dirname(__FILE__)}/test.rb" require 'stringio' require 'erubis' require 'erubis/engine/enhanced' require 'erubis/engine/optimized' class EnhancersTest < Test::Unit::TestCase testdata_list = load_yaml_datafile(__FILE__) define_testmethods(testdata_list) def _test() @src.gsub!(/\^/, ' ') @output.gsub!(/\^/, ' ') if @output.is_a?(String) if @class k = Erubis @class.split('::').each do |name| k = k.const_get(name) end @klass = k else @klass = Erubis::Eruby end @options ||= {} @chomp.each do |target| case target when 'src' ; @src.chomp! when 'input' ; @input.chomp! when 'expected' ; @expected.chomp! else raise "#{@name}: invalid chomp value: #{@chomp.inspect}" end end if @chomp if @testopt == 'load_file' filename = "tmp.#{@name}.eruby" begin File.open(filename, 'w') { |f| f.write(@input) } eruby = @klass.load_file(filename, @options) ensure cachename = filename + '.cache' File.unlink(cachename) if test(?f, cachename) File.unlink(filename) if test(?f, filename) end else #if @klass == Erubis::TinyEruby # eruby = @klass.new(@input) #else eruby = @klass.new(@input, @options) #end end assert_text_equal(@src, eruby.src) return if @testopt == 'skip_output' list = ['', 'b&b', '"ccc"'] context = @testopt == 'context' ? Erubis::Context.new : {} context[:list] = list case @testopt when /\Aeval\(/ eval eruby.src actual = eval @testopt assert_text_equal(@output, actual) when 'stdout', 'print' begin orig = $stdout $stdout = stringio = StringIO.new #actual = eruby.evaluate(context) actual = eruby.result(context) ensure $stdout = orig end if @testopt == 'stdout' assert_equal("", actual) else assert_nil(actual) end assert_text_equal(@output, stringio.string) when 'evaluate', 'context' actual = eruby.evaluate(context) assert_text_equal(@output, actual) when 'binding' actual = eruby.result(binding()) assert_text_equal(@output, actual) else actual = eruby.result(context) assert_text_equal(@output, actual) end end self.post_definition() end __END__ ## - name: basic1 class: Eruby input: &basic1_input| src: &basic1_src| _buf = ''; _buf << ' '; _buf.to_s output: &basic1_output| - name: xml1 class: XmlEruby input: |
       <% for item in list %>
        <%= item %>
        <%== item %>
       <% end %>
      
src: | _buf = ''; _buf << '
      ';  for item in list 
       _buf << '  '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '
      '; _buf << '  '; _buf << ( item ).to_s; _buf << '
      ';  end 
       _buf << '
'; _buf.to_s output: |
        <aaa>
        
        b&b
        b&b
        "ccc"
        "ccc"
      
## - name: xml2 class: XmlEruby testopt: skip_output input: | <% for item in list %> <%= item["var#{n}"] %> <%== item["var#{n}"] %> <%=== item["var#{n}"] %> <%==== item["var#{n}"] %> <% end %> src: | _buf = ''; for item in list _buf << ' '; _buf << Erubis::XmlHelper.escape_xml( item["var#{n}"] ); _buf << ' '; _buf << ' '; _buf << ( item["var#{n}"] ).to_s; _buf << ' '; _buf << ' '; $stderr.puts("*** debug: item[\"var\#{n}\"]=#{(item["var#{n}"]).inspect}"); _buf << ' '; _buf << ' '; _buf << ' '; end _buf.to_s output: | ## - name: printout1 class: PrintOutEruby testopt: print input: *basic1_input src: |4 print ' '; output: *basic1_output ## - name: printenabled1 class: PrintEnabledEruby input: &printenabled1_input| src: | @_buf = _buf = ''; _buf << ' '; _buf.to_s output: *basic1_output # ## - name: stdout1 class: StdoutEruby testopt: stdout input: *basic1_input # src: | _buf = $stdout; _buf << ' '; '' output: *basic1_output # ## - name: array1 class: ArrayEruby input: | src: | _buf = []; _buf << ' '; _buf output: - "\n" ## - name: arraybuffer1 class: ArrayBufferEruby input: *basic1_input src: | _buf = []; _buf << ' '; _buf.join output: *basic1_output - name: stringbuffer1 class: StringBufferEruby input: *basic1_input # src: | _buf = ''; _buf << ' '; _buf.to_s output: *basic1_output # ## - name: erbout1 class: ErboutEruby input: *basic1_input src: | _erbout = _buf = ''; _buf << ' '; _buf.to_s output: *basic1_output ## - name: stringio1 class: StringIOEruby input: *basic1_input src: | _buf = StringIO.new; _buf << ' '; _buf.string output: *basic1_output ## - name: notext1 class: NoTextEruby input: *basic1_input src: | _buf = ''; for item in list _buf << ( item ).to_s; end _buf.to_s output: 'b&b"ccc"' ## - name: nocode1 class: NoCodeEruby testopt: skip_output input: *basic1_input src: | output: ## - name: simplified class: SimplifiedEruby input: | src: | _buf = ''; _buf << ' '; _buf.to_s output: | ## - name: bipattern1 class: BiPatternEruby #options: { :bipattern : '\[= =\]' } input: | <% for item in list %> <%= item %> % <%== item %> [= item =] = [== item =] <% end %> src: | _buf = ''; for item in list _buf << ' '; _buf << ( item ).to_s; _buf << ' % '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << ' '; _buf << ' '; _buf << ( item ).to_s; _buf << ' = '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << ' '; end _buf.to_s output: |4 % <aaa> = <aaa> b&b % b&b b&b = b&b "ccc" % "ccc" "ccc" = "ccc" ## - name: bipattern2 class: BiPatternEruby options: { :bipattern: '\$\{ \}' } input: | <% for item in list %> <%=item%> % <%==item%> ${item} = ${=item} <% end %> src: | _buf = ''; for item in list _buf << ' '; _buf << (item).to_s; _buf << ' % '; _buf << Erubis::XmlHelper.escape_xml(item); _buf << ' '; _buf << ' '; _buf << (item).to_s; _buf << ' = '; _buf << Erubis::XmlHelper.escape_xml(item); _buf << ' '; end _buf.to_s output: |4 % <aaa> = <aaa> b&b % b&b b&b = b&b "ccc" % "ccc" "ccc" = "ccc" ## - name: percentline1 class: PercentLineEruby options: input: | % for item in list % end
<%= item %> <%== item %>
      %% double percent
       % spaced percent
      
src: | _buf = ''; _buf << ' '; for item in list _buf << ' '; end _buf << '
'; _buf << ( item ).to_s; _buf << ' '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '
      % double percent
       % spaced percent
      
'; _buf.to_s output: |
<aaa>
b&b b&b
"ccc" "ccc"
      % double percent
       % spaced percent
      
## - name: prefixedline1 class: PrefixedLineEruby options: { :prefixchar: '!' } input: | ! for item in list ! end
<%= item %> <%== item %>
        !! doubled characters
      
src: | _buf = ''; _buf << ' '; for item in list _buf << ' '; end _buf << '
'; _buf << ( item ).to_s; _buf << ' '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '
        ! doubled characters
      
'; _buf.to_s output: |
<aaa>
b&b b&b
"ccc" "ccc"
        ! doubled characters
      
## - name: headerfooter1 class: HeaderFooterEruby options: testopt: eval('ordered_list(list)') input: |
    <% for item in list %>
  1. <%==item%>
  2. <% end %>
src: |4 def ordered_list(list) _buf = ''; _buf << '
    '; for item in list _buf << '
  1. '; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '
  2. '; end _buf << '
'; _buf.to_s end output: |
  1. <aaa>
  2. b&b
  3. "ccc"
## - name: deleteindent1 class: DeleteIndentEruby options: testopt: input: *basic1_input src: | _buf = ''; _buf << '
    '; for item in list _buf << '
  • '; _buf << ( item ).to_s; _buf << '
  • '; end _buf << '
'; _buf.to_s output: |
  • b&b
  • "ccc"
## - name: interpolation1 class: InterpolationEruby options: testopt: input: *basic1_input src: | _buf = ''; _buf << %Q`
    \n` for item in list _buf << %Q`
  • #{ item }
  • \n` end _buf << %Q`
\n` _buf.to_s output: *basic1_output - name: interpolation2 desc: sharp, back-quote, and backslash should be escaped, but other quotes should not be escaped (reported by andrewj) class: InterpolationEruby options: testopt: input: |

`back-quote`

<%= `echo back-tick operator` %>

#{sharp}

'single quote'

"double quote"

backslash\n\t

src: | _buf = ''; _buf << %Q`

\`back-quote\`

#{ `echo back-tick operator` }

\#{sharp}

'single quote'

"double quote"

backslash\\n\\t

\n` _buf.to_s output: |

`back-quote`

back-tick operator

#{sharp}

'single quote'

"double quote"

backslash\n\t