##
## $Release: 2.7.0 $
## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
##
require "#{File.dirname(__FILE__)}/test.rb"
require 'erubis'
require 'erubis/engine/eruby'
require 'erubis/engine/ephp'
require 'erubis/engine/ec'
require 'erubis/engine/ecpp'
require 'erubis/engine/ejava'
require 'erubis/engine/escheme'
require 'erubis/engine/eperl'
require 'erubis/engine/ejavascript'
class EnginesTest < Test::Unit::TestCase
#load_yaml_documents(__FILE__)
testdata_list = load_yaml_datafile(__FILE__)
define_testmethods(testdata_list)
def _test()
klass = Erubis.const_get(@class)
engine = klass.new(@input, @options || {})
actual = engine.src
assert_text_equal(@expected, actual)
end
self.post_definition()
end
__END__
- name: ruby1
lang: ruby
class: Eruby
options:
input: |
<% i = 0
list.each_with_index do |item, i| %>
<%= i+1 %> |
<%== list %> |
<% end %>
<%=== i+1 %>
expected: |
_buf = ''; _buf << '
'; i = 0
list.each_with_index do |item, i|
_buf << '
'; _buf << ( i+1 ).to_s; _buf << ' |
'; _buf << Erubis::XmlHelper.escape_xml( list ); _buf << ' |
'; end
_buf << '
'; $stderr.puts("*** debug: i+1=#{(i+1).inspect}"); _buf << '
';
_buf.to_s
##
- name: ruby2_options
lang: ruby
class: Eruby
options: { :bufvar: '@_out_buf' }
input: |
<% for item in @items %>
<%= i+1 %> |
<%== list %> |
<% end %>
expected: |
@_out_buf = ''; @_out_buf << '
'; for item in @items
@_out_buf << '
'; @_out_buf << ( i+1 ).to_s; @_out_buf << ' |
'; @_out_buf << Erubis::XmlHelper.escape_xml( list ); @_out_buf << ' |
'; end
@_out_buf << '
';
@_out_buf.to_s
##
- name: php1
lang: php
class: Ephp
options:
input: |
<%
$i = 0;
foreach ($list as $item) {
$i++;
%>
<%= $i %> |
<%== $item %> |
<%
}
%>
<%=== $i %>
expected: |
##
- name: c1
lang: c
class: Ec
options: { :filename: foo.html, :indent: ' ' }
input: |4
<% for (i = 0; i < list; i++) { %>
<%= "%d", i %> |
<%== list[i] %> |
<% } %>
<%=== "%d", i %>
expected: |
#line 1 "foo.html"
fputs("\n"
" \n", stdout);
for (i = 0; i < list; i++) {
fputs(" \n"
" ", stdout); fprintf(stdout, "%d", i); fputs(" | \n"
" ", stdout); escape(list[i], stdout); fputs(" | \n"
"
\n", stdout);
}
fputs(" \n"
"
\n", stdout);
fprintf(stderr, "*** debug: i=" "%d", i); fputs("\n", stdout);
##
- name: cpp1
lang: cpp
class: Ecpp
options: { :filename: foo.html, :indent: ' ' }
input: |4
<% for (i = 0; i < n; i++) { %>
<%= i %> |
<%== list[i] %> |
<% } %>
<%=== i %>
expected: |
#line 1 "foo.html"
_buf << "\n"
" \n";
for (i = 0; i < n; i++) {
_buf << " \n"
" "; _buf << (i); _buf << " | \n"
" "; escape(list[i]); _buf << " | \n"
"
\n";
}
_buf << " \n"
"
\n";
std::cerr << "*** debug: i=" << (i); _buf << "\n";
##
- name: java1
lang: java
class: Ejava
options: { :buf: _buf, :bufclass: StringBuilder, :indent: ' ' }
input: |
<%
int i = 0;
for (Iterator it = list.iterator(); it.hasNext(); ) {
String s = (String)it.next();
i++;
%>
">
<%= i %> |
<%== s %> |
<%
}
%>
<%=== i %>
expected: |4
StringBuilder _buf = new StringBuilder(); _buf.append("\n"
+ " \n");
int i = 0;
for (Iterator it = list.iterator(); it.hasNext(); ) {
String s = (String)it.next();
i++;
_buf.append(" \n"
+ " "); _buf.append(i); _buf.append(" | \n"
+ " "); _buf.append(escape(s)); _buf.append(" | \n"
+ "
\n");
}
_buf.append(" \n"
+ "
\n");
System.err.println("*** debug: i="+(i)); _buf.append("\n");
return _buf.toString();
##
- name: scheme1
lang: scheme
class: Escheme
options:
input: &scheme1_input|
<% (let ((i 0)) %>
<%
(for-each
(lambda (item)
(set! i (+ i 1))
%>
<%= i %> |
<%== item %> |
<%
); lambda end
list); for-each end
%>
<%=== i %>
<% ); let end %>
expected: |4
(let ((_buf '())) (define (_add x) (set! _buf (cons x _buf))) (let ((i 0))
(_add "
\n")
(for-each
(lambda (item)
(set! i (+ i 1))
(_add "
")(_add i)(_add " |
")(_add (escape item))(_add " |
\n")
); lambda end
list); for-each end
(_add "
\n")
(display "*** debug: i=")(display i)(display "\n")(_add "\n")
); let end
(reverse _buf))
##
- name: scheme2
lang: scheme
class: Escheme
options: { :func: 'display' }
input: *scheme1_input
expected: |4
(let ((i 0))
(display "
\n")
(for-each
(lambda (item)
(set! i (+ i 1))
(display "
")(display i)(display " |
")(display (escape item))(display " |
\n")
); lambda end
list); for-each end
(display "
\n")
(display "*** debug: i=")(display i)(display "\n")(display "\n")
); let end
##
- name: perl1
lang: perl
class: Eperl
options:
input: |
<%
my $user = 'Erubis';
my @list = ('', 'b&b', '"ccc"');
%>
Hello <%= $user %>!
<% $i = 0; %>
<% for $item (@list) { %>
">
<%= $i %> |
<%== $item %> |
<% } %>
<%=== $i %>
expected: |4
use HTML::Entities;
my $user = 'Erubis';
my @list = ('', 'b&b', '"ccc"');
print('Hello '); print($user); print('!
'); $i = 0;
for $item (@list) {
print('
'); print($i); print(' |
'); print(encode_entities($item)); print(' |
'); }
print('
'); print('*** debug: $i=', $i, "\n");print('
');
##
- name: javascript1
lang: javascript
class: Ejavascript
options:
input: &javascript_input |
<%
var user = 'Erubis';
var list = ['', 'b&b', '"ccc"'];
%>
Hello <%= user %>!
<% var i; %>
<% for (i = 0; i < list.length; i++) { %>
">
<%= i %> |
<%= list[i] %> |
<% } %>
<%=== i %>
expected: |4
var _buf = [];
var user = 'Erubis';
var list = ['', 'b&b', '"ccc"'];
_buf.push("Hello "); _buf.push(user); _buf.push("!
\n\
\n\
\n");
var i;
for (i = 0; i < list.length; i++) {
_buf.push(" \n\
"); _buf.push(i); _buf.push(" | \n\
"); _buf.push(list[i]); _buf.push(" | \n\
\n");
}
_buf.push(" \n\
\n");
alert("*** debug: i="+(i)); _buf.push("\n");
document.write(_buf.join(""));
##
- name: javascript2
lang: javascript
class: Ejavascript
options: { :docwrite: false }
input: *javascript_input
expected: |4
var _buf = [];
var user = 'Erubis';
var list = ['', 'b&b', '"ccc"'];
_buf.push("Hello "); _buf.push(user); _buf.push("!
\n\
\n\
\n");
var i;
for (i = 0; i < list.length; i++) {
_buf.push(" \n\
"); _buf.push(i); _buf.push(" | \n\
"); _buf.push(list[i]); _buf.push(" | \n\
\n");
}
_buf.push(" \n\
\n");
alert("*** debug: i="+(i)); _buf.push("\n");
_buf.join("");
##