Permalink
Browse files

resurrect backtrace

  • Loading branch information...
2 parents db0d65d + f95fb54 commit cad12e638a9116add3e01af2d95955d30bbee769 Rick DeNatale committed May 18, 2011
Showing with 382 additions and 450 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 .rspec.example
  3. +13 −0 .watchr.example
  4. +1 −1 Gemfile
  5. +19 −8 Gemfile.lock
  6. +9 −6 README.rdoc
  7. +5 −6 Rakefile
  8. +4 −2 lib/rails-footnotes.rb
  9. +4 −4 lib/rails-footnotes/{notes → }/abstract_note.rb
  10. +0 −6 lib/rails-footnotes/footnotes.rb
  11. +1 −0 lib/rails-footnotes/notes/all.rb
  12. +3 −5 lib/rails-footnotes/notes/assigns_note.rb
  13. +1 −3 lib/rails-footnotes/notes/controller_note.rb
  14. +1 −3 lib/rails-footnotes/notes/cookies_note.rb
  15. +0 −2 lib/rails-footnotes/notes/env_note.rb
  16. +1 −3 lib/rails-footnotes/notes/files_note.rb
  17. +4 −6 lib/rails-footnotes/notes/filters_note.rb
  18. +1 −3 lib/rails-footnotes/notes/general_note.rb
  19. +1 −3 lib/rails-footnotes/notes/javascripts_note.rb
  20. +0 −2 lib/rails-footnotes/notes/layout_note.rb
  21. +4 −6 lib/rails-footnotes/notes/log_note.rb
  22. +0 −2 lib/rails-footnotes/notes/params_note.rb
  23. +0 −2 lib/rails-footnotes/notes/partials_note.rb
  24. +0 −2 lib/rails-footnotes/notes/queries_note.rb
  25. +1 −3 lib/rails-footnotes/notes/routes_note.rb
  26. +0 −30 lib/rails-footnotes/notes/rpm_note.rb
  27. +0 −2 lib/rails-footnotes/notes/session_note.rb
  28. +1 −3 lib/rails-footnotes/notes/stylesheets_note.rb
  29. +0 −2 lib/rails-footnotes/notes/view_note.rb
  30. +1 −1 lib/rails-footnotes/version.rb
  31. +2 −2 rails-footnotes.gemspec
  32. +80 −0 spec/abstract_note_spec.rb
  33. +215 −0 spec/footnotes_spec.rb
  34. +7 −3 test/test_helper.rb → spec/spec_helper.rb
  35. +0 −222 test/footnotes_test.rb
  36. +0 −107 test/notes/abstract_note_test.rb
