Skip to content

Commit

Permalink
Removed the Matchers namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jferris committed Jan 19, 2011
1 parent 11d7dca commit e7eee1a
Show file tree
Hide file tree
Showing 55 changed files with 1,825 additions and 1,882 deletions.
49 changes: 23 additions & 26 deletions lib/shoulda/action_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,31 +9,28 @@
require 'shoulda/action_controller/redirect_to_matcher' require 'shoulda/action_controller/redirect_to_matcher'
require 'shoulda/action_controller/render_template_matcher' require 'shoulda/action_controller/render_template_matcher'


module Shoulda # :nodoc: module Shoulda
module ActionController # :nodoc: # By using the matchers you can quickly and easily create concise and

# easy to read test suites.
# By using the matchers you can quickly and easily create concise and #
# easy to read test suites. # This code segment:
# #
# This code segment: # describe UsersController, "on GET to show with a valid id" do
# # before(:each) do
# describe UsersController, "on GET to show with a valid id" do # get :show, :id => User.first.to_param
# before(:each) do # end
# get :show, :id => User.first.to_param #
# end # it { should assign_to(:user) }
# # it { should respond_with(:success) }
# it { should assign_to(:user) } # it { should render_template(:show) }
# it { should respond_with(:success) } # it { should not_set_the_flash) }
# it { should render_template(:show) } #
# it { should not_set_the_flash) } # it "should do something else really cool" do
# # assigns[:user].id.should == 1
# it "should do something else really cool" do # end
# assigns[:user].id.should == 1 # end
# end #
# end # Would produce 5 tests for the show action
# module ActionController
# Would produce 5 tests for the show action
module Matchers
end
end end
end end
180 changes: 89 additions & 91 deletions lib/shoulda/action_controller/assign_to_matcher.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,114 +1,112 @@
module Shoulda # :nodoc: module Shoulda # :nodoc:
module ActionController # :nodoc: module ActionController # :nodoc:
module Matchers

# Ensures that the controller assigned to the named instance variable.
#
# Options:
# * <tt>with_kind_of</tt> - The expected class of the instance variable
# being checked.
# * <tt>with</tt> - The value that should be assigned.
#
# Example:
#
# it { should assign_to(:user) }
# it { should_not assign_to(:user) }
# it { should assign_to(:user).with_kind_of(User) }
# it { should assign_to(:user).with(@user) }
def assign_to(variable)
AssignToMatcher.new(variable)
end


class AssignToMatcher # :nodoc: # Ensures that the controller assigned to the named instance variable.
#
# Options:
# * <tt>with_kind_of</tt> - The expected class of the instance variable
# being checked.
# * <tt>with</tt> - The value that should be assigned.
#
# Example:
#
# it { should assign_to(:user) }
# it { should_not assign_to(:user) }
# it { should assign_to(:user).with_kind_of(User) }
# it { should assign_to(:user).with(@user) }
def assign_to(variable)
AssignToMatcher.new(variable)
end


def initialize(variable) class AssignToMatcher # :nodoc:
@variable = variable.to_s
@check_value = false
end


def with_kind_of(expected_class) def initialize(variable)
@expected_class = expected_class @variable = variable.to_s
self @check_value = false
end end


def with(expected_value = nil, &block) def with_kind_of(expected_class)
@check_value = true @expected_class = expected_class
@expected_value = expected_value self
@expectation_block = block end
self
end


def matches?(controller) def with(expected_value = nil, &block)
@controller = controller @check_value = true
@expected_value = @context.instance_eval(&@expectation_block) if @expectation_block @expected_value = expected_value
assigned_value? && kind_of_expected_class? && equal_to_expected_value? @expectation_block = block
end self
end


attr_reader :failure_message, :negative_failure_message def matches?(controller)
@controller = controller
@expected_value = @context.instance_eval(&@expectation_block) if @expectation_block
assigned_value? && kind_of_expected_class? && equal_to_expected_value?
end


def description attr_reader :failure_message, :negative_failure_message
description = "assign @#{@variable}"
description << " with a kind of #{@expected_class}" if @expected_class
description
end


def in_context(context) def description
@context = context description = "assign @#{@variable}"
self description << " with a kind of #{@expected_class}" if @expected_class
end description
end


