~ubuntu-branches/ubuntu/trusty/ruby-slim/trusty

« back to all changes in this revision

Viewing changes to test/core/test_slim_template.rb

  • Committer: Package Import Robot
  • Author(s): Jérémy Bobbio
  • Date: 2013-04-13 08:20:54 UTC
  • Revision ID: package-import@ubuntu.com-20130413082054-zr326qb62of78z1g
Tags: upstream-2.0.0~pre6
Import upstream version 2.0.0~pre6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'helper'
 
2
 
 
3
class ::MockError < NameError
 
4
end
 
5
 
 
6
class TestSlimTemplate < TestSlim
 
7
  def test_default_mime_type
 
8
    assert_equal 'text/html', Slim::Template.default_mime_type
 
9
  end
 
10
 
 
11
  def test_registered_extension
 
12
    assert_equal Slim::Template, Tilt['test.slim']
 
13
  end
 
14
 
 
15
  def test_preparing_and_evaluating
 
16
    template = Slim::Template.new { |t| "p Hello World!\n" }
 
17
    assert_equal "<p>Hello World!</p>", template.render
 
18
  end
 
19
 
 
20
  def test_evaluating_in_an_object_scope
 
21
    template = Slim::Template.new { "p = 'Hey ' + @name + '!'\n" }
 
22
    scope = Object.new
 
23
    scope.instance_variable_set :@name, 'Joe'
 
24
    assert_equal "<p>Hey Joe!</p>", template.render(scope)
 
25
  end
 
26
 
 
27
  def test_passing_a_block_for_yield
 
28
    template = Slim::Template.new { "p = 'Hey ' + yield + '!'\n" }
 
29
    assert_equal "<p>Hey Joe!</p>", template.render { 'Joe' }
 
30
  end
 
31
 
 
32
  def test_backtrace_file_and_line_reporting_without_locals
 
33
    data = File.read(__FILE__).split("\n__END__\n").last
 
34
    fail unless data[0] == ?h
 
35
    template = Slim::Template.new('test.slim', 10) { data }
 
36
    begin
 
37
      template.render
 
38
      fail 'should have raised an exception'
 
39
    rescue => ex
 
40
      assert_kind_of NameError, ex
 
41
      assert_backtrace(ex, 'test.slim:12')
 
42
    end
 
43
  end
 
44
 
 
45
  def test_backtrace_file_and_line_reporting_with_locals
 
46
    data = File.read(__FILE__).split("\n__END__\n").last
 
47
    fail unless data[0] == ?h
 
48
    template = Slim::Template.new('test.slim') { data }
 
49
    begin
 
50
      res = template.render(Object.new, :name => 'Joe', :foo => 'bar')
 
51
    rescue => ex
 
52
      assert_kind_of MockError, ex
 
53
      assert_backtrace(ex, 'test.slim:5')
 
54
    end
 
55
  end
 
56
 
 
57
  def test_compiling_template_source_to_a_method
 
58
    template = Slim::Template.new { |t| "Hello World!" }
 
59
    template.render
 
60
    method = template.send(:compiled_method, [])
 
61
    assert_kind_of UnboundMethod, method
 
62
  end
 
63
 
 
64
  def test_passing_locals
 
65
    template = Slim::Template.new { "p = 'Hey ' + name + '!'\n" }
 
66
    assert_equal "<p>Hey Joe!</p>", template.render(Object.new, :name => 'Joe')
 
67
  end
 
68
end
 
69
 
 
70
__END__
 
71
html
 
72
  body
 
73
    h1 = "Hey #{name}"
 
74
 
 
75
    = raise MockError
 
76
 
 
77
    p we never get here