View
2 .gitignore
@@ -1,9 +1,11 @@
.*
!.gitignore
!.gitmodules
+!*.example
*.gem
.bundle
Gemfile.lock
pkg/*
+coverage
tags
doc
View
1 .rspec.example
@@ -0,0 +1 @@
+--color
View
13 .watchr.example
@@ -0,0 +1,13 @@
+ROOT_PATH = File.dirname(__FILE__)
+
+def run_spec(file)
+ system "rspec #{file}" if File.exist?(File.join(ROOT_PATH, file))
+end
+
+watch('spec/.*_spec\.rb') {|m| run_spec m[0] }
+watch('spec/notes/.*_spec\.rb') {|m| run_spec m[0] }
+
+watch('lib/rails-footnotes/(.*)\.rb') {|m| run_spec("spec/#{m[1]}_spec.rb") }
+watch('lib/rails-footnotes/notes/.*_note\.rb') {|m| run_spec("spec/notes/#{m[1]}") }
+
+watch('Gemfile') {|m| run_spec("spec") }
View
2 Gemfile
@@ -7,6 +7,6 @@ gemspec
#
if RUBY_PLATFORM =~ /darwin/
group :test do
- gem "autotest-growl"
+ gem 'simplecov', '>= 0.4.0', :require => false
end
end
View
27 Gemfile.lock
@@ -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/
@@ -34,8 +35,8 @@ GEM
activesupport (= 3.0.5)
activesupport (3.0.5)
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)
@@ -45,7 +46,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)
@@ -66,18 +66,29 @@ 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)
+ simplecov (0.4.2)
+ simplecov-html (~> 0.4.4)
+ simplecov-html (0.4.5)
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
+ simplecov (>= 0.4.0)
+ watchr
View
15 README.rdoc
@@ -38,6 +38,14 @@ add something like this to config/initializers/footnotes.rb:
# ... other init code
end
+=== Post initialization
+
+If you want to add alternate logic to config footnotes without commit it to SCM, add your code to .footnotes:
+
+ Footnotes::Filter.notes = []
+
+this code temporarily disables notes for all controllers
+
=== Hooks
Footnotes.setup do |config|
@@ -50,7 +58,7 @@ add something like this to config/initializers/footnotes.rb:
If you are not using Textmate as text editor, in your environment.rb or
in an initializer do:
- Footnotes::Filter.prefix = 'mvim://open?url=file://%s&line=%d&column=%d'
+ Footnotes::Filter.prefix = 'mvim://open?url=file://%s&line=%d&column=%d'
for MacVim
@@ -68,11 +76,6 @@ Another option is to allow multiple notes to be opened at the same time:
Footnotes::Filter.multiple_notes = true
-If you have New Relic RPM installed, you may want to turn off query explains
-(explains can slows things down)
-
- Footnotes::Notes::QueriesNote.sql_explain = false
-
Finally, you can control which notes you want to show. The default are:
Footnotes::Filter.notes = [:session, :cookies, :params, :filters, :routes, :env, :queries, :log, :general]
View
11 Rakefile
@@ -1,16 +1,15 @@
require 'bundler'
Bundler::GemHelper.install_tasks
-require 'rake/testtask'
+require "rspec/core/rake_task"
require 'rake/rdoctask'
desc 'Default: run tests'
-task :default => :test
+task :default => :spec
desc 'Run tests for Footnotes.'
-Rake::TestTask.new(:test) do |t|
- t.libs << 'test'
- t.pattern = 'test/**/*_test.rb'
- t.verbose = true
+RSpec::Core::RakeTask.new(:spec) do |t|
+ t.pattern = 'spec/**/*_spec.rb'
+ t.rcov = false
end
desc 'Generate documentation for Footnotes.'
View
6 lib/rails-footnotes.rb
@@ -27,10 +27,12 @@ def self.included(base)
def self.run!
require 'rails-footnotes/footnotes'
require 'rails-footnotes/backtracer'
-
- Dir[File.join(File.dirname(__FILE__), 'rails-footnotes', 'notes', '*.rb')].each { |note| require note }
+ require 'rails-footnotes/abstract_note'
+ require 'rails-footnotes/notes/all'
ActionController::Base.send(:include, RailsFootnotesExtension)
+
+ load Rails.root.join('.footnotes') if Rails.root.join('.footnotes').exist?
end
def self.setup
View
8 lib/rails-footnotes/notes/abstract_note.rb → lib/rails-footnotes/abstract_note.rb
@@ -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
@@ -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.
#
@@ -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
View
6 lib/rails-footnotes/footnotes.rb
@@ -29,12 +29,6 @@ class Filter
# Show notes
@@notes += [ :assigns, :session, :cookies, :params, :filters, :routes, :env, :queries, :log, :general ]
- # Change queries for rpm note when available
- # if defined?(NewRelic)
- # @@notes.delete(:queries)
- # @@notes << :rpm
- # end
-
# :no_style => If you don't want the style to be appended to your pages
# :notes => Class variable that holds the notes to be processed
# :prefix => Prefix appended to FootnotesLinks
View
1 lib/rails-footnotes/notes/all.rb
@@ -0,0 +1 @@
+Dir[File.join(File.dirname(__FILE__), '*_note.rb')].each {|note| require note}
View
8 lib/rails-footnotes/notes/assigns_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class AssignsNote < AbstractNote
@@ -44,11 +42,11 @@ def content
def assigns
assign = []
ignored = @@ignored_assigns
-
+
@controller.instance_variables.each {|x| assign << x.intern }
@controller.protected_instance_variables.each {|x| ignored << x.intern } if @controller.respond_to? :protected_instance_variables
-
- assign -= ignored
+
+ assign -= ignored
return assign
end
View
4 lib/rails-footnotes/notes/controller_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class ControllerNote < AbstractNote
@@ -25,7 +23,7 @@ def controller_path
end
def controller_filename
- @controller_filename ||= Rails.root.join('app', 'controllers', "#{controller_path}.rb")
+ @controller_filename ||= Gem.find_files(self.controller_path).first # tnx https://github.com/MasterLambaster
end
def controller_text
View
4 lib/rails-footnotes/notes/cookies_note.rb
@@ -1,10 +1,8 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class CookiesNote < AbstractNote
def initialize(controller)
- @cookies = (controller.__send__(:cookies) || {}).symbolize_keys
+ @cookies = (controller.__send__(:cookies) || {}).dup.symbolize_keys
end
def title
View
2 lib/rails-footnotes/notes/env_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class EnvNote < AbstractNote
View
4 lib/rails-footnotes/notes/files_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class FilesNote < AbstractNote
@@ -41,4 +39,4 @@ def parse_files!
end
end
end
-end
+end
View
10 lib/rails-footnotes/notes/filters_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class FiltersNote < AbstractNote
@@ -35,12 +33,12 @@ def controller_filtered_actions(filter)
mock_controller.action_name = action
#remove conditions (this would call a Proc on the mock_controller)
- filter.options.merge!(:if => nil, :unless => nil)
+ filter.options.merge!(:if => nil, :unless => nil)
- filter.__send__(:should_run_callback?, mock_controller)
+ filter.__send__(:should_run_callback?, mock_controller)
}.map(&:to_sym)
end
-
+
def parse_method(method = '')
escape(method.inspect.gsub(RAILS_ROOT, ''))
end
@@ -50,4 +48,4 @@ def parse_method(method = '')
module Extensions
class MockController < Struct.new(:action_name); end
end
-end
+end
View
4 lib/rails-footnotes/notes/general_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class GeneralNote < AbstractNote
@@ -16,4 +14,4 @@ def content
end
end
end
-end
+end
View
4 lib/rails-footnotes/notes/javascripts_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/files_note"
-
module Footnotes
module Notes
class JavascriptsNote < FilesNote
@@ -13,4 +11,4 @@ def scan_text(text)
end
end
end
-end
+end
View
2 lib/rails-footnotes/notes/layout_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class LayoutNote < AbstractNote
View
10 lib/rails-footnotes/notes/log_note.rb
@@ -1,14 +1,12 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class LogNote < AbstractNote
@@log = []
-
+
def self.log(message)
@@log << message
end
-
+
def initialize(controller)
@controller = controller
end
@@ -20,7 +18,7 @@ def title
def content
escape(log.gsub(/\e\[.+?m/, '')).gsub("\n", '<br />')
end
-
+
def log
unless @log
@log = @@log.join('')
@@ -39,7 +37,7 @@ def add(*args, &block)
logged_message
end
end
-
+
Rails.logger.extend LoggingExtensions
end
end
View
2 lib/rails-footnotes/notes/params_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class ParamsNote < AbstractNote
View
2 lib/rails-footnotes/notes/partials_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/log_note"
-
module Footnotes
module Notes
class PartialsNote < LogNote
View
2 lib/rails-footnotes/notes/queries_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class QueriesNote < AbstractNote
View
4 lib/rails-footnotes/notes/routes_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class RoutesNote < AbstractNote
@@ -48,7 +46,7 @@ module Routes
#
def filtered_routes(filter = {})
return [] unless filter.is_a?(Hash)
- return routes.reject do |r|
+ return routes.reject do |r|
filter_diff = filter.diff(r.requirements)
route_diff = r.requirements.diff(filter)
(filter_diff == filter) || (filter_diff != route_diff)
View
30 lib/rails-footnotes/notes/rpm_note.rb
@@ -1,30 +0,0 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
-if defined?(NewRelic)
-module Footnotes
- module Notes
- class RpmNote < AbstractNote
- def initialize(controller)
- @rpm_id=NewRelic::Agent.instance.transaction_sampler.current_sample_id
- end
-
- def row
- :edit
- end
-
- def link
- #{:controller => 'newrelic', :action => 'show_sample_detail', :id => @rpm_id}
- "/newrelic/show_sample_detail/#{@rpm_id}" if @rpm_id
- end
-
- def valid?
- if defined?(NewRelic::Control)
- !NewRelic::Control.instance['skip_developer_route']
- else
- !NewRelic::Config.instance['skip_developer_route']
- end
- end
- end
- end
-end
-end
View
2 lib/rails-footnotes/notes/session_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class SessionNote < AbstractNote
View
4 lib/rails-footnotes/notes/stylesheets_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/files_note"
-
module Footnotes
module Notes
class StylesheetsNote < FilesNote
@@ -13,4 +11,4 @@ def scan_text(text)
end
end
end
-end
+end
View
2 lib/rails-footnotes/notes/view_note.rb
@@ -1,5 +1,3 @@
-require "#{File.dirname(__FILE__)}/abstract_note"
-
module Footnotes
module Notes
class ViewNote < AbstractNote
View
2 lib/rails-footnotes/version.rb
@@ -1,3 +1,3 @@
module Footnotes
- VERSION = "3.7.2"
+ VERSION = "3.7.3"
end
View
4 rails-footnotes.gemspec
@@ -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")
View
80 spec/abstract_note_spec.rb
@@ -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
View
215 spec/footnotes_spec.rb
@@ -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
View
10 test/test_helper.rb → spec/spec_helper.rb
@@ -1,11 +1,15 @@
+require 'simplecov'
+SimpleCov.start
+
require 'rubygems'
-require 'test/unit'
-require 'mocha'
ENV['RAILS_ENV'] = 'test'
require 'active_support'
require 'active_support/all' unless Class.respond_to?(:cattr_accessor)
require 'rails-footnotes/footnotes'
-require 'rails-footnotes/notes/abstract_note'
+require 'rails-footnotes/abstract_note'
require "rails-footnotes"
+
+RSpec.configure do |config|
+end
View
222 test/footnotes_test.rb
@@ -1,222 +0,0 @@
-require 'test_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
-
-class FootnotesTest < Test::Unit::TestCase
- def setup
- @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
-
- def test_footnotes_controller
- index = @controller.response.body.index(/This is the HTML page/)
- assert_equal 334, index
- end
-
- def test_foonotes_included
- footnotes_perform!
- assert_not_equal $html, @controller.response.body
- end
-
- def test_footnotes_not_included_when_request_is_xhr
- @controller.request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
- @controller.request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
-
- footnotes_perform!
- assert_equal $html, @controller.response.body
- end
-
- def test_footnotes_not_included_when_content_type_is_javascript
- @controller.response.headers['Content-Type'] = 'text/javascript'
-
- footnotes_perform!
- assert_equal $html, @controller.response.body
- end
-
- def test_footnotes_included_when_content_type_is_html
- @controller.response.headers['Content-Type'] = 'text/html'
-
- footnotes_perform!
- assert_not_equal $html, @controller.response.body
- end
-
- def test_footnotes_included_when_content_type_is_nil
- footnotes_perform!
- assert_not_equal $html, @controller.response.body
- end
-
- def test_not_included_when_body_is_not_a_string
- @controller.response.body = Proc.new{ Time.now }
- assert_nothing_raised do
- footnotes_perform!
- end
- end
-
- def test_footnotes_prefix
- assert_equal 'txmt://open?url=file://%s&amp;line=%d&amp;column=%d', Footnotes::Filter.prefix
- assert_equal 'txmt://open?url=file://file&amp;line=0&amp;column=0', Footnotes::Filter.prefix('file', 0, 0)
- assert_equal 'txmt://open?url=file://file&amp;line=10&amp;column=10', Footnotes::Filter.prefix('file', 10, 10)
- assert_equal 'txmt://open?url=file://file&amp;line=10&amp;column=10', Footnotes::Filter.prefix('file', 10, 10, 10)
- assert_equal 'txmt://open?url=file://file&amp;line=10&amp;column=10', Footnotes::Filter.prefix('file', '10', '10')
- end
-
- def test_notes_are_initialized
- footnotes_perform!
- test_note = @footnotes.instance_variable_get('@notes').first
- assert_equal 'Footnotes::Notes::TestNote', test_note.class.name
- assert_equal :test, test_note.to_sym
- end
-
- def test_notes_links
- note = Footnotes::Notes::TestNote.new
- note.expects(:row).times(2)
- @footnotes.instance_variable_set(:@notes, [note])
- footnotes_perform!
- end
-
- def test_notes_fieldset
- note = Footnotes::Notes::TestNote.new
- note.expects(:has_fieldset?).times(3)
- @footnotes.instance_variable_set(:@notes, [note])
- footnotes_perform!
- end
-
- def test_multiple_notes
- Footnotes::Filter.multiple_notes = true
- note = Footnotes::Notes::TestNote.new
- note.expects(:has_fieldset?).times(2)
- @footnotes.instance_variable_set(:@notes, [note])
- footnotes_perform!
- end
-
- def test_notes_are_reset
- note = Footnotes::Notes::TestNote.new
- note.class.expects(:close!)
- @footnotes.instance_variable_set(:@notes, [note])
- @footnotes.send(:close!, @controller)
- end
-
- def test_links_helper
- note = Footnotes::Notes::TestNote.new
- assert_equal '<a href="#" onclick="">Test</a>', @footnotes.send(:link_helper, note)
-
- note.expects(:link).times(1).returns(:link)
- assert_equal '<a href="link" onclick="">Test</a>', @footnotes.send(:link_helper, note)
- end
-
- def test_links_helper_has_fieldset?
- note = Footnotes::Notes::TestNote.new
- note.expects(:has_fieldset?).times(1).returns(true)
- assert_equal '<a href="#" onclick="Footnotes.hideAllAndToggle(\'test_debug_info\');return false;">Test</a>', @footnotes.send(:link_helper, note)
- end
-
- def test_links_helper_onclick
- note = Footnotes::Notes::TestNote.new
- note.expects(:onclick).times(2).returns(:onclick)
- assert_equal '<a href="#" onclick="onclick">Test</a>', @footnotes.send(:link_helper, note)
-
- note.expects(:has_fieldset?).times(1).returns(true)
- assert_equal '<a href="#" onclick="onclick">Test</a>', @footnotes.send(:link_helper, note)
- end
-
- def test_insert_style
- @controller.response.body = "<head></head><split><body></body>"
- @footnotes = Footnotes::Filter.new(@controller)
- footnotes_perform!
- assert @controller.response.body.split('<split>').first.include?('<!-- Footnotes Style -->')
- end
-
- def test_insert_footnotes_inside_body
- @controller.response.body = "<head></head><split><body></body>"
- @footnotes = Footnotes::Filter.new(@controller)
- footnotes_perform!
- assert @controller.response.body.split('<split>').last.include?('<!-- End Footnotes -->')
- end
-
- def test_insert_footnotes_inside_holder
- @controller.response.body = "<head></head><split><div id='footnotes_holder'></div>"
- @footnotes = Footnotes::Filter.new(@controller)
- footnotes_perform!
- assert @controller.response.body.split('<split>').last.include?('<!-- End Footnotes -->')
- end
-
- def test_insert_text
- @footnotes.send(:insert_text, :after, /<head>/, "Graffiti")
- after = " <head>Graffiti"
- assert_equal after, @controller.response.body.split("\n")[2]
-
- @footnotes.send(:insert_text, :before, /<\/body>/, "Notes")
- after = " Notes</body>"
- assert_equal after, @controller.response.body.split("\n")[12]
- end
-
- def test_hooks
- Footnotes::Filter.notes = [:note_x, :note_y, :note_z]
- Footnotes.setup {|config| config.before {|controller, filter| filter.notes -= [:note_y] }}
- Footnotes::Filter.start!(@controller)
- assert_equal [:note_x, :note_z], Footnotes::Filter.notes
-
- Footnotes.setup {|config| config.after {|controller, filter| filter.notes -= [:note_y] }}
- @footnotes.close!(@controller)
- assert_equal [:note_x, :note_z], Footnotes::Filter.notes
- end
-
- protected
- # First we make sure that footnotes will perform (long life to mocha!)
- # Then we call add_footnotes!
- #
- 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.expects(:template_format).returns(format)
- else
- @controller.template.expects(:format).returns(format)
- end
- 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
View
107 test/notes/abstract_note_test.rb
@@ -1,107 +0,0 @@
-require 'test_helper'
-
-class AbstractNoteTest < Test::Unit::TestCase
- def setup
- @note = Footnotes::Notes::AbstractNote.new
- Footnotes::Filter.notes = [:abstract]
- end
-
- def test_respond_to_start_and_close
- assert_respond_to Footnotes::Notes::AbstractNote, :start!
- assert_respond_to Footnotes::Notes::AbstractNote, :close!
- end
-
- def test_respond_to_sym
- assert_equal :abstract, Footnotes::Notes::AbstractNote.to_sym
- assert_equal :abstract, @note.to_sym
- end
-
- def test_respond_to_included?
- assert Footnotes::Notes::AbstractNote.included?
- Footnotes::Filter.notes = []
- assert !Footnotes::Notes::AbstractNote.included?
- end
-
- def test_respond_to_row
- assert_equal :show, @note.row
- end
-
- def test_respond_to_title
- assert_respond_to @note.class, :title
- end
-
- def test_respond_to_legend
- assert_respond_to @note, :legend
- end
-
- def test_respond_to_link
- assert_respond_to @note, :link
- end
-
- def test_respond_to_onclick
- assert_respond_to @note, :onclick
- end
-
- def test_respond_to_stylesheet
- assert_respond_to @note, :stylesheet
- end
-
- def test_respond_to_javascript
- assert_respond_to @note, :javascript
- end
-
- def test_respond_to_valid?
- assert_respond_to @note, :valid?
- assert @note.valid?
- end
-
- def test_respond_to_has_fieldset?
- assert_respond_to @note, :has_fieldset?
- assert !@note.has_fieldset?
- end
-
- def test_footnotes_prefix
- Footnotes::Filter.prefix = ''
- assert !@note.send(:prefix?)
- Footnotes::Filter.prefix = 'txmt://open?url=file://%s&amp;line=%d&amp;column=%d'
- assert @note.send(:prefix?)
- end
-
- def test_footnotes_escape
- assert_equal '&lt;', @note.send(:escape,'<')
- assert_equal '&amp;', @note.send(:escape,'&')
- assert_equal '&gt;', @note.send(:escape,'>')
- end
-
- def test_footnotes_mount_table
- assert_equal '', @note.send(:mount_table,[])
- assert_equal '', @note.send(:mount_table,[['h1','h2','h3']], :class => 'table')
-
- tab = <<-TABLE
- <table class="table" >
- <thead><tr><th>H1</th></tr></thead>
- <tbody><tr><td>r1c1</td></tr></tbody>
- </table>
- TABLE
-
- assert_equal tab, @note.send(:mount_table,[['h1'],['r1c1']], :class => 'table')
-
- 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
-
- assert_equal tab, @note.send(:mount_table,[['h1','h2','h3'],['r1c1','r1c2','r1c3']])
-
- 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
-
- assert_equal tab, @note.send(:mount_table,[['h1','h2','h3'],['r1c1','r1c2','r1c3'],['r2c1','r2c2','r2c3']])
- end
-end

0 comments on commit cad12e6

Please sign in to comment.