tenjin 0.6.2 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (282) hide show
  1. data/CHANGES.txt +163 -0
  2. data/MIT-LICENSE +16 -17
  3. data/README.txt +5 -5
  4. data/benchmark/Makefile +9 -0
  5. data/benchmark/bench.rb +3 -2
  6. data/bin/rbtenjin +6 -3
  7. data/doc/docstyle.css +25 -4
  8. data/doc/users-guide.html +2088 -1563
  9. data/lib/tenjin.rb +784 -158
  10. data/public_html/_layout.rbhtml +33 -0
  11. data/public_html/css/style.css +77 -0
  12. data/public_html/env.rbhtml +15 -0
  13. data/public_html/favicon.ico +0 -0
  14. data/public_html/hello.rbhtml +14 -0
  15. data/public_html/index.rbhtml +39 -0
  16. data/public_html/rbtenjin.cgi +188 -0
  17. data/tenjin.gemspec +9 -6
  18. data/test/data/examples/preprocessing/main.rb +1 -1
  19. data/test/data/examples/preprocessing/select.rbhtml +1 -1
  20. data/test/data/faq/{ex11-bench.rb → ex10-bench.rb} +3 -3
  21. data/test/data/faq/ex10-content.rbhtml +8 -11
  22. data/test/data/faq/{ex11-layout1.rbhtml → ex10-layout1.rbhtml} +0 -0
  23. data/test/data/faq/{ex11-layout2.rbhtml → ex10-layout2.rbhtml} +0 -0
  24. data/test/data/faq/{ex11.rb → ex10.rb} +1 -1
  25. data/test/data/faq/{ex11.rbhtml → ex10.rbhtml} +0 -0
  26. data/test/data/faq/{ex11.source → ex10.source} +2 -2
  27. data/test/data/faq/{ex11_arraybuffer.result → ex10_arraybuffer.result} +1 -1
  28. data/test/data/faq/{ex5.rbhtml → ex2.rbhtml} +0 -0
  29. data/test/data/faq/{ex5_template_args.source → ex2_template_args.source} +1 -1
  30. data/test/data/faq/{ex7-expr-pattern.rb → ex4-expr-pattern.rb} +1 -1
  31. data/test/data/faq/{ex7-expr-pattern.rbhtml → ex4-expr-pattern.rbhtml} +0 -0
  32. data/test/data/faq/{ex7_expr_pattern.result → ex4_expr_pattern.result} +1 -1
  33. data/test/data/faq/{ex6-content.rhtml → ex5-content.rhtml} +0 -0
  34. data/test/data/faq/{ex6-layout.rhtml → ex5-layout.rhtml} +0 -0
  35. data/test/data/faq/{ex6.rb → ex5.rb} +2 -2
  36. data/test/data/faq/{ex6_eruby.result → ex5_eruby.result} +1 -1
  37. data/test/data/faq/{ex2-content.rbhtml → ex6-content.rbhtml} +0 -0
  38. data/test/data/faq/{ex2-layout.rbhtml → ex6-layout.rbhtml} +0 -0
  39. data/test/data/faq/{ex2_removenl.result → ex6_removenl.result} +1 -1
  40. data/test/data/faq/ex7-m18n.rb +48 -0
  41. data/test/data/faq/{ex8-m18n.rbhtml → ex7-m18n.rbhtml} +0 -0
  42. data/test/data/faq/{ex8_m18n.result → ex7_m18n.result} +1 -1
  43. data/test/data/faq/ex8-baselayout.rbhtml +8 -0
  44. data/test/data/faq/ex8-content.rbhtml +6 -0
  45. data/test/data/faq/{ex9-mylayout.rbhtml → ex8-mylayout.rbhtml} +0 -0
  46. data/test/data/faq/{ex9_changelayout.result → ex8_changelayout.result} +1 -1
  47. data/test/data/faq/ex9-baselayout.rbhtml +24 -5
  48. data/test/data/faq/ex9-content.rbhtml +12 -6
  49. data/test/data/faq/{ex10-customlayout.rbhtml → ex9-customlayout.rbhtml} +1 -1
  50. data/test/data/faq/{ex10_inherit.result → ex9_inherit.result} +1 -1
  51. data/test/data/faq/helpers1.rb +17 -0
  52. data/test/data/faq/helpers1.rbhtml +4 -0
  53. data/test/data/faq/helpers1.result +11 -0
  54. data/test/data/faq/helpers2.rb +21 -0
  55. data/test/data/faq/helpers2.rbhtml +4 -0
  56. data/test/data/faq/helpers2.result +11 -0
  57. data/test/data/faq/weekday1.rb +6 -0
  58. data/test/data/faq/weekday1.rbhtml +9 -0
  59. data/test/data/faq/weekday1.result +23 -0
  60. data/test/data/faq/weekday2.rb +8 -0
  61. data/test/data/faq/weekday2.rbhtml +10 -0
  62. data/test/data/faq/weekday2.result +24 -0
  63. data/test/data/faq/weekday3.rb +22 -0
  64. data/test/data/faq/weekday3.rbhtml +3 -0
  65. data/test/data/faq/weekday3.result +28 -0
  66. data/test/data/users_guide/test_010/main.rb +14 -0
  67. data/test/data/users_guide/test_010/result.output +13 -0
  68. data/test/data/users_guide/test_010/views/page.rbhtml +11 -0
  69. data/test/data/users_guide/test_011/main.rb +14 -0
  70. data/test/data/users_guide/test_011/result.output +2 -0
  71. data/test/data/users_guide/test_011/views/page.rbhtml +11 -0
  72. data/test/data/users_guide/test_020/main.rb +14 -0
  73. data/test/data/users_guide/test_020/result.output +13 -0
  74. data/test/data/users_guide/test_020/views/page.rbhtml +11 -0
  75. data/test/data/users_guide/test_021/main.rb +12 -0
  76. data/test/data/users_guide/test_021/result.output +12 -0
  77. data/test/data/users_guide/test_021/views/page.rbhtml +11 -0
  78. data/test/data/users_guide/test_030/main.rb +14 -0
  79. data/test/data/users_guide/test_030/result.output +23 -0
  80. data/test/data/users_guide/test_030/views/_layout.rbhtml +10 -0
  81. data/test/data/users_guide/test_030/views/page.rbhtml +11 -0
  82. data/test/data/users_guide/test_040/main.rb +14 -0
  83. data/test/data/users_guide/test_040/result.output +23 -0
  84. data/test/data/users_guide/test_040/views/_layout.rbhtml +10 -0
  85. data/test/data/users_guide/test_040/views/page.rbhtml +12 -0
  86. data/test/data/users_guide/test_050/main.rb +14 -0
  87. data/test/data/users_guide/test_050/result.output +14 -0
  88. data/test/data/users_guide/test_050/views/_layout.rbhtml +10 -0
  89. data/test/data/users_guide/test_050/views/page.rbhtml +13 -0
  90. data/test/data/users_guide/test_051/main.rb +14 -0
  91. data/test/data/users_guide/test_051/result.output +12 -0
  92. data/test/data/users_guide/test_051/views/_layout.rbhtml +11 -0
  93. data/test/data/users_guide/test_051/views/page.rbhtml +13 -0
  94. data/test/data/users_guide/test_060/main.rb +14 -0
  95. data/test/data/users_guide/test_060/result.output +29 -0
  96. data/test/data/users_guide/test_060/views/_footer.rbhtml +3 -0
  97. data/test/data/users_guide/test_060/views/_header.rbhtml +3 -0
  98. data/test/data/users_guide/test_060/views/_layout.rbhtml +13 -0
  99. data/test/data/users_guide/test_060/views/page.rbhtml +13 -0
  100. data/test/data/users_guide/test_070/main.rb +14 -0
  101. data/test/data/users_guide/test_070/result.output +29 -0
  102. data/test/data/users_guide/test_070/views/_footer.rbhtml +3 -0
  103. data/test/data/users_guide/test_070/views/_header.rbhtml +3 -0
  104. data/test/data/users_guide/test_070/views/_layout.rbhtml +13 -0
  105. data/test/data/users_guide/test_070/views/page.rbhtml +13 -0
  106. data/test/data/users_guide/test_capturing/main.rb +24 -0
  107. data/test/data/users_guide/test_capturing/result.output +28 -0
  108. data/test/data/users_guide/test_capturing/views/_layout.rbhtml +21 -0
  109. data/test/data/users_guide/test_capturing/views/blog-post.rbhtml +13 -0
  110. data/test/data/users_guide/test_context/context.rb +5 -0
  111. data/test/data/users_guide/test_context/context.yaml +4 -0
  112. data/test/data/users_guide/test_context/example.rbhtml +5 -0
  113. data/test/data/users_guide/test_context/result1.output +6 -0
  114. data/test/data/users_guide/test_context/result2.output +6 -0
  115. data/test/data/users_guide/test_context/result3.output +6 -0
  116. data/test/data/users_guide/test_context/result4.output +6 -0
  117. data/test/data/users_guide/test_convert/example.rbhtml +5 -0
  118. data/test/data/users_guide/test_convert/result1.output +7 -0
  119. data/test/data/users_guide/test_convert/result2.output +6 -0
  120. data/test/data/users_guide/test_escape/main.rb +4 -0
  121. data/test/data/users_guide/test_escape/result.output +5 -0
  122. data/test/data/users_guide/test_escape/views/page.rbhtml +4 -0
  123. data/test/data/users_guide/test_execute/example.rbhtml +6 -0
  124. data/test/data/users_guide/test_execute/result.output +6 -0
  125. data/test/data/users_guide/test_fragmentcache/cache.d/items/1 +5 -0
  126. data/test/data/users_guide/test_fragmentcache/main.rb +21 -0
  127. data/test/data/users_guide/test_fragmentcache/result.output +8 -0
  128. data/test/data/users_guide/test_fragmentcache/result2.output +6 -0
  129. data/test/data/users_guide/test_fragmentcache/views/items.rbhtml +10 -0
  130. data/test/data/users_guide/test_logging/ex-logger.rb +11 -0
  131. data/test/data/users_guide/test_logging/example.rbhtml +0 -0
  132. data/test/data/users_guide/test_logging/result1.output +3 -0
  133. data/test/data/users_guide/test_logging/result2.output +2 -0
  134. data/test/data/users_guide/test_m17n/m17n.rb +44 -0
  135. data/test/data/users_guide/test_m17n/m17n.rbhtml +5 -0
  136. data/test/data/users_guide/test_m17n/result.output +9 -0
  137. data/test/data/users_guide/test_m17n/result_en.output +4 -0
  138. data/test/data/users_guide/test_m17n/result_fr.output +4 -0
  139. data/test/data/users_guide/test_nested/main.rb +8 -0
  140. data/test/data/users_guide/test_nested/result.output +15 -0
  141. data/test/data/users_guide/test_nested/views/_blog_layout.rbhtml +5 -0
  142. data/test/data/users_guide/{layout8_html.rbhtml → test_nested/views/_site_layout.rbhtml} +0 -0
  143. data/test/data/users_guide/test_nested/views/blog_post.rbhtml +4 -0
  144. data/test/data/users_guide/test_preprocessing/pp-example1.rb +14 -0
  145. data/test/data/users_guide/test_preprocessing/result1a.output +11 -0
  146. data/test/data/users_guide/test_preprocessing/result1b.output +5 -0
  147. data/test/data/users_guide/test_preprocessing/result1c.output +6 -0
  148. data/test/data/users_guide/test_preprocessing/result2a.output +10 -0
  149. data/test/data/users_guide/test_preprocessing/result2b.output +10 -0
  150. data/test/data/users_guide/test_preprocessing/result3a.output +2 -0
  151. data/test/data/users_guide/test_preprocessing/result3b.output +2 -0
  152. data/test/data/users_guide/test_preprocessing/views/pp-example1.rbhtml +4 -0
  153. data/test/data/users_guide/{example12.rbhtml → test_preprocessing/views/pp-example2.rbhtml} +4 -4
  154. data/test/data/users_guide/test_preprocessing/views/pp-example3.rbhtml +7 -0
  155. data/test/data/users_guide/test_retrieve/example.rbhtml +10 -0
  156. data/test/data/users_guide/test_retrieve/result1.output +11 -0
  157. data/test/data/users_guide/test_retrieve/result2.output +11 -0
  158. data/test/data/users_guide/test_retrieve/result3.output +11 -0
  159. data/test/data/users_guide/test_retrieve/result4.output +8 -0
  160. data/test/data/users_guide/test_retrieve/result5.output +5 -0
  161. data/test/data/users_guide/test_safe/result.output +6 -0
  162. data/test/data/users_guide/test_safe/safe-test.rb +21 -0
  163. data/test/data/users_guide/test_safehelper/main.rb +16 -0
  164. data/test/data/users_guide/test_safehelper/result.output +8 -0
  165. data/test/data/users_guide/{example3.rbhtml → test_syntax_check/example.rbhtml} +0 -0
  166. data/test/data/users_guide/test_syntax_check/result.output +2 -0
  167. data/test/data/users_guide/test_trace/layout.rbhtml +7 -0
  168. data/test/data/users_guide/test_trace/main.rbhtml +5 -0
  169. data/test/data/users_guide/test_trace/result.output +16 -0
  170. data/test/data/users_guide/test_trace/trace-example.rb +4 -0
  171. data/test/oktest.rb +755 -0
  172. data/test/test_all.rb +24 -14
  173. data/test/test_engine.rb +628 -63
  174. data/test/test_engine.yaml +40 -3
  175. data/test/test_examples.rb +14 -12
  176. data/test/test_faq.rb +17 -12
  177. data/test/test_htmlhelper.rb +104 -33
  178. data/test/test_main.rb +32 -21
  179. data/test/test_main.yaml +2 -2
  180. data/test/test_safe.rb +206 -0
  181. data/test/test_store.rb +220 -0
  182. data/test/test_tcache.rb +94 -0
  183. data/test/test_template.rb +65 -23
  184. data/test/test_template.yaml +7 -7
  185. data/test/test_users_guide.rb +75 -29
  186. data/test/testcase-helper.rb +20 -18
  187. data/test/testunit-assertions.rb +71 -0
  188. metadata +185 -159
  189. data/doc-api/classes/Tenjin.html +0 -141
  190. data/doc-api/classes/Tenjin/ArrayBufferTemplate.html +0 -270
  191. data/doc-api/classes/Tenjin/BaseContext.html +0 -329
  192. data/doc-api/classes/Tenjin/Context.html +0 -126
  193. data/doc-api/classes/Tenjin/ContextHelper.html +0 -461
  194. data/doc-api/classes/Tenjin/Engine.html +0 -616
  195. data/doc-api/classes/Tenjin/ErubisTemplate.html +0 -166
  196. data/doc-api/classes/Tenjin/HtmlHelper.html +0 -359
  197. data/doc-api/classes/Tenjin/Preprocessor.html +0 -242
  198. data/doc-api/classes/Tenjin/Template.html +0 -916
  199. data/doc-api/created.rid +0 -1
  200. data/doc-api/files/README_txt.html +0 -188
  201. data/doc-api/files/lib/tenjin_rb.html +0 -136
  202. data/doc-api/fr_class_index.html +0 -36
  203. data/doc-api/fr_file_index.html +0 -28
  204. data/doc-api/fr_method_index.html +0 -91
  205. data/doc-api/index.html +0 -24
  206. data/doc-api/rdoc-style.css +0 -208
  207. data/doc/examples.html +0 -312
  208. data/doc/faq.html +0 -909
  209. data/examples/preprocessing/select.rbhtml.cache +0 -17
  210. data/test/Rookbook.yaml +0 -14
  211. data/test/assert-text-equal.rb +0 -45
  212. data/test/data/faq/ex10-baselayout.rbhtml +0 -27
  213. data/test/data/faq/ex11-content.rbhtml +0 -9
  214. data/test/data/faq/ex8-m18n.rb +0 -77
  215. data/test/data/users_guide/content6.rbhtml +0 -3
  216. data/test/data/users_guide/content7.rbhtml +0 -5
  217. data/test/data/users_guide/content8.rbhtml +0 -2
  218. data/test/data/users_guide/contextdata.rb +0 -7
  219. data/test/data/users_guide/datafile.rb +0 -5
  220. data/test/data/users_guide/datafile.yaml +0 -10
  221. data/test/data/users_guide/ex.rbhtml +0 -6
  222. data/test/data/users_guide/ex.result +0 -7
  223. data/test/data/users_guide/ex.script +0 -5
  224. data/test/data/users_guide/ex_script.result +0 -7
  225. data/test/data/users_guide/ex_source.result +0 -8
  226. data/test/data/users_guide/example1.rbhtml +0 -12
  227. data/test/data/users_guide/example1.result +0 -17
  228. data/test/data/users_guide/example10.rbhtml +0 -4
  229. data/test/data/users_guide/example10_template_args.result +0 -6
  230. data/test/data/users_guide/example11.rbhtml +0 -5
  231. data/test/data/users_guide/example11_template_args_result +0 -2
  232. data/test/data/users_guide/example12_preprocessed.result +0 -10
  233. data/test/data/users_guide/example12_preprocessed_source.result +0 -10
  234. data/test/data/users_guide/example13.rbhtml +0 -6
  235. data/test/data/users_guide/example13_preprocessed.result +0 -2
  236. data/test/data/users_guide/example13_preprocessed_source.result +0 -2
  237. data/test/data/users_guide/example14.rb +0 -32
  238. data/test/data/users_guide/example14.rbhtml +0 -6
  239. data/test/data/users_guide/example14_tmplclass.result +0 -15
  240. data/test/data/users_guide/example15.rb +0 -10
  241. data/test/data/users_guide/example15_escapefunc.result +0 -14
  242. data/test/data/users_guide/example16.rbhtml +0 -4
  243. data/test/data/users_guide/example16a.rb +0 -10
  244. data/test/data/users_guide/example16a.result +0 -4
  245. data/test/data/users_guide/example16b.rb +0 -13
  246. data/test/data/users_guide/example16b.result +0 -4
  247. data/test/data/users_guide/example16c.rb +0 -12
  248. data/test/data/users_guide/example16c.result +0 -4
  249. data/test/data/users_guide/example16d.rb +0 -27
  250. data/test/data/users_guide/example16d.result +0 -4
  251. data/test/data/users_guide/example1_S.result +0 -14
  252. data/test/data/users_guide/example1_SXNC.result +0 -10
  253. data/test/data/users_guide/example1_source.result +0 -14
  254. data/test/data/users_guide/example2.rbhtml +0 -3
  255. data/test/data/users_guide/example2_sb.result2 +0 -9
  256. data/test/data/users_guide/example3_syntaxcheck.result +0 -2
  257. data/test/data/users_guide/example4.rbhtml +0 -13
  258. data/test/data/users_guide/example4_datafile_rb.result +0 -13
  259. data/test/data/users_guide/example4_yaml.result +0 -13
  260. data/test/data/users_guide/example5.rbhtml +0 -9
  261. data/test/data/users_guide/example5_datastr_rb.result +0 -9
  262. data/test/data/users_guide/example5_datastr_yaml.result +0 -9
  263. data/test/data/users_guide/example6.rbhtml +0 -19
  264. data/test/data/users_guide/example6_layout.result +0 -29
  265. data/test/data/users_guide/example6_nested.result +0 -28
  266. data/test/data/users_guide/example7_layout2.result +0 -13
  267. data/test/data/users_guide/example8_layout3.result +0 -8
  268. data/test/data/users_guide/example9.rbhtml +0 -18
  269. data/test/data/users_guide/example9_capture.result +0 -26
  270. data/test/data/users_guide/footer.html +0 -5
  271. data/test/data/users_guide/footer.rbhtml +0 -4
  272. data/test/data/users_guide/layout6.rbhtml +0 -17
  273. data/test/data/users_guide/layout7.rbhtml +0 -9
  274. data/test/data/users_guide/layout8_xhtml.rbhtml +0 -6
  275. data/test/data/users_guide/layout9.rbhtml +0 -25
  276. data/test/data/users_guide/sidemenu.rbhtml +0 -5
  277. data/test/data/users_guide/user_app.cgi +0 -39
  278. data/test/data/users_guide/user_app.result +0 -30
  279. data/test/data/users_guide/user_create.rbhtml +0 -6
  280. data/test/data/users_guide/user_edit.rbhtml +0 -7
  281. data/test/data/users_guide/user_form.rbhtml +0 -10
  282. data/test/data/users_guide/user_layout.rbhtml +0 -16
