From 7873ed4a29c743f7276a9ef91889d847d25e29ef Mon Sep 17 00:00:00 2001 From: Gabe Berke-Williams Date: Tue, 24 Apr 2012 17:14:05 -0500 Subject: [PATCH] more config stuff --- .../action_controller/assign_to_matcher.rb | 75 ++++++++++++------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/lib/shoulda/matchers/action_controller/assign_to_matcher.rb b/lib/shoulda/matchers/action_controller/assign_to_matcher.rb index 04ae4063..516b989a 100644 --- a/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +++ b/lib/shoulda/matchers/action_controller/assign_to_matcher.rb @@ -24,30 +24,35 @@ module Shoulda # :nodoc: def initialize(variable) @variable = variable.to_s - @check_value = false + @options = {} + @options[:check_value] = false end def with_kind_of(expected_class) - @expected_class = expected_class + @options[:expected_class] = expected_class self end def with(expected_value = nil, &block) - @check_value = true - @expected_value = expected_value - @expectation_block = block + @options[:check_value] = true + @options[:expected_value] = expected_value + @options[:expectation_block] = block self end 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? + normalize_expected_value! + assigned_value? && + kind_of_expected_class? && + equal_to_expected_value? end def description description = "assign @#{@variable}" - description << " with a kind of #{@expected_class}" if @expected_class + if @options.key?(:expected_class) + description << " with a kind of #{@options[:expected_class]}" + end description end @@ -72,33 +77,45 @@ module Shoulda # :nodoc: end def kind_of_expected_class? - return true unless @expected_class - if assigned_value.kind_of?(@expected_class) - @negative_failure_message = - "Didn't expect action to assign a kind of #{@expected_class} " << - "for #{@variable}, but got one anyway" - true + if @options.key?(:expected_class) + if assigned_value.kind_of?(@options[:expected_class]) + @negative_failure_message = + "Didn't expect action to assign a kind of #{@options[:expected_class]} " << + "for #{@variable}, but got one anyway" + true + else + @failure_message = + "Expected action to assign a kind of #{@options[:expected_class]} " << + "for #{@variable}, but got #{assigned_value.inspect} " << + "(#{assigned_value.class.name})" + false + end else - @failure_message = - "Expected action to assign a kind of #{@expected_class} " << - "for #{@variable}, but got #{assigned_value.inspect} " << - "(#{assigned_value.class.name})" - false + true end end def equal_to_expected_value? - 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 + if @options[:check_value] + if @options[:expected_value] == assigned_value + @negative_failure_message = + "Didn't expect action to assign #{@options[:expected_value].inspect} " << + "for #{@variable}, but got it anyway" + true + else + @failure_message = + "Expected action to assign #{@options[:expected_value].inspect} " << + "for #{@variable}, but got #{assigned_value.inspect}" + false + end else - @failure_message = - "Expected action to assign #{@expected_value.inspect} " << - "for #{@variable}, but got #{assigned_value.inspect}" - false + true + end + end + + def normalize_expected_value! + if @options[:expectation_block] + @options[:expected_value] = @context.instance_eval(&@options[:expectation_block]) end end