Skip to content

Commit

Permalink
[^] Migrate to RSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
nordringrayhide committed May 13, 2011
1 parent 7243773 commit 702a050
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 12 deletions.
21 changes: 15 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
rails-footnotes (3.6.7)
rails-footnotes (3.7.2)
rails (>= 3.0.0)

GEM
remote: http://rubygems.org/
Expand Down Expand Up @@ -36,6 +37,7 @@ GEM
arel (2.0.9)
autotest-growl (0.2.9)
builder (2.1.2)
diff-lcs (1.1.2)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.5.0)
Expand All @@ -45,7 +47,6 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
mocha (0.9.12)
polyglot (0.3.1)
rack (1.2.2)
rack-mount (0.6.13)
Expand All @@ -66,18 +67,26 @@ GEM
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
redgreen (1.2.2)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.2)
rspec-expectations (2.5.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.25)
watchr (0.7)

PLATFORMS
ruby

DEPENDENCIES
autotest-growl
mocha
rails (>= 3.0.5)
rails (>= 3.0.0)
rails-footnotes!
redgreen
rspec
watchr
8 changes: 4 additions & 4 deletions lib/rails-footnotes/notes/abstract_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ def legend

# Set href field for Footnotes links.
# If it's nil, Footnotes will use '#'.
#
#
def link
end

# Set onclick field for Footnotes links.
# If it's nil, Footnotes will make it open the fieldset.
#
#
def onclick
end

Expand Down Expand Up @@ -123,7 +123,7 @@ def has_fieldset?

# Some helpers to generate notes.
#
protected
public
# Return if Footnotes::Filter.prefix exists or not.
# Some notes only work with prefix set.
#
Expand Down Expand Up @@ -171,7 +171,7 @@ def hash_to_xml_attributes(hash)
newstring = ""
hash.each do |key, value|
newstring << "#{key.to_s}=\"#{value.gsub('"','\"')}\" "
end
end
return newstring
end
end
Expand Down
4 changes: 2 additions & 2 deletions rails-footnotes.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Gem::Specification.new do |s|

s.add_dependency "rails", ">= 3.0.0"

s.add_development_dependency "autotest"
s.add_development_dependency "mocha"
s.add_development_dependency "rails", ">= 3.0.0"
s.add_development_dependency "rspec"
s.add_development_dependency "watchr"

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
215 changes: 215 additions & 0 deletions spec/footnotes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
require "spec_helper"
require 'action_controller'
require 'action_controller/test_case'

class FootnotesController < ActionController::Base
attr_accessor :template, :performed_render
end

module Footnotes::Notes
class TestNote < AbstractNote
def self.to_sym; :test; end
def valid?; true; end
end

class NoteXNote < TestNote; end
class NoteYNote < TestNote; end
class NoteZNote < TestNote; end
end

describe "Footnotes" do
before do
@controller = FootnotesController.new
@controller.template = Object.new
@controller.request = ActionController::TestRequest.new
@controller.response = ActionController::TestResponse.new
@controller.response_body = $html.dup
@controller.params = {}

Footnotes::Filter.notes = [ :test ]
Footnotes::Filter.multiple_notes = false
@footnotes = Footnotes::Filter.new(@controller)
end

it "footnotes_controller" do
index = @controller.response.body.index(/This is the HTML page/)
index.should eql 334
end

it "foonotes_included" do
footnotes_perform!
@controller.response_body.should_not eql $html
end

specify "footnotes_not_included_when_request_is_xhr" do
@controller.request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
@controller.request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
footnotes_perform!
@controller.response.body.should eql $html
end

specify "footnotes_not_included_when_content_type_is_javascript" do
@controller.response.headers['Content-Type'] = 'text/javascript'
footnotes_perform!
@controller.response.body.should eql $html
end

specify "footnotes_included_when_content_type_is_html" do
@controller.response.headers['Content-Type'] = 'text/html'
footnotes_perform!
@controller.response.body.should_not eql $html
end

