Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Better fix for let definitions eagerly included #942

Merged
merged 6 commits into from
Jun 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/rspec/core/example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,13 @@ def self.subclass(parent, args, &example_group_block)
subclass = Class.new(parent)
subclass.set_it_up(*args)
subclass.module_eval(&example_group_block) if example_group_block

# The LetDefinitions module must be included _after_ other modules
# to ensure that it takes precendence when there are name collisions.
# Thus, we delay including it until after the example group block
# has been eval'd.
MemoizedHelpers.define_helpers_on(subclass)

subclass
end

Expand Down
55 changes: 20 additions & 35 deletions lib/rspec/core/memoized_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ module MemoizedHelpers
#
# @see #should
def subject
raise NotImplementedError, 'This definition is here for documentation purposes only'
' - it is overriden anyway below when this module gets included.'
__memoized.fetch(:subject) do
__memoized[:subject] = begin
described = described_class || self.class.description
Class === described ? described.new : described
end
end
end

# When `should` is called with no explicit receiver, the call is
Expand Down Expand Up @@ -148,12 +152,6 @@ def preserve_accessed_lets

def self.included(mod)
mod.extend(ClassMethods)

# This logic defines an implicit subject
mod.subject do
described = described_class || self.class.description
Class === described ? described.new : described
end
end

module ClassMethods
Expand Down Expand Up @@ -190,12 +188,16 @@ module ClassMethods
# end
# end
def let(name, &block)
MemoizedHelpers.define_memoized_method(
self,
name,
MemoizedHelpers.memoized_method_name_for(name, 'let'),
&block
)
# We have to pass the block directly to `define_method` to
# allow it to use method constructs like `super` and `return`.
raise "#let or #subject called without a block" if block.nil?
MemoizedHelpers.module_for(self).send(:define_method, name, &block)

# Apply the memoization. The method has been defined in an ancestor
# module so we can use `super` here to get the value.
define_method(name) do
__memoized.fetch(name) { |k| __memoized[k] = super(&nil) }
end
end

# Just like `let`, except the block is invoked by an implicit `before`
Expand Down Expand Up @@ -287,11 +289,10 @@ def let!(name, &block)
# @see MemoizedHelpers#should
def subject(name=nil, &block)
if name
subject_method_name = MemoizedHelpers.memoized_method_name_for(name, "subject")
MemoizedHelpers.define_memoized_method(self, name, subject_method_name, &block)
let(name, &block)
alias_method :subject, name

self::NamedSubjectPreventSuper.__send__(:define_method, subject_method_name) do
self::NamedSubjectPreventSuper.send(:define_method, name) do
raise NotImplementedError, "`super` in named subjects is not supported"
end
else
Expand Down Expand Up @@ -468,30 +469,14 @@ def self.module_for(example_group)
}
end

example_group.__send__(:include, mod)
example_group.const_set(:LetDefinitions, mod)
mod
end
end

# @api private
def self.memoized_method_name_for(name, let_or_subject)
"__rspec_#{let_or_subject}_definition_#{name}"
end

# @api private
def self.define_memoized_method(example_group, name, memoized_method_name, &block)
# We have to pass the block directly to `define_method` to
# allow it to use method constructs like `super` and `return`.
raise "#let or #subject called without a block" if block.nil?

MemoizedHelpers.module_for(example_group).__send__(:define_method, memoized_method_name, &block)

# Apply the memoization. The method has been defined in an ancestor
# module so we can use `super` here to get the value.
example_group.__send__(:define_method, name) do
__memoized.fetch(name) { |k| __memoized[k] = send(memoized_method_name, &nil) }
end
def self.define_helpers_on(example_group)
example_group.send(:include, module_for(example_group))
end

if Module.method(:const_defined?).arity == 1 # for 1.8
Expand Down
37 changes: 20 additions & 17 deletions lib/rspec/core/shared_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,34 @@ module Core
# # ...
# end
module SharedContext
include Hooks
include MemoizedHelpers::ClassMethods

def included(group)
[:before, :after].each do |type|
[:all, :each].each do |scope|
group.hooks[type][scope].concat hooks[type][scope]
end
end
_nested_group_declarations.each do |name, block, *args|
group.describe name, *args, &block
__shared_context_recordings.each do |recording|
recording.playback_onto(group)
end
end

def describe(name, *args, &block)
_nested_group_declarations << [name, block, *args]
def __shared_context_recordings
@__shared_context_recordings ||= []
end

alias_method :context, :describe

private
Recording = Struct.new(:method_name, :args, :block) do
def playback_onto(group)
group.__send__(method_name, *args, &block)
end
end

def _nested_group_declarations
@_nested_group_declarations ||= []
def self.record(*methods)
methods.each do |meth|
class_eval <<-EOS, __FILE__, __LINE__ + 1
def #{meth}(*args, &block)
__shared_context_recordings << Recording.new(:#{meth}, args, block)
end
EOS
end
end

record :describe, :context, *Hooks.instance_methods(false),
*MemoizedHelpers::ClassMethods.instance_methods(false)
end
end

Expand Down