@@ -1,23 +1,33 @@
1
1
  ###
2
- ### $Rev: 45 $
3
- ### $Release: 0.6.2 $
4
- ### copyright(c) 2007-2008 kuwata-lab.com all rights reserved.
2
+ ### $Rev$
3
+ ### $Release: 0.7.0 $
4
+ ### copyright(c) 2007-2011 kuwata-lab.com all rights reserved
5
5
  ###
6
6
 
7
7
  basedir = File.dirname(File.dirname(File.expand_path(__FILE__)))
8
- testdir = basedir + '/test'
9
- $: << testdir
8
+ libdir = File.join(basedir, 'lib')
9
+ testdir = File.join(basedir, 'test')
10
+ $:.unshift testdir
11
+ $:.unshift libdir
12
+ ENV['PATH'] = File.join(basedir, "bin") + File::PATH_SEPARATOR + ENV['PATH']
10
13
 
11
- require 'test/unit'
14
+ #require 'test/unit'
12
15
  #require 'testutil'
16
+ require 'oktest'
13
17
  require 'testcase-helper'
14
- require 'assert-text-equal'
18
+ #require 'testunit-assertions'
15
19
  require 'tenjin'
16
20
 
17
- Dir.chdir testdir
18
- load 'test_template.rb'
19
- load 'test_engine.rb'
20
- load 'test_main.rb'
21
- load 'test_htmlhelper.rb'
22
- load 'test_users_guide.rb'
23
- load 'test_faq.rb'
21
+ if __FILE__ == $0
22
+ Dir.chdir testdir
23
+ load 'test_template.rb'
24
+ load 'test_engine.rb'
25
+ load 'test_main.rb'
26
+ load 'test_htmlhelper.rb'
27
+ load 'test_safe.rb'
28
+ load 'test_store.rb'
29
+ load 'test_tcache.rb'
30
+ load 'test_users_guide.rb'
31
+ load 'test_faq.rb'
32
+ load 'test_examples.rb'
33
+ end
@@ -1,13 +1,12 @@
1
1
  ###
