123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646 |
- ##
- ## $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 = ['<aaa>', '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|
- <ul>
- <% for item in list %>
- <li><%= item %></li>
- <% end %>
- </ul>
- src: &basic1_src|
- _buf = ''; _buf << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf.to_s
- output: &basic1_output|
- <ul>
- <li><aaa></li>
- <li>b&b</li>
- <li>"ccc"</li>
- </ul>
- - name: xml1
- class: XmlEruby
- input: |
- <pre>
- <% for item in list %>
- <%= item %>
- <%== item %>
- <% end %>
- </pre>
- src: |
- _buf = ''; _buf << '<pre>
- '; for item in list
- _buf << ' '; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '
- '; _buf << ' '; _buf << ( item ).to_s; _buf << '
- '; end
- _buf << '</pre>
- ';
- _buf.to_s
- output: |
- <pre>
- <aaa>
- <aaa>
- b&b
- b&b
- "ccc"
- "ccc"
- </pre>
- ##
- - 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 '<ul>
- '; for item in list
- print ' <li>'; print(( item ).to_s); print '</li>
- '; end
- print '</ul>
- ';
- output: *basic1_output
- ##
- - name: printenabled1
- class: PrintEnabledEruby
- input: &printenabled1_input|
- <ul>
- <% for item in list %>
- <li><% print item %></li>
- <% end %>
- </ul>
- src: |
- @_buf = _buf = ''; _buf << '<ul>
- '; for item in list
- _buf << ' <li>'; print item ; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf.to_s
- output: *basic1_output
- # <ul>
- # <li><aaa></li>
- # <li>b&b</li>
- # <li>"ccc"</li>
- # </ul>
- ##
- - name: stdout1
- class: StdoutEruby
- testopt: stdout
- input: *basic1_input
- # <ul>
- # <% for item in list %>
- # <li><%= item %></li>
- # <% end %>
- # </ul>
- src: |
- _buf = $stdout; _buf << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- ''
- output: *basic1_output
- # <ul>
- # <li><aaa></li>
- # <li>b&b</li>
- # <li>"ccc"</li>
- # </ul>
- ##
- - name: array1
- class: ArrayEruby
- input: |
- <ul>
- <% for item in list %>
- <li><%= item %></li>
- <% end %>
- </ul>
- src: |
- _buf = []; _buf << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf
- output:
- - "<ul>\n"
- - " <li>"
- - "<aaa>"
- - "</li>\n"
- - " <li>"
- - "b&b"
- - "</li>\n"
- - " <li>"
- - "\"ccc\""
- - "</li>\n"
- - "</ul>\n"
- ##
- - name: arraybuffer1
- class: ArrayBufferEruby
- input: *basic1_input
- src: |
- _buf = []; _buf << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf.join
- output: *basic1_output
- - name: stringbuffer1
- class: StringBufferEruby
- input: *basic1_input
- # <ul>
- # <% for item in list %>
- # <li><%= item %></li>
- # <% end %>
- # </ul>
- src: |
- _buf = ''; _buf << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf.to_s
- output: *basic1_output
- # <ul>
- # <li><aaa></li>
- # <li>b&b</li>
- # <li>"ccc"</li>
- # </ul>
- ##
- - name: erbout1
- class: ErboutEruby
- input: *basic1_input
- src: |
- _erbout = _buf = ''; _buf << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf.to_s
- output: *basic1_output
- ##
- - name: stringio1
- class: StringIOEruby
- input: *basic1_input
- src: |
- _buf = StringIO.new; _buf << '<ul>
- '; for item in list
- _buf << ' <li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _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: '<aaa>b&b"ccc"'
- ##
- - name: nocode1
- class: NoCodeEruby
- testopt: skip_output
- input: *basic1_input
- src: |
- <ul>
- <li></li>
- </ul>
- output:
- ##
- - name: simplified
- class: SimplifiedEruby
- input: |
- <ul>
- <% for item in list %>
- <li>
- <%= item %>
- </li>
- <% end %>
- </ul>
- src: |
- _buf = ''; _buf << '<ul>
- '; for item in list ; _buf << '
- <li>
- '; _buf << ( item ).to_s; _buf << '
- </li>
- '; end ; _buf << '
- </ul>
- ';
- _buf.to_s
- output: |
- <ul>
- ^
- <li>
- <aaa>
- </li>
- ^
- <li>
- b&b
- </li>
- ^
- <li>
- "ccc"
- </li>
- ^
- </ul>
- ##
- - 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>
- <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>
- <aaa> = <aaa>
- b&b % b&b
- b&b = b&b
- "ccc" % "ccc"
- "ccc" = "ccc"
- ##
- - name: percentline1
- class: PercentLineEruby
- options:
- input: |
- <table>
- % for item in list
- <tr>
- <td><%= item %></td>
- <td><%== item %></td>
- </tr>
- % end
- </table>
- <pre>
- %% double percent
- % spaced percent
- </pre>
- src: |
- _buf = ''; _buf << '<table>
- '; for item in list
- _buf << ' <tr>
- <td>'; _buf << ( item ).to_s; _buf << '</td>
- <td>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</td>
- </tr>
- '; end
- _buf << '</table>
- <pre>
- % double percent
- % spaced percent
- </pre>
- ';
- _buf.to_s
- output: |
- <table>
- <tr>
- <td><aaa></td>
- <td><aaa></td>
- </tr>
- <tr>
- <td>b&b</td>
- <td>b&b</td>
- </tr>
- <tr>
- <td>"ccc"</td>
- <td>"ccc"</td>
- </tr>
- </table>
- <pre>
- % double percent
- % spaced percent
- </pre>
- ##
- - name: prefixedline1
- class: PrefixedLineEruby
- options: { :prefixchar: '!' }
- input: |
- <table>
- ! for item in list
- <tr>
- <td><%= item %></td>
- <td><%== item %></td>
- </tr>
- ! end
- </table>
- <pre>
- !! doubled characters
- </pre>
- src: |
- _buf = ''; _buf << '<table>
- '; for item in list
- _buf << ' <tr>
- <td>'; _buf << ( item ).to_s; _buf << '</td>
- <td>'; _buf << Erubis::XmlHelper.escape_xml( item ); _buf << '</td>
- </tr>
- '; end
- _buf << '</table>
- <pre>
- ! doubled characters
- </pre>
- ';
- _buf.to_s
- output: |
- <table>
- <tr>
- <td><aaa></td>
- <td><aaa></td>
- </tr>
- <tr>
- <td>b&b</td>
- <td>b&b</td>
- </tr>
- <tr>
- <td>"ccc"</td>
- <td>"ccc"</td>
- </tr>
- </table>
- <pre>
- ! doubled characters
- </pre>
- ##
- - name: headerfooter1
- class: HeaderFooterEruby
- options:
- testopt: eval('ordered_list(list)')
- input: |
- <!--#header:
- def ordered_list(list)
- #-->
- <ol>
- <% for item in list %>
- <li><%==item%></li>
- <% end %>
- </ol>
- <!--#footer: end #-->
- src: |4
-
- def ordered_list(list)
-
- _buf = ''; _buf << '<ol>
- '; for item in list
- _buf << ' <li>'; _buf << Erubis::XmlHelper.escape_xml(item); _buf << '</li>
- '; end
- _buf << '</ol>
- ';
- _buf.to_s
- end
- output: |
- <ol>
- <li><aaa></li>
- <li>b&b</li>
- <li>"ccc"</li>
- </ol>
- ##
- - name: deleteindent1
- class: DeleteIndentEruby
- options:
- testopt:
- input: *basic1_input
- src: |
- _buf = ''; _buf << '<ul>
- '; for item in list
- _buf << '<li>'; _buf << ( item ).to_s; _buf << '</li>
- '; end
- _buf << '</ul>
- ';
- _buf.to_s
- output: |
- <ul>
- <li><aaa></li>
- <li>b&b</li>
- <li>"ccc"</li>
- </ul>
- ##
- - name: interpolation1
- class: InterpolationEruby
- options:
- testopt:
- input: *basic1_input
- src: |
- _buf = ''; _buf << %Q`<ul>\n`
- for item in list
- _buf << %Q` <li>#{ item }</li>\n`
- end
- _buf << %Q`</ul>\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: |
- <p>`back-quote`</p>
- <p><%= `echo back-tick operator` %></p>
- <p>#{sharp}</p>
- <p>'single quote'</p>
- <p>"double quote"</p>
- <p>backslash\n\t</p>
- src: |
- _buf = ''; _buf << %Q`<p>\`back-quote\`</p>
- <p>#{ `echo back-tick operator` }</p>
- <p>\#{sharp}</p>
- <p>'single quote'</p>
- <p>"double quote"</p>
- <p>backslash\\n\\t</p>\n`
- _buf.to_s
- output: |
- <p>`back-quote`</p>
- <p>back-tick operator
- </p>
- <p>#{sharp}</p>
- <p>'single quote'</p>
- <p>"double quote"</p>
- <p>backslash\n\t</p>
|