-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
1,825 additions
and
1,882 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.