thoughtbot--shoulda-matchers/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb

164 lines
4.2 KiB
Ruby
Raw Normal View History

2010-12-15 22:34:19 +00:00
module Shoulda # :nodoc:
module Matchers
module ActionController # :nodoc:
# Ensures that the flash contains the given value. Can be a String, a
# Regexp, or nil (indicating that the flash should not be set).
#
# Example:
#
# it { should set_the_flash }
2012-12-20 05:04:27 +00:00
# it { should set_the_flash.to('Thank you for placing this order.') }
2010-12-15 22:34:19 +00:00
# it { should set_the_flash.to(/created/i) }
2012-12-20 05:04:27 +00:00
# it { should set_the_flash[:alert].to('Password does not match') }
2010-12-15 22:34:19 +00:00
# it { should set_the_flash.to(/logged in/i).now }
# it { should_not set_the_flash }
def set_the_flash
SetTheFlashMatcher.new
end
class SetTheFlashMatcher # :nodoc:
2012-05-07 15:17:33 +00:00
def initialize
@options = {}
end
2010-12-15 22:34:19 +00:00
def to(value)
if !value.is_a?(String) && !value.is_a?(Regexp)
raise "cannot match against #{value.inspect}"
end
2010-12-15 22:34:19 +00:00
@value = value
self
end
def now
2012-05-07 15:17:33 +00:00
@options[:now] = true
2010-12-15 22:34:19 +00:00
self
end
def [](key)
2012-05-07 15:17:33 +00:00
@options[:key] = key
self
end
2010-12-15 22:34:19 +00:00
def matches?(controller)
@controller = controller
sets_the_flash? && string_value_matches? && regexp_value_matches?
end
def description
description = "set the #{expected_flash_invocation}"
2010-12-15 22:34:19 +00:00
description << " to #{@value.inspect}" unless @value.nil?
description
end
def failure_message
2010-12-15 22:34:19 +00:00
"Expected #{expectation}"
end
alias failure_message_for_should failure_message
2010-12-15 22:34:19 +00:00
def failure_message_when_negated
2010-12-15 22:34:19 +00:00
"Did not expect #{expectation}"
end
alias failure_message_for_should_not failure_message_when_negated
2010-12-15 22:34:19 +00:00
private
def sets_the_flash?
flash_values.any?
2010-12-15 22:34:19 +00:00
end
def string_value_matches?
2012-03-30 14:51:00 +00:00
if @value.is_a?(String)
flash_values.any? {|value| value == @value }
else
true
end
2010-12-15 22:34:19 +00:00
end
def regexp_value_matches?
2012-03-30 14:51:00 +00:00
if @value.is_a?(Regexp)
flash_values.any? {|value| value =~ @value }
else
true
end
end
def flash_values
2012-05-07 15:17:33 +00:00
if @options.key?(:key)
[flash.to_hash[@options[:key]]]
else
flash.to_hash.values
end
2010-12-15 22:34:19 +00:00
end
def flash
@flash ||= copy_of_flash_from_controller
end
def copy_of_flash_from_controller
@controller.flash.dup.tap do |flash|
copy_flashes(@controller.flash, flash)
copy_discard_if_necessary(@controller.flash, flash)
sweep_flash_if_necessary(flash)
end
end
def copy_flashes(original_flash, new_flash)
flashes_ivar = Shoulda::Matchers::RailsShim.flashes_ivar
flashes = original_flash.instance_variable_get(flashes_ivar).dup
new_flash.instance_variable_set(flashes_ivar, flashes)
end
def copy_discard_if_necessary(original_flash, new_flash)
discard_ivar = :@discard
if original_flash.instance_variable_defined?(discard_ivar)
discard = original_flash.instance_variable_get(discard_ivar).dup
new_flash.instance_variable_set(discard_ivar, discard)
2012-03-30 14:51:00 +00:00
end
2010-12-15 22:34:19 +00:00
end
def sweep_flash_if_necessary(flash)
2012-05-11 13:49:35 +00:00
unless @options[:now]
flash.sweep
2012-05-11 13:49:35 +00:00
end
end
2010-12-15 22:34:19 +00:00
def expectation
expectation = "the #{expected_flash_invocation} to be set"
2010-12-15 22:34:19 +00:00
expectation << " to #{@value.inspect}" unless @value.nil?
expectation << ", but #{flash_description}"
expectation
end
def flash_description
if flash.blank?
2012-12-20 05:04:27 +00:00
'no flash was set'
2010-12-15 22:34:19 +00:00
else
"was #{flash.inspect}"
end
end
def expected_flash_invocation
2012-05-07 15:17:33 +00:00
"flash#{pretty_now}#{pretty_key}"
end
2012-05-07 15:17:33 +00:00
def pretty_now
if @options[:now]
2012-12-20 05:04:27 +00:00
'.now'
2012-05-07 15:17:33 +00:00
else
2012-12-20 05:04:27 +00:00
''
2012-03-09 17:17:59 +00:00
end
2012-05-07 15:17:33 +00:00
end
2010-12-15 22:34:19 +00:00
2012-05-07 15:17:33 +00:00
def pretty_key
if @options[:key]
"[:#{@options[:key]}]"
2012-05-07 15:17:33 +00:00
else
2012-12-20 05:04:27 +00:00
''
2012-03-09 17:17:59 +00:00
end
end
end
2010-12-15 22:34:19 +00:00
end
end
end