socialcast_shoulda_ext 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/.gitignore +6 -0
  2. data/Gemfile +13 -0
  3. data/MIT-LICENSE +22 -0
  4. data/README.rdoc +126 -0
  5. data/Rakefile +12 -0
  6. data/lib/shoulda_ext.rb +7 -0
  7. data/lib/shoulda_ext/assertions.rb +14 -0
  8. data/lib/shoulda_ext/integrations/test_unit.rb +13 -0
  9. data/lib/shoulda_ext/matchers.rb +9 -0
  10. data/lib/shoulda_ext/matchers/record_count_change.rb +127 -0
  11. data/lib/shoulda_ext/matchers/respond_with_json.rb +99 -0
  12. data/lib/shoulda_ext/matchers/trigger_callback.rb +131 -0
  13. data/lib/shoulda_ext/shoulda_patches/context_with_matcher_before_hooks.rb +32 -0
  14. data/lib/shoulda_ext/version.rb +4 -0
  15. data/socialcast_shoulda_ext.gemspec +22 -0
  16. data/test/helper.rb +30 -0
  17. data/test/rails3_root/.gitignore +4 -0
  18. data/test/rails3_root/Rakefile +7 -0
  19. data/test/rails3_root/app/controllers/application_controller.rb +3 -0
  20. data/test/rails3_root/app/controllers/blogs_controller.rb +11 -0
  21. data/test/rails3_root/app/controllers/comments_controller.rb +2 -0
  22. data/test/rails3_root/app/helpers/application_helper.rb +2 -0
  23. data/test/rails3_root/app/helpers/blogs_helper.rb +2 -0
  24. data/test/rails3_root/app/helpers/comments_helper.rb +2 -0
  25. data/test/rails3_root/app/models/blog.rb +3 -0
  26. data/test/rails3_root/app/models/comment.rb +3 -0
  27. data/test/rails3_root/app/views/blogs/index.html.erb +6 -0
  28. data/test/rails3_root/app/views/layouts/application.html.erb +14 -0
  29. data/test/rails3_root/config.ru +4 -0
  30. data/test/rails3_root/config/application.rb +17 -0
  31. data/test/rails3_root/config/boot.rb +6 -0
  32. data/test/rails3_root/config/database.yml +22 -0
  33. data/test/rails3_root/config/environment.rb +5 -0
  34. data/test/rails3_root/config/environments/development.rb +26 -0
  35. data/test/rails3_root/config/environments/production.rb +49 -0
  36. data/test/rails3_root/config/environments/test.rb +35 -0
  37. data/test/rails3_root/config/initializers/backtrace_silencers.rb +7 -0
  38. data/test/rails3_root/config/initializers/inflections.rb +10 -0
  39. data/test/rails3_root/config/initializers/mime_types.rb +5 -0
  40. data/test/rails3_root/config/initializers/secret_token.rb +7 -0
  41. data/test/rails3_root/config/initializers/session_store.rb +8 -0
  42. data/test/rails3_root/config/locales/en.yml +5 -0
  43. data/test/rails3_root/config/routes.rb +4 -0
  44. data/test/rails3_root/db/migrate/20110411025326_create_blogs.rb +12 -0
  45. data/test/rails3_root/db/migrate/20110411025332_create_comments.rb +13 -0
  46. data/test/rails3_root/db/seeds.rb +7 -0
  47. data/test/rails3_root/lib/tasks/.gitkeep +0 -0
  48. data/test/rails3_root/public/404.html +26 -0
  49. data/test/rails3_root/public/422.html +26 -0
  50. data/test/rails3_root/public/500.html +26 -0
  51. data/test/rails3_root/public/favicon.ico +0 -0
  52. data/test/rails3_root/public/images/rails.png +0 -0
  53. data/test/rails3_root/public/index.html +239 -0
  54. data/test/rails3_root/public/javascripts/application.js +2 -0
  55. data/test/rails3_root/public/javascripts/controls.js +965 -0
  56. data/test/rails3_root/public/javascripts/dragdrop.js +974 -0
  57. data/test/rails3_root/public/javascripts/effects.js +1123 -0
  58. data/test/rails3_root/public/javascripts/prototype.js +6001 -0
  59. data/test/rails3_root/public/javascripts/rails.js +191 -0
  60. data/test/rails3_root/public/robots.txt +5 -0
  61. data/test/rails3_root/public/stylesheets/.gitkeep +0 -0
  62. data/test/rails3_root/script/rails +6 -0
  63. data/test/rails3_root/test/fixtures/blogs.yml +5 -0
  64. data/test/rails3_root/test/fixtures/comments.yml +0 -0
  65. data/test/rails3_root/test/functional/blogs_controller_test.rb +8 -0
  66. data/test/rails3_root/test/functional/comments_controller_test.rb +8 -0
  67. data/test/rails3_root/test/shoulda_ext.sqlite3 +0 -0
  68. data/test/rails3_root/test/test_helper.rb +13 -0
  69. data/test/rails3_root/test/unit/blog_test.rb +8 -0
  70. data/test/rails3_root/test/unit/comment_test.rb +8 -0
  71. data/test/rails3_root/test/unit/helpers/blogs_helper_test.rb +4 -0
  72. data/test/rails3_root/test/unit/helpers/comments_helper_test.rb +4 -0
  73. data/test/rails3_root/vendor/plugins/.gitkeep +0 -0
  74. data/test/test_assertions.rb +14 -0
  75. data/test/test_record_count_change_matcher.rb +150 -0
  76. data/test/test_respond_with_json.rb +42 -0
  77. data/test/test_trigger_callback_matcher.rb +38 -0
  78. metadata +362 -0