2
- ### $Rev: 72 $
3
- ### $Release: 0.6.2 $
4
- ### copyright(c) 2007-2008 kuwata-lab.com all rights reserved.
2
+ ### $Rev$
3
+ ### $Release: 0.7.0 $
4
+ ### copyright(c) 2007-2011 kuwata-lab.com all rights reserved
5
5
  ###
6
6
 
7
- require 'test/unit'
8
- #require 'testutil'
9
- require 'testcase-helper'
10
- require 'assert-text-equal'
7
+ require "#{File.dirname(File.expand_path(__FILE__))}/test_all"
8
+ require 'fileutils'
9
+
11
10
  if defined?(RBX_VERSION)
12
11
  require 'kwalify'
13
12
  def load_yaml_str(s)
@@ -20,10 +19,10 @@ else
20
19
  end
21
20
  end
22
21
 
23
- require 'tenjin'
24
22
 
25
23
 
26
- class TenjinEngineTest < Test::Unit::TestCase
24
+ class TenjinEngineTest
25
+ include Oktest::TestCase
27
26
 
28
27
  s = File.read(__FILE__.sub(/\.\w+$/, '.yaml'))
29
28
  s.gsub!(/^\t/, ' ' * 8)
@@ -50,6 +49,10 @@ class TenjinEngineTest < Test::Unit::TestCase
50
49
  end
