PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/ruby/1.9.1/gems/tilt-1.3.3/test/tilt_erbtemplate_test.rb

https://bitbucket.org/sqctest02/sample_app_3_1
Ruby | 234 lines | 214 code | 19 blank | 1 comment | 6 complexity | 5fd8727db48a67a9b61fabfae5c2d1df MD5 | raw file
Possible License(s): GPL-2.0
  1. # coding: utf-8
  2. require 'contest'
  3. require 'tilt'
  4. require 'erb'
  5. require 'tempfile'
  6. class ERBTemplateTest < Test::Unit::TestCase
  7. test "registered for '.erb' files" do
  8. assert Tilt.mappings['erb'].include?(Tilt::ERBTemplate)
  9. end
  10. test "registered for '.rhtml' files" do
  11. assert Tilt.mappings['rhtml'].include?(Tilt::ERBTemplate)
  12. end
  13. test "loading and evaluating templates on #render" do
  14. template = Tilt::ERBTemplate.new { |t| "Hello World!" }
  15. assert_equal "Hello World!", template.render
  16. end
  17. test "can be rendered more than once" do
  18. template = Tilt::ERBTemplate.new { |t| "Hello World!" }
  19. 3.times { assert_equal "Hello World!", template.render }
  20. end
  21. test "passing locals" do
  22. template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
  23. assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
  24. end
  25. test "evaluating in an object scope" do
  26. template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' }
  27. scope = Object.new
  28. scope.instance_variable_set :@name, 'Joe'
  29. assert_equal "Hey Joe!", template.render(scope)
  30. end
  31. class MockOutputVariableScope
  32. attr_accessor :exposed_buffer
  33. end
  34. test "exposing the buffer to the template by default" do
  35. begin
  36. Tilt::ERBTemplate.default_output_variable = '@_out_buf'
  37. template = Tilt::ERBTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
  38. scope = MockOutputVariableScope.new
  39. template.render(scope)
  40. assert_not_nil scope.exposed_buffer
  41. assert_equal scope.exposed_buffer, 'hey'
  42. ensure
  43. Tilt::ERBTemplate.default_output_variable = '_erbout'
  44. end
  45. end
  46. test "passing a block for yield" do
  47. template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
  48. assert_equal "Hey Joe!", template.render { 'Joe' }
  49. end
  50. test "backtrace file and line reporting without locals" do
  51. data = File.read(__FILE__).split("\n__END__\n").last
  52. fail unless data[0] == ?<
  53. template = Tilt::ERBTemplate.new('test.erb', 11) { data }
  54. begin
  55. template.render
  56. fail 'should have raised an exception'
  57. rescue => boom
  58. assert_kind_of NameError, boom
  59. line = boom.backtrace.grep(/^test\.erb:/).first
  60. assert line, "Backtrace didn't contain test.erb"
  61. file, line, meth = line.split(":")
  62. assert_equal '13', line
  63. end
  64. end
  65. test "backtrace file and line reporting with locals" do
  66. data = File.read(__FILE__).split("\n__END__\n").last
  67. fail unless data[0] == ?<
  68. template = Tilt::ERBTemplate.new('test.erb', 1) { data }
  69. begin
  70. template.render(nil, :name => 'Joe', :foo => 'bar')
  71. fail 'should have raised an exception'
  72. rescue => boom
  73. assert_kind_of RuntimeError, boom
  74. line = boom.backtrace.first
  75. file, line, meth = line.split(":")
  76. assert_equal 'test.erb', file
  77. assert_equal '6', line
  78. end
  79. end
  80. test "default stripping trim mode" do
  81. template = Tilt::ERBTemplate.new('test.erb', 1) { "\n<%= 1 + 1 %>\n" }
  82. assert_equal "\n2", template.render
  83. end
  84. test "stripping trim mode" do
  85. template = Tilt::ERBTemplate.new('test.erb', 1, :trim => '-') { "\n<%= 1 + 1 -%>\n" }
  86. assert_equal "\n2", template.render
  87. end
  88. test "shorthand whole line syntax trim mode" do
  89. template = Tilt::ERBTemplate.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
  90. assert_equal "\nhello\n", template.render
  91. end
  92. test "using an instance variable as the outvar" do
  93. template = Tilt::ERBTemplate.new(nil, :outvar => '@buf') { "<%= 1 + 1 %>" }
  94. scope = Object.new
  95. scope.instance_variable_set(:@buf, 'original value')
  96. assert_equal '2', template.render(scope)
  97. assert_equal 'original value', scope.instance_variable_get(:@buf)
  98. end
  99. end
  100. class CompiledERBTemplateTest < Test::Unit::TestCase
  101. def teardown
  102. GC.start
  103. end
  104. class Scope
  105. end
  106. test "compiling template source to a method" do
  107. template = Tilt::ERBTemplate.new { |t| "Hello World!" }
  108. template.render(Scope.new)
  109. method = template.send(:compiled_method, [])
  110. assert_kind_of UnboundMethod, method
  111. end
  112. test "loading and evaluating templates on #render" do
  113. template = Tilt::ERBTemplate.new { |t| "Hello World!" }
  114. assert_equal "Hello World!", template.render(Scope.new)
  115. assert_equal "Hello World!", template.render(Scope.new)
  116. end
  117. test "passing locals" do
  118. template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
  119. assert_equal "Hey Joe!", template.render(Scope.new, :name => 'Joe')
  120. end
  121. test "evaluating in an object scope" do
  122. template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' }
  123. scope = Scope.new
  124. scope.instance_variable_set :@name, 'Joe'
  125. assert_equal "Hey Joe!", template.render(scope)
  126. scope.instance_variable_set :@name, 'Jane'
  127. assert_equal "Hey Jane!", template.render(scope)
  128. end
  129. test "passing a block for yield" do
  130. template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
  131. assert_equal "Hey Joe!", template.render(Scope.new) { 'Joe' }
  132. assert_equal "Hey Jane!", template.render(Scope.new) { 'Jane' }
  133. end
  134. test "backtrace file and line reporting without locals" do
  135. data = File.read(__FILE__).split("\n__END__\n").last
  136. fail unless data[0] == ?<
  137. template = Tilt::ERBTemplate.new('test.erb', 11) { data }
  138. begin
  139. template.render(Scope.new)
  140. fail 'should have raised an exception'
  141. rescue => boom
  142. assert_kind_of NameError, boom
  143. line = boom.backtrace.grep(/^test\.erb:/).first
  144. assert line, "Backtrace didn't contain test.erb"
  145. file, line, meth = line.split(":")
  146. assert_equal '13', line
  147. end
  148. end
  149. test "backtrace file and line reporting with locals" do
  150. data = File.read(__FILE__).split("\n__END__\n").last
  151. fail unless data[0] == ?<
  152. template = Tilt::ERBTemplate.new('test.erb') { data }
  153. begin
  154. template.render(Scope.new, :name => 'Joe', :foo => 'bar')
  155. fail 'should have raised an exception'
  156. rescue => boom
  157. assert_kind_of RuntimeError, boom
  158. line = boom.backtrace.first
  159. file, line, meth = line.split(":")
  160. assert_equal 'test.erb', file
  161. assert_equal '6', line
  162. end
  163. end
  164. test "default stripping trim mode" do
  165. template = Tilt::ERBTemplate.new('test.erb') { "\n<%= 1 + 1 %>\n" }
  166. assert_equal "\n2", template.render(Scope.new)
  167. end
  168. test "stripping trim mode" do
  169. template = Tilt::ERBTemplate.new('test.erb', :trim => '-') { "\n<%= 1 + 1 -%>\n" }
  170. assert_equal "\n2", template.render(Scope.new)
  171. end
  172. test "shorthand whole line syntax trim mode" do
  173. template = Tilt::ERBTemplate.new('test.erb', :trim => '%') { "\n% if true\nhello\n%end\n" }
  174. assert_equal "\nhello\n", template.render(Scope.new)
  175. end
  176. test "encoding with magic comment" do
  177. f = Tempfile.open("template")
  178. f.puts('<%# coding: UTF-8 %>')
  179. f.puts('ふが <%= @hoge %>')
  180. f.close()
  181. @hoge = "ほげ"
  182. erb = Tilt::ERBTemplate.new(f.path)
  183. 3.times { erb.render(self) }
  184. f.delete
  185. end
  186. test "encoding with :default_encoding" do
  187. f = Tempfile.open("template")
  188. f.puts('ふが <%= @hoge %>')
  189. f.close()
  190. @hoge = "ほげ"
  191. erb = Tilt::ERBTemplate.new(f.path, :default_encoding => 'UTF-8')
  192. 3.times { erb.render(self) }
  193. f.delete
  194. end
  195. end
  196. __END__
  197. <html>
  198. <body>
  199. <h1>Hey <%= name %>!</h1>
  200. <p><% fail %></p>
  201. </body>
  202. </html>