PageRenderTime 81ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/sqctest01/sample_app_3_1
Ruby | 92 lines | 77 code | 15 blank | 0 comment | 2 complexity | 97297557c4397989f1dc1420cf5aeeed MD5 | raw file
Possible License(s): GPL-2.0
  1. require 'contest'
  2. require 'tilt'
  3. begin
  4. require 'yajl'
  5. class YajlTemplateTest < Test::Unit::TestCase
  6. test "is registered for '.yajl' files" do
  7. assert_equal Tilt::YajlTemplate, Tilt['test.yajl']
  8. end
  9. test "compiles and evaluates the template on #render" do
  10. template = Tilt::YajlTemplate.new { "json = { :integer => 3, :string => 'hello' }" }
  11. assert_equal '{"integer":3,"string":"hello"}', template.render
  12. end
  13. test "can be rendered more than once" do
  14. template = Tilt::YajlTemplate.new { "json = { :integer => 3, :string => 'hello' }" }
  15. 3.times { assert_equal '{"integer":3,"string":"hello"}', template.render }
  16. end
  17. test "evaluating ruby code" do
  18. template = Tilt::YajlTemplate.new { "json = { :integer => (3 * 2) }" }
  19. assert_equal '{"integer":6}', template.render
  20. end
  21. test "evaluating in an object scope" do
  22. template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + @name + '!' }" }
  23. scope = Object.new
  24. scope.instance_variable_set :@name, 'Joe'
  25. assert_equal '{"string":"Hey Joe!"}', template.render(scope)
  26. end
  27. test "passing locals" do
  28. template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + name + '!' }" }
  29. assert_equal '{"string":"Hey Joe!"}', template.render(Object.new, :name => 'Joe')
  30. end
  31. test "passing a block for yield" do
  32. template = Tilt::YajlTemplate.new { "json = { :string => 'Hey ' + yield + '!' }" }
  33. assert_equal '{"string":"Hey Joe!"}', template.render { 'Joe' }
  34. assert_equal '{"string":"Hey Moe!"}', template.render { 'Moe' }
  35. end
  36. test "template multiline" do
  37. template = Tilt::YajlTemplate.new { %Q{
  38. json = {
  39. :string => "hello"
  40. }
  41. } }
  42. assert_equal '{"string":"hello"}', template.render
  43. end
  44. test "template can reuse existing json buffer" do
  45. template = Tilt::YajlTemplate.new { "json.merge! :string => 'hello'" }
  46. assert_equal '{"string":"hello"}', template.render
  47. end
  48. test "template can end with any statement" do
  49. template = Tilt::YajlTemplate.new { %Q{
  50. json = {
  51. :string => "hello"
  52. }
  53. four = 2 * 2
  54. json[:integer] = four
  55. nil
  56. } }
  57. assert_equal '{"string":"hello","integer":4}', template.render
  58. end
  59. test "option callback" do
  60. options = { :callback => 'foo' }
  61. template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
  62. assert_equal 'foo({"string":"hello"});', template.render
  63. end
  64. test "option variable" do
  65. options = { :variable => 'output' }
  66. template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
  67. assert_equal 'var output = {"string":"hello"};', template.render
  68. end
  69. test "option callback and variable" do
  70. options = { :callback => 'foo', :variable => 'output' }
  71. template = Tilt::YajlTemplate.new(nil, options) { "json = { :string => 'hello' }" }
  72. assert_equal 'var output = {"string":"hello"}; foo(output);', template.render
  73. end
  74. end
  75. rescue LoadError
  76. warn "Tilt::YajlTemplateTest (disabled)\n"
  77. end