tilt 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,44 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'builder'
6
+ class BuilderTemplateTest < Test::Unit::TestCase
7
+ test "registered for '.builder' files" do
8
+ assert_equal Tilt::BuilderTemplate, Tilt['test.builder']
9
+ assert_equal Tilt::BuilderTemplate, Tilt['test.xml.builder']
10
+ end
11
+
12
+ test "compiling and evaluating the template on #render" do
13
+ template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" }
14
+ assert_equal "<em>Hello World!</em>\n", template.render
15
+ end
16
+
17
+ test "passing locals" do
18
+ template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + name + '!')" }
19
+ assert_equal "<em>Hey Joe!</em>\n", template.render(Object.new, :name => 'Joe')
20
+ end
21
+
22
+ test "evaluating in an object scope" do
23
+ template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + @name + '!')" }
24
+ scope = Object.new
25
+ scope.instance_variable_set :@name, 'Joe'
26
+ assert_equal "<em>Hey Joe!</em>\n", template.render(scope)
27
+ end
28
+
29
+ test "passing a block for yield" do
30
+ template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + yield + '!')" }
31
+ assert_equal "<em>Hey Joe!</em>\n", template.render { 'Joe' }
32
+ end
33
+
34
+ test "block style templates" do
35
+ template =
36
+ Tilt::BuilderTemplate.new do |t|
37
+ lambda { |xml| xml.em('Hey Joe!') }
38
+ end
39
+ assert_equal "<em>Hey Joe!</em>\n", template.render
40
+ end
41
+ end
42
+ rescue LoadError
43
+ warn "Tilt::BuilderTemplate (disabled)"
44
+ end
@@ -0,0 +1,33 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ class TiltCacheTest < Test::Unit::TestCase
5
+ setup { @cache = Tilt::Cache.new }
6
+
7
+ test "caching with single simple argument to #fetch" do
8
+ template = nil
9
+ result = @cache.fetch('hello') { template = Tilt::StringTemplate.new {''} }
10
+ assert_same template, result
11
+ result = @cache.fetch('hello') { fail 'should be cached' }
12
+ assert_same template, result
13
+ end
14
+
15
+ test "caching with multiple complex arguments to #fetch" do
16
+ template = nil
17
+ args = ['hello', {:foo => 'bar', :baz => 'bizzle'}]
18
+ result = @cache.fetch(*args) { template = Tilt::StringTemplate.new {''} }
19
+ assert_same template, result
20
+ result = @cache.fetch(*args) { fail 'should be cached' }
21
+ assert_same template, result
22
+ end
23
+
24
+ test "clearing the cache with #clear" do
25
+ template, other = nil
26
+ result = @cache.fetch('hello') { template = Tilt::StringTemplate.new {''} }
27
+ assert_same template, result
28
+
29
+ @cache.clear
30
+ result = @cache.fetch('hello') { other = Tilt::StringTemplate.new {''} }
31
+ assert_same other, result
32
+ end
33
+ end
@@ -0,0 +1,93 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+ require 'erb'
4
+
5
+ class ERBTemplateTest < Test::Unit::TestCase
6
+ test "registered for '.erb' files" do
7
+ assert_equal Tilt::ERBTemplate, Tilt['test.erb']
8
+ assert_equal Tilt::ERBTemplate, Tilt['test.html.erb']
9
+ end
10
+
11
+ test "registered for '.rhtml' files" do
12
+ assert_equal Tilt::ERBTemplate, Tilt['test.rhtml']
13
+ end
14
+
15
+ test "compiling and evaluating templates on #render" do
16
+ template = Tilt::ERBTemplate.new { |t| "Hello World!" }
17
+ assert_equal "Hello World!", template.render
18
+ end
19
+
20
+ test "passing locals" do
21
+ template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
22
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
23
+ end
24
+
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
+
32
+ test "passing a block for yield" do
33
+ template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
34
+ assert_equal "Hey Joe!", template.render { 'Joe' }
35
+ end
36
+
37
+ test "backtrace file and line reporting without locals" do
38
+ data = File.read(__FILE__).split("\n__END__\n").last
39
+ fail unless data[0] == ?<
40
+ template = Tilt::ERBTemplate.new('test.erb', 11) { data }
41
+ begin
42
+ template.render
43
+ fail 'should have raised an exception'
44
+ rescue => boom
45
+ assert_kind_of NameError, boom
46
+ line = boom.backtrace.first
47
+ file, line, meth = line.split(":")
48
+ assert_equal 'test.erb', file
49
+ assert_equal '13', line
50
+ end
51
+ end
52
+
53
+ test "backtrace file and line reporting with locals" do
54
+ data = File.read(__FILE__).split("\n__END__\n").last
55
+ fail unless data[0] == ?<
56
+ template = Tilt::ERBTemplate.new('test.erb', 1) { data }
57
+ begin
58
+ template.render(nil, :name => 'Joe', :foo => 'bar')
59
+ fail 'should have raised an exception'
60
+ rescue => boom
61
+ assert_kind_of RuntimeError, boom
62
+ line = boom.backtrace.first
63
+ file, line, meth = line.split(":")
64
+ assert_equal 'test.erb', file
65
+ assert_equal '6', line
66
+ end
67
+ end
68
+
69
+ test "default non-stripping trim mode" do
70
+ template = Tilt.new('test.erb', 1) { "\n<%= 1 + 1 %>\n" }
71
+ assert_equal "\n2\n", template.render
72
+ end
73
+
74
+ test "stripping trim mode" do
75
+ template = Tilt.new('test.erb', 1, :trim => '-') { "\n<%= 1 + 1 -%>\n" }
76
+ assert_equal "\n2", template.render
77
+ end
78
+
79
+ test "shorthand whole line syntax trim mode" do
80
+ template = Tilt.new('test.erb', 1, :trim => '%') { "\n% if true\nhello\n%end\n" }
81
+ assert_equal "\nhello\n", template.render
82
+ end
83
+ end
84
+
85
+ __END__
86
+ <html>
87
+ <body>
88
+ <h1>Hey <%= name %>!</h1>
89
+
90
+
91
+ <p><% fail %></p>
92
+ </body>
93
+ </html>
@@ -0,0 +1,85 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'erubis'
6
+ class ErubisTemplateTest < Test::Unit::TestCase
7
+ test "registered for '.erubis' files" do
8
+ assert_equal Tilt::ErubisTemplate, Tilt['test.erubis']
9
+ assert_equal Tilt::ErubisTemplate, Tilt['test.html.erubis']
10
+ end
11
+
12
+ test "compiling and evaluating templates on #render" do
13
+ template = Tilt::ErubisTemplate.new { |t| "Hello World!" }
14
+ assert_equal "Hello World!", template.render
15
+ end
16
+
17
+ test "passing locals" do
18
+ template = Tilt::ErubisTemplate.new { 'Hey <%= name %>!' }
19
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
20
+ end
21
+
22
+ test "evaluating in an object scope" do
23
+ template = Tilt::ErubisTemplate.new { 'Hey <%= @name %>!' }
24
+ scope = Object.new
25
+ scope.instance_variable_set :@name, 'Joe'
26
+ assert_equal "Hey Joe!", template.render(scope)
27
+ end
28
+
29
+ test "passing a block for yield" do
30
+ template = Tilt::ErubisTemplate.new { 'Hey <%= yield %>!' }
31
+ assert_equal "Hey Joe!", template.render { 'Joe' }
32
+ end
33
+
34
+ test "backtrace file and line reporting without locals" do
35
+ data = File.read(__FILE__).split("\n__END__\n").last
36
+ fail unless data[0] == ?<
37
+ template = Tilt::ErubisTemplate.new('test.erubis', 11) { data }
38
+ begin
39
+ template.render
40
+ fail 'should have raised an exception'
41
+ rescue => boom
42
+ assert_kind_of NameError, boom
43
+ line = boom.backtrace.first
44
+ file, line, meth = line.split(":")
45
+ assert_equal 'test.erubis', file
46
+ assert_equal '13', line
47
+ end
48
+ end
49
+
50
+ test "backtrace file and line reporting with locals" do
51
+ data = File.read(__FILE__).split("\n__END__\n").last
52
+ fail unless data[0] == ?<
53
+ template = Tilt::ErubisTemplate.new('test.erubis', 1) { data }
54
+ begin
55
+ template.render(nil, :name => 'Joe', :foo => 'bar')
56
+ fail 'should have raised an exception'
57
+ rescue => boom
58
+ assert_kind_of RuntimeError, boom
59
+ line = boom.backtrace.first
60
+ file, line, meth = line.split(":")
61
+ assert_equal 'test.erubis', file
62
+ assert_equal '6', line
63
+ end
64
+ end
65
+
66
+ test "erubis template options" do
67
+ template = Tilt::ErubisTemplate.new(nil, :pattern => '\{% %\}') { 'Hey {%= @name %}!' }
68
+ scope = Object.new
69
+ scope.instance_variable_set :@name, 'Joe'
70
+ assert_equal "Hey Joe!", template.render(scope)
71
+ end
72
+ end
73
+ rescue LoadError => boom
74
+ warn "Tilt::ErubisTemplate (disabled)\n"
75
+ end
76
+
77
+ __END__
78
+ <html>
79
+ <body>
80
+ <h1>Hey <%= name %>!</h1>
81
+
82
+
83
+ <p><% fail %></p>
84
+ </body>
85
+ </html>
@@ -1,4 +1,4 @@
1
- require 'bacon'
1
+ require 'contest'
2
2
  require 'tilt'