51
50
  end
52
51
 
52
+ def _read_file(fname)
53
+ File.open(fname, 'rb') {|f| f.read() }
54
+ end
55
+
53
56
  def _remove_files(*filenames)
54
57
  for filename in filenames.flatten
55
58
  for fname in Dir.glob("#{filename}*")
@@ -81,7 +84,7 @@ class TenjinEngineTest < Test::Unit::TestCase
81
84
  filename = 'user_%s.rbhtml' % action
82
85
  tplname = shortp ? action.intern : filename
83
86
  output = engine.render(tplname, context, layoutp)
84
- assert_text_equal(expected, output)
87
+ ok_(output) == expected
85
88
 
86
89
  ## teardown
87
90
  ensure
@@ -181,7 +184,7 @@ class TenjinEngineTest < Test::Unit::TestCase
181
184
  engine = Tenjin::Engine.new(:prefix=>'user_', :postfix=>'.rbhtml', :layout=>:layout, :templateclass=>_template_class())
182
185
  context = { :items => %w[AAA BBB CCC] }
183
186
  result = engine.render(:content, context)
184
- assert_text_equal(expected, result)
187
+ ok_(result) == expected
185
188
  ensure
186
189
  [layout_filename, layout_filename + '.cache',
187
190
  content_filename, content_filename + '.cache'].each do |filename|
@@ -206,7 +209,7 @@ class TenjinEngineTest < Test::Unit::TestCase
206
209
  end
207
210
  engine = Tenjin::Engine.new(:postfix=>'.rbhtml', :templateclass=>_template_class())
208
211
  result = engine.render(:content, context)
209
- assert_text_equal(expected, result)
212
+ ok_(result) == expected
210
213
  ensure
211
214
  for filename, content in files
212
215
  for fname in Dir.glob("#{filename}*")
@@ -231,19 +234,19 @@ class TenjinEngineTest < Test::Unit::TestCase
231
234
  content_html = hash['content_html']
232
235
  File.write(fname.call('content_html'), content_html)
233
236
  actual = engine.render(:content_html, context)
234
- assert_text_equal(hash['expected_html'], actual)
237
+ ok_(actual) == hash['expected_html']
235
238
  ##
236
239
  sleep(1)
237
240
  content_html = hash['content_html'] + "<?rb @_layout = :layout_xhtml ?>\n"
238
241
  File.write(fname.call('content_html'), content_html)
239
242
  actual = engine.render(:content_html, context)
240
- assert_text_equal(hash['expected_xhtml'], actual)
243
+ ok_(actual) == hash['expected_xhtml']
241
244
  ##
242
245
  sleep(1)
243
246
  content_html = hash['content_html'] + "<?rb @_layout = false ?>\n"
244
247
  File.write(fname.call('content_html'), content_html)
245
248
  actual = engine.render(:content_html, context)
246
- assert_text_equal(hash['expected_nolayout'], actual)
249
+ ok_(actual) == hash['expected_nolayout']
247
250
  ensure
248
251
  names.collect {|name| fname.call(name)}.each do |filename|
249
252
  File.unlink(filename) if test(?f, filename)
@@ -273,29 +276,29 @@ class TenjinEngineTest < Test::Unit::TestCase
273
276
  args[:cache] = false
274
277
  engine = Tenjin::Engine.new(args)
275
278
  output = engine.render(:create, context)
276
- assert_text_equal(expected, output)
277
- assert !test(?f, 'account_create.rbhtml.cache')
278
- #assert !test(?f, 'account_create.rbhtml.pstore')
279
- assert !test(?f, 'account_form.rbhtml.cache')
280
- #assert !test(?f, 'account_form.rbhtml.pstore')
279
+ ok_(output) == expected
280
+ not_ok_('account_create.rbhtml.cache').exist?
281
+ #not_ok_('account_create.rbhtml.pstore').exist?
282
+ not_ok_('account_form.rbhtml.cache').exist?
283
+ not_ok_('account_form.rbhtml.pstore').exist?
281
284
  ## ruby code caching
282
285
  args[:cache] = true
283
286
  engine = Tenjin::Engine.new(args)
284
287
  output = engine.render(:create, context)
285
- assert test(?f, 'account_create.rbhtml.cache')
286
- #assert !test(?f, 'account_create.rbhtml.pstore')
287
- assert test(?f, 'account_form.rbhtml.cache')
288
- #assert !test(?f, 'account_form.rbhtml.pstore')
288
+ ok_('account_create.rbhtml.cache').file?
289
+ #not_ok_('account_create.rbhtml.cache').exist?
290
+ ok_('account_form.rbhtml.cache').file?
291
+ #not_ok_('account_form.rbhtml.pstore').exist?
289
292
  File.unlink('account_create.rbhtml.cache')
290
293
  File.unlink('account_form.rbhtml.cache')
291
294
  ## pstore caching
292
295
  #args[:cache] = true
293
296
  #engine = Tenjin::Engine(args)
294
297
  #output = engine.render(:create, context)
295
- #assert !test(?f, 'account_create.rbhtml.cache')
296
- #assert !test(?f, 'account_create.rbhtml.pstore')
297
- #assert !test(?f, 'account_form.rbhtml.cache')
298
- #assert !test(?f, 'account_form.rbhtml.pstore')
298
+ #not_ok_('account_create.rbhtml.cache').file?
299
+ #not_ok_('account_create.rbhtml.pstore').file?
300
+ #not_ok_('account_form.rbhtml.cache').file?
301
+ #not_ok_('account_form.rbhtml.pstore').file?
299
302
  ensure
300
303
  for key, filename in filenames
301
304
  for fname in [filename, filename+'.cache', filename+'.pstore']
@@ -320,30 +323,30 @@ class TenjinEngineTest < Test::Unit::TestCase
320
323
  end
321
324
  # when no cache file
322
325
  args1 = nil;
323
- ex = assert_raise(exception) do
324
- assert(!test(?f, 'content.rbhtml.cache'))
325
- engine = Tenjin::Engine.new(:cache=>true)
326
- args1 = engine.get_template('content.rbhtml').args
327
- assert_not_nil(args1)
328
- output = engine.render('content.rbhtml', context)
329
- end
326
+ ex = ok_(proc {
327
+ not_ok_('content.rbhtml.cache').file?
328
+ engine = Tenjin::Engine.new(:cache=>true)
329
+ args1 = engine.get_template('content.rbhtml').args
330
+ not_ok_(args1) == nil
331
+ output = engine.render('content.rbhtml', context)
332
+ }).raise?(exception)
330
333
  msg = ex.to_s.sub(/:0x[0-9a-fA-F]\w+/, ':0x12345')
331
334
  msg = msg[0, errormsg.length-1]+'>' if defined?(RBX_VERSION)
332
- assert_equal(errormsg, msg)
335
+ ok_(msg) == errormsg
333
336
  # when cache file exist
