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,40 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'rdiscount'
6
+
7
+ class RDiscountTemplateTest < Test::Unit::TestCase
8
+ test "registered for '.markdown' files" do
9
+ assert_equal Tilt::RDiscountTemplate, Tilt['test.markdown']
10
+ end
11
+
12
+ test "registered for '.md' files" do
13
+ assert_equal Tilt::RDiscountTemplate, Tilt['test.md']
14
+ end
15
+
16
+ test "registered for '.mkd' files" do
17
+ assert_equal Tilt::RDiscountTemplate, Tilt['test.mkd']
18
+ end
19
+
20
+ test "compiling and evaluating templates on #render" do
21
+ template = Tilt::RDiscountTemplate.new { |t| "# Hello World!" }
22
+ assert_equal "<h1>Hello World!</h1>\n", template.render
23
+ end
24
+
25
+ test "smartypants when :smart is set" do
26
+ template = Tilt::RDiscountTemplate.new(nil, :smart => true) { |t|
27
+ "OKAY -- 'Smarty Pants'" }
28
+ assert_equal "<p>OKAY &mdash; &lsquo;Smarty Pants&rsquo;</p>\n",
29
+ template.render
30
+ end
31
+
32
+ test "stripping HTML when :filter_html is set" do
33
+ template = Tilt::RDiscountTemplate.new(nil, :filter_html => true) { |t|
34
+ "HELLO <blink>WORLD</blink>" }
35
+ assert_equal "<p>HELLO &lt;blink>WORLD&lt;/blink></p>\n", template.render
36
+ end
37
+ end
38
+ rescue LoadError => boom
39
+ warn "Tilt::RDiscountTemplate (disabled)\n"
40
+ end
@@ -0,0 +1,19 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'rdoc/markup'
6
+ require 'rdoc/markup/to_html'
7
+ class RDocTemplateTest < Test::Unit::TestCase
8
+ test "is registered for '.rdoc' files" do
9
+ assert_equal Tilt::RDocTemplate, Tilt['test.rdoc']
10
+ end
11
+
12
+ test "compiling and evaluating the template with #render" do
13
+ template = Tilt::RDocTemplate.new { |t| "= Hello World!" }
14
+ assert_equal "<h1>Hello World!</h1>\n", template.render
15
+ end
16
+ end
17
+ rescue LoadError => boom
18
+ warn "Tilt::RDocTemplate (disabled)\n"
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'redcloth'
6
+
7
+ class RedClothTemplateTest < Test::Unit::TestCase
8
+ test "is registered for '.textile' files" do
9
+ assert_equal Tilt::RedClothTemplate, Tilt['test.textile']
10
+ end
11
+
12
+ test "compiles and evaluates the template on #render" do
13
+ template = Tilt::RedClothTemplate.new { |t| "h1. Hello World!" }
14
+ assert_equal "<h1>Hello World!</h1>", template.render
15
+ end
16
+
17
+ end
18
+ rescue LoadError => boom
19
+ warn "Tilt::RedClothTemplate (disabled)\n"
20
+ end
@@ -0,0 +1,21 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ begin
5
+ require 'haml'
6
+ require 'sass'
7
+
8
+ class SassTemplateTest < Test::Unit::TestCase
9
+ test "is registered for '.sass' files" do
10
+ assert_equal Tilt::SassTemplate, Tilt['test.sass']
11
+ end
12
+
13
+ test "compiles and evaluates the template on #render" do
14
+ template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000ff" }
15
+ assert_equal "#main {\n background-color: #0000ff; }\n", template.render
16
+ end
17
+ end
18
+
19
+ rescue LoadError => boom
20
+ warn "Tilt::SassTemplate (disabled)\n"
21
+ end
@@ -1,67 +1,67 @@
1
- require 'bacon'
1
+ require 'contest'
2
2
  require 'tilt'
3
3
 
4
- describe Tilt::StringTemplate do
5
- it "is registered for '.str' files" do
6
- Tilt['test.str'].should.equal Tilt::StringTemplate
4
+ class StringTemplateTest < Test::Unit::TestCase
5
+ test "registered for '.str' files" do
6
+ assert_equal Tilt::StringTemplate, Tilt['test.str']
7
7
  end
8
8
 