3
3
 
4
4
  begin
@@ -7,34 +7,34 @@ begin
7
7
 
8
8
  require 'haml'
9
9
 
10
- describe Tilt::HamlTemplate do
11
- it "is registered for '.haml' files" do
12
- Tilt['test.haml'].should.equal Tilt::HamlTemplate
10
+ class HamlTemplateTest < Test::Unit::TestCase
11
+ test "registered for '.haml' files" do
12
+ assert_equal Tilt::HamlTemplate, Tilt['test.haml']
13
13
  end
14
14
 
15
- it "compiles and evaluates the template on #render" do
15
+ test "compiling and evaluating templates on #render" do
16
16
  template = Tilt::HamlTemplate.new { |t| "%p Hello World!" }
17
- template.render.should.equal "<p>Hello World!</p>\n"
17
+ assert_equal "<p>Hello World!</p>\n", template.render
18
18
  end
19
19
 
20
- it "supports locals" do
20
+ test "passing locals" do
21
21
  template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" }
22
- template.render(Object.new, :name => 'Joe').should.equal "<p>Hey Joe!</p>\n"
22
+ assert_equal "<p>Hey Joe!</p>\n", template.render(Object.new, :name => 'Joe')
23
23
  end
24
24
 
25
- it "is evaluated in the object scope provided" do
25
+ test "evaluating in an object scope" do
26
26
  template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" }