334
- ex = assert_raise(NameError) do
335
- #File.unlink('content.rbhtml');
336
- assert(test(?f, 'content.rbhtml.cache'))
337
- engine = Tenjin::Engine.new(:cache=>true)
338
- args2 = engine.get_template('content.rbhtml').args
339
- assert_not_nil(args2)
340
- assert_equal(args1, args2)
341
- output = engine.render('content.rbhtml', context)
342
- end
343
- #assert_equal(errormsg, ex.to_s.sub(/:0x\w+>/, '>'))
337
+ ex = ok_(proc {
338
+ #File.unlink('content.rbhtml');
339
+ ok_('content.rbhtml.cache').file?
340
+ engine = Tenjin::Engine.new(:cache=>true)
341
+ args2 = engine.get_template('content.rbhtml').args
342
+ not_ok_(args2) == nil
343
+ ok_(args2) == args1
344
+ output = engine.render('content.rbhtml', context)
345
+ }).raise?(NameError)
346
+ #ok_(ex.to_s.sub(/:0x\w+>/, '>')) == errormsg
344
347
  msg = ex.to_s.sub(/:0x[0-9a-fA-F]\w+/, ':0x12345')
345
348
  msg = msg[0, errormsg.length-1]+'>' if defined?(RBX_VERSION)
346
- assert_equal(errormsg, msg)
349
+ ok_(msg) == errormsg
347
350
  ensure
348
351
  _remove_files(['content'])
349
352
  end
@@ -361,25 +364,25 @@ class TenjinEngineTest < Test::Unit::TestCase
361
364
  cachename = filename+'.cache'
362
365
  engine = Tenjin::Engine.new(:cache=>cacheflag)
363
366
  t = engine.get_template(filename)
364
- assert_equal(args, t.args)
365
- assert_text_equal(script, t.script)
367
+ ok_(t.args) == args
368
+ ok_(t.script) == script
366
369
  cache_actual = File.read(engine.cachename(filename))
367
- assert_text_equal(cache, cache_actual)
370
+ ok_(cache_actual) == cache
368
371
  end
369
372
  #
370
373
  ## args=[x,y,z], cache=1
371
374
  for f in Dir.glob(filename+'*') do File.unlink(f) end
372
375
  File.write(filename, data["input1"])
373
- assert(!test(?f, cachename))
376
+ not_ok_(cachename).file?
374
377
  _testproc.call(cacheflag=true, n=1)
375
- assert(test(?f, cachename))
378
+ ok_(cachename).file?
376
379
  _testproc.call(cacheflag=true, n=1)
377
380
  ## args=[], cache=1
378
381
  sleep(1)
379
382
  File.write(filename, data["input2"])
380
- #assert(test(?f, cachename))
383
+ #ok_(cachename).file?
381
384
  _testproc.call(cacheflag=true, n=2)
382
- #assert(test(?f, cachename))
385
+ #ok_(cachename).file?
383
386
  _testproc.call(cacheflag=true, n=2)
384
387
  ensure
385
388
  _remove_files(['input.pyhtml'])
@@ -410,9 +413,8 @@ class TenjinEngineTest < Test::Unit::TestCase
410
413
  output = engine.render(:body, context)
411
414
  #
412
415
  expected = data["expected_#{keys.join('_')}"]
413
- assert_text_equal(expected, output)
416
+ ok_(output) == expected
414
417
  ensure
415
- require 'fileutils'
416
418
  FileUtils.rm_rf(basedir)
417
419
  end
418
420
  end
@@ -462,26 +464,584 @@ class TenjinEngineTest < Test::Unit::TestCase
462
464
  #
463
465
  context = { :title=>'Create', :action=>'create', :params=>{'state'=>:NY} }
464
466
  actual = engine.render(:create, context) # 1st
465
- assert_text_equal(data['expected1'], actual)
467
+ ok_(actual) == data['expected1']
466
468
  context[:params] = {'state'=>:xx}
467
469
  actual = engine.render(:create, context) # 2nd
468
- #assert_text_equal(data['expected1'], actual)
469
- assert_text_equal(data['expected1'].sub(/ checked="checked"/, ''), actual)
470
+ #ok_(actual) == data['expected1']
471
+ ok_(actual) == data['expected1'].sub(/ checked="checked"/, '')
470
472
  #
471
473
  context = { :title=>'Update', :action=>'update', :params=>{'state'=>:NY} }
472
474
  actual = engine.render(:update, context) # 1st
473
- assert_text_equal(data['expected2'], actual)
475
+ ok_(actual) == data['expected2']
474
476
  context[:params] = {'state'=>:xx}
475
477
  actual = engine.render(:update, context) # 2nd
476
- assert_text_equal(data['expected2'], actual) # not changed!
477
- #assert_text_equal(data['expected2'].sub(/ checked="checked"/, ''), actual)
478
+ ok_(actual) == data['expected2'] # not changed!
479
+ #ok_(actual) == data['expected2'].sub(/ checked="checked"/, '')
478
480
  ensure
479
481
  _remove_files(Dir.glob('prep_*'))
480
482
  end
481
483
 
482
484
 