@@ -0,0 +1,131 @@
1
+ module ShouldaExt # :nodoc:
2
+ module Matchers # :nodoc:
3
+
4
+ CALLBACK_EVENTS = [:before, :after, :after_commit_on]
5
+ CALLBACK_TYPES = [:create, :update, :destroy, :save]
6
+
7
+ # Test if create, update, destroy, or save callbacks were triggered.
8
+ # Examples:
9
+ #
10
+ # context "doing nothing to a record" do
11
+ # subject { Blog.new :title => 'blog title' }
12
+ # should_not trigger_callbacks
13
+ # end
14
+ #
15
+ # context "creating a record" do
16
+ # subject { Blog.create! :title => 'blog title' }
17
+ # should trigger_callbacks.for :create
18
+ # should_not trigger_callbacks.for :update, :destroy
19
+ # end
20
+ #
21
+ def trigger_callbacks
22
+ TriggerCallbackMatcher.new.any
23
+ end
24
+
25
+ class TriggerCallbackMatcher
26
+ module ActiveRecordHooks # :nodoc:
27
+
28
+ def self.included(base)
29
+ class << base
30
+ def inherited_with_hooks(subclass)
31
+ inherited_without_hooks(subclass)
32
+ ActiveRecordHooks.attach_to subclass
33
+ end
34
+ alias_method_chain :inherited, :hooks
35
+ end
36
+ end
37
+
38
+ def self.attach_to(model)
39
+ puts "Attaching trigger_callback test hooks in #{model.name}"
40
+ class << model
41
+ attr_accessor :callback_tester_attrs
42
+ end
43
+ model.class_eval do
44
+ @callback_tester_attrs = []
45
+ CALLBACK_EVENTS.each do |ce|
46
+ CALLBACK_TYPES.each do |ct|
47
+ callback_name = :"#{ce}_#{ct}"
48
+ callback_attr = :"called_#{callback_name}"
49
+ callback_method, has_on_option = (ce.to_s =~ /_on/ ? [ce.to_s.gsub('_on',''), true] : [callback_name, false])
50
+ @callback_tester_attrs << callback_attr
51
+ attr_accessor callback_attr
52
+ send( callback_method, (has_on_option ? {:on => ct} : {})) {
53
+ instance_variable_set(:"@#{callback_attr}", true)
54
+ }
55
+
56
+ define_method :"#{callback_attr}?" do
57
+ !!instance_variable_get(:"@#{callback_attr}")
58
+ end
59
+ end # - each
60
+ end # - each
61
+ end # - class_eval
62
+ end
63
+
64
+ def reset_callback_flags!
65
+ self.class.callback_tester_attrs.each do |attr|
66
+ send("#{attr}=", false)
67
+ end
68
+ end
69
+
70
+ end # - ActiveRecordHooks
71
+
72
+ # Attach the ActiveRecord callback hooks needed for using
73
+ # the trigger_callbacks matcher
74
+ def self.attach_active_record_callback_hooks!
75
+ puts "Attaching callback hooks into ActiveRecord"
76
+ ActiveRecord::Base.descendants.each do |model|
77
+ TriggerCallbackMatcher::ActiveRecordHooks.attach_to model
78
+ end
79
+ ActiveRecord::Base.send :include, TriggerCallbackMatcher::ActiveRecordHooks
80
+ end
81
+
82
+ # Define the set of callback types (create, update, destroy, save) to test
83
+ def for(*callback_types)
84
+ @callback_types = Array.wrap(callback_types)
85
+ @any_callbacks = false
86
+ self
87
+ end
88
+
89
+ # Check to see if any of the callback types have been triggered
90
+ def any
91
+ @callback_types = CALLBACK_TYPES
92
+ @any_callbacks = true
93
+ self
94
+ end
95
+
96
+ def failure_message
97
+ "Expected #{@subject} #{expectation}:"
98
+ end
99
+
100
+ def negative_failure_message
101
+ "Did not expect #{@subject} #{expectation}:"
102
+ end
103
+
104
+ def description
105
+ "check that #{@callback_types.join(', ')} callbacks were called"
106
+ end
107
+
108
+ def expectation
109
+ @expectations.join("\n")
110
+ end
111
+
112
+ def matches?(subject)
113
+ @subject = subject
114
+ @expectations = []
115
+ result = !@any_callbacks
116
+ @callback_types.each do |ct|
117
+ CALLBACK_EVENTS.each do |ce|
118
+ called = @subject.send(:"called_#{ce}_#{ct}?")
119
+ if @any_callbacks
120
+ result ||= called
121
+ else
122
+ result &&= called
123
+ end
124
+ @expectations << "#{ce}_#{ct} callbacks to be triggered"
125
+ end
126
+ end
127
+ result
128
+ end
129
+ end # - TriggerCallbackMatcher
130
+ end # - Matchers
131
+ end # - ShouldaExt
@@ -0,0 +1,32 @@
1
+
2
+ # Adds the ability to define a 'before' method on a matcher and allow
3
+ # it to be run before the subject/setup phase of the current context
4
+ #
5
+ # Pull Request for getting this functionality into shoulda-context
6
+ # https://github.com/thoughtbot/shoulda-context/pull/2
7
+ module Shoulda
8
+ class Context
9
+ def should(name_or_matcher, options = {}, &blk)
10
+ if name_or_matcher.respond_to?(:description) && name_or_matcher.respond_to?(:matches?)
11
+ name = name_or_matcher.description
12
+ blk = lambda { assert_accepts name_or_matcher, subject }
13
+ options[:before] = lambda { name_or_matcher.before } if name_or_matcher.respond_to?(:before)
14
+ else
15
+ name = name_or_matcher
16
+ end
17
+
18
+ if blk
19
+ self.shoulds << { :name => name, :before => options[:before], :block => blk }
20
+ else
21
+ self.should_eventuallys << { :name => name }
22
+ end
23
+ end
24
+
25
+ def should_not(matcher)
26
+ name = matcher.description
27
+ blk = lambda { assert_rejects matcher, subject }
28
+ before = matcher.respond_to?(:before) ? lambda { matcher.before } : nil
29
+ self.shoulds << { :name => "not #{name}", :block => blk, :before => before }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module ShouldaExt
3
+ VERSION = "0.1.2"
4
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "shoulda_ext/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "socialcast_shoulda_ext"
7
+ s.version = ShouldaExt::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Geoffrey Hichborn"]
10
+ s.email = ["geoff@socialcast.com"]
11
+ s.homepage = "http://github.com/socialcast/socialcast-shoulda-ext"
12
+ s.summary = %q{adds new shoulda matchers and assertions}
13
+ s.description = File.read('README.rdoc')
14
+
15
+ s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.0"])
16
+ s.add_runtime_dependency(%q<shoulda>, [">= 2.11.1"])
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,30 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+
3
+ # Load Rails 3 instance
4
+ rails_root = File.join(File.dirname(__FILE__), 'rails3_root')
5
+ ENV['BUNDLE_GEMFILE'] = File.expand_path('../../Gemfile', __FILE__)
6
+ require "#{rails_root}/config/environment.rb"
7
+ require 'rails/test_help'
8
+
9
+ # Load shoulda extensions
10
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
11
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
12
+ require 'shoulda_ext'
13
+
14
+ # Activate ActiveRecord hooks for trigger_callbacks matcher
15
+ ShouldaExt::Matchers::TriggerCallbackMatcher.attach_active_record_callback_hooks!
16
+
17
+ # Run the migrations
18
+ ActiveRecord::Migration.verbose = false
19
+ ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
20
+
21
+ # Setup the fixtures path
22
+ ActiveSupport::TestCase.fixture_path =
23
+ File.join(File.dirname(__FILE__), "rails3_root/test/fixtures")
24
+
25
+ class ActiveSupport::TestCase #:nodoc:
26
+ self.use_transactional_fixtures = false
27
+ self.use_instantiated_fixtures = false
28
+ end
29
+
30
+
@@ -0,0 +1,4 @@
1
+ .bundle
2
+ db/*.sqlite3
3
+ log/*.log
4
+ tmp/
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Rails3Root::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,11 @@
1
+ class BlogsController < ApplicationController
2
+ def index
3
+ respond_to do |format|
4
+ format.html do
5
+ end
6
+ format.json do
7
+ render :json => Blog.all.to_json
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ class CommentsController < ApplicationController
2
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module BlogsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module CommentsHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class Blog < ActiveRecord::Base
2
+ has_many :comments
3
+ end
@@ -0,0 +1,3 @@
1
+ class Comment < ActiveRecord::Base
2
+ belongs_to :blog
3
+ end
@@ -0,0 +1,6 @@
1
+ <ul>
2
+ <% Blog.all.each do |blog| %>
3
+ <li><%= blog.title %></li>
4
+ <% end %>
5
+ </ul>
6
+
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Rails3Root</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Rails3Root::Application
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ # If you have a Gemfile, require the gems listed there, including any gems
6
+ # you've limited to :test, :development, or :production.
7
+ Bundler.require(:default, Rails.env) if defined?(Bundler)
8
+
9
+ module Rails3Root
10
+ class Application < Rails::Application
11
+ # Configure the default encoding used in templates for Ruby 1.9.
12
+ config.encoding = "utf-8"
13
+
14
+ # Configure sensitive parameters which will be filtered from the log file.
15
+ config.filter_parameters += [:password]
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+
3
+ # Set up gems listed in the Gemfile.
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
5
+
6
+ require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Rails3Root::Application.initialize!
@@ -0,0 +1,26 @@
1
+ Rails3Root::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+
@@ -0,0 +1,49 @@
1
+ Rails3Root::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end