Skip to content

Commit

Permalink
Don't split an Array or inject over a Hash
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Oct 2, 2010
1 parent ca072c4 commit daf9973
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/rspec/core/subject.rb
@@ -1,3 +1,5 @@
require 'ostruct'

module RSpec
module Core
module Subject
Expand Down Expand Up @@ -65,7 +67,6 @@ def should_not(matcher=nil, message=nil)
end

module ClassMethods
require 'ostruct' unless defined?(OpenStruct)
# Creates a nested example group named by the submitted +attribute+,
# and then generates an example using the submitted block.
#
Expand Down Expand Up @@ -112,15 +113,17 @@ module ClassMethods
# its(:keys) { should include(:max_users) }
# its(:count) { should == 2 }
# end
#
def its(attribute, &block)
describe(attribute) do
example do
self.class.class_eval do
define_method(:subject) do
attribute.to_s.split('.').inject(super()) do |target, method|
target = OpenStruct.new(target) if target.is_a?(Hash) && attribute.is_a?(Array)
target.send(method)
if super.is_a?(Hash) && attribute.is_a?(Array)
OpenStruct.new(super).send(attribute.first)
else
attribute.to_s.split('.').inject(super()) do |target, method|
target.send(method)
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/rspec/core/example_group_spec.rb
Expand Up @@ -603,7 +603,9 @@ def name
'another_attribute' => 'another_value' }
end
its([:attribute]) { should == 'value' }
its([:attribute]) { should_not == 'another_value' }
its([:another_attribute]) { should == 'another_value' }
its([:another_attribute]) { should_not == 'value' }
its(:keys) { should == ['another_attribute', :attribute] }

context "when referring to an attribute without the proper array syntax" do
Expand Down

0 comments on commit daf9973

Please sign in to comment.