483
- self.select_target_test()
485
+ def test_include_with_preprocess
486
+ data = TESTDATA['test_include_with_preprocess']
487
+ index_rbhtml = data['index_html']
488
+ show_rbhtml = data['show_html']
489
+ expected = data['expected']
490
+ testopts = data['testopts']
491
+ #
492
+ File.write("index.rbhtml", index_rbhtml)
493
+ File.write("show.rbhtml", show_rbhtml)
494
+ #
495
+ engine = Tenjin::Engine.new(:cache=>false, :preprocess=>false)
496
+ actual = engine.render("index.rbhtml")
497
+ ok_(actual) == expected
498
+ #
499
+ engine = Tenjin::Engine.new(:cache=>false, :preprocess=>true)
500
+ actual = engine.render("index.rbhtml")
501
+ ok_(actual) == expected
502
+ ensure
503
+ %w[index.rbhtml show.rbhtml].each {|x| File.unlink(x) if File.exist?(x) }
504
+ end
505
+
506
+
507
+ def test_fragmentcache
508
+ input = <<'END'
509
+ <html>
510
+ <body>
511
+ <?rb cache_with("entries/index", 5*60) do ?>
512
+ <?rb entries = @entries.call ?>
513
+ <ul>
514
+ <?rb for entry in entries ?>
515
+ <li>${entry}</li>
516
+ <?rb end ?>
517
+ </ul>
518
+ <?rb end ?>
519
+ </body>
520
+ </html>
521
+ END
522
+ expected_output = <<'END'
523
+ <html>
524
+ <body>
525
+ <ul>
526
+ <li>Haruhi</li>
527
+ <li>Mikuru</li>
528
+ <li>Yuki</li>
529
+ <li>Kyon</li>
530
+ <li>Itsuki</li>
531
+ </ul>
532
+ </body>
533
+ </html>
534
+ END
535
+ expected_cache = <<'END'
536
+ <ul>
537
+ <li>Haruhi</li>
538
+ <li>Mikuru</li>
539
+ <li>Yuki</li>
540
+ <li>Kyon</li>
541
+ <li>Itsuki</li>
542
+ </ul>
543
+ END
544
+ expected_output1 = expected_output.gsub(/^.*(Kyon|Itsuki).*\n/, '')
545
+ expected_cache1 = expected_cache .gsub(/^.*(Kyon|Itsuki).*\n/, '')
546
+ expected_output2 = expected_output.gsub(/^.*(Itsuki).*\n/, '')
547
+ expected_cache2 = expected_cache .gsub(/^.*(Itsuki).*\n/, '')
548
+ expected_output3 = expected_output
549
+ expected_cache3 = expected_cache
550
+ #
551
+ begin
552
+ fname = "fragtest.rbhtml"
553
+ File.open(fname, 'wb') {|f| f.write(input) }
554
+ cachedir = ".test.fragcache"
555
+ Dir.mkdir(cachedir)
556
+ fragcache_fpath = "#{cachedir}/entries/index"
557
+ kv_store = Tenjin::FileBaseStore.new(cachedir)
558
+ Tenjin::Engine.data_cache = kv_store
559
+ engine = Tenjin::Engine.new
560
+ # or engine = Tenjin::Engine.new(:data_cache=>kv_store)
561
+ spec "if called first time then calls block and save output to cache store" do
562
+ called = false
563
+ entries = proc { called = true; ['Haruhi', 'Mikuru', 'Yuki'] }
564
+ html = engine.render(fname, {:entries => entries})
565
+ ok_(called) == true
566
+ ok_(fragcache_fpath).exist?
567
+ ok_(html) == expected_output1
568
+ ok_(_read_file(fragcache_fpath)) == expected_cache1
569
+ end
570
+ spec "if called second time then don't call block and reuse cached data" do
571
+ called = false
572
+ entries = proc { called = true; ['Haruhi', 'Mikuru', 'Yuki'] }
573
+ html = engine.render(fname, {:entries => entries})
574
+ ok_(called) == false
575
+ ok_(html) == expected_output1
576
+ end
577
+ spec "if called after cache is expired then block is called again" do
578
+ called = false
579
+ entries = proc { called = true; ['Haruhi', 'Mikuru', 'Yuki', 'Kyon'] }
580
+ ## expire cache
581
+ atime = File.atime(fragcache_fpath)
582
+ mtime = File.mtime(fragcache_fpath)
583
+ File.utime(atime, mtime-5*60, fragcache_fpath)
584
+ mtime = File.mtime(fragcache_fpath)
585
+ ##
586
+ html = engine.render(fname, {:entries => entries})
587
+ ok_(called) == true
588
+ ok_(File.mtime(fragcache_fpath)) > mtime+5*60-1
589
+ ok_(html) == expected_output2
590
+ ok_(_read_file(fragcache_fpath)) == expected_cache2
591
+ end
592
+ spec "if template file is updated then block is called again" do
593
+ ## update template timestamp
594
+ #t = Time.now + 1
595
+ sleep(1)
596
+ t = Time.now
597
+ File.utime(t, t, fname)
598
+ ##
599
+ called = false
600
+ entries = proc { called = true; ['Haruhi', 'Mikuru', 'Yuki', 'Kyon', 'Itsuki'] }
601
+ html = engine.render(fname, {:entries => entries})
602
+ ok_(called) == true
603
+ ok_(html) == expected_output3
604
+ ok_(_read_file(fragcache_fpath)) == expected_cache3
605
+ end
606
+ ensure
607
+ FileUtils.rm_rf(cachedir)
608
+ [fname, "#{fname}.cache"].each do |x|
609
+ File.unlink(x) if File.file?(x)
610
+ end
611
+ end
612
+ end
613
+
614
+ def test_default_datacache
615
+ spec "if datastore is not speicified then @@datastore is used instead" do
616
+ begin
617
+ backup = Tenjin::Engine.data_cache
618
+ Tenjin::Engine.data_cache = store = Tenjin::FileBaseStore.new('/tmp')
619
+ engine = Tenjin::Engine.new
620
+ ok_(engine.data_cache).same?(store)
621
+ ensure
622
+ Tenjin::Engine.data_cache = backup
623
+ end
624
+ end
625
+ end
626
+
627
+
628
+ ###
629
+
630
+ def self.before_all
631
+ Tenjin::Engine.class_eval do
632
+ @_methods = [ :cachename, :to_filename,
633
+ :_get_template_in_memory, :_get_template_in_cache, :_timestamp_changed?,
634
+ :_preprocess, :create_template, :hook_context ]
635
+ public(*@_methods)
636
+ end
637
+ end
638
+
639
+ def self.after_all
640
+ Tenjin::Engine.class_eval do
641
+ private(*@_methods)
642
+ end
643
+ end
644
+
645
+ def _write(filename, content)
646
+ File.open(filename, 'wb') {|f| f.write(content) }
647
+ end
648
+
649
+ def _with_dummy_files
650
+ engine = Tenjin::Engine.new(:path=>['_views/blog', '_views'], :postfix=>'.rbhtml')
651
+ begin
652
+ FileUtils.mkdir_p('_views/blog')
653
+ _write('_views/blog/index.rbhtml', 'xxx')
654
+ _write('_views/index.rbhtml', '<<#{{@dummy_value}}>>')
655
+ _write('_views/layout.rbhtml', '<div>#{_content}</div>')
656
+ yield(engine)
657
+ ensure
658
+ FileUtils.rm_rf('_views')
659
+ end
660
+ end
661
+
662
+ def test__template_cache
663
+ engine = Tenjin::Engine.new()
664
+ spec "if cache is nil or true then return @@template_cache" do
665
+ expected = Tenjin::Engine.template_cache
666
+ ok_(engine.__send__(:_template_cache, nil)).same?(expected)
667
+ ok_(engine.__send__(:_template_cache, true)).same?(expected)
668
+ end
669
+ spec "if cache is false tehn return NullemplateCache object" do
670
+ ok_(engine.__send__(:_template_cache, false)).is_a?(Tenjin::NullTemplateCache)
671
+ end
672
+ spec "if cache is an instnce of TemplateClass then return it" do
673
+ eval 'class FooTemplateCache < Tenjin::TemplateCache; end'
674
+ cache = FooTemplateCache.new
675
+ ok_(engine.__send__(:_template_cache, cache)).same?(cache)
676
+ end
677
+ spec "if else then raises error" do
678
+ f = proc { engine.__send__(:_template_cache, "hoge") }
679
+ ok_(f).raise?(ArgumentError, ":cache is expected true, false, or TemplateCache object")
680
+ end
681
+ end
682
+
683
+ def test_cachename
684
+ engine = Tenjin::Engine.new
685
+ fpath = 'foobar.rbhtml'.taint
686
+ spec "return cache file name which is untainted." do
687
+ ok_(fpath.tainted?) == true
688
+ ret = engine.cachename(fpath)
689
+ ok_(ret) == fpath + '.cache'
690
+ ok_(ret.tainted?) == false
691
+ end
692
+ spec "if lang is provided then add it to cache filename." do
693
+ engine.lang = 'en'
694
+ ok_(engine.cachename(fpath)) == 'foobar.rbhtml.en.cache'
695
+ end
696
+ end
697
+
698
+ def test_to_filename
699
+ engine = Tenjin::Engine.new(:prefix=>'views/', :postfix=>'.rbhtml')
700
+ spec "if template_name is a Symbol, add prefix and postfix to it." do
701
+ ok_(engine.to_filename(:index)) == "views/index.rbhtml"
702
+ end
703
+ spec "if template_name is not a Symbol, just return it." do
704
+ ok_(engine.to_filename('index')) == 'index'
705
+ end
706
+ end
707
+
708
+ def test_register_template
709
+ engine = Tenjin::Engine.new(:postfix=>'.rbhtml')
710
+ template = Tenjin::Template.new(nil)
711
+ spec "register template object without file path." do
712
+ engine.register_template(:foo, template)
713
+ ok_(engine.instance_variable_get('@_templates')) == {'foo.rbhtml'=>[template, nil]}
714
+ ok_(engine.get_template(:foo)) == template
715
+ end
716
+ end
717
+
718
+ def test__timestamp_changed?
719
+ _with_dummy_files do |engine|
720
+ e = engine
721
+ t = Tenjin::Template.new('_views/index.rbhtml')
722
+ mtime = File.mtime('_views/index.rbhtml')
723
+ spec "if checked within a sec, skip timestamp check and return false." do
724
+ t.timestamp = mtime + 30
725
+ ok_(e._timestamp_changed?(t)) == true
726
+ t._last_checked_at = Time.now - 0.5
727
+ ok_(e._timestamp_changed?(t)) == false
728
+ end
729
+ spec "if timestamp is same as file, return false." do
730
+ t._last_checked_at = nil
731
+ t.timestamp = mtime
732
+ ok_(e._timestamp_changed?(t)) == false
733
+ ok_(t._last_checked_at.to_f).in_delta?(Time.now.to_f, 0.001)
734
+ end
735
+ spec "if timestamp is changed, return true." do
736
+ t._last_checked_at = nil
737
+ t.timestamp = mtime + 1
738
+ ok_(e._timestamp_changed?(t)) == true
739
+ end
740
+ end
741
+ end
742
+
743
+ def test__get_template_in_memory
744
+ fname = 'index.rbhtml'
745
+ fpath = '_views/blog/index.rbhtml'
746
+ _with_dummy_files do |engine|
747
+ e = engine
748
+ t = Tenjin::Template.new(fpath)
749
+ _templates = e.instance_variable_get('@_templates')
750
+ spec "if template object is not in memory cache then return nil." do
751
+ ok_(e._get_template_in_memory(fname)) == nil
752
+ end
753
+ spec "if without filepath, don't check timestamp and return it." do
754
+ _templates[fname] = ["foo", nil]
755
+ ok_(e._get_template_in_memory(fname)) == "foo"
756
+ end
757
+ spec "if timestamp of template file is not changed, return it." do
758
+ _templates[fname] = [t, fpath]
759
+ t.timestamp = File.mtime(fpath)
760
+ ok_(e._get_template_in_memory(fname)) == t
761
+ end
762
+ spec "if timestamp of template file is changed, clear it and return nil." do
763
+ t._last_checked_at = nil
764
+ _templates[fname] = [t, fpath]
765
+ t.timestamp = File.mtime(fpath) + 1
766
+ ok_(e._get_template_in_memory(fname)) == nil
767
+ ok_(_templates[fname]) == nil
768
+ end
769
+ end
770
+ end
771
+
772
+ def test__get_template_in_cache
773
+ _with_dummy_files do |engine|
774
+ e = engine
775
+ _templates = e.instance_variable_get('@_templates')
776
+ fname = 'index.rbhtml'
777
+ fpath = '_views/blog/index.rbhtml'
778
+ cpath = '_views/blog/index.rbhtml.cache'
779
+ t = nil
780
+ spec "if template is not found in cache file, return nil." do
781
+ not_ok_(cpath).exist?
782
+ ok_(e._get_template_in_cache(fpath, cpath)) == nil
783
+ t = e.get_template(:index)
784
+ ok_(cpath).exist?
785
+ t2 = e._get_template_in_cache(fpath, cpath)
786
+ ok_(t2.filename) == t.filename
787
+ ok_(t2.timestamp) == t.timestamp
788
+ ok_(t2.script) == t.script
789
+ end
790
+ spec "if cache returns script and args then build a template object from them." do
791
+ ok_(e.cache.load(cpath)) == [" _buf << %Q`xxx`; \n", [], File.mtime(cpath)]
792
+ ok_(t).is_a?(Tenjin::Template)
793
+ end
794
+ spec "if timestamp of template is changed then ignore it." do
795
+ t._last_checked_at = nil
796
+ ts = Time.now + 1
797
+ File.utime(ts, ts, cpath)
798
+ ok_(e._get_template_in_cache(fpath, cpath)) == nil
799
+ end
800
+ spec "if timestamp is not changed then return it." do
801
+ t._last_checked_at = nil
802
+ ts = Time.now.to_i.to_f
803
+ File.utime(ts, ts, fpath)
804
+ File.utime(ts, ts, cpath)
805
+ t2 = e._get_template_in_cache(fpath, cpath)
806
+ ok_(t2).is_a?(Tenjin::Template)
807
+ ok_(t2.filename) == fpath
808
+ ok_(t2.timestamp.to_f) == ts
809
+ end
810
+ end
811
+ end
812
+
813
+ def test_get_template
814
+ _with_dummy_files do |engine|
815
+ e = engine
816
+ _templates = e.instance_variable_get('@_templates')
817
+ fname = "index.rbhtml"
818
+ fpath = "_views/blog/index.rbhtml"
819
+ cpath = "_views/blog/index.rbhtml.cache"
820
+ #fpath = "#{Dir.pwd}/_views/blog/index.rbhtml"
821
+ pre_cond { not_ok_(cpath).exist? }
822
+ t = nil
823
+ spec "accept template name such as :index" do
824
+ t = e.get_template(:index)
825
+ ok_(t.filename) == '_views/blog/index.rbhtml'
826
+ end
827
+ spec "if template object is memory cache then return it." do
828
+ pre_cond { ok_(_templates[fname].first).same?(t) }
829
+ pre_cond { ok_(t.timestamp) == File.mtime(fpath) }
830
+ ok_(e.get_template(:index)).same?(t)
831
+ sleep 1
832
+ ok_(e.get_template(:index)).same?(t)
833
+ end
834
+ spec "if template is cached in file then store it into memory and return it." do
835
+ _templates.clear() # clear memory cache
836
+ File.open(cpath, 'wb') {|f| f.write("\#@ARGS \n\nSOS") } # change cache content
837
+ mtime = File.mtime(fpath) # make timestamps to the same value
838
+ File.utime(mtime, mtime, cpath)
839
+ t2 = e.get_template(:index)
840
+ ok_(t2.script) == "\nSOS"
841
+ ok_(_templates) == {"index.rbhtml" => [t2, "_views/blog/index.rbhtml"]}
842
+ end
843
+ spec "if template file is not found then raise TemplateNotFoundError" do
844
+ errmsg = 'index2.rbhtml: template not found (path=["_views/blog", "_views"]).'
845
+ ok_(proc { e.get_template(:index2) }).raise?(Tenjin::TemplateNotFoundError, errmsg)
846
+ end
847
+ spec "if @preprocess is true then preprocess template file" do
848
+ e2 = Tenjin::Engine.new(:preprocess=>true)
849
+ t2 = e2.get_template('_views/index.rbhtml', {:dummy_value=>'ZOZ'})
850
+ ok_(t2.script) == ' _buf << %Q`<<ZOZ>>`; ' + "\n"
851
+ end
852
+ t1 = t2 = nil
853
+ spec "if template is not found in memory nor cache then create new one." do
854
+ t1 = e.get_template(:index)
855
+ File.unlink(cpath) # remove cache file
856
+ ok_(e.get_template(:index)).same?(t1)
857
+ not_ok_(cpath).exist?
858
+ _templates.clear() # clear memory cache
859
+ t2 = e.get_template(:index)
860
+ ok_(t2).is_a?(Tenjin::Template)
861
+ not_ok_(t2).same?(t1)
862
+ end
863
+ spec "save template object into file cache and memory cache." do
864
+ ok_(cpath).exist?
865
+ ok_(_templates) == {fname => [t2, fpath]}
866
+ end
867
+ spec "return template object." do
868
+ ok_(t2).is_a?(Tenjin::Template)
869
+ end
870
+ end
871
+ end
872
+
873
+ def test__preprocess
874
+ engine = Tenjin::Engine.new
875
+ spec "preprocess input with _context and return result" do
876
+ ret = engine._preprocess('<<#{{@name}}>>', nil, {:name=>'SOS'})
877
+ ok_(ret) == '<<SOS>>'
878
+ end
879
+ end
880
+
881
+ def test_create_template
882
+ engine = Tenjin::Engine.new(:path=>['_views/blog', '_views'])
883
+ t = nil
884
+ spec "create template object and return it." do
885
+ t1 = engine.create_template()
886
+ t2 = engine.create_template()
887
+ ok_(t1).is_a?(Tenjin::Template)
888
+ ok_(t2).is_a?(Tenjin::Template)
889
+ not_ok(t1).same?(t2)
890
+ end
891
+ spec "if input is specified then convert it into script." do
892
+ t = engine.create_template("Hello ${name}!", "foo.txt")
893
+ ok_(t.script) == ' _buf << %Q`Hello #{escape((name).to_s)}!`; ' + "\n"
894
+ ok_(t.filename) == 'foo.txt'
895
+ end
896
+ end
897
+
898
+ def test_hook_context
899
+ engine = Tenjin::Engine.new
900
+ ctx = nil
901
+ spec "return context object"
902
+ spec "if context is nil then create new Context object" do
903
+ ctx = engine.hook_context(nil)
904
+ ok_(ctx).is_a?(Tenjin::Context)
905
+ end
906
+ spec "if context is a Hash object then convert it into Context object" do
907
+ ctx = engine.hook_context({:x=>10})
908
+ ok_(ctx).is_a?(Tenjin::Context)
909
+ ok_(ctx[:x]) == 10
910
+ end
911
+ spec "if context is an object then use it as context object" do
912
+ obj = Object.new
913
+ obj.extend(Tenjin::ContextHelper)
914
+ ok_(engine.hook_context(obj)).same?(obj)
915
+ end
916
+ spec "set _engine attribute" do
917
+ ok_(ctx._engine).same?(engine)
918
+ end
919
+ spec "set _engine attribute" do
920
+ ok_(ctx._layout) == nil
921
+ end
922
+ end
923
+
924
+ def test_render
925
+ input1 = '<p>Hello #{@name}!</p>'
926
+ input2 = '<p>_context.class=#{_context.class}</p>'
927
+ input3 = '<p>@_template.class=#{@_template.class}</p>'
928
+ input4 = "<?rb @_layout = :_layout2 ?>\n" + input1
929
+ layout1 = '<div>#{@_content}</div>'
930
+ layout2 = '<body>#{@_content}</body>'
931
+ layout3 = "<?rb @_layout = :_layout2 ?>\n" +
932
+ '<section>#{@_content}</section>'
933
+ layout4 = '<b>@_content=#{@_content.inspect}</b>'
934
+ engine = Tenjin::Engine.new(:path=>['_views/blog', '_views'],
935
+ :postfix=>'.rbhtml', :layout=>:_layout1)
936
+ context = {:name=>'SOS'}
937
+ _with_dummy_files do
938
+ File.open('_views/blog/ex1.rbhtml', 'wb') {|f| f.write(input1) }
939
+ File.open('_views/blog/ex2.rbhtml', 'wb') {|f| f.write(input2) }
940
+ File.open('_views/blog/ex3.rbhtml', 'wb') {|f| f.write(input3) }
941
+ File.open('_views/blog/ex4.rbhtml', 'wb') {|f| f.write(input4) }
942
+ File.open('_views/_layout1.rbhtml', 'wb') {|f| f.write(layout1) }
943
+ File.open('_views/_layout2.rbhtml', 'wb') {|f| f.write(layout2) }
944
+ File.open('_views/blog/_layout3.rbhtml', 'wb') {|f| f.write(layout3) }
945
+ File.open('_views/_layout4.rbhtml', 'wb') {|f| f.write(layout4) }
946
+ spec "if context is a Hash object, convert it into Context object." do
947
+ output = engine.render(:ex2, {:foo=>1})
948
+ ok_(output) == "<div><p>_context.class=Tenjin::Context</p></div>"
949
+ end
950
+ spec "set template object into context (required for cache_with() helper)" do
951
+ output = engine.render(:ex3)
952
+ ok_(output) == "<div><p>@_template.class=Tenjin::Template</p></div>"
953
+ end
954
+ spec "if @_layout is specified, use it as layoute template name" do
955
+ output = engine.render(:ex4, context)
956
+ ok_(output) == "<body><p>Hello SOS!</p></body>"
957
+ output = engine.render(:ex1, context, :_layout2)
958
+ ok_(output) == "<body><p>Hello SOS!</p></body>"
959
+ end
960
+ spec "use default layout template if layout is true or nil" do
961
+ expected = "<div><p>Hello SOS!</p></div>"
962
+ ok_(engine.render(:ex1, context)) == expected
963
+ ok_(engine.render(:ex1, context, true)) == expected
964
+ ok_(engine.render(:ex1, context, nil)) == expected
965
+ end
966
+ spec "if layout is false then don't use layout template" do
967
+ output = engine.render(:ex1, context, false)
968
+ ok_(output) == "<p>Hello SOS!</p>"
969
+ end
970
+ spec "set layout name as next template name" do
971
+ output = engine.render(:ex1, context, :_layout3)
972
+ ok_(output) == "<body><section><p>Hello SOS!</p></section></body>"
973
+ end
974
+ spec "set output into @_content for layout template" do
975
+ output = engine.render(:ex1, context, :_layout4)
976
+ ok_(output) == '<b>@_content="<p>Hello SOS!</p>"</b>'
977
+ end
978
+ end
979
+ end
980
+
981
+ end
982
+
983
+
984
+ class FileFinderTest
985
+ include Oktest::TestCase
484
986
 