27
27
  scope = Object.new
28
28
  scope.instance_variable_set :@name, 'Joe'
29
- template.render(scope).should.equal "<p>Hey Joe!</p>\n"
29
+ assert_equal "<p>Hey Joe!</p>\n", template.render(scope)
30
30
  end
31
31
 
32
- it "evaluates template_source with yield support" do
32
+ test "passing a block for yield" do
33
33
  template = Tilt::HamlTemplate.new { "%p= 'Hey ' + yield + '!'" }
34
- template.render { 'Joe' }.should.equal "<p>Hey Joe!</p>\n"
34
+ assert_equal "<p>Hey Joe!</p>\n", template.render { 'Joe' }
35
35
  end
36
36
 
37
- it "reports the file and line properly in backtraces without locals" do
37
+ test "backtrace file and line reporting without locals" do
38
38
  data = File.read(__FILE__).split("\n__END__\n").last
39
39
  fail unless data[0] == ?%
40
40
  template = Tilt::HamlTemplate.new('test.haml', 10) { data }
@@ -42,26 +42,26 @@ begin
42
42
  template.render
43
43
  fail 'should have raised an exception'
44
44
  rescue => boom
45
- boom.should.be.kind_of NameError
45
+ assert_kind_of NameError, boom
46
46
  line = boom.backtrace.first
