test-engines.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. ##
  2. ## $Release: 2.7.0 $
  3. ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
  4. ##
  5. require "#{File.dirname(__FILE__)}/test.rb"
  6. require 'erubis'
  7. require 'erubis/engine/eruby'
  8. require 'erubis/engine/ephp'
  9. require 'erubis/engine/ec'
  10. require 'erubis/engine/ecpp'
  11. require 'erubis/engine/ejava'
  12. require 'erubis/engine/escheme'
  13. require 'erubis/engine/eperl'
  14. require 'erubis/engine/ejavascript'
  15. class EnginesTest < Test::Unit::TestCase
  16. #load_yaml_documents(__FILE__)
  17. testdata_list = load_yaml_datafile(__FILE__)
  18. define_testmethods(testdata_list)
  19. def _test()
  20. klass = Erubis.const_get(@class)
  21. engine = klass.new(@input, @options || {})
  22. actual = engine.src
  23. assert_text_equal(@expected, actual)
  24. end
  25. self.post_definition()
  26. end
  27. __END__
  28. - name: ruby1
  29. lang: ruby
  30. class: Eruby
  31. options:
  32. input: |
  33. <table>
  34. <tbody>
  35. <% i = 0
  36. list.each_with_index do |item, i| %>
  37. <tr>
  38. <td><%= i+1 %></td>
  39. <td><%== list %></td>
  40. </tr>
  41. <% end %>
  42. </tbody>
  43. </table>
  44. <%=== i+1 %>
  45. expected: |
  46. _buf = ''; _buf << '<table>
  47. <tbody>
  48. '; i = 0
  49. list.each_with_index do |item, i|
  50. _buf << ' <tr>
  51. <td>'; _buf << ( i+1 ).to_s; _buf << '</td>
  52. <td>'; _buf << Erubis::XmlHelper.escape_xml( list ); _buf << '</td>
  53. </tr>
  54. '; end
  55. _buf << ' </tbody>
  56. </table>
  57. '; $stderr.puts("*** debug: i+1=#{(i+1).inspect}"); _buf << '
  58. ';
  59. _buf.to_s
  60. ##
  61. - name: ruby2_options
  62. lang: ruby
  63. class: Eruby
  64. options: { :bufvar: '@_out_buf' }
  65. input: |
  66. <table>
  67. <% for item in @items %>
  68. <tr>
  69. <td><%= i+1 %></td>
  70. <td><%== list %></td>
  71. </tr>
  72. <% end %>
  73. </table>
  74. expected: |
  75. @_out_buf = ''; @_out_buf << '<table>
  76. '; for item in @items
  77. @_out_buf << ' <tr>
  78. <td>'; @_out_buf << ( i+1 ).to_s; @_out_buf << '</td>
  79. <td>'; @_out_buf << Erubis::XmlHelper.escape_xml( list ); @_out_buf << '</td>
  80. </tr>
  81. '; end
  82. @_out_buf << '</table>
  83. ';
  84. @_out_buf.to_s
  85. ##
  86. - name: php1
  87. lang: php
  88. class: Ephp
  89. options:
  90. input: |
  91. <table>
  92. <tbody>
  93. <%
  94. $i = 0;
  95. foreach ($list as $item) {
  96. $i++;
  97. %>
  98. <tr>
  99. <td><%= $i %></td>
  100. <td><%== $item %></td>
  101. </tr>
  102. <%
  103. }
  104. %>
  105. </tbody>
  106. </table>
  107. <%=== $i %>
  108. expected: |
  109. <table>
  110. <tbody>
  111. <?php
  112. $i = 0;
  113. foreach ($list as $item) {
  114. $i++;
  115. ?>
  116. <tr>
  117. <td><?php echo $i; ?></td>
  118. <td><?php echo htmlspecialchars($item); ?></td>
  119. </tr>
  120. <?php
  121. }
  122. ?>
  123. </tbody>
  124. </table>
  125. <?php error_log('*** debug: $i='.($i), 0); ?>
  126. ##
  127. - name: c1
  128. lang: c
  129. class: Ec
  130. options: { :filename: foo.html, :indent: ' ' }
  131. input: |4
  132. <table>
  133. <tbody>
  134. <% for (i = 0; i < list; i++) { %>
  135. <tr>
  136. <td><%= "%d", i %></td>
  137. <td><%== list[i] %></td>
  138. </tr>
  139. <% } %>
  140. </tbody>
  141. </table>
  142. <%=== "%d", i %>
  143. expected: |
  144. #line 1 "foo.html"
  145. fputs("<table>\n"
  146. " <tbody>\n", stdout);
  147. for (i = 0; i < list; i++) {
  148. fputs(" <tr>\n"
  149. " <td>", stdout); fprintf(stdout, "%d", i); fputs("</td>\n"
  150. " <td>", stdout); escape(list[i], stdout); fputs("</td>\n"
  151. " </tr>\n", stdout);
  152. }
  153. fputs(" </tbody>\n"
  154. "</table>\n", stdout);
  155. fprintf(stderr, "*** debug: i=" "%d", i); fputs("\n", stdout);
  156. ##
  157. - name: cpp1
  158. lang: cpp
  159. class: Ecpp
  160. options: { :filename: foo.html, :indent: ' ' }
  161. input: |4
  162. <table>
  163. <tbody>
  164. <% for (i = 0; i < n; i++) { %>
  165. <tr>
  166. <td><%= i %></td>
  167. <td><%== list[i] %></td>
  168. </tr>
  169. <% } %>
  170. </tbody>
  171. </table>
  172. <%=== i %>
  173. expected: |
  174. #line 1 "foo.html"
  175. _buf << "<table>\n"
  176. " <tbody>\n";
  177. for (i = 0; i < n; i++) {
  178. _buf << " <tr>\n"
  179. " <td>"; _buf << (i); _buf << "</td>\n"
  180. " <td>"; escape(list[i]); _buf << "</td>\n"
  181. " </tr>\n";
  182. }
  183. _buf << " </tbody>\n"
  184. "</table>\n";
  185. std::cerr << "*** debug: i=" << (i); _buf << "\n";
  186. ##
  187. - name: java1
  188. lang: java
  189. class: Ejava
  190. options: { :buf: _buf, :bufclass: StringBuilder, :indent: ' ' }
  191. input: |
  192. <table>
  193. <tbody>
  194. <%
  195. int i = 0;
  196. for (Iterator it = list.iterator(); it.hasNext(); ) {
  197. String s = (String)it.next();
  198. i++;
  199. %>
  200. <tr class="<%= i%2==0 ? "even" : "odd" %>">
  201. <td><%= i %></td>
  202. <td><%== s %></td>
  203. </tr>
  204. <%
  205. }
  206. %>
  207. <tbody>
  208. </table>
  209. <%=== i %>
  210. expected: |4
  211. StringBuilder _buf = new StringBuilder(); _buf.append("<table>\n"
  212. + " <tbody>\n");
  213. int i = 0;
  214. for (Iterator it = list.iterator(); it.hasNext(); ) {
  215. String s = (String)it.next();
  216. i++;
  217. _buf.append(" <tr class=\""); _buf.append(i%2==0 ? "even" : "odd"); _buf.append("\">\n"
  218. + " <td>"); _buf.append(i); _buf.append("</td>\n"
  219. + " <td>"); _buf.append(escape(s)); _buf.append("</td>\n"
  220. + " </tr>\n");
  221. }
  222. _buf.append(" <tbody>\n"
  223. + "</table>\n");
  224. System.err.println("*** debug: i="+(i)); _buf.append("\n");
  225. return _buf.toString();
  226. ##
  227. - name: scheme1
  228. lang: scheme
  229. class: Escheme
  230. options:
  231. input: &scheme1_input|
  232. <% (let ((i 0)) %>
  233. <table>
  234. <tbody>
  235. <%
  236. (for-each
  237. (lambda (item)
  238. (set! i (+ i 1))
  239. %>
  240. <tr>
  241. <td><%= i %></td>
  242. <td><%== item %></td>
  243. </tr>
  244. <%
  245. ); lambda end
  246. list); for-each end
  247. %>
  248. </tbody>
  249. </table>
  250. <%=== i %>
  251. <% ); let end %>
  252. expected: |4
  253. (let ((_buf '())) (define (_add x) (set! _buf (cons x _buf))) (let ((i 0))
  254. (_add "<table>
  255. <tbody>\n")
  256. (for-each
  257. (lambda (item)
  258. (set! i (+ i 1))
  259. (_add " <tr>
  260. <td>")(_add i)(_add "</td>
  261. <td>")(_add (escape item))(_add "</td>
  262. </tr>\n")
  263. ); lambda end
  264. list); for-each end
  265. (_add " </tbody>
  266. </table>\n")
  267. (display "*** debug: i=")(display i)(display "\n")(_add "\n")
  268. ); let end
  269. (reverse _buf))
  270. ##
  271. - name: scheme2
  272. lang: scheme
  273. class: Escheme
  274. options: { :func: 'display' }
  275. input: *scheme1_input
  276. expected: |4
  277. (let ((i 0))
  278. (display "<table>
  279. <tbody>\n")
  280. (for-each
  281. (lambda (item)
  282. (set! i (+ i 1))
  283. (display " <tr>
  284. <td>")(display i)(display "</td>
  285. <td>")(display (escape item))(display "</td>
  286. </tr>\n")
  287. ); lambda end
  288. list); for-each end
  289. (display " </tbody>
  290. </table>\n")
  291. (display "*** debug: i=")(display i)(display "\n")(display "\n")
  292. ); let end
  293. ##
  294. - name: perl1
  295. lang: perl
  296. class: Eperl
  297. options:
  298. input: |
  299. <%
  300. my $user = 'Erubis';
  301. my @list = ('<aaa>', 'b&b', '"ccc"');
  302. %>
  303. <p>Hello <%= $user %>!</p>
  304. <table>
  305. <tbody>
  306. <% $i = 0; %>
  307. <% for $item (@list) { %>
  308. <tr bgcolor=<%= ++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
  309. <td><%= $i %></td>
  310. <td><%== $item %></td>
  311. </tr>
  312. <% } %>
  313. </tbody>
  314. </table>
  315. <%=== $i %>
  316. expected: |4
  317. use HTML::Entities;
  318. my $user = 'Erubis';
  319. my @list = ('<aaa>', 'b&b', '"ccc"');
  320. print('<p>Hello '); print($user); print('!</p>
  321. <table>
  322. <tbody>
  323. '); $i = 0;
  324. for $item (@list) {
  325. print(' <tr bgcolor='); print(++$i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); print('">
  326. <td>'); print($i); print('</td>
  327. <td>'); print(encode_entities($item)); print('</td>
  328. </tr>
  329. '); }
  330. print(' </tbody>
  331. </table>
  332. '); print('*** debug: $i=', $i, "\n");print('
  333. ');
  334. ##
  335. - name: javascript1
  336. lang: javascript
  337. class: Ejavascript
  338. options:
  339. input: &javascript_input |
  340. <%
  341. var user = 'Erubis';
  342. var list = ['<aaa>', 'b&b', '"ccc"'];
  343. %>
  344. <p>Hello <%= user %>!</p>
  345. <table>
  346. <tbody>
  347. <% var i; %>
  348. <% for (i = 0; i < list.length; i++) { %>
  349. <tr bgcolor=<%= ++i % 2 == 0 ? '#FFCCCC' : '#CCCCFF' %>">
  350. <td><%= i %></td>
  351. <td><%= list[i] %></td>
  352. </tr>
  353. <% } %>
  354. </tbody>
  355. </table>
  356. <%=== i %>
  357. expected: |4
  358. var _buf = [];
  359. var user = 'Erubis';
  360. var list = ['<aaa>', 'b&b', '"ccc"'];
  361. _buf.push("<p>Hello "); _buf.push(user); _buf.push("!</p>\n\
  362. <table>\n\
  363. <tbody>\n");
  364. var i;
  365. for (i = 0; i < list.length; i++) {
  366. _buf.push(" <tr bgcolor="); _buf.push(++i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); _buf.push("\">\n\
  367. <td>"); _buf.push(i); _buf.push("</td>\n\
  368. <td>"); _buf.push(list[i]); _buf.push("</td>\n\
  369. </tr>\n");
  370. }
  371. _buf.push(" </tbody>\n\
  372. </table>\n");
  373. alert("*** debug: i="+(i)); _buf.push("\n");
  374. document.write(_buf.join(""));
  375. ##
  376. - name: javascript2
  377. lang: javascript
  378. class: Ejavascript
  379. options: { :docwrite: false }
  380. input: *javascript_input
  381. expected: |4
  382. var _buf = [];
  383. var user = 'Erubis';
  384. var list = ['<aaa>', 'b&b', '"ccc"'];
  385. _buf.push("<p>Hello "); _buf.push(user); _buf.push("!</p>\n\
  386. <table>\n\
  387. <tbody>\n");
  388. var i;
  389. for (i = 0; i < list.length; i++) {
  390. _buf.push(" <tr bgcolor="); _buf.push(++i % 2 == 0 ? '#FFCCCC' : '#CCCCFF'); _buf.push("\">\n\
  391. <td>"); _buf.push(i); _buf.push("</td>\n\
  392. <td>"); _buf.push(list[i]); _buf.push("</td>\n\
  393. </tr>\n");
  394. }
  395. _buf.push(" </tbody>\n\
  396. </table>\n");
  397. alert("*** debug: i="+(i)); _buf.push("\n");
  398. _buf.join("");
  399. ##