lib/shoulda/context/context.rb in shoulda-context-2.0.0 vs lib/shoulda/context/context.rb in shoulda-context-3.0.0.rc1
- old
+ new
@@ -54,14 +54,24 @@
def teardown(&blk)
self.teardown_blocks << blk
end
- def should(name_or_matcher, options = {}, &blk)
+ class LambdaWithLocation < Proc
+ attr_reader :source_location
+
+ def initialize(source_location, &blk)
+ @source_location = source_location
+ super(&blk)
+ end
+ end
+
+ def should(name_or_matcher, options = {}, source_location = (loc = caller_locations(1, 1)[0]
+ [loc.path, loc.lineno]), &blk)
if name_or_matcher.respond_to?(:description) && name_or_matcher.respond_to?(:matches?)
name = name_or_matcher.description
- blk = lambda { assert_accepts name_or_matcher, subject }
+ blk = LambdaWithLocation.new(source_location, &lambda { assert_accepts name_or_matcher, subject })
else
name = name_or_matcher
end
if blk
@@ -69,13 +79,14 @@
else
self.should_eventuallys << { :name => name }
end
end
- def should_not(matcher)
+ def should_not(matcher, source_location = (loc = caller_locations(1, 1)[0]
+ [loc.path, loc.lineno]))
name = matcher.description
- blk = lambda { assert_rejects matcher, subject }
+ blk = LambdaWithLocation.new(source_location, &lambda { assert_rejects matcher, subject })
self.shoulds << { :name => "not #{name}", :block => blk }
end
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
@@ -98,11 +109,11 @@
am_subcontext? ? parent.test_unit_class : parent
end
def test_methods
@test_methods ||= Hash.new { |h,k|
- h[k] = Hash[k.instance_methods.map { |n| [n, true] }]
+ h[k] = k.instance_methods.each_with_object({}) { |n, a| a[n] = true }
}
end
def create_test_from_should_hash(should)
test_name = build_test_name_from(should)
@@ -211,6 +222,5 @@
end
class DuplicateTestError < RuntimeError; end
end
end
-