9
- it "compiles and evaluates the template on #render" do
9
+ test "compiling and evaluating templates on #render" do
10
10
  template = Tilt::StringTemplate.new { |t| "Hello World!" }
11
- template.render.should.equal "Hello World!"
11
+ assert_equal "Hello World!", template.render
12
12
  end
13
13
 
14
- it "supports locals" do
14
+ test "passing locals" do
15
15
  template = Tilt::StringTemplate.new { 'Hey #{name}!' }
16
- template.render(Object.new, :name => 'Joe').should.equal "Hey Joe!"
16
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
17
17
  end
18
18
 
19
- it "is evaluated in the object scope provided" do
19
+ test "evaluating in an object scope" do
20
20
  template = Tilt::StringTemplate.new { 'Hey #{@name}!' }
21
21
  scope = Object.new
22
22
  scope.instance_variable_set :@name, 'Joe'
23
- template.render(scope).should.equal "Hey Joe!"
23
+ assert_equal "Hey Joe!", template.render(scope)
24
24
  end
25
25
 
26
- it "evaluates template_source with yield support" do
26
+ test "passing a block for yield" do
27
27
  template = Tilt::StringTemplate.new { 'Hey #{yield}!' }
28
- template.render { 'Joe' }.should.equal "Hey Joe!"
28
+ assert_equal "Hey Joe!", template.render { 'Joe' }
29
29
  end
30
30
 
31
- it "renders multiline templates" do
31
+ test "multiline templates" do
32
32
  template = Tilt::StringTemplate.new { "Hello\nWorld!\n" }
33
- template.render.should.equal "Hello\nWorld!\n"
33
+ assert_equal "Hello\nWorld!\n", template.render
34
34
  end
35
35
 
36
- it "reports the file and line properly in backtraces without locals" do
36
+ test "backtrace file and line reporting without locals" do
37
37
  data = File.read(__FILE__).split("\n__END__\n").last
38
38
  fail unless data[0] == ?<
39
39
  template = Tilt::StringTemplate.new('test.str', 11) { data }
40
40
  begin
41
41
  template.render
42
- flunk 'should have raised an exception'
42
+ fail 'should have raised an exception'
43
43
  rescue => boom
44
- boom.should.be.kind_of NameError
44
+ assert_kind_of NameError, boom
45
45
  line = boom.backtrace.first
46
46
  file, line, meth = line.split(":")
47
- file.should.equal 'test.str'
48
- line.should.equal '13'
47
+ assert_equal 'test.str', file
48
+ assert_equal '13', line
49
49
  end
50
50
  end
51
51
 
52
- it "reports the file and line properly in backtraces with locals" do
52
+ test "backtrace file and line reporting with locals" do
53
53
  data = File.read(__FILE__).split("\n__END__\n").last
54
54
  fail unless data[0] == ?<
55
55
  template = Tilt::StringTemplate.new('test.str', 1) { data }
56
56
  begin
57
57
  template.render(nil, :name => 'Joe', :foo => 'bar')
58
- flunk 'should have raised an exception'
58
+ fail 'should have raised an exception'
59
59
  rescue => boom
60
- boom.should.be.kind_of RuntimeError
60
+ assert_kind_of RuntimeError, boom
61
61
  line = boom.backtrace.first
62
62
  file, line, meth = line.split(":")
63
- file.should.equal 'test.str'
64
- line.should.equal '6'
63
+ assert_equal 'test.str', file
64
+ assert_equal '6', line
65
65
  end
66
66
  end
67
67
  end
