lib/rspec/its.rb in rspec-its-1.0.1 vs lib/rspec/its.rb in rspec-its-1.1.0
- old
+ new
@@ -45,15 +45,17 @@
# @example
#
# describe "a configuration Hash" do
# subject do
# { :max_users => 3,
- # 'admin' => :all_permissions }
+ # 'admin' => :all_permissions.
+ # 'john_doe' => {:permissions => [:read, :write]}}
# end
#
# its([:max_users]) { should eq(3) }
# its(['admin']) { should eq(:all_permissions) }
+ # its(['john_doe', :permissions]) { should eq([:read, :write]) }
#
# # You can still access to its regular methods this way:
# its(:keys) { should include(:max_users) }
# its(:count) { should eq(2) }
# end
@@ -65,10 +67,29 @@
#
# describe Array do
# its(:size) { is_expected.to eq(0) }
# end
#
+ # You can pass more than one arguments on the `its` block to add
+ # some options to the generated example
+ #
+ # @example
+ #
+ # # This ...
+ # describe Array do
+ # its(:size, :focus) { should eq(0) }
+ # end
+ #
+ # # ... generates the same runtime structure as this:
+ # describe Array do
+ # describe "size" do
+ # it "should eq(0)", :focus do
+ # subject.size.should eq(0)
+ # end
+ # end
+ # end
+ #
# Note that this method does not modify `subject` in any way, so if you
# refer to `subject` in `let` or `before` blocks, you're still
# referring to the outer subject.
#
# @example
@@ -76,16 +97,21 @@
# describe Person do
# subject { Person.new }
# before { subject.age = 25 }
# its(:age) { should eq(25) }
# end
- def its(attribute, &block)
- describe(attribute.to_s) do
- if Array === attribute
- let(:__its_subject) { subject[*attribute] }
- else
- let(:__its_subject) do
+ def its(attribute, *options, &block)
+ its_caller = caller.select {|file_line| file_line !~ %r(/lib/rspec/its) }
+ describe(attribute.to_s, :caller => its_caller) do
+ let(:__its_subject) do
+ if Array === attribute
+ if Hash === subject
+ attribute.inject(subject) {|inner, attr| inner[attr] }
+ else
+ subject[*attribute]
+ end
+ else
attribute_chain = attribute.to_s.split('.')
attribute_chain.inject(subject) do |inner_subject, attr|
inner_subject.send(attr)
end
end
@@ -101,11 +127,11 @@
def should_not(matcher=nil, message=nil)
RSpec::Expectations::NegativeExpectationHandler.handle_matcher(__its_subject, matcher, message)
end
- its_caller = caller.select {|file_line| file_line !~ %r(/lib/rspec/its) }
- example(nil, :caller => its_caller, &block)
+ options << { :caller => its_caller }
+ example(nil, *options, &block)
end
end
end