tilt_test.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. require 'contest'
  2. require 'tilt'
  3. class TiltTest < Test::Unit::TestCase
  4. class MockTemplate
  5. attr_reader :args, :block
  6. def initialize(*args, &block)
  7. @args = args
  8. @block = block
  9. end
  10. end
  11. test "registering template implementation classes by file extension" do
  12. Tilt.register('mock', MockTemplate)
  13. end
  14. test "an extension is registered if explicit handle is found" do
  15. Tilt.register('mock', MockTemplate)
  16. assert Tilt.registered?('mock')
  17. end
  18. test "registering template classes by symbol file extension" do
  19. Tilt.register(:mock, MockTemplate)
  20. end
  21. test "looking up template classes by exact file extension" do
  22. Tilt.register('mock', MockTemplate)
  23. impl = Tilt['mock']
  24. assert_equal MockTemplate, impl
  25. end
  26. test "looking up template classes by implicit file extension" do
  27. Tilt.register('mock', MockTemplate)
  28. impl = Tilt['.mock']
  29. assert_equal MockTemplate, impl
  30. end
  31. test "looking up template classes with multiple file extensions" do
  32. Tilt.register('mock', MockTemplate)
  33. impl = Tilt['index.html.mock']
  34. assert_equal MockTemplate, impl
  35. end
  36. test "looking up template classes by file name" do
  37. Tilt.register('mock', MockTemplate)
  38. impl = Tilt['templates/test.mock']
  39. assert_equal MockTemplate, impl
  40. end
  41. test "looking up non-existant template class" do
  42. assert_nil Tilt['none']
  43. end
  44. test "accessing template class mappings at Tilt::mappings" do
  45. assert Tilt.respond_to?(:mappings)
  46. assert Tilt.mappings.respond_to?(:[])
  47. end
  48. test "creating new template instance with a filename" do
  49. Tilt.register('mock', MockTemplate)
  50. template = Tilt.new('foo.mock', 1, :key => 'val') { 'Hello World!' }
  51. assert_equal ['foo.mock', 1, {:key => 'val'}], template.args
  52. assert_equal 'Hello World!', template.block.call
  53. end
  54. end