Browse files

switched tests to rspec

  • Loading branch information...
1 parent 9eec0a1 commit 10e8084a90b373549dea554f52e7af1ffd8c98b9 @mokolabs mokolabs committed Jul 6, 2008
Showing with 117 additions and 115 deletions.
  1. +7 −8 Rakefile
  2. +1 −0 init.rb
  3. +101 −0 spec/headliner_spec.rb
  4. +8 −0 spec/spec_helper.rb
  5. +0 −107 test/headliner_test.rb
View
15 Rakefile
@@ -1,15 +1,14 @@
require 'rake'
-require 'rake/testtask'
+require 'spec/rake/spectask'
require 'rake/rdoctask'
-desc 'Default: run unit tests.'
-task :default => :test
+desc 'Default: run specs.'
+task :default => :spec
-desc 'Test the Headliner plugin.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
+desc 'Run the specs'
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
+ t.spec_files = FileList['spec/**/*_spec.rb']
end
desc 'Generate documentation for the Headliner plugin.'
View
1 init.rb
@@ -1 +1,2 @@
+require 'headliner'
ActionView::Base.send :include, Headliner
View
101 spec/headliner_spec.rb
@@ -0,0 +1,101 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe "Headliner" do
+
+ before(:each) do
+ @view = ActionView::Base.new
+ end
+
+ describe "loading plugin" do
+
+ it "should be mixed into ActionView::Base" do
+ ActionView::Base.included_modules.include?(Headliner).should be_true
+ end
+
+ it "should respond to 'title' helper" do
+ @view.should respond_to(:title)
+ end
+
+ it "should respond to 'title' helper alias" do
+ @view.should respond_to(:t)
+ end
+
+ end
+
+ describe "saving title" do
+
+ it "should save title" do
+ @view.title("iPhone").should eql("iPhone")
+ end
+
+ it "should save headline if present" do
+ @view.title("iPhone", "iPhone 3G is now on sale!").should eql("iPhone 3G is now on sale!")
+ end
+
+ end
+
+ describe "displaying title" do
+
+ it "should use website name if title is empty" do
+ @view.title(:site => "Apple").should eql("<title>Apple</title>")
+ end
+
+ it "should use website before page by default" do
+ save_basic_title
+ @view.title(:site => "Apple").should eql("<title>Apple | iPhone</title>")
+ end
+
+ it "should only use markup in titles in the view" do
+ save_basic_title("<b>iPhone</b>").should eql("<b>iPhone</b>")
+ @view.title(:site => "Apple").should eql("<title>Apple | iPhone</title>")
+ end
+
+ it "should use page before website if :reverse" do
+ save_basic_title
+ @view.title(:site => "Apple", :reverse => true).should eql("<title>iPhone | Apple</title>")
+ end
+
+ it "should be lowercase if :lowercase" do
+ save_basic_title
+ @view.title(:site => "apple", :lowercase => true).should eql("<title>apple | iphone</title>")
+ end
+
+ it "should use custom separator if :separator" do
+ save_basic_title
+ @view.title(:site => "Apple", :separator => "-").should eql("<title>Apple - iPhone</title>")
+ @view.title(:site => "Apple", :separator => ":").should eql("<title>Apple : iPhone</title>")
+ @view.title(:site => "Apple", :separator => "&mdash;").should eql("<title>Apple &mdash; iPhone</title>")
+ end
+
+ it "should use custom prefix and suffix if available" do
+ save_basic_title
+ @view.title(:site => "Apple", :prefix => " |", :suffix => "| ").should eql("<title>Apple ||| iPhone</title>")
+ end
+
+ it "should collapse prefix if false" do
+ save_basic_title
+ @view.title(:site => "Apple", :prefix => false, :separator => ":").should eql("<title>Apple: iPhone</title>")
+ end
+
+ it "should collapse suffix if false" do
+ save_basic_title
+ @view.title(:site => "Apple", :suffix => false, :separator => "~").should eql("<title>Apple ~iPhone</title>")
+ end
+
+ it "should use all custom options if available" do
+ save_basic_title
+ @view.title(:site => "apple",
+ :prefix => " ",
+ :suffix => " ",
+ :separator => "-",
+ :lowercase => true,
+ :reverse => true).should eql("<title>iphone - apple</title>")
+ end
+
+ end
+
+ def save_basic_title(title='iPhone')
+ @view.title(title)
+ end
+
+end
View
8 spec/spec_helper.rb
@@ -0,0 +1,8 @@
+require 'rubygems'
+require 'action_controller'
+require 'action_view'
+require File.join(File.dirname(__FILE__), "../lib/headliner")
+
+ActionView::Base.class_eval do
+ include Headliner
+end
View
107 test/headliner_test.rb
@@ -1,107 +0,0 @@
-require 'rubygems'
-require 'test/unit'
-require File.dirname(__FILE__) + '/../lib/headliner'
-require 'action_view/helpers/tag_helper'
-
-class HeadlinerTest < Test::Unit::TestCase
-
- include ActionView::Helpers::TagHelper
- include Headliner
-
- # Make the blank? method available without loading Rails
- Object.class_eval do
- def blank?
- if respond_to?(:empty?) && respond_to?(:strip)
- empty? or strip.empty?
- elsif respond_to?(:empty?)
- empty?
- else
- !self
- end
- end
- end
-
- def test_title_is_saved
- title "Headliner", ""
- assert_equal "Headliner", @title
- end
-
- def test_title_is_saved_with_headline
- headline = save_title "Headliner", "Headliner: a plugin for setting page titles"
- assert_equal "Headliner", @title
- assert_equal "Headliner: a plugin for setting page titles", headline
- end
-
- def test_title_is_site_when_empty
- title :site => "the.railsi.st"
- assert_equal nil, @title
- end
-
- def test_title_removes_all_tags
- title 'Headliner is <strong class="underline">cool</strong>'
- assert_equal "Headliner is cool", @title
- end
-
- def test_title_removes_all_tags_but_displays_tags_in_views
- original_title = save_title 'Headliner is <strong class="underline">cool</strong>', ''
- assert_equal "Headliner is cool", @title
- assert_equal 'Headliner is <strong class="underline">cool</strong>', original_title
- end
-
- def test_title_is_lowercase
- @title = "Headliner"
- full_title = display_title :site => "the.railsi.st", :lowercase => true
- assert_equal "<title>the.railsi.st | headliner</title>", full_title
- end
-
- def test_title_is_reversed
- @title = "Headliner"
- full_title = display_title :site => "the.railsi.st", :reverse => true
- assert_equal "<title>Headliner | the.railsi.st</title>", full_title
- end
-
- def test_title_has_custom_separator
- @title = "Headliner"
- full_title = display_title :site => "the.railsi.st", :separator => "&mdash;"
- assert_equal "<title>the.railsi.st &mdash; Headliner</title>", full_title
- end
-
- def test_title_has_custom_prefix_and_suffix
- @title = "Headliner"
- full_title = display_title :site => "the.railsi.st", :prefix => " ", :suffix => " "
- assert_equal "<title>the.railsi.st | Headliner</title>", full_title
- end
-
- def test_title_has_no_prefix_and_has_custom_separator
- @title = "Headliner"
- full_title = display_title :site => "the.railsi.st", :prefix => false, :separator => ":"
- assert_equal "<title>the.railsi.st: Headliner</title>", full_title
- end
-
- def test_title_has_no_suffix_and_has_custom_separator
- @title = "Headliner"
- full_title = display_title :site => "the.railsi.st", :suffix => false, :separator => "~"
- assert_equal "<title>the.railsi.st ~Headliner</title>", full_title
- end
-
- def test_title_has_no_prefix_and_suffix_and_has_custom_separator
- @title = "Headliner"
- full_title = display_title :site => "the.railsi.st",
- :prefix => false,
- :suffix => false,
- :separator => "&mdash;"
- assert_equal "<title>the.railsi.st&mdash;Headliner</title>", full_title
- end
-
- def test_title_has_all_custom_options
- @title = "Headliner"
- full_title = display_title :site => "the.railsi.st",
- :prefix => " ",
- :suffix => " ",
- :separator => ".:.",
- :lowercase => true,
- :reverse => true
- assert_equal "<title>headliner .:. the.railsi.st</title>", full_title
- end
-
-end

0 comments on commit 10e8084

Please sign in to comment.