specify "footnotes_included_when_content_type_is_nil" do
footnotes_perform!
@controller.response.body.should_not eql $html
end

specify "not_included_when_body_is_not_a_string" do
@controller.response.body = Proc.new { Time.now }
expect { footnotes_perform! }.should_not raise_exception
end

specify "notes_are_initialized" do
footnotes_perform!
test_note = @footnotes.instance_variable_get('@notes').first
test_note.class.name.should eql 'Footnotes::Notes::TestNote'
test_note.to_sym.should eql :test
end

specify "notes_links" do
note = Footnotes::Notes::TestNote.new
note.should_receive(:row).twice
@footnotes.instance_variable_set(:@notes, [note])
footnotes_perform!
end

specify "notes_fieldset" do
note = Footnotes::Notes::TestNote.new
note.should_receive(:has_fieldset?).exactly(3).times
@footnotes.instance_variable_set(:@notes, [note])
footnotes_perform!
end

specify "multiple_notes" do
Footnotes::Filter.multiple_notes = true
note = Footnotes::Notes::TestNote.new
note.should_receive(:has_fieldset?).twice
@footnotes.instance_variable_set(:@notes, [note])
footnotes_perform!
end

specify "notes_are_reset" do
note = Footnotes::Notes::TestNote.new
note.class.should_receive(:close!)
@footnotes.instance_variable_set(:@notes, [note])
@footnotes.send(:close!, @controller)
end

specify "links_helper" do
note = Footnotes::Notes::TestNote.new
@footnotes.send(:link_helper, note).should eql '<a href="#" onclick="">Test</a>'

note.should_receive(:link).once.and_return(:link)
@footnotes.send(:link_helper, note).should eql '<a href="link" onclick="">Test</a>'
end

specify "links_helper_has_fieldset?" do
note = Footnotes::Notes::TestNote.new
note.should_receive(:has_fieldset?).once.and_return(true)
@footnotes.send(:link_helper, note).should eql '<a href="#" onclick="Footnotes.hideAllAndToggle(\'test_debug_info\');return false;">Test</a>'
end

specify "links_helper_onclick" do
note = Footnotes::Notes::TestNote.new
note.should_receive(:onclick).twice.and_return(:onclick)
@footnotes.send(:link_helper, note).should eql '<a href="#" onclick="onclick">Test</a>'

note.should_receive(:has_fieldset?).once.and_return(true)
@footnotes.send(:link_helper, note).should eql '<a href="#" onclick="onclick">Test</a>'
end

specify "insert_style" do
@controller.response.body = "<head></head><split><body></body>"
@footnotes = Footnotes::Filter.new(@controller)
footnotes_perform!
@controller.response.body.split('<split>').first.include?('<!-- Footnotes Style -->').should be
end

specify "insert_footnotes_inside_body" do
@controller.response.body = "<head></head><split><body></body>"
@footnotes = Footnotes::Filter.new(@controller)
footnotes_perform!
@controller.response.body.split('<split>').last.include?('<!-- End Footnotes -->').should be
end

specify "insert_footnotes_inside_holder" do
@controller.response.body = "<head></head><split><div id='footnotes_holder'></div>"
@footnotes = Footnotes::Filter.new(@controller)
footnotes_perform!
@controller.response.body.split('<split>').last.include?('<!-- End Footnotes -->').should be
end

specify "insert_text" do
@footnotes.send(:insert_text, :after, /<head>/, "Graffiti")
after = " <head>Graffiti"
@controller.response.body.split("\n")[2].should eql after

@footnotes.send(:insert_text, :before, /<\/body>/, "Notes")
after = " Notes</body>"
@controller.response.body.split("\n")[12].should eql after
end