@@ -0,0 +1,136 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ class TiltTemplateTest < Test::Unit::TestCase
5
+ test "needs a file or block" do
6
+ assert_raise(ArgumentError) { Tilt::Template.new }
7
+ end
8
+
9
+ test "initializing with a file" do
10
+ inst = Tilt::Template.new('foo.erb')
11
+ assert_equal 'foo.erb', inst.file
12
+ end
13
+
14
+ test "initializing with a file and line" do
15
+ inst = Tilt::Template.new('foo.erb', 55)
16
+ assert_equal 'foo.erb', inst.file
17
+ assert_equal 55, inst.line
18
+ end
19
+
20
+ test "uses correct eval_file" do
21
+ inst = Tilt::Template.new('foo.erb', 55)
22
+ assert_equal 'foo.erb', inst.eval_file
23
+ end
24
+
25
+ test "uses a default filename for #eval_file when no file provided" do
26
+ inst = Tilt::Template.new { 'Hi' }
27
+ assert_not_nil inst.eval_file
28
+ assert !inst.eval_file.include?("\n")
29
+ end
30
+
31
+ test "calculating template's #basename" do
32
+ inst = Tilt::Template.new('/tmp/templates/foo.html.erb')
33
+ assert_equal 'foo.html.erb', inst.basename
34
+ end
35
+
36
+ test "calculating the template's #name" do
37
+ inst = Tilt::Template.new('/tmp/templates/foo.html.erb')
38
+ assert_equal 'foo', inst.name
39
+ end
40
+
41
+ test "initializing with a data loading block" do
42
+ Tilt::Template.new { |template| "Hello World!" }
43
+ end
44
+
45
+ class InitializingMockTemplate < Tilt::Template
46
+ @@initialized_count = 0
47
+ def self.initialized_count
48
+ @@initialized_count
49
+ end
50
+
51
+ def initialize_engine
52
+ @@initialized_count += 1
53
+ end
54
+
55
+ def compile!
56
+ end
57
+ end
58
+
59
+ test "one-time template engine initialization" do
60
+ assert_nil InitializingMockTemplate.engine_initialized
61
+ assert_equal 0, InitializingMockTemplate.initialized_count
62
+
63
+ InitializingMockTemplate.new { "Hello World!" }
64
+ assert InitializingMockTemplate.engine_initialized
65
+ assert_equal 1, InitializingMockTemplate.initialized_count
66
+
67
+ InitializingMockTemplate.new { "Hello World!" }
68
+ assert_equal 1, InitializingMockTemplate.initialized_count
69
+ end
70
+
71
+ class CompilingMockTemplate < Tilt::Template
72
+ include Test::Unit::Assertions
73
+ def compile!
74
+ assert !data.nil?
75
+ @compiled = true
76
+ end
77
+ def compiled? ; @compiled ; end
78
+ end
79
+
80
+ test "raises NotImplementedError when #compile! not defined" do
81
+ inst = Tilt::Template.new { |template| "Hello World!" }
82
+ assert_raise(NotImplementedError) { inst.render }
83
+ end
84
+
85
+ test "raises NotImplementedError when #evaluate or #template_source not defined" do
86
+ inst = CompilingMockTemplate.new { |t| "Hello World!" }
87
+ assert_raise(NotImplementedError) { inst.render }
88
+ assert inst.compiled?
89
+ end
90
+
91
+ class SimpleMockTemplate < CompilingMockTemplate
92
+ include Test::Unit::Assertions
93
+ def evaluate(scope, locals, &block)
94
+ assert compiled?
95
+ assert !scope.nil?
96
+ assert !locals.nil?
97
+ "<em>#{@data}</em>"
98
+ end
99
+ end
100
+
101
+ test "compiles and evaluates the template on #render" do
102
+ inst = SimpleMockTemplate.new { |t| "Hello World!" }
103
+ assert_equal "<em>Hello World!</em>", inst.render
104
+ assert inst.compiled?
105
+ end
106
+
107
+ class SourceGeneratingMockTemplate < CompilingMockTemplate
108
+ def template_source
109
+ "foo = [] ; foo << %Q{#{data}} ; foo.join"
110
+ end
111
+ end
112
+
113
+ test "template_source with locals" do
114
+ inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' }
115
+ assert_equal "Hey Joe!", inst.render(Object.new, :name => 'Joe')
116
+ assert inst.compiled?
117
+ end
118
+
119
+ class Person
120
+ attr_accessor :name
121
+ def initialize(name)
122
+ @name = name
123
+ end
124
+ end
125
+
126
+ test "template_source with an object scope" do
127
+ inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{@name}!' }
128
+ scope = Person.new('Joe')
129
+ assert_equal "Hey Joe!", inst.render(scope)
130
+ end
131
+
132
+ test "template_source with a block for yield" do
133
+ inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' }
134
+ assert_equal "Hey Joe!", inst.render(Object.new){ 'Joe' }
135
+ end
136
+ end
@@ -0,0 +1,57 @@
1
+ require 'contest'
2
+ require 'tilt'
3
+
4
+ class TiltTest < Test::Unit::TestCase
5
+ class MockTemplate
6
+ attr_reader :args, :block
7
+ def initialize(*args, &block)
8
+ @args = args
9
+ @block = block
10
+ end
11
+ end
12
+
13
+ test "registering template implementation classes by file extension" do
14
+ Tilt.register('mock', MockTemplate)
15
+ end
16
+
17
+ test "registering template classes by symbol file extension" do
18
+ Tilt.register(:mock, MockTemplate)
19
+ end
20
+
21
+ test "looking up template classes by file extension" do
22
+ Tilt.register('mock', MockTemplate)
23
+ impl = Tilt['mock']
24
+ assert_equal MockTemplate, impl
25
+
26
+ impl = Tilt['.mock']
27
+ assert_equal MockTemplate, impl
28
+ end
29
+
30
+ test "looking up template classes with multiple file extensions" do
31
+ Tilt.register('mock', MockTemplate)
32
+ impl = Tilt['index.html.mock']
33
+ assert_equal MockTemplate, impl
34
+ end
35
+
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
+
42
+ test "looking up non-existant template class" do
43
+ assert_nil Tilt['none']
44
+ end
45
+
46
+ test "accessing template class mappings at Tilt::mappings" do
47
+ assert Tilt.respond_to?(:mappings)
48
+ assert Tilt.mappings.respond_to?(:[])
49
+ end
50
+
51
+ test "creating new template instance with a filename" do
52
+ Tilt.register('mock', MockTemplate)
53
+ template = Tilt.new('foo.mock', 1, :key => 'val') { 'Hello World!' }
54
+ assert_equal ['foo.mock', 1, {:key => 'val'}], template.args
55
+ assert_equal 'Hello World!', template.block.call
56
+ end
57
+ end
@@ -3,8 +3,8 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'tilt'
6
- s.version = '0.3'
7
- s.date = '2009-10-16'
6
+ s.version = '0.4'
7
+ s.date = '2009-12-13'
8
8
 