47
47
  file, line, meth = line.split(":")
48
- file.should.equal 'test.haml'
49
- line.should.equal '12'
48
+ assert_equal 'test.haml', file
49
+ assert_equal '12', line
50
50
  end
51
51
  end
52
52
 
53
- it "reports the file and line properly in backtraces with locals" do
53
+ test "backtrace file and line reporting with locals" do
54
54
  data = File.read(__FILE__).split("\n__END__\n").last
55
55
  fail unless data[0] == ?%
56
56
  template = Tilt::HamlTemplate.new('test.haml') { data }
57
57
  begin
58
58
  res = template.render(Object.new, :name => 'Joe', :foo => 'bar')
59
59
  rescue => boom
60
- boom.should.be.kind_of ::MockError
60
+ assert_kind_of MockError, boom
61
61
  line = boom.backtrace.first
62
62
  file, line, meth = line.split(":")
63
- file.should.equal 'test.haml'
64
- line.should.equal '5'
63
+ assert_equal 'test.haml', file
64
+ assert_equal '5', line
65
65
  end
66
66
  end
67
67
  end
@@ -0,0 +1,73 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'liquid'
6
+
7
+ class LiquidTemplateTest < Test::Unit::TestCase
8
+ test "registered for '.liquid' files" do
9
+ assert_equal Tilt::LiquidTemplate, Tilt['test.liquid']
10
+ end
11
+
12
+ test "compiling and evaluating templates on #render" do
13
+ template = Tilt::LiquidTemplate.new { |t| "Hello World!" }
14
+ assert_equal "Hello World!", template.render
15
+ end
16
+
17
+ test "passing locals" do
18
+ template = Tilt::LiquidTemplate.new { "Hey {{ name }}!" }
19
+ assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
20
+ end
21
+
22
+ # Object's passed as "scope" to LiquidTemplate may respond to
23
+ # #to_h with a Hash. The Hash's contents are merged underneath
24
+ # Tilt locals.
25
+ class ExampleLiquidScope
26
+ def to_h
27
+ { :beer => 'wet', :whisky => 'wetter' }
28
+ end
29
+ end
30
+
31
+ test "combining scope and locals when scope responds to #to_h" do
32
+ template =
33
+ Tilt::LiquidTemplate.new {
34
+ 'Beer is {{ beer }} but Whisky is {{ whisky }}.'
35
+ }
36
+ scope = ExampleLiquidScope.new
37
+ assert_equal "Beer is wet but Whisky is wetter.", template.render(scope)
38
+ end
39
+
40
+ test "precedence when locals and scope define same variables" do
41
+ template =
42
+ Tilt::LiquidTemplate.new {
43
+ 'Beer is {{ beer }} but Whisky is {{ whisky }}.'
44
+ }
45
+ scope = ExampleLiquidScope.new
46
+ assert_equal "Beer is great but Whisky is greater.",
47
+ template.render(scope, :beer => 'great', :whisky => 'greater')
48
+ end
49
+
50
+ # Object's passed as "scope" to LiquidTemplate that do not
51
+ # respond to #to_h are silently ignored.
52
+ class ExampleIgnoredLiquidScope
53
+ end
54
+
55
+ test "handling scopes that do not respond to #to_h" do
56
+ template = Tilt::LiquidTemplate.new { 'Whisky' }
57
+ scope = ExampleIgnoredLiquidScope.new
58
+ assert_equal "Whisky", template.render(scope)
59
+ end
60
+
61
+ test "passing a block for yield" do
62
+ template =
63
+ Tilt::LiquidTemplate.new {
64
+ 'Beer is {{ yield }} but Whisky is {{ content }}ter.'
65
+ }
66
+ assert_equal "Beer is wet but Whisky is wetter.",
67
+ template.render({}) { 'wet' }
68
+ end
69
+ end
70
+
71
+ rescue LoadError => boom
72
+ warn "Tilt::LiquidTemplate (disabled)\n"
73
+ end
@@ -1,40 +1,40 @@
1
- require 'bacon'
1
+ require 'contest'
2
2
  require 'tilt'