describe 'Hooks' do
before {Footnotes::Filter.notes = [:note_x, :note_y, :note_z]}
context 'before' do
specify do
Footnotes.setup {|config| config.before {|controller, filter| filter.notes -= [:note_y] }}
Footnotes::Filter.start!(@controller)
Footnotes::Filter.notes.should eql [:note_x, :note_z]
end
end
context "after" do
specify do
Footnotes.setup {|config| config.after {|controller, filter| filter.notes -= [:note_y] }}
Footnotes::Filter.start!(@controller)
Footnotes::Filter.notes.should eql [:note_x, :note_z]
end
end
end

protected
def footnotes_perform!
template_expects('html')
@controller.performed_render = true

Footnotes::Filter.start!(@controller)
@footnotes.add_footnotes!
end

def template_expects(format)
if @controller.template.respond_to?(:template_format)
@controller.template.stub(:template_format).and_return(format)
else
@controller.template.stub(:format).and_return(format)
end
end

$html = <<HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HTML to XHTML Example: HTML page</title>
<link rel="Stylesheet" href="htmltohxhtml.css" type="text/css" media="screen">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<p>This is the HTML page. It works and is encoded just like any HTML page you
have previously done. View <a href="htmltoxhtml2.htm">the XHTML version</a> of
this page to view the difference between HTML and XHTML.</p>
<p>You will be glad to know that no changes need to be made to any of your CSS files.</p>
</body>
</html>
HTML

end
80 changes: 80 additions & 0 deletions spec/notes/abstract_note_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require "spec_helper"

describe Footnotes::Notes::AbstractNote do
before do
@note = Footnotes::Notes::AbstractNote.new
Footnotes::Filter.notes = [:abstract]
end

it {described_class.should respond_to :start!}
it {described_class.should respond_to :close!}
it {described_class.should respond_to :title}

it {should respond_to :to_sym}
its(:to_sym) {should eql :abstract}

it { described_class.should be_included }
specify do
Footnotes::Filter.notes = []
described_class.should_not be_included
end

it { should respond_to :row }
it { should respond_to :legend }
it { should respond_to :link }
it { should respond_to :onclick }
it { should respond_to :stylesheet }
it { should respond_to :javascript }

it { should respond_to :valid? }
it { should be_valid }

it { should respond_to :has_fieldset? }
it { should_not have_fieldset }

specify { Footnotes::Filter.prefix = ''; should_not be_prefix }
specify do
Footnotes::Filter.prefix = 'txmt://open?url=file://%s&amp;line=%d&amp;column=%d'
should be_prefix
end

#TODO should be moved to builder
#helpers
specify { subject.escape('<').should eql '&lt;' }
specify { subject.escape('&').should eql '&amp;' }
specify { subject.escape('>').should eql '&gt;' }

specify { subject.mount_table([]).should be_blank }
specify { subject.mount_table([['h1', 'h2', 'h3']], :class => 'table').should be_blank }

specify {
tab = <<-TABLE
<table class="table" >
<thead><tr><th>H1</th></tr></thead>
<tbody><tr><td>r1c1</td></tr></tbody>
</table>
TABLE

subject.mount_table([['h1'],['r1c1']], :class => 'table').should eql tab
}

specify {
tab = <<-TABLE
<table >
<thead><tr><th>H1</th><th>H2</th><th>H3</th></tr></thead>
<tbody><tr><td>r1c1</td><td>r1c2</td><td>r1c3</td></tr></tbody>
</table>
TABLE
subject.mount_table([['h1', 'h2', 'h3'],['r1c1', 'r1c2', 'r1c3']]).should eql tab
}

specify {
tab = <<-TABLE
<table >
<thead><tr><th>H1</th><th>H2</th><th>H3</th></tr></thead>
<tbody><tr><td>r1c1</td><td>r1c2</td><td>r1c3</td></tr><tr><td>r2c1</td><td>r2c2</td><td>r2c3</td></tr></tbody>
</table>
TABLE
subject.mount_table([['h1', 'h2', 'h3'], ['r1c1', 'r1c2', 'r1c3'], ['r2c1', 'r2c2', 'r2c3']])
}
end

0 comments on commit 702a050

Please sign in to comment.