9
9
  s.description = "Generic interface to multiple Ruby template engines"
10
10
  s.summary = s.description
@@ -20,23 +20,32 @@ Gem::Specification.new do |s|
20
20
  TEMPLATES.md
21
21
  bin/tilt
22
22
  lib/tilt.rb
23
- test/.bacon
24
- test/spec_tilt.rb
25
- test/spec_tilt_buildertemplate.rb
26
- test/spec_tilt_erbtemplate.rb
27
- test/spec_tilt_erubistemplate.rb
28
- test/spec_tilt_hamltemplate.rb
29
- test/spec_tilt_liquid_template.rb
30
- test/spec_tilt_mustachetemplate.rb
31
- test/spec_tilt_rdiscount.rb
32
- test/spec_tilt_sasstemplate.rb
33
- test/spec_tilt_stringtemplate.rb
34
- test/spec_tilt_template.rb
23
+ test/tilt_buildertemplate_test.rb
24
+ test/tilt_cache_test.rb
25
+ test/tilt_erbtemplate_test.rb
26
+ test/tilt_erubistemplate_test.rb
27
+ test/tilt_hamltemplate_test.rb
28
+ test/tilt_liquidtemplate_test.rb
29
+ test/tilt_mustachetemplate_test.rb
30
+ test/tilt_rdiscounttemplate_test.rb
31
+ test/tilt_rdoctemplate_test.rb
32
+ test/tilt_redclothtemplate_test.rb
33
+ test/tilt_sasstemplate_test.rb
34
+ test/tilt_stringtemplate_test.rb
35
+ test/tilt_template_test.rb
36
+ test/tilt_test.rb
35
37
  tilt.gemspec
36
38
  ]
37
39
  # = MANIFEST =
38
40
 
39
41
  s.test_files = s.files.select {|path| path =~ /^test\/spec_.*.rb/}
42
+ s.add_development_dependency 'contest'
43
+ s.add_development_dependency 'builder'
44
+ s.add_development_dependency 'erubis'
45
+ s.add_development_dependency 'haml'
46
+ s.add_development_dependency 'mustache'
47
+ s.add_development_dependency 'rdiscount'
48
+ s.add_development_dependency 'liquid'
40
49
 
41
50
  s.extra_rdoc_files = %w[COPYING]
42
51