Browse files

port unit tests to Test::Unit + contest

  • Loading branch information...
1 parent 3435557 commit 3cc6357ebc8c2e75b0a9ff59901ca095a2732707 @rtomayko committed Dec 13, 2009
View
13 Rakefile
@@ -1,4 +1,4 @@
-task :default => :spec
+task :default => :test
# SPECS =====================================================================
@@ -9,12 +9,11 @@ end
desc 'Run specs with unit test style output'
task :test do |t|
- sh 'bacon -qa'
-end
-
-desc 'Run specs with story style output'
-task :spec do |t|
- sh 'bacon -a'
+ if system('type turn >/dev/null 2>&1')
+ sh 'turn -Ilib test/*_test.rb'
+ else
+ sh 'testrb -Ilib -rubygems test/*_test.rb'
+ end
end
# PACKAGING =================================================================
View
0 test/.bacon
No changes.
View
53 test/spec_tilt.rb
@@ -1,53 +0,0 @@
-require 'bacon'
-require 'tilt'
-
-describe Tilt do
- class MockTemplate
- attr_reader :args, :block
- def initialize(*args, &block)
- @args = args
- @block = block
- end
- end
-
- it "registers template implementation classes by file extension" do
- lambda { Tilt.register('mock', MockTemplate) }.should.not.raise
- end
-
- it "registers template implementation classes by a file extension given as a symbol" do
- lambda { Tilt.register(:mock, MockTemplate) }.should.not.raise
- end
-
- it "looks up template implementation classes by file extension" do
- impl = Tilt['mock']
- impl.should.equal MockTemplate
-
- impl = Tilt['.mock']
- impl.should.equal MockTemplate
- end
-
- it "looks up template implementation classes with multiple file extensions" do
- impl = Tilt['index.html.mock']
- impl.should.equal MockTemplate
- end
-
- it "looks up template implementation classes by file name" do
- impl = Tilt['templates/test.mock']
- impl.should.equal MockTemplate
- end
-
- it "gives nil when no template implementation classes exist for a filename" do
- Tilt['none'].should.be.nil
- end
-
- it "exposes the file pattern -> template class Hash at Tilt::mappings" do
- Tilt.should.respond_to :mappings
- Tilt.mappings.should.respond_to :[]
- end
-
- it "creates a new template instance given a filename" do
- template = Tilt.new('foo.mock', 1, :key => 'val') { 'Hello World!' }
- template.args.should.equal ['foo.mock', 1, {:key => 'val'}]
- template.block.call.should.equal 'Hello World!'
- end
-end
View
41 test/spec_tilt_buildertemplate.rb
@@ -1,41 +0,0 @@
-require 'bacon'
-require 'tilt'
-require 'erb'
-require 'builder'
-
-describe Tilt::BuilderTemplate do
- it "is registered for '.builder' files" do
- Tilt['test.builder'].should.equal Tilt::BuilderTemplate
- Tilt['test.xml.builder'].should.equal Tilt::BuilderTemplate
- end
-
- it "compiles and evaluates the template on #render" do
- template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" }
- template.render.should.equal "<em>Hello World!</em>\n"
- end
-
- it "supports locals" do
- template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + name + '!')" }
- template.render(Object.new, :name => 'Joe').should.equal "<em>Hey Joe!</em>\n"
- end
-
- it "is evaluated in the object scope provided" do
- template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + @name + '!')" }
- scope = Object.new
- scope.instance_variable_set :@name, 'Joe'
- template.render(scope).should.equal "<em>Hey Joe!</em>\n"
- end
-
- it "evaluates template_source with yield support" do
- template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + yield + '!')" }
- template.render { 'Joe' }.should.equal "<em>Hey Joe!</em>\n"
- end
-
- it "calls a block directly when" do
- template =
- Tilt::BuilderTemplate.new do |t|
- lambda { |xml| xml.em('Hey Joe!') }
- end
- template.render.should.equal "<em>Hey Joe!</em>\n"
- end
-end
View
93 test/spec_tilt_erbtemplate.rb
@@ -1,93 +0,0 @@
-require 'bacon'
-require 'tilt'
-require 'erb'
-
-describe Tilt::ERBTemplate do
- it "is registered for '.erb' files" do
- Tilt['test.erb'].should.equal Tilt::ERBTemplate
- Tilt['test.html.erb'].should.equal Tilt::ERBTemplate
- end
-
- it "is registered for '.rhtml' files" do
- Tilt['test.rhtml'].should.equal Tilt::ERBTemplate
- end
-
- it "compiles and evaluates the template on #render" do
- template = Tilt::ERBTemplate.new { |t| "Hello World!" }
- template.render.should.equal "Hello World!"
- end
-
- it "supports locals" do
- template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
- template.render(Object.new, :name => 'Joe').should.equal "Hey Joe!"
- end
-
- it "is evaluated in the object scope provided" do
- template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' }
- scope = Object.new
- scope.instance_variable_set :@name, 'Joe'
- template.render(scope).should.equal "Hey Joe!"
- end
-
- it "evaluates template_source with yield support" do
- template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
- template.render { 'Joe' }.should.equal "Hey Joe!"
- end
-
- it "reports the file and line properly in backtraces without locals" do
- data = File.read(__FILE__).split("\n__END__\n").last
- fail unless data[0] == ?<
- template = Tilt::ERBTemplate.new('test.erb', 11) { data }
- begin
- template.render
- flunk 'should have raised an exception'
- rescue => boom
- boom.should.be.kind_of NameError
- line = boom.backtrace.first
- file, line, meth = line.split(":")
- file.should.equal 'test.erb'
- line.should.equal '13'
- end
- end
-
- it "reports the file and line properly in backtraces with locals" do
- data = File.read(__FILE__).split("\n__END__\n").last
- fail unless data[0] == ?<
- template = Tilt::ERBTemplate.new('test.erb', 1) { data }
- begin
- template.render(nil, :name => 'Joe', :foo => 'bar')
- flunk 'should have raised an exception'
- rescue => boom
- boom.should.be.kind_of RuntimeError
- line = boom.backtrace.first
- file, line, meth = line.split(":")
- file.should.equal 'test.erb'
- line.should.equal '6'
- end
- end
-
- it "renders erb templates with new lines by default" do
- template = Tilt.new('test.erb', 1) { "\n<%= 1 + 1 %>\n" }
- template.render.should.equal "\n2\n"
- end
-
- it "strips new lines explicitly when :trim option is set to '-'" do
- template = Tilt.new('test.erb', 1, :trim => '-') { "\n<%= 1 + 1 -%>\n" }
- template.render.should.equal "\n2"
- end
-
- it "processes lines start with % when :trim option is set to '%'" do
- template = Tilt.new('test.erb', 1, :trim => '%') { "\n% if true\nhello\n%end\n" }
- template.render.should.equal "\nhello\n"
- end
-end
-
-__END__
-<html>
-<body>
- <h1>Hey <%= name %>!</h1>
-
-
- <p><% fail %></p>
-</body>
-</html>
View
41 test/spec_tilt_rdiscounttemplate.rb
@@ -1,41 +0,0 @@
-require 'bacon'
-require 'tilt'
-
-begin
- require 'rdiscount'
-
- describe Tilt::RDiscountTemplate do
- it "is registered for '.markdown' files" do
- Tilt['test.markdown'].should.equal Tilt::RDiscountTemplate
- end
-
- it "is registered for '.md' files" do
- Tilt['test.md'].should.equal Tilt::RDiscountTemplate
- end
-
- it "is registered for '.mkd' files" do
- Tilt['test.mkd'].should.equal Tilt::RDiscountTemplate
- end
-
- it "compiles and evaluates the template on #render" do
- template = Tilt::RDiscountTemplate.new { |t| "# Hello World!" }
- template.render.should.equal "<h1>Hello World!</h1>\n"
- end
-
- it "uses smartypants style punctuation replacements when :smart is set" do
- template = Tilt::RDiscountTemplate.new(nil, :smart => true) { |t|
- "OKAY -- 'Smarty Pants'" }
- template.render.should.equal \
- "<p>OKAY &mdash; &lsquo;Smarty Pants&rsquo;</p>\n"
- end
-
- it "strips HTML when :filter_html is set" do
- template = Tilt::RDiscountTemplate.new(nil, :filter_html => true) { |t|
- "HELLO <blink>WORLD</blink>" }
- template.render.should.equal \
- "<p>HELLO &lt;blink>WORLD&lt;/blink></p>\n"
- end
- end
-rescue LoadError => boom
- warn "Tilt::RDiscountTemplate (disabled)\n"
-end
View
17 test/spec_tilt_rdoctemplate.rb
@@ -1,17 +0,0 @@
-require 'bacon'
-require 'tilt'
-
-begin
- describe Tilt::RDocTemplate do
- it "is registered for '.rdoc' files" do
- Tilt['test.rdoc'].should.equal Tilt::RDocTemplate
- end
-
- it "compiles and evaluates the template on #render" do
- template = Tilt::RDocTemplate.new { |t| "= Hello World!" }
- template.render.should.equal "<h1>Hello World!</h1>\n"
- end
- end
-rescue LoadError => boom
- warn "Tilt::RDocTemplate (disabled)\n"
-end
View
20 test/spec_tilt_redclothtemplate.rb
@@ -1,20 +0,0 @@
-require 'bacon'
-require 'tilt'
-
-begin
- require 'redcloth'
-
- describe Tilt::RedClothTemplate do
- it "is registered for '.textile' files" do
- Tilt['test.textile'].should.equal Tilt::RedClothTemplate
- end
-
- it "compiles and evaluates the template on #render" do
- template = Tilt::RedClothTemplate.new { |t| "h1. Hello World!" }
- template.render.should.equal "<h1>Hello World!</h1>"
- end
-
- end
-rescue LoadError => boom
- warn "Tilt::RedClothTemplate (disabled)\n"
-end
View
21 test/spec_tilt_sasstemplate.rb
@@ -1,21 +0,0 @@
-require 'bacon'
-require 'tilt'
-
-begin
- require 'haml'
- require 'sass'
-
- describe Tilt::SassTemplate do
- it "is registered for '.sass' files" do
- Tilt['test.sass'].should.equal Tilt::SassTemplate
- end
-
- it "compiles and evaluates the template on #render" do
- template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000ff" }
- template.render.should.equal "#main {\n background-color: #0000ff; }\n"
- end
- end
-
-rescue LoadError => boom
- warn "Tilt::SassTemplate (disabled)\n"
-end
View
136 test/spec_tilt_template.rb
@@ -1,136 +0,0 @@
-require 'bacon'
-require 'tilt'
-
-describe Tilt::Template do
- it "raises ArgumentError when a file or block not given" do
- lambda { Tilt::Template.new }.should.raise ArgumentError
- end
-
- it "can be constructed with a file" do
- inst = Tilt::Template.new('foo.erb')
- inst.file.should.equal 'foo.erb'
- end
-
- it "can be constructed with a file and line" do
- inst = Tilt::Template.new('foo.erb', 55)
- inst.file.should.equal 'foo.erb'
- inst.line.should.equal 55
- end
-
- it "uses the filename provided for #eval_file" do
- inst = Tilt::Template.new('foo.erb', 55)
- inst.eval_file.should.equal 'foo.erb'
- end
-
- it "uses a default filename for #eval_file when no file provided" do
- inst = Tilt::Template.new { 'Hi' }
- inst.eval_file.should.not.be.nil
- inst.eval_file.should.not.include "\n"
- end
-
- it "responds to #basename with the file's basename" do
- inst = Tilt::Template.new('/tmp/templates/foo.html.erb')
- inst.basename.should.equal 'foo.html.erb'
- end
-
- it "responds to #name with the file's basename minus file extensions" do
- inst = Tilt::Template.new('/tmp/templates/foo.html.erb')
- inst.name.should.equal 'foo'
- end
-
- it "can be constructed with a data loading block" do
- lambda {
- Tilt::Template.new { |template| "Hello World!" }
- }.should.not.raise
- end
-
- class InitializingMockTemplate < Tilt::Template
- @@initialized_count = 0
- def self.initialized_count
- @@initialized_count
- end
-
- def initialize_engine
- @@initialized_count += 1
- end
-
- def compile!
- end
- end
-
- it "calls #initialize_engine the very first time " do
- InitializingMockTemplate.engine_initialized.should.be.nil
- InitializingMockTemplate.initialized_count.should.equal 0
-
- InitializingMockTemplate.new { "Hello World!" }
- InitializingMockTemplate.engine_initialized.should.equal true
- InitializingMockTemplate.initialized_count.should.equal 1
-
- InitializingMockTemplate.new { "Hello World!" }
- InitializingMockTemplate.initialized_count.should.equal 1
- end
-
- class CompilingMockTemplate < Tilt::Template
- def compile!
- data.should.not.be.nil
- @compiled = true
- end
- def compiled? ; @compiled ; end
- end
-
- it "raises NotImplementedError when #compile! not defined" do
- inst = Tilt::Template.new { |template| "Hello World!" }
- lambda { inst.render }.should.raise NotImplementedError
- end
-
- it "raises NotImplementedError when #evaluate or #template_source not defined" do
- inst = CompilingMockTemplate.new { |t| "Hello World!" }
- lambda { inst.render }.should.raise NotImplementedError
- inst.should.be.compiled
- end
-
- class SimpleMockTemplate < CompilingMockTemplate
- def evaluate(scope, locals, &block)
- should.be.compiled
- scope.should.not.be.nil
- locals.should.not.be.nil
- "<em>#{@data}</em>"
- end
- end
-
- it "compiles and evaluates the template on #render" do
- inst = SimpleMockTemplate.new { |t| "Hello World!" }
- inst.render.should.equal "<em>Hello World!</em>"
- inst.should.be.compiled
- end
-
- class SourceGeneratingMockTemplate < CompilingMockTemplate
- def template_source
- "foo = [] ; foo << %Q{#{data}} ; foo.join"
- end
- end
-
- it "evaluates template_source with locals support" do
- inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' }
- inst.render(Object.new, :name => 'Joe').should.equal "Hey Joe!"
- inst.should.be.compiled
- end
-
- class Person
- attr_accessor :name
- def initialize(name)
- @name = name
- end
- end
-
- it "evaluates template_source in the object scope provided" do
- inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{@name}!' }
- scope = Person.new('Joe')
- inst.render(scope).should.equal "Hey Joe!"
- end
-
- it "evaluates template_source with yield support" do
- inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' }
- inst.render(Object.new){ 'Joe' }.should.equal "Hey Joe!"
- end
-end
View
40 test/tilt_buildertemplate_test.rb
@@ -0,0 +1,40 @@
+require 'contest'
+require 'builder'
+require 'tilt'
+
+class BuilderTemplateTest < Test::Unit::TestCase
+ test "registered for '.builder' files" do
+ assert_equal Tilt::BuilderTemplate, Tilt['test.builder']
+ assert_equal Tilt::BuilderTemplate, Tilt['test.xml.builder']
+ end
+
+ test "compiling and evaluating the template on #render" do
+ template = Tilt::BuilderTemplate.new { |t| "xml.em 'Hello World!'" }
+ assert_equal "<em>Hello World!</em>\n", template.render
+ end
+
+ test "passing locals" do
+ template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + name + '!')" }
+ assert_equal "<em>Hey Joe!</em>\n", template.render(Object.new, :name => 'Joe')
+ end
+
+ test "evaluating in an object scope" do
+ template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + @name + '!')" }
+ scope = Object.new
+ scope.instance_variable_set :@name, 'Joe'
+ assert_equal "<em>Hey Joe!</em>\n", template.render(scope)
+ end
+
+ test "passing a block for yield" do
+ template = Tilt::BuilderTemplate.new { "xml.em('Hey ' + yield + '!')" }
+ assert_equal "<em>Hey Joe!</em>\n", template.render { 'Joe' }
+ end
+
+ test "block style templates" do
+ template =
+ Tilt::BuilderTemplate.new do |t|
+ lambda { |xml| xml.em('Hey Joe!') }
+ end
+ assert_equal "<em>Hey Joe!</em>\n", template.render
+ end
+end
View
26 test/spec_tilt_cache.rb → test/tilt_cache_test.rb
@@ -1,35 +1,33 @@
-require 'bacon'
+require 'contest'
require 'tilt'
-describe Tilt::Cache do
- before do
- @cache = Tilt::Cache.new
- end
+class TiltCacheTest < Test::Unit::TestCase
+ setup { @cache = Tilt::Cache.new }
- it "caches with single simple argument to #fetch" do
+ test "caching with single simple argument to #fetch" do
template = nil
result = @cache.fetch('hello') { template = Tilt::StringTemplate.new {''} }
- result.object_id.should.equal template.object_id
+ assert_same template, result
result = @cache.fetch('hello') { fail 'should be cached' }
- result.object_id.should.equal template.object_id
+ assert_same template, result
end
- it "caches with multiple complex arguments to #fetch" do
+ test "caching with multiple complex arguments to #fetch" do
template = nil
args = ['hello', {:foo => 'bar', :baz => 'bizzle'}]
result = @cache.fetch(*args) { template = Tilt::StringTemplate.new {''} }
- result.object_id.should.equal template.object_id
+ assert_same template, result
result = @cache.fetch(*args) { fail 'should be cached' }
- result.object_id.should.equal template.object_id
+ assert_same template, result
end
- it "clears the cache on #clear" do
+ test "clearing the cache with #clear" do
template, other = nil
result = @cache.fetch('hello') { template = Tilt::StringTemplate.new {''} }
- result.object_id.should.equal template.object_id
+ assert_same template, result
@cache.clear
result = @cache.fetch('hello') { other = Tilt::StringTemplate.new {''} }
- result.object_id.should.equal other.object_id
+ assert_same other, result
end
end
View
93 test/tilt_erbtemplate_test.rb
@@ -0,0 +1,93 @@
+require 'contest'
+require 'tilt'
+require 'erb'
+
+class ERBTemplateTest < Test::Unit::TestCase
+ test "registered for '.erb' files" do
+ assert_equal Tilt::ERBTemplate, Tilt['test.erb']
+ assert_equal Tilt::ERBTemplate, Tilt['test.html.erb']
+ end
+
+ test "registered for '.rhtml' files" do
+ assert_equal Tilt::ERBTemplate, Tilt['test.rhtml']
+ end
+
+ test "compiling and evaluating templates on #render" do
+ template = Tilt::ERBTemplate.new { |t| "Hello World!" }
+ assert_equal "Hello World!", template.render
+ end
+
+ test "passing locals" do
+ template = Tilt::ERBTemplate.new { 'Hey <%= name %>!' }
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
+ end
+
+ test "evaluating in an object scope" do
+ template = Tilt::ERBTemplate.new { 'Hey <%= @name %>!' }
+ scope = Object.new
+ scope.instance_variable_set :@name, 'Joe'
+ assert_equal "Hey Joe!", template.render(scope)
+ end
+
+ test "passing a block for yield" do
+ template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
+ assert_equal "Hey Joe!", template.render { 'Joe' }
+ end
+
+ test "backtrace file and line reporting without locals" do
+ data = File.read(__FILE__).split("\n__END__\n").last
+ fail unless data[0] == ?<
+ template = Tilt::ERBTemplate.new('test.erb', 11) { data }
+ begin
+ template.render
+ fail 'should have raised an exception'
+ rescue => boom
+ assert_kind_of NameError, boom
+ line = boom.backtrace.first
+ file, line, meth = line.split(":")
+ assert_equal 'test.erb', file
+ assert_equal '13', line
+ end
+ end
+
+ test "backtrace file and line reporting with locals" do
+ data = File.read(__FILE__).split("\n__END__\n").last
+ fail unless data[0] == ?<
+ template = Tilt::ERBTemplate.new('test.erb', 1) { data }
+ begin
+ template.render(nil, :name => 'Joe', :foo => 'bar')
+ fail 'should have raised an exception'
+ rescue => boom
+ assert_kind_of RuntimeError, boom
+ line = boom.backtrace.first
+ file, line, meth = line.split(":")
+ assert_equal 'test.erb', file
+ assert_equal '6', line
+ end
+ end
+
+ test "default non-stripping trim mode" do
+ template = Tilt.new('test.erb', 1) { "\n<%= 1 + 1 %>\n" }
+ assert_equal "\n2\n", template.render
+ end
+
+ test "stripping trim mode" do
+ template = Tilt.new('test.erb', 1, :trim => '-') { "\n<%= 1 + 1 -%>\n" }
+ assert_equal "\n2", template.render
+ end
+
+ test "shorthand whole line syntax trim mode" do
+ template = Tilt.new('test.erb', 1, :trim => '%') { "\n% if true\nhello\n%end\n" }
+ assert_equal "\nhello\n", template.render
+ end
+end
+
+__END__
+<html>
+<body>
+ <h1>Hey <%= name %>!</h1>
+
+
+ <p><% fail %></p>
+</body>
+</html>
View
50 test/spec_tilt_erubistemplate.rb → test/tilt_erubistemplate_test.rb
@@ -1,73 +1,73 @@
-require 'bacon'
+require 'contest'
require 'tilt'
begin
require 'erubis'
- describe "Tilt::ErubisTemplate" do
- it "is registered for '.erubis' files" do
- Tilt['test.erubis'].should.equal Tilt::ErubisTemplate
- Tilt['test.html.erubis'].should.equal Tilt::ErubisTemplate
+ class ErubisTemplateTest < Test::Unit::TestCase
+ test "registered for '.erubis' files" do
+ assert_equal Tilt::ErubisTemplate, Tilt['test.erubis']
+ assert_equal Tilt::ErubisTemplate, Tilt['test.html.erubis']
end
- it "compiles and evaluates the template on #render" do
+ test "compiling and evaluating templates on #render" do
template = Tilt::ErubisTemplate.new { |t| "Hello World!" }
- template.render.should.equal "Hello World!"
+ assert_equal "Hello World!", template.render
end
- it "supports locals" do
+ test "passing locals" do
template = Tilt::ErubisTemplate.new { 'Hey <%= name %>!' }
- template.render(Object.new, :name => 'Joe').should.equal "Hey Joe!"
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
end
- it "is evaluated in the object scope provided" do
+ test "evaluating in an object scope" do
template = Tilt::ErubisTemplate.new { 'Hey <%= @name %>!' }
scope = Object.new
scope.instance_variable_set :@name, 'Joe'
- template.render(scope).should.equal "Hey Joe!"
+ assert_equal "Hey Joe!", template.render(scope)
end
- it "evaluates template_source with yield support" do
+ test "passing a block for yield" do
template = Tilt::ErubisTemplate.new { 'Hey <%= yield %>!' }
- template.render { 'Joe' }.should.equal "Hey Joe!"
+ assert_equal "Hey Joe!", template.render { 'Joe' }
end
- it "reports the file and line properly in backtraces without locals" do
+ test "backtrace file and line reporting without locals" do
data = File.read(__FILE__).split("\n__END__\n").last
fail unless data[0] == ?<
template = Tilt::ErubisTemplate.new('test.erubis', 11) { data }
begin
template.render
- flunk 'should have raised an exception'
+ fail 'should have raised an exception'
rescue => boom
- boom.should.be.kind_of NameError
+ assert_kind_of NameError, boom
line = boom.backtrace.first
file, line, meth = line.split(":")
- file.should.equal 'test.erubis'
- line.should.equal '13'
+ assert_equal 'test.erubis', file
+ assert_equal '13', line
end
end
- it "reports the file and line properly in backtraces with locals" do
+ test "backtrace file and line reporting with locals" do
data = File.read(__FILE__).split("\n__END__\n").last
fail unless data[0] == ?<
template = Tilt::ErubisTemplate.new('test.erubis', 1) { data }
begin
template.render(nil, :name => 'Joe', :foo => 'bar')
- flunk 'should have raised an exception'
+ fail 'should have raised an exception'
rescue => boom
- boom.should.be.kind_of RuntimeError
+ assert_kind_of RuntimeError, boom
line = boom.backtrace.first
file, line, meth = line.split(":")
- file.should.equal 'test.erubis'
- line.should.equal '6'
+ assert_equal 'test.erubis', file
+ assert_equal '6', line
end
end
- it "passes options to erubis" do
+ test "erubis template options" do
template = Tilt::ErubisTemplate.new(nil, :pattern => '\{% %\}') { 'Hey {%= @name %}!' }
scope = Object.new
scope.instance_variable_set :@name, 'Joe'
- template.render(scope).should.equal "Hey Joe!"
+ assert_equal "Hey Joe!", template.render(scope)
end
end
rescue LoadError => boom
View
40 test/spec_tilt_hamltemplate.rb → test/tilt_hamltemplate_test.rb
@@ -1,4 +1,4 @@
-require 'bacon'
+require 'contest'
require 'tilt'
begin
@@ -7,61 +7,61 @@ class ::MockError < NameError
require 'haml'
- describe Tilt::HamlTemplate do
- it "is registered for '.haml' files" do
- Tilt['test.haml'].should.equal Tilt::HamlTemplate
+ class HamlTemplateTest < Test::Unit::TestCase
+ test "registered for '.haml' files" do
+ assert_equal Tilt::HamlTemplate, Tilt['test.haml']
end
- it "compiles and evaluates the template on #render" do
+ test "compiling and evaluating templates on #render" do
template = Tilt::HamlTemplate.new { |t| "%p Hello World!" }
- template.render.should.equal "<p>Hello World!</p>\n"
+ assert_equal "<p>Hello World!</p>\n", template.render
end
- it "supports locals" do
+ test "passing locals" do
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" }
- template.render(Object.new, :name => 'Joe').should.equal "<p>Hey Joe!</p>\n"
+ assert_equal "<p>Hey Joe!</p>\n", template.render(Object.new, :name => 'Joe')
end
- it "is evaluated in the object scope provided" do
+ test "evaluating in an object scope" do
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" }
scope = Object.new
scope.instance_variable_set :@name, 'Joe'
- template.render(scope).should.equal "<p>Hey Joe!</p>\n"
+ assert_equal "<p>Hey Joe!</p>\n", template.render(scope)
end
- it "evaluates template_source with yield support" do
+ test "passing a block for yield" do
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + yield + '!'" }
- template.render { 'Joe' }.should.equal "<p>Hey Joe!</p>\n"
+ assert_equal "<p>Hey Joe!</p>\n", template.render { 'Joe' }
end
- it "reports the file and line properly in backtraces without locals" do
+ test "backtrace file and line reporting without locals" do
data = File.read(__FILE__).split("\n__END__\n").last
fail unless data[0] == ?%
template = Tilt::HamlTemplate.new('test.haml', 10) { data }
begin
template.render
fail 'should have raised an exception'
rescue => boom
- boom.should.be.kind_of NameError
+ assert_kind_of NameError, boom
line = boom.backtrace.first
file, line, meth = line.split(":")
- file.should.equal 'test.haml'
- line.should.equal '12'
+ assert_equal 'test.haml', file
+ assert_equal '12', line
end
end
- it "reports the file and line properly in backtraces with locals" do
+ test "backtrace file and line reporting with locals" do
data = File.read(__FILE__).split("\n__END__\n").last
fail unless data[0] == ?%
template = Tilt::HamlTemplate.new('test.haml') { data }
begin
res = template.render(Object.new, :name => 'Joe', :foo => 'bar')
rescue => boom
- boom.should.be.kind_of ::MockError
+ assert_kind_of MockError, boom
line = boom.backtrace.first
file, line, meth = line.split(":")
- file.should.equal 'test.haml'
- line.should.equal '5'
+ assert_equal 'test.haml', file
+ assert_equal '5', line
end
end
end
View
36 test/spec_tilt_liquid_template.rb → test/tilt_liquidtemplate_test.rb
@@ -1,22 +1,22 @@
-require 'bacon'
+require 'contest'
require 'tilt'
begin
require 'liquid'
- describe Tilt::LiquidTemplate do
- it "is registered for '.liquid' files" do
- Tilt['test.liquid'].should.equal Tilt::LiquidTemplate
+ class LiquidTemplateTest < Test::Unit::TestCase
+ test "registered for '.liquid' files" do
+ assert_equal Tilt::LiquidTemplate, Tilt['test.liquid']
end
- it "compiles and evaluates the template on #render" do
+ test "compiling and evaluating templates on #render" do
template = Tilt::LiquidTemplate.new { |t| "Hello World!" }
- template.render.should.equal "Hello World!"
+ assert_equal "Hello World!", template.render
end
- it "supports locals" do
+ test "passing locals" do
template = Tilt::LiquidTemplate.new { "Hey {{ name }}!" }
- template.render(nil, :name => 'Joe').should.equal "Hey Joe!"
+ assert_equal "Hey Joe!", template.render(nil, :name => 'Joe')
end
# Object's passed as "scope" to LiquidTemplate may respond to
@@ -28,43 +28,43 @@ def to_h
end
end
- it "merges scope Hash in under locals when scope responds to #to_h" do
+ test "combining scope and locals when scope responds to #to_h" do
template =
Tilt::LiquidTemplate.new {
'Beer is {{ beer }} but Whisky is {{ whisky }}.'
}
scope = ExampleLiquidScope.new
- template.render(scope).should.equal "Beer is wet but Whisky is wetter."
+ assert_equal "Beer is wet but Whisky is wetter.", template.render(scope)
end
- it "gives locals presendence over scope defined variables" do
+ test "precedence when locals and scope define same variables" do
template =
Tilt::LiquidTemplate.new {
'Beer is {{ beer }} but Whisky is {{ whisky }}.'
}
scope = ExampleLiquidScope.new
- template.render(scope, :beer => 'great', :whisky => 'greater').
- should.equal "Beer is great but Whisky is greater."
+ assert_equal "Beer is great but Whisky is greater.",
+ template.render(scope, :beer => 'great', :whisky => 'greater')
end
# Object's passed as "scope" to LiquidTemplate that do not
# respond to #to_h are silently ignored.
class ExampleIgnoredLiquidScope
end
- it "does not freak out when scope does not respond to #to_h" do
+ test "handling scopes that do not respond to #to_h" do
template = Tilt::LiquidTemplate.new { 'Whisky' }
scope = ExampleIgnoredLiquidScope.new
- template.render(scope).should.equal "Whisky"
+ assert_equal "Whisky", template.render(scope)
end
- it "supports yield in templates" do
+ test "passing a block for yield" do
template =
Tilt::LiquidTemplate.new {
'Beer is {{ yield }} but Whisky is {{ content }}ter.'
}
- template.render({}) { 'wet' }.
- should.equal "Beer is wet but Whisky is wetter."
+ assert_equal "Beer is wet but Whisky is wetter.",
+ template.render({}) { 'wet' }
end
end
View
46 test/spec_tilt_mustachetemplate.rb → test/tilt_mustachetemplate_test.rb
@@ -1,40 +1,40 @@
-require 'bacon'
+require 'contest'
require 'tilt'
begin
require 'mustache'
raise LoadError, "mustache version must be > 0.2.2" if !Mustache.respond_to?(:compiled?)
- describe Tilt::MustacheTemplate do
- it "is registered for '.mustache' files" do
- Tilt['test.mustache'].should.equal Tilt::MustacheTemplate
+ module Views
+ class Foo < Mustache
+ attr_reader :foo
+ end
+ end
+
+ class MustacheTemplateTest < Test::Unit::TestCase
+ test "registered for '.mustache' files" do
+ assert_equal Tilt::MustacheTemplate, Tilt['test.mustache']
end
- it "compiles and evaluates the template on #render" do
+ test "compiling and evaluating templates on #render" do
template = Tilt::MustacheTemplate.new { |t| "Hello World!" }
- template.render.should.equal "Hello World!"
+ assert_equal "Hello World!", template.render
end
- it "supports locals" do
+ test "passing locals" do
template = Tilt::MustacheTemplate.new { "<p>Hey {{name}}!</p>" }
- template.render(nil, :name => 'Joe').should.equal "<p>Hey Joe!</p>"
+ assert_equal "<p>Hey Joe!</p>", template.render(nil, :name => 'Joe')
end
- it "evaluates template_source with yield support" do
+ test "passing a block for yield" do
template = Tilt::MustacheTemplate.new { "<p>Hey {{yield}}!</p>" }
- template.render { 'Joe' }.should.equal "<p>Hey Joe!</p>"
- end
-
- module Views
- class Foo < Mustache
- attr_reader :foo
- end
+ assert_equal "<p>Hey Joe!</p>", template.render { 'Joe' }
end
- it "locates views defined at the top-level by default" do
+ test "locating views defined at the top-level" do
template = Tilt::MustacheTemplate.new('foo.mustache') { "<p>Hey {{foo}}!</p>" }
template.compile
- template.engine.should.equal Views::Foo
+ assert_equal Views::Foo, template.engine
end
module Bar
@@ -44,18 +44,18 @@ class Bizzle < Mustache
end
end
- it "locates views defined in a custom namespace" do
+ test "locating views defined in a custom namespace" do
template = Tilt::MustacheTemplate.new('bizzle.mustache', :namespace => Bar) { "<p>Hello World!</p>" }
template.compile
- template.engine.should.equal Bar::Views::Bizzle
- template.render.should.equal "<p>Hello World!</p>"
+ assert_equal Bar::Views::Bizzle, template.engine
+ assert_equal "<p>Hello World!</p>", template.render
end
- it "copies instance variables from scope object" do
+ test "copying instance variables from scope object" do
template = Tilt::MustacheTemplate.new('foo.mustache') { "<p>Hey {{foo}}!</p>" }
scope = Object.new
scope.instance_variable_set(:@foo, 'Jane!')
- template.render(scope).should.equal "<p>Hey Jane!!</p>"
+ assert_equal "<p>Hey Jane!!</p>", template.render(scope)
end
end
View
40 test/tilt_rdiscounttemplate_test.rb
@@ -0,0 +1,40 @@
+require 'contest'
+require 'tilt'
+
+begin
+ require 'rdiscount'
+
+ class RDiscountTemplateTest < Test::Unit::TestCase
+ test "registered for '.markdown' files" do
+ assert_equal Tilt::RDiscountTemplate, Tilt['test.markdown']
+ end
+
+ test "registered for '.md' files" do
+ assert_equal Tilt::RDiscountTemplate, Tilt['test.md']
+ end
+
+ test "registered for '.mkd' files" do
+ assert_equal Tilt::RDiscountTemplate, Tilt['test.mkd']
+ end
+
+ test "compiling and evaluating templates on #render" do
+ template = Tilt::RDiscountTemplate.new { |t| "# Hello World!" }
+ assert_equal "<h1>Hello World!</h1>\n", template.render
+ end
+
+ test "smartypants when :smart is set" do
+ template = Tilt::RDiscountTemplate.new(nil, :smart => true) { |t|
+ "OKAY -- 'Smarty Pants'" }
+ assert_equal "<p>OKAY &mdash; &lsquo;Smarty Pants&rsquo;</p>\n",
+ template.render
+ end
+
+ test "stripping HTML when :filter_html is set" do
+ template = Tilt::RDiscountTemplate.new(nil, :filter_html => true) { |t|
+ "HELLO <blink>WORLD</blink>" }
+ assert_equal "<p>HELLO &lt;blink>WORLD&lt;/blink></p>\n", template.render
+ end
+ end
+rescue LoadError => boom
+ warn "Tilt::RDiscountTemplate (disabled)\n"
+end
View
17 test/tilt_rdoctemplate_test.rb
@@ -0,0 +1,17 @@
+require 'contest'
+require 'tilt'
+
+begin
+ class RDocTemplateTest < Test::Unit::TestCase
+ test "is registered for '.rdoc' files" do
+ assert_equal Tilt::RDocTemplate, Tilt['test.rdoc']
+ end
+
+ test "compiling and evaluating the template with #render" do
+ template = Tilt::RDocTemplate.new { |t| "= Hello World!" }
+ assert_equal "<h1>Hello World!</h1>\n", template.render
+ end
+ end
+rescue LoadError => boom
+ warn "Tilt::RDocTemplate (disabled)\n"
+end
View
20 test/tilt_redclothtemplate_test.rb
@@ -0,0 +1,20 @@
+require 'contest'
+require 'tilt'
+
+begin
+ require 'redcloth'
+
+ class RedClothTemplateTest < Test::Unit::TestCase
+ test "is registered for '.textile' files" do
+ assert_equal Tilt::RedClothTemplate, Tilt['test.textile']
+ end
+
+ test "compiles and evaluates the template on #render" do
+ template = Tilt::RedClothTemplate.new { |t| "h1. Hello World!" }
+ assert_equal "<h1>Hello World!</h1>", template.render
+ end
+
+ end
+rescue LoadError => boom
+ warn "Tilt::RedClothTemplate (disabled)\n"
+end
View
21 test/tilt_sasstemplate_test.rb
@@ -0,0 +1,21 @@
+require 'contest'
+require 'tilt'
+
+begin
+ require 'haml'
+ require 'sass'
+
+ class SassTemplateTest < Test::Unit::TestCase
+ test "is registered for '.sass' files" do
+ assert_equal Tilt::SassTemplate, Tilt['test.sass']
+ end
+
+ test "compiles and evaluates the template on #render" do
+ template = Tilt::SassTemplate.new { |t| "#main\n :background-color #0000ff" }
+ assert_equal "#main {\n background-color: #0000ff; }\n", template.render
+ end
+ end
+
+rescue LoadError => boom
+ warn "Tilt::SassTemplate (disabled)\n"
+end
View
48 test/spec_tilt_stringtemplate.rb → test/tilt_stringtemplate_test.rb
@@ -1,67 +1,67 @@
-require 'bacon'
+require 'contest'
require 'tilt'
-describe Tilt::StringTemplate do
- it "is registered for '.str' files" do
- Tilt['test.str'].should.equal Tilt::StringTemplate
+class StringTemplateTest < Test::Unit::TestCase
+ test "registered for '.str' files" do
+ assert_equal Tilt::StringTemplate, Tilt['test.str']
end
- it "compiles and evaluates the template on #render" do
+ test "compiling and evaluating templates on #render" do
template = Tilt::StringTemplate.new { |t| "Hello World!" }
- template.render.should.equal "Hello World!"
+ assert_equal "Hello World!", template.render
end
- it "supports locals" do
+ test "passing locals" do
template = Tilt::StringTemplate.new { 'Hey #{name}!' }
- template.render(Object.new, :name => 'Joe').should.equal "Hey Joe!"
+ assert_equal "Hey Joe!", template.render(Object.new, :name => 'Joe')
end
- it "is evaluated in the object scope provided" do
+ test "evaluating in an object scope" do
template = Tilt::StringTemplate.new { 'Hey #{@name}!' }
scope = Object.new
scope.instance_variable_set :@name, 'Joe'
- template.render(scope).should.equal "Hey Joe!"
+ assert_equal "Hey Joe!", template.render(scope)
end
- it "evaluates template_source with yield support" do
+ test "passing a block for yield" do
template = Tilt::StringTemplate.new { 'Hey #{yield}!' }
- template.render { 'Joe' }.should.equal "Hey Joe!"
+ assert_equal "Hey Joe!", template.render { 'Joe' }
end
- it "renders multiline templates" do
+ test "multiline templates" do
template = Tilt::StringTemplate.new { "Hello\nWorld!\n" }
- template.render.should.equal "Hello\nWorld!\n"
+ assert_equal "Hello\nWorld!\n", template.render
end
- it "reports the file and line properly in backtraces without locals" do
+ test "backtrace file and line reporting without locals" do
data = File.read(__FILE__).split("\n__END__\n").last
fail unless data[0] == ?<
template = Tilt::StringTemplate.new('test.str', 11) { data }
begin
template.render
- flunk 'should have raised an exception'
+ fail 'should have raised an exception'
rescue => boom
- boom.should.be.kind_of NameError
+ assert_kind_of NameError, boom
line = boom.backtrace.first
file, line, meth = line.split(":")
- file.should.equal 'test.str'
- line.should.equal '13'
+ assert_equal 'test.str', file
+ assert_equal '13', line
end
end
- it "reports the file and line properly in backtraces with locals" do
+ test "backtrace file and line reporting with locals" do
data = File.read(__FILE__).split("\n__END__\n").last
fail unless data[0] == ?<
template = Tilt::StringTemplate.new('test.str', 1) { data }
begin
template.render(nil, :name => 'Joe', :foo => 'bar')
- flunk 'should have raised an exception'
+ fail 'should have raised an exception'
rescue => boom
- boom.should.be.kind_of RuntimeError
+ assert_kind_of RuntimeError, boom
line = boom.backtrace.first
file, line, meth = line.split(":")
- file.should.equal 'test.str'
- line.should.equal '6'
+ assert_equal 'test.str', file
+ assert_equal '6', line
end
end
end
View
136 test/tilt_template_test.rb
@@ -0,0 +1,136 @@
+require 'contest'
+require 'tilt'
+
+class TiltTemplateTest < Test::Unit::TestCase
+ test "needs a file or block" do
+ assert_raise(ArgumentError) { Tilt::Template.new }
+ end
+
+ test "initializing with a file" do
+ inst = Tilt::Template.new('foo.erb')
+ assert_equal 'foo.erb', inst.file
+ end
+
+ test "initializing with a file and line" do
+ inst = Tilt::Template.new('foo.erb', 55)
+ assert_equal 'foo.erb', inst.file
+ assert_equal 55, inst.line
+ end
+
+ test "uses correct eval_file" do
+ inst = Tilt::Template.new('foo.erb', 55)
+ assert_equal 'foo.erb', inst.eval_file
+ end
+
+ test "uses a default filename for #eval_file when no file provided" do
+ inst = Tilt::Template.new { 'Hi' }
+ assert_not_nil inst.eval_file
+ assert !inst.eval_file.include?("\n")
+ end
+
+ test "calculating template's #basename" do
+ inst = Tilt::Template.new('/tmp/templates/foo.html.erb')
+ assert_equal 'foo.html.erb', inst.basename
+ end
+
+ test "calculating the template's #name" do
+ inst = Tilt::Template.new('/tmp/templates/foo.html.erb')
+ assert_equal 'foo', inst.name
+ end
+
+ test "initializing with a data loading block" do
+ Tilt::Template.new { |template| "Hello World!" }
+ end
+
+ class InitializingMockTemplate < Tilt::Template
+ @@initialized_count = 0
+ def self.initialized_count
+ @@initialized_count
+ end
+
+ def initialize_engine
+ @@initialized_count += 1
+ end
+
+ def compile!
+ end
+ end
+
+ test "one-time template engine initialization" do
+ assert_nil InitializingMockTemplate.engine_initialized
+ assert_equal 0, InitializingMockTemplate.initialized_count
+
+ InitializingMockTemplate.new { "Hello World!" }
+ assert InitializingMockTemplate.engine_initialized
+ assert_equal 1, InitializingMockTemplate.initialized_count
+
+ InitializingMockTemplate.new { "Hello World!" }
+ assert_equal 1, InitializingMockTemplate.initialized_count
+ end
+
+ class CompilingMockTemplate < Tilt::Template
+ include Test::Unit::Assertions
+ def compile!
+ assert !data.nil?
+ @compiled = true
+ end
+ def compiled? ; @compiled ; end
+ end
+
+ test "raises NotImplementedError when #compile! not defined" do
+ inst = Tilt::Template.new { |template| "Hello World!" }
+ assert_raise(NotImplementedError) { inst.render }
+ end
+
+ test "raises NotImplementedError when #evaluate or #template_source not defined" do
+ inst = CompilingMockTemplate.new { |t| "Hello World!" }
+ assert_raise(NotImplementedError) { inst.render }
+ assert inst.compiled?
+ end
+
+ class SimpleMockTemplate < CompilingMockTemplate
+ include Test::Unit::Assertions
+ def evaluate(scope, locals, &block)
+ assert compiled?
+ assert !scope.nil?
+ assert !locals.nil?
+ "<em>#{@data}</em>"
+ end
+ end
+
+ test "compiles and evaluates the template on #render" do
+ inst = SimpleMockTemplate.new { |t| "Hello World!" }
+ assert_equal "<em>Hello World!</em>", inst.render
+ assert inst.compiled?
+ end
+
+ class SourceGeneratingMockTemplate < CompilingMockTemplate
+ def template_source
+ "foo = [] ; foo << %Q{#{data}} ; foo.join"
+ end
+ end
+
+ test "template_source with locals" do
+ inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{name}!' }
+ assert_equal "Hey Joe!", inst.render(Object.new, :name => 'Joe')
+ assert inst.compiled?
+ end
+
+ class Person
+ attr_accessor :name
+ def initialize(name)
+ @name = name
+ end
+ end
+
+ test "template_source with an object scope" do
+ inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{@name}!' }
+ scope = Person.new('Joe')
+ assert_equal "Hey Joe!", inst.render(scope)
+ end
+
+ test "template_source with a block for yield" do
+ inst = SourceGeneratingMockTemplate.new { |t| 'Hey #{yield}!' }
+ assert_equal "Hey Joe!", inst.render(Object.new){ 'Joe' }
+ end
+end
View
57 test/tilt_test.rb
@@ -0,0 +1,57 @@
+require 'contest'
+require 'tilt'
+
+class TiltTest < Test::Unit::TestCase
+ class MockTemplate
+ attr_reader :args, :block
+ def initialize(*args, &block)
+ @args = args
+ @block = block
+ end
+ end
+
+ test "registering template implementation classes by file extension" do
+ Tilt.register('mock', MockTemplate)
+ end
+
+ test "registering template classes by symbol file extension" do
+ Tilt.register(:mock, MockTemplate)
+ end
+
+ test "looking up template classes by file extension" do
+ Tilt.register('mock', MockTemplate)
+ impl = Tilt['mock']
+ assert_equal MockTemplate, impl
+
+ impl = Tilt['.mock']
+ assert_equal MockTemplate, impl
+ end
+
+ test "looking up template classes with multiple file extensions" do
+ Tilt.register('mock', MockTemplate)
+ impl = Tilt['index.html.mock']
+ assert_equal MockTemplate, impl
+ end
+
+ test "looking up template classes by file name" do
+ Tilt.register('mock', MockTemplate)
+ impl = Tilt['templates/test.mock']
+ assert_equal MockTemplate, impl
+ end
+
+ test "looking up non-existant template class" do
+ assert_nil Tilt['none']
+ end
+
+ test "accessing template class mappings at Tilt::mappings" do
+ assert Tilt.respond_to?(:mappings)
+ assert Tilt.mappings.respond_to?(:[])
+ end
+
+ test "creating new template instance with a filename" do
+ Tilt.register('mock', MockTemplate)
+ template = Tilt.new('foo.mock', 1, :key => 'val') { 'Hello World!' }
+ assert_equal ['foo.mock', 1, {:key => 'val'}], template.args
+ assert_equal 'Hello World!', template.block.call
+ end
+end
View
2 tilt.gemspec
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
# = MANIFEST =
s.test_files = s.files.select {|path| path =~ /^test\/spec_.*.rb/}
- s.add_development_dependency 'bacon'
+ s.add_development_dependency 'contest'
s.add_development_dependency 'builder'
s.add_development_dependency 'erubis'
s.add_development_dependency 'haml'

0 comments on commit 3cc6357

Please sign in to comment.