987
+ def _write(filename, content)
988
+ File.open(filename, 'wb') {|f| f.write(content) }
989
+ end
990
+
991
+ def _with_dummies
992
+ finder = Tenjin::FileFinder.new
993
+ path = ["_views/blog", "_views"]
994
+ begin
995
+ FileUtils.mkdir_p("_views/blog")
996
+ _write("_views/index.rbhtml", "AAA")
997
+ _write("_views/blog/index.rbhtml", "BBB")
998
+ _write("_views/layout.rbhtml", '<<#{_content}>>')
999
+ yield(finder, path)
1000
+ ensure
1001
+ FileUtils.rm_rf("_views")
1002
+ end
1003
+ end
1004
+
1005
+ def test_find
1006
+ _with_dummies do |finder, path|
1007
+ spec "if dirs specified then find file from it." do
1008
+ ok_(finder.find('index.rbhtml', path)) == "_views/blog/index.rbhtml"
1009
+ ok_(finder.find('layout.rbhtml', path)) == "_views/layout.rbhtml"
1010
+ end
1011
+ spec "if dirs not specified then return filename if it exists." do
1012
+ ok_(finder.find('_views/index.rbhtml')) == "_views/index.rbhtml"
1013
+ end
1014
+ spec "if file not found then return nil." do
1015
+ ok_(finder.find('not-found.rbhtml', path)) == nil
1016
+ ok_(finder.find('not-found.rbhtml')) == nil
1017
+ end
1018
+ end
1019
+ end
1020
+
1021
+ def test_timestamp
1022
+ _with_dummies do |finder, path|
1023
+ spec "return mtime of filepath." do
1024
+ ts = (Time.now - 30.0).to_i.to_f
1025
+ fpath = '_views/index.rbhtml'
1026
+ not_ok_(finder.timestamp(fpath).to_f) == ts
1027
+ File.utime(ts, ts, fpath)
1028
+ ok_(finder.timestamp(fpath).to_f) == ts
1029
+ end
1030
+ end
1031
+ end
1032
+
1033
+ def test_read
1034
+ _with_dummies do |finder, path|
1035
+ spec "if file exists then return file content and mtime." do
1036
+ fpath = '_views/blog/index.rbhtml'
1037
+ ret = finder.read(fpath)
1038
+ ok_(ret) == ['BBB', File.mtime(fpath)]
1039
+ end
1040
+ spec "if file not found then return nil." do
1041
+ ok_(finder.read('hogehoge')) == nil
1042
+ end
1043
+ end
1044
+ end
485
1045
 
486
1046
  end
487
1047
 
@@ -493,3 +1053,8 @@ end
493
1053
  # end
494
1054
  # end
495
1055
  #end
1056
+
1057
+
1058
+ if __FILE__ == $0
1059
+ Oktest.run_all()
1060
+ end