3
3
 
4
4
  begin
5
5
  require 'mustache'
6
6
  raise LoadError, "mustache version must be > 0.2.2" if !Mustache.respond_to?(:compiled?)
7
7
 
8
- describe Tilt::MustacheTemplate do
9
- it "is registered for '.mustache' files" do
10
- Tilt['test.mustache'].should.equal Tilt::MustacheTemplate
8
+ module Views
9
+ class Foo < Mustache
10
+ attr_reader :foo
11
+ end
12
+ end
13
+
14
+ class MustacheTemplateTest < Test::Unit::TestCase
15
+ test "registered for '.mustache' files" do
16
+ assert_equal Tilt::MustacheTemplate, Tilt['test.mustache']
11
17
  end
12
18
 
13
- it "compiles and evaluates the template on #render" do
19
+ test "compiling and evaluating templates on #render" do
14
20
  template = Tilt::MustacheTemplate.new { |t| "Hello World!" }
15
- template.render.should.equal "Hello World!"
21
+ assert_equal "Hello World!", template.render
16
22
  end
17
23
 
18
- it "supports locals" do
24
+ test "passing locals" do
19
25
  template = Tilt::MustacheTemplate.new { "<p>Hey {{name}}!</p>" }
20
- template.render(nil, :name => 'Joe').should.equal "<p>Hey Joe!</p>"
26
+ assert_equal "<p>Hey Joe!</p>", template.render(nil, :name => 'Joe')
21
27
  end
22
28
 
23
- it "evaluates template_source with yield support" do
29
+ test "passing a block for yield" do
24
30
  template = Tilt::MustacheTemplate.new { "<p>Hey {{yield}}!</p>" }
25
- template.render { 'Joe' }.should.equal "<p>Hey Joe!</p>"
26
- end
27
-
28
- module Views
29
- class Foo < Mustache
30
- attr_reader :foo
31
- end
31
+ assert_equal "<p>Hey Joe!</p>", template.render { 'Joe' }
32
32
  end
33
33
 
34
- it "locates views defined at the top-level by default" do
34
+ test "locating views defined at the top-level" do
35
35
  template = Tilt::MustacheTemplate.new('foo.mustache') { "<p>Hey {{foo}}!</p>" }
36
36
  template.compile
37
- template.engine.should.equal Views::Foo
37
+ assert_equal Views::Foo, template.engine
38
38
  end
39
39
 
40
40
  module Bar
@@ -44,18 +44,18 @@ begin
44
44
  end
45
45
  end
46
46
 
47
- it "locates views defined in a custom namespace" do
47
+ test "locating views defined in a custom namespace" do
48
48
  template = Tilt::MustacheTemplate.new('bizzle.mustache', :namespace => Bar) { "<p>Hello World!</p>" }
49
49
  template.compile
50
- template.engine.should.equal Bar::Views::Bizzle
51
- template.render.should.equal "<p>Hello World!</p>"
50
+ assert_equal Bar::Views::Bizzle, template.engine
51
+ assert_equal "<p>Hello World!</p>", template.render
52
52
  end
53
53
 
54
- it "copies instance variables from scope object" do
54
+ test "copying instance variables from scope object" do
55
55
  template = Tilt::MustacheTemplate.new('foo.mustache') { "<p>Hey {{foo}}!</p>" }
56
56
  scope = Object.new
57
57
  scope.instance_variable_set(:@foo, 'Jane!')
58
- template.render(scope).should.equal "<p>Hey Jane!!</p>"
58
+ assert_equal "<p>Hey Jane!!</p>", template.render(scope)
59
59
  end
60
60
  end
61
61