private def in_context(context)

@context = context
def assigned_value? self
if !@controller.instance_variables.include?("@#{@variable}") end
@failure_message =
"Expected action to assign a value for @#{@variable}"
false
else
@negative_failure_message =
"Didn't expect action to assign a value for @#{@variable}, " <<
"but it was assigned to #{assigned_value.inspect}"
true
end
end


def kind_of_expected_class? private
return true unless @expected_class
if assigned_value.kind_of?(@expected_class) def assigned_value?
@negative_failure_message = if !@controller.instance_variables.include?("@#{@variable}")
"Didn't expect action to assign a kind of #{@expected_class} " << @failure_message =
"for #{@variable}, but got one anyway" "Expected action to assign a value for @#{@variable}"
true false
else else
@failure_message = @negative_failure_message =
"Expected action to assign a kind of #{@expected_class} " << "Didn't expect action to assign a value for @#{@variable}, " <<
"for #{@variable}, but got #{@variable.inspect} " << "but it was assigned to #{assigned_value.inspect}"
"(#{@variable.class.name})" true
false
end
end end
end


def equal_to_expected_value? def kind_of_expected_class?
return true unless @check_value return true unless @expected_class
if @expected_value == assigned_value if assigned_value.kind_of?(@expected_class)
@negative_failure_message = @negative_failure_message =
"Didn't expect action to assign #{@expected_value.inspect} " << "Didn't expect action to assign a kind of #{@expected_class} " <<
"for #{@variable}, but got it anyway" "for #{@variable}, but got one anyway"
true true
else else
@failure_message = @failure_message =
"Expected action to assign #{@expected_value.inspect} " << "Expected action to assign a kind of #{@expected_class} " <<
"for #{@variable}, but got #{assigned_value.inspect}" "for #{@variable}, but got #{@variable.inspect} " <<
false "(#{@variable.class.name})"
end false
end end
end


def assigned_value def equal_to_expected_value?
@controller.instance_variable_get("@#{@variable}") return true unless @check_value
if @expected_value == assigned_value
@negative_failure_message =
"Didn't expect action to assign #{@expected_value.inspect} " <<
"for #{@variable}, but got it anyway"
true
else
@failure_message =
"Expected action to assign #{@expected_value.inspect} " <<
"for #{@variable}, but got #{assigned_value.inspect}"
false
end end
end


def assigned_value
@controller.instance_variable_get("@#{@variable}")
end end


end end

end end
end end
66 changes: 32 additions & 34 deletions lib/shoulda/action_controller/filter_param_matcher.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,50 +1,48 @@
module Shoulda # :nodoc: module Shoulda # :nodoc:
module ActionController # :nodoc: module ActionController # :nodoc:
module Matchers

# Ensures that filter_parameter_logging is set for the specified key.
#
# Example:
#
# it { should filter_param(:password) }
def filter_param(key)
FilterParamMatcher.new(key)
end


class FilterParamMatcher # :nodoc: # Ensures that filter_parameter_logging is set for the specified key.
#
# Example:
#
# it { should filter_param(:password) }
def filter_param(key)
FilterParamMatcher.new(key)
end


def initialize(key) class FilterParamMatcher # :nodoc:
@key = key.to_s
end


def matches?(controller) def initialize(key)
@controller = controller @key = key.to_s
filters_key? end
end


def failure_message def matches?(controller)
"Expected #{@key} to be filtered; filtered keys: #{filtered_keys.join(', ')}" @controller = controller
end filters_key?
end


def negative_failure_message def failure_message
"Did not expect #{@key} to be filtered" "Expected #{@key} to be filtered; filtered keys: #{filtered_keys.join(', ')}"
end end


def description def negative_failure_message
"filter #{@key}" "Did not expect #{@key} to be filtered"
end end


private def description
"filter #{@key}"
end


def filters_key? private
filtered_keys.include?(@key)
end


def filtered_keys def filters_key?
Rails.application.config.filter_parameters.map { |filter| filter.to_s } filtered_keys.include?(@key)
end
end end


def filtered_keys
Rails.application.config.filter_parameters.map { |filter| filter.to_s }
end
end end

end end
end end
Loading

0 comments on commit e7eee1a

Please sign in to comment.